Added sentence cache system

This commit is contained in:
2018-09-16 13:55:19 +01:00
parent 9fb3d1f816
commit 43d80c00d9
6 changed files with 100 additions and 3 deletions

View File

@@ -51,7 +51,7 @@
<target name="macapp" depends="build">
<mkdir dir="tmp"/>
<mkdir dir="tmp"/>
<jarbundler
name="AudiobookRecorder"
@@ -92,7 +92,15 @@
<launch4j configFile="dist/windows/windows.xml" outfile="AudiobookRecorder.exe" />
</target>
<target name="dist" depends="macapp,winapp" />
<target name="linuxapp" depends="build">
<concat destfile="AudiobookRecorder.run" binary="true">
<file file="dist/linux/stub" />
<file file="AudiobookRecorder.jar" />
</concat>
<chmod perm="0755" file="AudiobookRecorder.run" />
</target>
<target name="dist" depends="macapp,winapp,linuxapp" />
<target name="tag">
<exec executable="git">
@@ -148,6 +156,15 @@
<arg value="-f"/> <arg value="AudiobookRecorder.exe"/>
<arg value="-n"/> <arg value="AudiobookRecorder.exe"/>
</exec>
<echo>Uploading AudiobookRecorder.run</echo>
<exec executable="github-release">
<arg value="upload"/>
<arg value="-u"/> <arg value="MajenkoProjects"/>
<arg value="-r"/> <arg value="AudiobookRecorder"/>
<arg value="-t"/> <arg value="v${version}"/>
<arg value="-f"/> <arg value="AudiobookRecorder.run"/>
<arg value="-n"/> <arg value="AudiobookRecorder.run"/>
</exec>
</target>
</project>

View File

@@ -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);
}
}

View File

@@ -0,0 +1,28 @@
package uk.co.majenko.audiobookrecorder;
import java.util.*;
public class CacheManager {
static ArrayList<Cacheable> cache = new ArrayList<Cacheable>();
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;
}
}

View File

@@ -0,0 +1,6 @@
package uk.co.majenko.audiobookrecorder;
public interface Cacheable {
public abstract void clearCache();
public abstract boolean lockedInCache();
}

View File

@@ -26,6 +26,7 @@ public class Options extends JDialog {
JComboBox<KVPair> bitRate;
JComboBox<KVPair> exportRate;
JCheckBox enableParsing;
JSpinner cacheSize;
static HashMap<String, String> 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();
}

View File

@@ -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");
}
}