From 7545e33d2f69225ea33a26efa5bc53c1384aff24 Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Sat, 20 Jul 2019 23:03:05 +0100 Subject: [PATCH] Make effects two-layerd --- .../audiobookrecorder/AudiobookRecorder.java | 26 +++++++++++--- .../majenko/audiobookrecorder/Sentence.java | 34 ++++++++----------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java index f0495e3..c3a5892 100644 --- a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java +++ b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java @@ -31,6 +31,8 @@ public class AudiobookRecorder extends JFrame { public static final int PLAYBACK_CHUNK_SIZE = 256; // Was 1024 + public static final String SPHINX_MODEL = "resource:/edu/cmu/sphinx/models/en-us/en-us"; + static Properties config = new Properties(); HashMap effects; @@ -72,8 +74,6 @@ public class AudiobookRecorder extends JFrame { JScrollPane mainScroll; - JDialog equaliserWindow = null; - Book book = null; JTree bookTree; @@ -116,7 +116,7 @@ public class AudiobookRecorder extends JFrame { void initSphinx() { sphinxConfig = new Configuration(); - sphinxConfig.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us"); + sphinxConfig.setAcousticModelPath(AudiobookRecorder.SPHINX_MODEL); sphinxConfig.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict"); sphinxConfig.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin"); @@ -821,7 +821,7 @@ public class AudiobookRecorder extends JFrame { try { Configuration sphinxConfig = new Configuration(); - sphinxConfig.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us"); + sphinxConfig.setAcousticModelPath(AudiobookRecorder.SPHINX_MODEL); sphinxConfig.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict"); sphinxConfig.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin"); @@ -2114,6 +2114,16 @@ public class AudiobookRecorder extends JFrame { play.write(data, 0, data.length); } data = s.getPCMData(); + DefaultMutableTreeNode next = s.getNextSibling(); + if (next != null) { + Thread t = new Thread(new Runnable() { + public void run() { + Sentence ns = (Sentence)next; + ns.loadFile(); // Cache it + } + }); + t.start(); + } for (int pos = 0; pos < data.length; pos += PLAYBACK_CHUNK_SIZE) { sampleWaveform.setPlayMarker(pos / format.getFrameSize()); int l = data.length - pos; @@ -2121,7 +2131,6 @@ public class AudiobookRecorder extends JFrame { play.write(data, pos, l); } - DefaultMutableTreeNode next = s.getNextSibling(); boolean last = false; if (next == null) { last = true; @@ -2860,6 +2869,9 @@ System.err.println(format); while (effectChain.getItemCount() > 0) { effectChain.removeItemAt(0); } + + KVPair none = new KVPair("none", "None"); + effectChain.addItem(none); for (String k : effects.keySet()) { Effect e = effects.get(k); KVPair p = new KVPair(k, e.toString()); @@ -2890,4 +2902,8 @@ System.err.println(format); updateWaveform(); } } + + public String getDefaultEffectsChain() { + return defaultEffectChain; + } } diff --git a/src/uk/co/majenko/audiobookrecorder/Sentence.java b/src/uk/co/majenko/audiobookrecorder/Sentence.java index 70525e9..e8522d5 100644 --- a/src/uk/co/majenko/audiobookrecorder/Sentence.java +++ b/src/uk/co/majenko/audiobookrecorder/Sentence.java @@ -503,7 +503,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { while ((result = recognizer.getResult()) != null) { res += result.getHypothesis(); res += " "; -System.err.println(res); } recognizer.stopRecognition(); @@ -520,7 +519,7 @@ System.err.println(res); try { Configuration sphinxConfig = new Configuration(); - sphinxConfig.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us"); + sphinxConfig.setAcousticModelPath(AudiobookRecorder.SPHINX_MODEL); sphinxConfig.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict"); sphinxConfig.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.bin"); @@ -925,8 +924,6 @@ System.err.println(res); return samples; } - - public void loadFile() { if (audioData != null) return; @@ -963,28 +960,27 @@ System.err.println(res); } // Add processing in here. - if (effectChain == null) effectChain = AudiobookRecorder.window.defaultEffectChain; - - Effect eff = AudiobookRecorder.window.effects.get(effectChain); - if (eff == null) { - effectChain = AudiobookRecorder.window.defaultEffectChain; - eff = AudiobookRecorder.window.effects.get(effectChain); - } + String def = AudiobookRecorder.window.getDefaultEffectsChain(); + Effect eff = AudiobookRecorder.window.effects.get(def); + if (eff != null) { eff.init(getAudioFormat().getFrameRate()); for (int i = 0; i < samples.length; i++) { samples[i] = eff.process(samples[i]); } } - - // Cuts out the computer hum. -// Biquad bq = new Biquad(Biquad.Notch, 140d/44100d, 20.0d, -50); -// DelayLine dl = new DelayLine(); -// dl.addDelayLine(2205, 0.8); -// dl.addDelayLine(4410, 0.4); -// dl.addDelayLine(6615, 0.2); + if (effectChain != null) { + eff = AudiobookRecorder.window.effects.get(effectChain); + if (eff != null) { + eff.init(getAudioFormat().getFrameRate()); + for (int i = 0; i < samples.length; i++) { + samples[i] = eff.process(samples[i]); + } + } + } + // Add final master gain stage for (int i = 0; i < samples.length; i++) { samples[i] = samples[i] * gain; @@ -1031,7 +1027,7 @@ System.err.println(res); } public String getEffectChain() { - if (effectChain == null) return AudiobookRecorder.window.defaultEffectChain; + if (effectChain == null) return "none"; return effectChain; } }