From b523d80c25fd5b96238e4fb90147517634bf5655 Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Tue, 25 Aug 2020 17:10:39 +0100 Subject: [PATCH] Move normalize chapter to queue system --- .../audiobookrecorder/AudiobookRecorder.java | 52 ++++--------------- .../majenko/audiobookrecorder/Sentence.java | 24 ++++++++- 2 files changed, 33 insertions(+), 43 deletions(-) diff --git a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java index c752f35..1d7f75c 100644 --- a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java +++ b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java @@ -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; diff --git a/src/uk/co/majenko/audiobookrecorder/Sentence.java b/src/uk/co/majenko/audiobookrecorder/Sentence.java index d970ad6..ac812c5 100644 --- a/src/uk/co/majenko/audiobookrecorder/Sentence.java +++ b/src/uk/co/majenko/audiobookrecorder/Sentence.java @@ -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; + } } } }