From cd24beb8a61dbba418bb7af487ac3e736e073985 Mon Sep 17 00:00:00 2001 From: Matt Jenkins Date: Thu, 5 Sep 2019 00:20:29 +0100 Subject: [PATCH] Removed pointless garbage collection calls. Massive speedup --- .../audiobookrecorder/AudiobookRecorder.java | 116 +++++++++++++++--- .../audiobookrecorder/CacheManager.java | 4 - .../co/majenko/audiobookrecorder/Debug.java | 12 ++ .../majenko/audiobookrecorder/Sentence.java | 17 +-- 4 files changed, 110 insertions(+), 39 deletions(-) create mode 100644 src/uk/co/majenko/audiobookrecorder/Debug.java diff --git a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java index f93ea64..aa5cb19 100644 --- a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java +++ b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java @@ -1165,26 +1165,28 @@ public class AudiobookRecorder extends JFrame { JMenuObject peak = new JMenuObject("Auto-trim all (Peak)", c, new ActionListener() { public void actionPerformed(ActionEvent e) { JMenuObject o = (JMenuObject)e.getSource(); - Chapter c = (Chapter)o.getObject(); - for (Enumeration s = c.children(); s.hasMoreElements();) { - Sentence snt = (Sentence)s.nextElement(); - if (!snt.isLocked()) { - snt.autoTrimSamplePeak(); - } - } + Chapter chap = (Chapter)o.getObject(); + + ProgressDialog ed = new ProgressDialog("Auto-trimming " + chap.getName()); + + AutoTrimThread t = new AutoTrimThread(chap, ed, AutoTrimThread.Peak); + Thread nt = new Thread(t); + nt.start(); + ed.setVisible(true); } }); JMenuObject fft = new JMenuObject("Auto-trim all (FFT)", c, new ActionListener() { public void actionPerformed(ActionEvent e) { JMenuObject o = (JMenuObject)e.getSource(); - Chapter c = (Chapter)o.getObject(); - for (Enumeration s = c.children(); s.hasMoreElements();) { - Sentence snt = (Sentence)s.nextElement(); - if (!snt.isLocked()) { - snt.autoTrimSampleFFT(); - } - } + Chapter chap = (Chapter)o.getObject(); + + ProgressDialog ed = new ProgressDialog("Auto-trimming " + chap.getName()); + + AutoTrimThread t = new AutoTrimThread(chap, ed, AutoTrimThread.FFT); + Thread nt = new Thread(t); + nt.start(); + ed.setVisible(true); } }); @@ -1329,12 +1331,16 @@ public class AudiobookRecorder extends JFrame { JMenuObject normalizeAll = new JMenuObject("Normalize chapter", c, new ActionListener() { public void actionPerformed(ActionEvent e) { + JMenuObject o = (JMenuObject)e.getSource(); - Chapter c = (Chapter)o.getObject(); - for (Enumeration s = c.children(); s.hasMoreElements();) { - Sentence snt = (Sentence)s.nextElement(); - snt.normalize(); - } + 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); } }); @@ -2215,6 +2221,72 @@ public class AudiobookRecorder extends JFrame { playingThread.start(); } + class NormalizeThread implements Runnable { + ProgressDialog dialog; + Chapter chapter; + + public NormalizeThread(Chapter c, ProgressDialog e) { + super(); + dialog = e; + chapter = c; + } + + @SuppressWarnings("unchecked") + public void run() { + + int numKids = chapter.getChildCount(); + int kidCount = 0; + for (Enumeration s = chapter.children(); s.hasMoreElements();) { + kidCount++; + dialog.setProgress(kidCount * 2000 / numKids); + Sentence snt = (Sentence)s.nextElement(); + snt.normalize(); + } + + dialog.closeDialog(); + } + } + + class AutoTrimThread implements Runnable { + ProgressDialog dialog; + Chapter chapter; + int type; + + public final static int FFT = 0; + public final static int Peak = 1; + + public AutoTrimThread(Chapter c, ProgressDialog e, int t) { + super(); + dialog = e; + chapter = c; + type = t; + } + + @SuppressWarnings("unchecked") + public void run() { + + int numKids = chapter.getChildCount(); + int kidCount = 0; + for (Enumeration s = chapter.children(); s.hasMoreElements();) { + kidCount++; + dialog.setProgress(kidCount * 2000 / numKids); + Sentence snt = (Sentence)s.nextElement(); + switch (type) { + case FFT: + snt.autoTrimSampleFFT(); + break; + case Peak: + snt.autoTrimSamplePeak(); + break; + } + } + + dialog.closeDialog(); + } + } + + + class ExportThread implements Runnable { ProgressDialog exportDialog; Chapter chapter; @@ -2398,6 +2470,12 @@ public class AudiobookRecorder extends JFrame { play.write(data, 0, data.length); playing = null; } else { + play.drain(); + play.stop(); + play.close(); + play.open(format); + play.start(); + play.drain(); data = getRoomNoise(s.getPostGap()); play.write(data, 0, data.length); } diff --git a/src/uk/co/majenko/audiobookrecorder/CacheManager.java b/src/uk/co/majenko/audiobookrecorder/CacheManager.java index 537a5a8..22a2675 100644 --- a/src/uk/co/majenko/audiobookrecorder/CacheManager.java +++ b/src/uk/co/majenko/audiobookrecorder/CacheManager.java @@ -15,15 +15,12 @@ public class CacheManager { } else { if (ob instanceof Sentence) { Sentence s = (Sentence)ob; - s.debug("Normal removal from cache"); } ob.clearCache(); } } cache.add(c); - - System.gc(); } public static void setCacheSize(int c) { @@ -33,7 +30,6 @@ public class CacheManager { public static void removeFromCache(Cacheable c) { if (c instanceof Sentence) { Sentence s = (Sentence)c; - s.debug("Manual removal from cache"); } cache.remove(c); c.clearCache(); diff --git a/src/uk/co/majenko/audiobookrecorder/Debug.java b/src/uk/co/majenko/audiobookrecorder/Debug.java new file mode 100644 index 0000000..3134850 --- /dev/null +++ b/src/uk/co/majenko/audiobookrecorder/Debug.java @@ -0,0 +1,12 @@ +package uk.co.majenko.audiobookrecorder; + +public class Debug { + static long timestamp; + + static void debug(String msg) { + long now = System.currentTimeMillis(); + long diff = now - timestamp; + timestamp = now; + System.err.println(String.format("%8d - %s", diff, msg)); + } +} diff --git a/src/uk/co/majenko/audiobookrecorder/Sentence.java b/src/uk/co/majenko/audiobookrecorder/Sentence.java index 7a2d859..62f8abb 100644 --- a/src/uk/co/majenko/audiobookrecorder/Sentence.java +++ b/src/uk/co/majenko/audiobookrecorder/Sentence.java @@ -161,7 +161,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { return false; } - debug("Start recording based purge"); CacheManager.removeFromCache(this); recordingThread = new RecordingThread(getTempFile(), getFile(), Options.getAudioFormat()); @@ -183,7 +182,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { } } - debug("Stop recording based purge"); CacheManager.removeFromCache(this); if (!id.equals("room-noise")) { @@ -224,7 +222,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { samples = getProcessedAudioData(); } if (samples == null) { - debug("Error: loading data failed!"); return; } @@ -306,7 +303,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { updateCrossings(useRaw); intens = null; samples = null; - System.gc(); } @@ -587,7 +583,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { } public void clearCache() { - debug("Clearing cached data"); audioData = null; processedAudio = null; storedFormat = null; @@ -723,7 +718,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { int gint = (int)(g * 100d); int gainint = (int)(gain * 100d); if (gint != gainint) { - debug("Gain based purge"); CacheManager.removeFromCache(this); } gain = g; @@ -770,7 +764,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { proc.waitFor(); } catch (Exception e) { } - debug("External editor based purge"); CacheManager.removeFromCache(Sentence.this); AudiobookRecorder.window.updateWaveform(); } @@ -861,7 +854,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { } } - debug("External processor based purge"); CacheManager.removeFromCache(Sentence.this); AudiobookRecorder.window.updateWaveform(); } @@ -905,7 +897,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { e.printStackTrace(); } - debug("Undo based purge"); CacheManager.removeFromCache(this); AudiobookRecorder.window.updateWaveform(); } @@ -1080,7 +1071,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { break; } - debug("Write based purge"); CacheManager.removeFromCache(this); } @@ -1169,7 +1159,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { synchronized public double[][] getProcessedAudioData(boolean effectsEnabled) { loadFile(); if (processedAudio != null) { - debug("Returning cached data"); return processedAudio; } @@ -1186,7 +1175,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { Effect eff = AudiobookRecorder.window.effects.get(def); if (effectsEnabled) { - debug("Processing audio effects"); if (eff != null) { eff.init(getAudioFormat().getFrameRate()); eff.process(processedAudio); @@ -1204,7 +1192,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { } } - debug("Processing done"); // Add final master gain stage for (int i = 0; i < processedAudio.length; i++) { @@ -1212,7 +1199,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { processedAudio[i][RIGHT] *= gain; } - debug("Gain applied"); return processedAudio; } @@ -1272,7 +1258,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { public void setEffectChain(String key) { if ((effectChain != null) && (!effectChain.equals(key))) { - debug("Effects chain based purge"); CacheManager.removeFromCache(this); } effectChain = key; @@ -1321,6 +1306,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable { } public void debug(String txt) { - System.err.println(id + ": " + txt); + Debug.debug(String.format("%s: %s", id, txt)); } }