Move normalize chapter to queue system

This commit is contained in:
2020-08-25 17:10:39 +01:00
parent 34d1f504c0
commit b523d80c25
2 changed files with 33 additions and 43 deletions

View File

@@ -1689,12 +1689,17 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
JMenuObject o = (JMenuObject)e.getSource();
Chapter chap = (Chapter)o.getObject();
ProgressDialog ed = new ProgressDialog("Normalizing " + chap.getName());
NormalizeThread t = new NormalizeThread(chap, ed);
Thread nt = new Thread(t);
nt.start();
ed.setVisible(true);
for (Enumeration s = c.children(); s.hasMoreElements();) {
Sentence snt = (Sentence)s.nextElement();
if (!snt.isLocked()) {
queueJob(new SentenceJob(snt) {
public void run() {
sentence.normalize();
sentence.autoAddPeakGainPoints();
}
});
}
}
}
});
@@ -2444,41 +2449,6 @@ public class AudiobookRecorder extends JFrame implements DocumentListener {
playingThread.start();
}
class NormalizeThread implements Runnable {
ProgressDialog dialog;
Chapter chapter;
public NormalizeThread(Chapter c, ProgressDialog e) {
super();
Debug.trace();
dialog = e;
chapter = c;
}
@SuppressWarnings("unchecked")
public void run() {
Debug.trace();
int numKids = chapter.getChildCount();
int kidCount = 0;
double lastGain = -1;
double variance = Options.getInteger("audio.recording.variance") / 100d;
for (Enumeration s = chapter.children(); s.hasMoreElements();) {
kidCount++;
dialog.setProgress(kidCount * 2000 / numKids);
Sentence snt = (Sentence)s.nextElement();
if (lastGain == -1) {
lastGain = snt.normalize();
} else {
lastGain = snt.normalize(lastGain - variance, lastGain + variance);
}
snt.autoAddPeakGainPoints();
}
dialog.closeDialog();
}
}
class ExportThread implements Runnable {
ProgressDialog exportDialog;
Chapter chapter;

View File

@@ -803,7 +803,9 @@ public class Sentence extends BookTreeNode implements Cacheable {
public boolean lockedInCache() {
Debug.trace();
return id.equals("room-noise");
if (id.equals("room-noise")) return true;
if (isProcessing()) return true;
return false;
}
public int findNearestZeroCrossing(int pos, int range) {
@@ -1497,7 +1499,12 @@ public class Sentence extends BookTreeNode implements Cacheable {
eff = book.effects.get(effectChain);
if (eff != null) {
eff.init(getAudioFormat().getFrameRate());
eff.process(processedAudio);
// There is a chance another thread could cripple the audio data cache
// so we'll just ignore any errors here.
try {
eff.process(processedAudio);
} catch (Exception ex) {
}
}
}
}
@@ -2029,6 +2036,11 @@ public class Sentence extends BookTreeNode implements Cacheable {
int pos = 0;
double peak = 0;
if (samples == null) {
System.err.println("Um.... no samples...?");
return -1;
}
for (int i = 0; i < samples[LEFT].length; i++) {
if (Math.abs(samples[LEFT][i]) > peak) {
@@ -2069,6 +2081,7 @@ public class Sentence extends BookTreeNode implements Cacheable {
while (true) {
double[][] samples = getProcessedAudioData();
int pos = findBiggestPeak();
if (pos == -1) return;
System.err.println("Biggest peak: " + pos);
if ((Math.abs(samples[LEFT][pos]) < 0.708) && (Math.abs(samples[RIGHT][pos]) < 0.708)) {
System.err.println("Not a peak!");
@@ -2090,8 +2103,15 @@ public class Sentence extends BookTreeNode implements Cacheable {
addGainPoint(pos, 1d);
addGainPoint(end, 1d);
double val = 1d;
while (isClipping(start, end)) {
adjustGainPoint(pos, -0.05);
val -= 0.05d;
if (val < 0.04d) {
System.err.println("Aborting: gain too low");
break;
}
}
}
}