Fix tree collapse on recording finish

This commit is contained in:
2020-02-02 17:09:06 +00:00
parent b206fb33aa
commit 1997b0bf9b
3 changed files with 65 additions and 105 deletions

View File

@@ -1684,10 +1684,11 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
Sentence snt = (Sentence)s.nextElement(); Sentence snt = (Sentence)s.nextElement();
if (!snt.isLocked()) { if (!snt.isLocked()) {
if (!snt.beenDetected()) { if (!snt.beenDetected()) {
Debug.d("Queueing recognition of", snt.getId()); queueJob(new SentenceJob(snt) {
Runnable r = snt.getRecognitionRunnable(); public void run() {
snt.setQueued(); sentence.doRecognition();
queueJob(r); }
});
} }
} }
} }
@@ -2096,7 +2097,7 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
if (recording == null) return; if (recording == null) return;
recording.stopRecording(); recording.stopRecording();
book.reloadTree(); // book.reloadTree();
bookTree.expandPath(new TreePath(((DefaultMutableTreeNode)recording.getParent()).getPath())); bookTree.expandPath(new TreePath(((DefaultMutableTreeNode)recording.getParent()).getPath()));
bookTree.setSelectionPath(new TreePath(recording.getPath())); bookTree.setSelectionPath(new TreePath(recording.getPath()));

View File

@@ -450,6 +450,7 @@ public class Book extends BookTreeNode {
} }
public void reloadTree() { public void reloadTree() {
Debug.trace();
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
public void run() { public void run() {
AudiobookRecorder.window.bookTreeModel.reload(Book.this); AudiobookRecorder.window.bookTreeModel.reload(Book.this);

View File

@@ -222,7 +222,7 @@ public class Sentence extends BookTreeNode implements Cacheable {
if (text.equals("")) text = id; if (text.equals("")) text = id;
if ((crossStartOffset == -1) || (crossEndOffset == -1)) { if ((crossStartOffset == -1) || (crossEndOffset == -1)) {
updateCrossings(true); updateCrossings();
} }
if (runtime <= 0.01d) getLength(); if (runtime <= 0.01d) getLength();
@@ -260,26 +260,40 @@ public class Sentence extends BookTreeNode implements Cacheable {
CacheManager.removeFromCache(this); CacheManager.removeFromCache(this);
if (!id.equals("room-noise")) { if (!id.equals("room-noise")) {
autoTrimSample(true); autoTrimSample();
if (Options.getBoolean("process.sphinx")) { if (Options.getBoolean("process.sphinx")) {
recognise(); AudiobookRecorder.window.queueJob(new SentenceJob(this) {
public void run() {
sentence.doRecognition();
}
});
} }
} }
} }
public void autoTrimSample() { public void autoTrimSample(boolean ignored) {
Debug.trace(); Debug.trace();
autoTrimSample(false); autoTrimSample();
} }
public void autoTrimSample(boolean useRaw) { public void autoTrimSample() {
Debug.trace(); Debug.trace();
String tm = Options.get("audio.recording.trim"); String tm = Options.get("audio.recording.trim");
if (tm.equals("peak")) { if (tm.equals("peak")) {
autoTrimSamplePeak(useRaw); AudiobookRecorder.window.queueJob(new SentenceJob(this) {
public void run() {
sentence.autoTrimSamplePeak();
AudiobookRecorder.window.updateWaveformMarkers();
}
});
} else if (tm.equals("fft")) { } else if (tm.equals("fft")) {
autoTrimSampleFFT(useRaw); AudiobookRecorder.window.queueJob(new SentenceJob(this) {
public void run() {
sentence.autoTrimSampleFFT();
AudiobookRecorder.window.updateWaveformMarkers();
}
});
} else { } else {
startOffset = 0; startOffset = 0;
crossStartOffset = 0; crossStartOffset = 0;
@@ -288,13 +302,13 @@ public class Sentence extends BookTreeNode implements Cacheable {
processed = false; processed = false;
// peak = -1d; // peak = -1d;
} }
AudiobookRecorder.window.updateWaveform(true);
} }
public static final int FFTBuckets = 1024; public static final int FFTBuckets = 1024;
public void autoTrimSampleFFT() { public void autoTrimSampleFFT(boolean ignored) {
Debug.trace(); Debug.trace();
autoTrimSampleFFT(false);
} }
public double bucketDifference(double[] a, double[] b) { public double bucketDifference(double[] a, double[] b) {
@@ -308,16 +322,12 @@ public class Sentence extends BookTreeNode implements Cacheable {
return diff; return diff;
} }
public void autoTrimSampleFFT(boolean useRaw) { public void autoTrimSampleFFT() {
Debug.trace(); Debug.trace();
crossStartOffset = -1; crossStartOffset = -1;
crossEndOffset = -1; crossEndOffset = -1;
double[][] samples; double[][] samples;
if (useRaw) { samples = getProcessedAudioData();
samples = getRawAudioData();
} else {
samples = getProcessedAudioData();
}
if (samples == null) { if (samples == null) {
return; return;
} }
@@ -389,7 +399,7 @@ public class Sentence extends BookTreeNode implements Cacheable {
if (endOffset <= startOffset) endOffset = startOffset + fftSize; if (endOffset <= startOffset) endOffset = startOffset + fftSize;
if (endOffset < 0) endOffset = 0; if (endOffset < 0) endOffset = 0;
if (endOffset >= samples[LEFT].length) endOffset = samples[LEFT].length; if (endOffset >= samples[LEFT].length) endOffset = samples[LEFT].length;
updateCrossings(useRaw); updateCrossings();
intens = null; intens = null;
samples = null; samples = null;
processed = true; processed = true;
@@ -440,21 +450,17 @@ public class Sentence extends BookTreeNode implements Cacheable {
} }
public void autoTrimSamplePeak() { public void autoTrimSamplePeak(boolean ignored) {
Debug.trace(); Debug.trace();
autoTrimSamplePeak(false); autoTrimSamplePeak();
} }
public void autoTrimSamplePeak(boolean useRaw) { public void autoTrimSamplePeak() {
Debug.trace(); Debug.trace();
crossStartOffset = -1; crossStartOffset = -1;
crossEndOffset = -1; crossEndOffset = -1;
double[][] samples; double[][] samples;
if (useRaw) { samples = getProcessedAudioData();
samples = getRawAudioData();
} else {
samples = getProcessedAudioData();
}
if (samples == null) return; if (samples == null) return;
double noiseFloor = AudiobookRecorder.window.getNoiseFloor(); double noiseFloor = AudiobookRecorder.window.getNoiseFloor();
noiseFloor *= 1.1; noiseFloor *= 1.1;
@@ -494,7 +500,7 @@ public class Sentence extends BookTreeNode implements Cacheable {
if (startOffset < 0) startOffset = 0; if (startOffset < 0) startOffset = 0;
if (endOffset >= samples[LEFT].length) endOffset = samples[LEFT].length-1; if (endOffset >= samples[LEFT].length) endOffset = samples[LEFT].length-1;
updateCrossings(useRaw); updateCrossings();
processed = true; processed = true;
reloadTree(); reloadTree();
} }
@@ -601,38 +607,23 @@ public class Sentence extends BookTreeNode implements Cacheable {
public void updateCrossings() { public void updateCrossings() {
Debug.trace(); Debug.trace();
updateCrossings(false); updateStartCrossing();
} updateEndCrossing();
public void updateCrossings(boolean useRaw) {
Debug.trace();
updateStartCrossing(useRaw);
updateEndCrossing(useRaw);
runtime = -1d; runtime = -1d;
getLength(); getLength();
} }
public void updateStartCrossing() { public void updateStartCrossing() {
Debug.trace();
updateStartCrossing(false);
}
public void updateStartCrossing(boolean useRaw) {
Debug.trace(); Debug.trace();
if (crossStartOffset == -1) { if (crossStartOffset == -1) {
crossStartOffset = findNearestZeroCrossing(useRaw, startOffset, 4096); crossStartOffset = findNearestZeroCrossing(startOffset, 4096);
} }
} }
public void updateEndCrossing() { public void updateEndCrossing() {
Debug.trace();
updateEndCrossing(false);
}
public void updateEndCrossing(boolean useRaw) {
Debug.trace(); Debug.trace();
if (crossEndOffset == -1) { if (crossEndOffset == -1) {
crossEndOffset = findNearestZeroCrossing(useRaw, endOffset, 4096); crossEndOffset = findNearestZeroCrossing(endOffset, 4096);
} }
} }
@@ -698,16 +689,6 @@ public class Sentence extends BookTreeNode implements Cacheable {
return null; return null;
} }
public Runnable getRecognitionRunnable() {
Runnable r = new Runnable() {
public void run() {
Debug.d("Starting recognition of", getId());
doRecognition();
}
};
return r;
}
public void doRecognition() { public void doRecognition() {
Debug.trace(); Debug.trace();
try { try {
@@ -735,12 +716,6 @@ public class Sentence extends BookTreeNode implements Cacheable {
} }
} }
public void recognise() {
Debug.trace();
Thread t = new Thread(getRecognitionRunnable());
t.start();
}
public void setLocked(boolean l) { public void setLocked(boolean l) {
Debug.trace(); Debug.trace();
if (locked == l) return; if (locked == l) return;
@@ -776,18 +751,9 @@ public class Sentence extends BookTreeNode implements Cacheable {
} }
public int findNearestZeroCrossing(int pos, int range) { public int findNearestZeroCrossing(int pos, int range) {
Debug.trace();
return findNearestZeroCrossing(false, pos, range);
}
public int findNearestZeroCrossing(boolean useRaw, int pos, int range) {
Debug.trace(); Debug.trace();
double[][] data = null; double[][] data = null;
if (useRaw) { data = getProcessedAudioData();
data = getRawAudioData();
} else {
data = getProcessedAudioData();
}
if (data == null) return 0; if (data == null) return 0;
if (data[LEFT].length == 0) return 0; if (data[LEFT].length == 0) return 0;
@@ -855,7 +821,6 @@ public class Sentence extends BookTreeNode implements Cacheable {
File to = sentence.getFile(); File to = sentence.getFile();
Files.copy(from.toPath(), to.toPath()); Files.copy(from.toPath(), to.toPath());
// sentence.updateCrossings();
return sentence; return sentence;
} }
@@ -873,24 +838,15 @@ public class Sentence extends BookTreeNode implements Cacheable {
public double getPeakValue() { public double getPeakValue() {
Debug.trace(); Debug.trace();
return getPeakValue(false, true); return getPeakValue(true);
} }
public double getPeakValue(boolean useRaw) { public double getPeakValue(boolean applyGain) {
Debug.trace();
return getPeakValue(useRaw, true);
}
public double getPeakValue(boolean useRaw, boolean applyGain) {
Debug.trace(); Debug.trace();
double oldGain = gain; double oldGain = gain;
gain = 1.0d; gain = 1.0d;
double[][] samples = null; double[][] samples = null;
if (useRaw) { samples = getProcessedAudioData(true, applyGain);
samples = getRawAudioData();
} else {
samples = getProcessedAudioData(true, applyGain);
}
gain = oldGain; gain = oldGain;
if (samples == null) { if (samples == null) {
return 0; return 0;
@@ -937,7 +893,7 @@ public class Sentence extends BookTreeNode implements Cacheable {
public double normalize(double low, double high) { public double normalize(double low, double high) {
Debug.trace(); Debug.trace();
if (locked) return gain; if (locked) return gain;
double max = getPeakValue(true, false); double max = getPeakValue(false);
double d = 0.708 / max; double d = 0.708 / max;
if (d > 1d) d = 1d; if (d > 1d) d = 1d;
if (d < low) d = low; if (d < low) d = low;
@@ -952,7 +908,7 @@ public class Sentence extends BookTreeNode implements Cacheable {
public double normalize() { public double normalize() {
Debug.trace(); Debug.trace();
if (locked) return gain; if (locked) return gain;
double max = getPeakValue(true, false); double max = getPeakValue(false);
double d = 0.708 / max; double d = 0.708 / max;
if (d > 1d) d = 1d; if (d > 1d) d = 1d;
setGain(d); setGain(d);
@@ -1427,21 +1383,23 @@ public class Sentence extends BookTreeNode implements Cacheable {
String def = AudiobookRecorder.window.getDefaultEffectsChain(); String def = AudiobookRecorder.window.getDefaultEffectsChain();
Effect eff = AudiobookRecorder.window.effects.get(def); if ((def != null) && (AudiobookRecorder.window.effects != null)) {
Effect eff = AudiobookRecorder.window.effects.get(def);
if (effectsEnabled) {
if (eff != null) { if (effectsEnabled) {
eff.init(getAudioFormat().getFrameRate()); if (eff != null) {
eff.process(processedAudio); eff.init(getAudioFormat().getFrameRate());
} eff.process(processedAudio);
}
if (effectChain != null) { if (effectChain != null) {
// Don't double up the default chain // Don't double up the default chain
if (!effectChain.equals(def)) { if (!effectChain.equals(def)) {
eff = AudiobookRecorder.window.effects.get(effectChain); eff = AudiobookRecorder.window.effects.get(effectChain);
if (eff != null) { if (eff != null) {
eff.init(getAudioFormat().getFrameRate()); eff.init(getAudioFormat().getFrameRate());
eff.process(processedAudio); eff.process(processedAudio);
}
} }
} }
} }