diff --git a/build.xml b/build.xml index b9b3984..de64466 100644 --- a/build.xml +++ b/build.xml @@ -51,7 +51,7 @@ - + - + + + + + + + + + @@ -148,6 +156,15 @@ + Uploading AudiobookRecorder.run + + + + + + + + diff --git a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java index 7e698e9..2dc077c 100644 --- a/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java +++ b/src/uk/co/majenko/audiobookrecorder/AudiobookRecorder.java @@ -225,6 +225,7 @@ public class AudiobookRecorder extends JFrame { Options.loadPreferences(); + CacheManager.setCacheSize(Options.getInteger("cache.size")); setLayout(new BorderLayout()); @@ -495,18 +496,30 @@ public class AudiobookRecorder extends JFrame { centralPanel.getActionMap().put("startRecord", new AbstractAction() { public void actionPerformed(ActionEvent e) { if (bookTree.isEditing()) return; + if (getNoiseFloor() == 0) { + alertNoRoomNoise(); + return; + } startRecording(); } }); centralPanel.getActionMap().put("startRecordNewPara", new AbstractAction() { public void actionPerformed(ActionEvent e) { if (bookTree.isEditing()) return; + if (getNoiseFloor() == 0) { + alertNoRoomNoise(); + return; + } startRecordingNewParagraph(); } }); centralPanel.getActionMap().put("startRerecord", new AbstractAction() { public void actionPerformed(ActionEvent e) { if (bookTree.isEditing()) return; + if (getNoiseFloor() == 0) { + alertNoRoomNoise(); + return; + } startReRecording(); } }); @@ -559,6 +572,8 @@ public class AudiobookRecorder extends JFrame { } catch (Exception e) { e.printStackTrace(); } + + AudiobookRecorder frame = new AudiobookRecorder(); } @@ -1554,6 +1569,10 @@ public class AudiobookRecorder extends JFrame { public void playFromSelectedSentence() { if (selectedSentence == null) return; if (playing != null) return; + if (getNoiseFloor() == 0) { + alertNoRoomNoise(); + return; + } playing = selectedSentence; toolBar.disableSentence(); toolBar.enableStop(); @@ -1649,4 +1668,8 @@ public class AudiobookRecorder extends JFrame { } playing = null; } + + public void alertNoRoomNoise() { + JOptionPane.showMessageDialog(this, "You must record room noise\nbefore recording or playback", "Error", JOptionPane.ERROR_MESSAGE); + } } diff --git a/src/uk/co/majenko/audiobookrecorder/CacheManager.java b/src/uk/co/majenko/audiobookrecorder/CacheManager.java new file mode 100644 index 0000000..428d20e --- /dev/null +++ b/src/uk/co/majenko/audiobookrecorder/CacheManager.java @@ -0,0 +1,28 @@ +package uk.co.majenko.audiobookrecorder; + +import java.util.*; + +public class CacheManager { + static ArrayList cache = new ArrayList(); + + static int cacheSize = 10; + + public static void addToCache(Cacheable c) { + while (cache.size() >= cacheSize) { + Cacheable ob = cache.remove(0); + if (ob.lockedInCache()) { + cache.add(ob); + } else { + ob.clearCache(); + System.err.println("Purged " + ob); + } + } + + cache.add(c); + System.err.println("Cached " + c); + } + + public static void setCacheSize(int c) { + cacheSize = c; + } +} diff --git a/src/uk/co/majenko/audiobookrecorder/Cacheable.java b/src/uk/co/majenko/audiobookrecorder/Cacheable.java new file mode 100644 index 0000000..99d90e8 --- /dev/null +++ b/src/uk/co/majenko/audiobookrecorder/Cacheable.java @@ -0,0 +1,6 @@ +package uk.co.majenko.audiobookrecorder; + +public interface Cacheable { + public abstract void clearCache(); + public abstract boolean lockedInCache(); +} diff --git a/src/uk/co/majenko/audiobookrecorder/Options.java b/src/uk/co/majenko/audiobookrecorder/Options.java index 4d609ee..d6d7afb 100644 --- a/src/uk/co/majenko/audiobookrecorder/Options.java +++ b/src/uk/co/majenko/audiobookrecorder/Options.java @@ -26,6 +26,7 @@ public class Options extends JDialog { JComboBox bitRate; JComboBox exportRate; JCheckBox enableParsing; + JSpinner cacheSize; static HashMap defaultPrefs; @@ -260,6 +261,9 @@ public class Options extends JDialog { addSeparator(); + cacheSize = addSpinner("Cache size:", 0, 5000, 1, getInteger("cache.size"), ""); + + addSeparator(); setTitle("Options"); @@ -427,6 +431,8 @@ public class Options extends JDialog { defaultPrefs.put("process.sphinx", "false"); + defaultPrefs.put("cache.size", "100"); + if (prefs == null) { prefs = Preferences.userNodeForPackage(AudiobookRecorder.class); } @@ -487,6 +493,7 @@ public class Options extends JDialog { set("audio.export.bitrate", ((KVPair)bitRate.getSelectedItem()).key); set("audio.export.samplerate", ((KVPair)exportRate.getSelectedItem()).key); set("process.sphinx", enableParsing.isSelected() ? "true" : "false"); + set("cache.size", "" + cacheSize.getValue()); savePreferences(); } diff --git a/src/uk/co/majenko/audiobookrecorder/Sentence.java b/src/uk/co/majenko/audiobookrecorder/Sentence.java index a64b7cf..abd7500 100644 --- a/src/uk/co/majenko/audiobookrecorder/Sentence.java +++ b/src/uk/co/majenko/audiobookrecorder/Sentence.java @@ -13,7 +13,7 @@ import edu.cmu.sphinx.api.*; import edu.cmu.sphinx.decoder.adaptation.*; import edu.cmu.sphinx.result.*; -public class Sentence extends DefaultMutableTreeNode { +public class Sentence extends DefaultMutableTreeNode implements Cacheable { String text; String id; @@ -33,6 +33,8 @@ public class Sentence extends DefaultMutableTreeNode { AudioInputStream inputStream; Thread recordingThread = null; + + int[] storedAudioData = null; public Sentence() { super(""); @@ -119,6 +121,7 @@ public class Sentence extends DefaultMutableTreeNode { e.printStackTrace(); } recording = false; + storedAudioData = null; if (!id.equals("room-noise")) { autoTrimSampleFFT(); @@ -306,6 +309,9 @@ public class Sentence extends DefaultMutableTreeNode { } public int[] getAudioData() { + if (storedAudioData != null) { + return storedAudioData; + } File f = getFile(); try { AudioInputStream s = AudioSystem.getAudioInputStream(f); @@ -335,6 +341,8 @@ public class Sentence extends DefaultMutableTreeNode { } s.close(); sampleSize = samples.length; + storedAudioData = samples; + CacheManager.addToCache(this); return samples; } catch (Exception e) { } @@ -530,4 +538,12 @@ public class Sentence extends DefaultMutableTreeNode { public boolean isInSample() { return inSample; } + + public void clearCache() { + storedAudioData = null; + } + + public boolean lockedInCache() { + return id.equals("room-noise"); + } }