Compare commits

...

12 Commits

20 changed files with 408 additions and 210 deletions

View File

@@ -1 +1 @@
version=0.1.7
version=0.1.9

View File

@@ -25,31 +25,31 @@ public class AGC implements Effect {
return getName();
}
public void process(Sample[] samples) {
public void process(double[][] samples) {
gain = 1d;
for (int i = 0; i < samples.length; i++) {
double absSampleLeft = Math.abs(samples[i].left) * gain;
double absSampleRight = Math.abs(samples[i].right) * gain;
double absSampleLeft = Math.abs(samples[i][Sentence.LEFT]) * gain;
double absSampleRight = Math.abs(samples[i][Sentence.RIGHT]) * gain;
double factor = 0.0d;
if (absSampleLeft > ceiling) {
gain -= attack;
if (gain < 0) gain = 0;
factor = -attack;
}
if (absSampleRight > ceiling) {
gain -= attack;
if (gain < 0) gain = 0;
factor = -attack;
}
if ((absSampleLeft < ceiling) && (absSampleRight < ceiling)) {
gain += decay;
if (gain > limit) {
gain = limit;
}
factor = decay;
}
samples[i].left *= gain;
samples[i].right *= gain;
gain += factor;
if (gain > limit) gain = limit;
samples[i][Sentence.LEFT] *= gain;
samples[i][Sentence.RIGHT] *= gain;
}
}

View File

@@ -19,10 +19,10 @@ public class Amplifier implements Effect {
return null;
}
public void process(Sample[] samples) {
public void process(double[][] samples) {
for (int i = 0; i < samples.length; i++) {
samples[i].left *= gain;
samples[i].right *= gain;
samples[i][Sentence.LEFT] *= gain;
samples[i][Sentence.RIGHT] *= gain;
}
}

View File

@@ -24,6 +24,7 @@ import edu.cmu.sphinx.api.*;
import edu.cmu.sphinx.decoder.adaptation.*;
import edu.cmu.sphinx.result.*;
import org.w3c.dom.Node;
import java.util.concurrent.*;
public class AudiobookRecorder extends JFrame {
@@ -38,6 +39,11 @@ public class AudiobookRecorder extends JFrame {
String defaultEffectChain = "none";
public final static int IDLE = 0;
public final static int RECORDING = 1;
public final static int STOPPING = 2;
public int state = IDLE;
MainToolBar toolBar;
JMenuBar menuBar;
@@ -92,6 +98,7 @@ public class AudiobookRecorder extends JFrame {
JSpinner gainPercent;
JCheckBox locked;
JCheckBox attention;
JCheckBox rawAudio;
JButtonSpacePlay reprocessAudioFFT;
JButtonSpacePlay reprocessAudioPeak;
@@ -298,6 +305,7 @@ public class AudiobookRecorder extends JFrame {
window = this;
try {
String clsname = "com.jtattoo.plaf.hifi.HiFiLookAndFeel";
UIManager.setLookAndFeel(clsname);
@@ -312,8 +320,6 @@ public class AudiobookRecorder extends JFrame {
Method mth = cls.getMethod("setCurrentTheme", cArg);
mth.invoke(cls, p);
} catch (Exception e) {
e.printStackTrace();
}
@@ -457,6 +463,11 @@ public class AudiobookRecorder extends JFrame {
controlsLeft.add(reprocessAudioPeak);
controlsLeft.add(normalizeAudio);
rawAudio = new JCheckBox("Raw Audio");
rawAudio.setFocusable(false);
controlsTop.add(rawAudio);
locked = new JCheckBox("Phrase locked");
locked.setFocusable(false);
@@ -543,6 +554,7 @@ public class AudiobookRecorder extends JFrame {
int i = effectChain.getSelectedIndex();
KVPair<String, String> p = effectChain.getItemAt(i);
if (p == null) return;
CacheManager.removeFromCache(selectedSentence);
selectedSentence.setEffectChain(p.getKey());
updateWaveform();
}
@@ -586,8 +598,13 @@ public class AudiobookRecorder extends JFrame {
centralPanel.getActionMap().put("startRecord", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (bookTree.isEditing()) return;
if (!getLock()) return;
if (bookTree.isEditing()) {
freeLock();
return;
}
if (getNoiseFloor() == 0) {
freeLock();
alertNoRoomNoise();
return;
}
@@ -596,8 +613,13 @@ public class AudiobookRecorder extends JFrame {
});
centralPanel.getActionMap().put("startRecordShort", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (bookTree.isEditing()) return;
if (!getLock()) return;
if (bookTree.isEditing()) {
freeLock();
return;
}
if (getNoiseFloor() == 0) {
freeLock();
alertNoRoomNoise();
return;
}
@@ -606,9 +628,14 @@ public class AudiobookRecorder extends JFrame {
});
centralPanel.getActionMap().put("startRecordNewPara", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (bookTree.isEditing()) return;
if (!getLock()) return;
if (bookTree.isEditing()) {
freeLock();
return;
}
if (getNoiseFloor() == 0) {
alertNoRoomNoise();
freeLock();
return;
}
startRecordingNewParagraph();
@@ -616,8 +643,13 @@ public class AudiobookRecorder extends JFrame {
});
centralPanel.getActionMap().put("startRecordNewSection", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (bookTree.isEditing()) return;
if (!getLock()) return;
if (bookTree.isEditing()) {
freeLock();
return;
}
if (getNoiseFloor() == 0) {
freeLock();
alertNoRoomNoise();
return;
}
@@ -626,8 +658,13 @@ public class AudiobookRecorder extends JFrame {
});
centralPanel.getActionMap().put("startRerecord", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (bookTree.isEditing()) return;
if (!getLock()) return;
if (bookTree.isEditing()) {
freeLock();
return;
}
if (getNoiseFloor() == 0) {
freeLock();
alertNoRoomNoise();
return;
}
@@ -637,7 +674,9 @@ public class AudiobookRecorder extends JFrame {
centralPanel.getActionMap().put("stopRecord", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
if (bookTree.isEditing()) return;
stopLock();
stopRecording();
freeLock();
}
});
centralPanel.getActionMap().put("deleteLast", new AbstractAction() {
@@ -1943,14 +1982,14 @@ public class AudiobookRecorder extends JFrame {
public double getNoiseFloor() {
if (roomNoise == null) return 0;
Sample[] samples = roomNoise.getDoubleAudioData();
double[][] samples = roomNoise.getDoubleAudioData();
if (samples == null) {
return 0;
}
double ms = 0;
for (int i = 0; i < samples.length; i++) {
if (Math.abs(samples[i].getMono()) > ms) {
ms = Math.abs(samples[i].getMono());
if (Math.abs((samples[i][Sentence.LEFT] + samples[i][Sentence.RIGHT]) / 2d) > ms) {
ms = Math.abs((samples[i][Sentence.LEFT] + samples[i][Sentence.RIGHT]) / 2d);
}
}
@@ -2115,15 +2154,15 @@ public class AudiobookRecorder extends JFrame {
}
data = s.getPCMData();
DefaultMutableTreeNode next = s.getNextSibling();
if (next != null) {
Thread t = new Thread(new Runnable() {
public void run() {
Sentence ns = (Sentence)next;
ns.getProcessedAudioData(); // Cache it
}
});
t.start();
}
// if (next != null) {
// Thread t = new Thread(new Runnable() {
// public void run() {
// Sentence ns = (Sentence)next;
// ns.getProcessedAudioData(); // 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;
@@ -2644,7 +2683,11 @@ public class AudiobookRecorder extends JFrame {
public void updateWaveform() {
if (selectedSentence != null) {
sampleWaveform.setData(selectedSentence.getDoubleAudioData());
if (rawAudio.isSelected()) {
sampleWaveform.setData(selectedSentence.getRawAudioData());
} else {
sampleWaveform.setData(selectedSentence.getDoubleAudioData());
}
}
}
@@ -2659,6 +2702,7 @@ public class AudiobookRecorder extends JFrame {
public void loadEffectsFromFolder(File dir) {
if (dir == null) return;
if (!dir.exists()) return;
File[] files = dir.listFiles();
for (File f : files) {
if (f.getName().endsWith(".eff")) {
@@ -2715,6 +2759,11 @@ public class AudiobookRecorder extends JFrame {
if (eff != null) {
group.addEffect(eff);
}
} else if (e.getTagName().equals("chain")) {
Effect eff = (Effect)loadChain(e);
if (eff != null) {
group.addEffect(eff);
}
} else if (e.getTagName().equals("group")) {
Effect eff = (Effect)loadEffectGroup(e);
if (eff != null) {
@@ -2815,6 +2864,11 @@ public class AudiobookRecorder extends JFrame {
if (eff != null) {
store.addEffect(eff);
}
} else if (ie.getTagName().equals("chain")) {
Effect eff = (Effect)loadChain(ie);
if (eff != null) {
store.addEffect(eff);
}
} else if (ie.getTagName().equals("group")) {
Effect eff = (Effect)loadEffectGroup(ie);
if (eff != null) {
@@ -2850,6 +2904,11 @@ public class AudiobookRecorder extends JFrame {
return a;
}
public Chain loadChain(Element root) {
Chain c = new Chain(root.getAttribute("src"));
return c;
}
public Pan loadPan(Element root) {
Pan p = new Pan(Utils.s2d(root.getAttribute("pan")));
return p;
@@ -2925,4 +2984,32 @@ public class AudiobookRecorder extends JFrame {
public String getDefaultEffectsChain() {
return defaultEffectChain;
}
public synchronized boolean getLock() {
if (state == RECORDING) return false;
int counts = 0;
while (state == STOPPING) {
try {
Thread.sleep(100);
} catch (Exception ex) {
ex.printStackTrace();
}
counts++;
if (counts > 100) return false;
}
state = RECORDING;
return true;
}
public void freeLock() {
state = IDLE;
}
public void stopLock() {
state = STOPPING;
}
}

View File

@@ -89,24 +89,24 @@ public class Biquad implements Effect {
setPeakGain(peakGainDB);
}
public void process(Sample[] samples) {
public void process(double[][] samples) {
lz1 = 0d;
lz2 = 0d;
rz1 = 0d;
rz2 = 0d;
for (Sample in : samples) {
double lout = in.left * a0 + lz1;
for (double[] in : samples) {
double lout = in[Sentence.LEFT] * a0 + lz1;
lz1 = in.left * a1 + lz2 - b1 * lout;
lz2 = in.left * a2 - b2 * lout;
lz1 = in[Sentence.LEFT] * a1 + lz2 - b1 * lout;
lz2 = in[Sentence.LEFT] * a2 - b2 * lout;
double rout = in.right * a0 + rz1;
double rout = in[Sentence.RIGHT] * a0 + rz1;
rz1 = in.right * a1 + rz2 - b1 * rout;
rz2 = in.right * a2 - b2 * rout;
rz1 = in[Sentence.RIGHT] * a1 + rz2 - b1 * rout;
rz2 = in[Sentence.RIGHT] * a2 - b2 * rout;
in.left = lout;
in.right = rout;
in[Sentence.LEFT] = lout;
in[Sentence.RIGHT] = rout;
}
}

View File

@@ -18,9 +18,16 @@ public class CacheManager {
}
cache.add(c);
System.gc();
}
public static void setCacheSize(int c) {
cacheSize = c;
}
public static void removeFromCache(Cacheable c) {
cache.remove(c);
c.clearCache();
}
}

View File

@@ -0,0 +1,58 @@
package uk.co.majenko.audiobookrecorder;
import java.util.ArrayList;
public class Chain implements Effect {
String target;
public Chain(String t) {
target = t;
}
public Chain() {
target = null;
}
public void process(double[][] samples) {
if (target != null) {
Effect t = AudiobookRecorder.window.effects.get(target);
if (t != null) {
t.process(samples);
}
}
}
public void setTarget(String t) {
target = t;
}
public String getTarget() {
return target;
}
public String toString() {
return "Chain to " + target;
}
public void dump() {
System.out.println(toString());
}
public void init(double sf) {
if (target != null) {
Effect t = AudiobookRecorder.window.effects.get(target);
if (t != null) {
t.init(sf);
}
}
}
public ArrayList<Effect> getChildEffects() {
return null;
}
public String getName() {
return toString();
}
}

View File

@@ -122,7 +122,7 @@ public class Chapter extends DefaultMutableTreeNode {
AudioFormat sampleformat = AudiobookRecorder.window.roomNoise.getAudioFormat();
AudioFormat format = new AudioFormat(sampleformat.getSampleRate(), 16, 1, true, false);
AudioFormat format = new AudioFormat(sampleformat.getSampleRate(), 16, 2, true, false);
byte[] data;
int fullLength = 0;

View File

@@ -19,12 +19,12 @@ public class Clipping implements Effect {
return null;
}
public void process(Sample[] samples) {
for (Sample sample : samples) {
if (sample.left > clip) sample.left = clip;
if (sample.left < -clip) sample.left = -clip;
if (sample.right > clip) sample.right = clip;
if (sample.right < -clip) sample.right = -clip;
public void process(double[][] samples) {
for (double[] sample : samples) {
if (sample[Sentence.LEFT] > clip) sample[Sentence.LEFT] = clip;
if (sample[Sentence.LEFT] < -clip) sample[Sentence.LEFT] = -clip;
if (sample[Sentence.RIGHT] > clip) sample[Sentence.RIGHT] = clip;
if (sample[Sentence.RIGHT] < -clip) sample[Sentence.RIGHT] = -clip;
}
}

View File

@@ -16,22 +16,28 @@ public class DelayLine implements Effect {
return "Delay Line (" + delayLines.size() + " lines)";
}
public void process(Sample[] samples) {
Sample[] savedSamples = new Sample[samples.length];
public void process(double[][] samples) {
double[][] savedSamples = new double[samples.length][2];
for (int i = 0; i < samples.length; i++) {
savedSamples[i] = new Sample(samples[i].left, samples[i].right);
savedSamples[i][Sentence.LEFT] = samples[i][Sentence.LEFT];
savedSamples[i][Sentence.RIGHT] = samples[i][Sentence.RIGHT];
}
if (wetOnly) {
for (int i = 0; i < samples.length; i++) {
samples[i].left = 0d;
samples[i].right = 0d;
samples[i][Sentence.LEFT] = 0d;
samples[i][Sentence.RIGHT] = 0d;
}
}
double[][] subSamples = new double[samples.length][2];
for (int i = 0; i < samples.length; i++) {
subSamples[i][Sentence.LEFT] = savedSamples[i][Sentence.LEFT];
subSamples[i][Sentence.RIGHT] = savedSamples[i][Sentence.RIGHT];
}
for (DelayLineStore d : delayLines) {
Sample[] subSamples = new Sample[samples.length];
for (int i = 0; i < samples.length; i++) {
subSamples[i] = new Sample(savedSamples[i].left, savedSamples[i].right);
subSamples[i][Sentence.LEFT] = savedSamples[i][Sentence.LEFT];
subSamples[i][Sentence.RIGHT] = savedSamples[i][Sentence.RIGHT];
}
d.process(subSamples);
@@ -40,31 +46,31 @@ public class DelayLine implements Effect {
int off = i + d.getSamples();
if ((off < samples.length) && (off > 0)) {
Sample ns = mix(samples[off], subSamples[i]);
samples[off].left = ns.left;
samples[off].right = ns.right;
double[] ns = mix(samples[off], subSamples[i]);
samples[off][Sentence.LEFT] = ns[Sentence.LEFT];
samples[off][Sentence.RIGHT] = ns[Sentence.RIGHT];
}
}
}
}
Sample mix(Sample a, Sample b) {
Sample out = new Sample(0, 0);
double[] mix(double[] a, double[] b) {
double[] out = new double[2];
if ((a.left < 0) && (b.left < 0)) {
out.left = (a.left + b.left) - (a.left * b.left);
} else if ((a.left > 0) && (b.left > 0)) {
out.left = (a.left + b.left) - (a.left * b.left);
if ((a[Sentence.LEFT] < 0) && (b[Sentence.LEFT] < 0)) {
out[Sentence.LEFT] = (a[Sentence.LEFT] + b[Sentence.LEFT]) - (a[Sentence.LEFT] * b[Sentence.LEFT]);
} else if ((a[Sentence.LEFT] > 0) && (b[Sentence.LEFT] > 0)) {
out[Sentence.LEFT] = (a[Sentence.LEFT] + b[Sentence.LEFT]) - (a[Sentence.LEFT] * b[Sentence.LEFT]);
} else {
out.left = a.left + b.left;
out[Sentence.LEFT] = a[Sentence.LEFT] + b[Sentence.LEFT];
}
if ((a.right < 0) && (b.right < 0)) {
out.right = (a.right + b.right) - (a.right * b.right);
} else if ((a.right > 0) && (b.right > 0)) {
out.right = (a.right + b.right) - (a.right * b.right);
if ((a[Sentence.RIGHT] < 0) && (b[Sentence.RIGHT] < 0)) {
out[Sentence.RIGHT] = (a[Sentence.RIGHT] + b[Sentence.RIGHT]) - (a[Sentence.RIGHT] * b[Sentence.RIGHT]);
} else if ((a[Sentence.RIGHT] > 0) && (b[Sentence.RIGHT] > 0)) {
out[Sentence.RIGHT] = (a[Sentence.RIGHT] + b[Sentence.RIGHT]) - (a[Sentence.RIGHT] * b[Sentence.RIGHT]);
} else {
out.right = a.right + b.right;
out[Sentence.RIGHT] = a[Sentence.RIGHT] + b[Sentence.RIGHT];
}
return out;

View File

@@ -24,21 +24,21 @@ public class DelayLineStore {
effects = new ArrayList<Effect>();
}
public void process(Sample[] samples) {
public void process(double[][] samples) {
for (Effect e : effects) {
e.process(samples);
}
for (Sample sample : samples) {
sample.left *= gain;
sample.right *= gain;
for (double[] sample : samples) {
sample[Sentence.LEFT] *= gain;
sample[Sentence.RIGHT] *= gain;
if (pan < 0) {
double p = 1 + pan;
sample.right *= p;
sample[Sentence.RIGHT] *= p;
} else {
double p = 1 - pan;
sample.left *= p;
sample[Sentence.LEFT] *= p;
}
}
}

View File

@@ -3,7 +3,7 @@ package uk.co.majenko.audiobookrecorder;
import java.util.ArrayList;
public interface Effect {
public void process(Sample[] samples);
public void process(double[][] samples);
public String getName();
public ArrayList<Effect> getChildEffects();
public void dump();

View File

@@ -16,7 +16,7 @@ public class EffectGroup implements Effect {
effects = new ArrayList<Effect>();
}
public void process(Sample[] samples) {
public void process(double[][] samples) {
for (Effect e : effects) {
e.process(samples);
}

View File

@@ -22,8 +22,8 @@ public class LFO implements Effect {
phase = p;
}
public void process(Sample[] samples) {
for (Sample sample : samples) {
public void process(double[][] samples) {
for (double[] sample : samples) {
double v = Math.sin(phase);
phase += sampleStep;
if (phase > (Math.PI * 2d)) {
@@ -38,8 +38,8 @@ public class LFO implements Effect {
v *= depth;
// Apply it to the sample
sample.left += (sample.left * v);
sample.right += (sample.right * v);
sample[Sentence.LEFT] += (sample[Sentence.LEFT] * v);
sample[Sentence.RIGHT] += (sample[Sentence.RIGHT] * v);
}
}

View File

@@ -331,7 +331,7 @@ public class Options extends JDialog {
addSeparator(optionsPanel);
cacheSize = addSpinner(optionsPanel, "Cache size:", 0, 5000, 1, getInteger("cache.size"), "");
cacheSize = addSpinner(optionsPanel, "Cache size:", 2, 100, 1, getInteger("cache.size"), "");
addSeparator(optionsPanel);
tabs.add("Options", new JScrollPane(optionsPanel));
@@ -775,9 +775,10 @@ public class Options extends JDialog {
}
public static KVPair[] getTrimMethods() {
KVPair[] pairs = new KVPair[2];
pairs[0] = new KVPair<String, String>("peak", "Peak Amplitude");
pairs[1] = new KVPair<String, String>("fft", "FFT Analysis");
KVPair[] pairs = new KVPair[3];
pairs[0] = new KVPair<String, String>("none", "None");
pairs[1] = new KVPair<String, String>("peak", "Peak Amplitude");
pairs[2] = new KVPair<String, String>("fft", "FFT Analysis");
return pairs;
}

View File

@@ -19,14 +19,14 @@ public class Pan implements Effect {
return null;
}
public void process(Sample[] samples) {
for (Sample sample : samples) {
public void process(double[][] samples) {
for (double[] sample : samples) {
if (pan < 0) {
double p = 1 + pan;
sample.right *= p;
sample[Sentence.RIGHT] *= p;
} else {
double p = 1 - pan;
sample.left *= p;
sample[Sentence.LEFT] *= p;
}
}
}

View File

@@ -1,28 +0,0 @@
package uk.co.majenko.audiobookrecorder;
public class Sample {
public double left;
public double right;
public Sample(double m) {
left = m;
right = m;
}
public Sample(double l, double r) {
left = l;
right = r;
}
public double getLeft() {
return left;
}
public double getRight() {
return right;
}
public double getMono() {
return (left + right) / 2.0;
}
}

View File

@@ -58,14 +58,18 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
public void setOverrideText(String s) { overrideText = s; }
public String getOverrideText() { return overrideText; }
public static final int LEFT = 0;
public static final int RIGHT = 1;
TargetDataLine line;
AudioInputStream inputStream;
AudioFormat storedFormat = null;
double storedLength = -1d;
Sample[] audioData = null;
Sample[] processedAudio = null;
double[][] audioData = null;
double[][] processedAudio = null;
RecordingThread recordingThread;
boolean effectEthereal = false;
@@ -90,7 +94,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
try {
running = true;
recording = true;
byte[] buf = new byte[AudiobookRecorder.window.microphone.getBufferSize()];
byte[] buf = new byte[1024]; //AudiobookRecorder.window.microphone.getBufferSize()];
FileOutputStream fos = new FileOutputStream(tempFile);
int len = 0;
AudiobookRecorder.window.microphone.flush();
@@ -134,7 +138,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
}
public Sentence() {
super("");
id = UUID.randomUUID().toString();
@@ -157,6 +160,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
return false;
}
CacheManager.removeFromCache(this);
recordingThread = new RecordingThread(getTempFile(), getFile(), Options.getAudioFormat());
@@ -173,63 +177,49 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
CacheManager.removeFromCache(this);
audioData = null;
storedFormat = null;
storedLength = -1;
processedAudio = null;
if (!id.equals("room-noise")) {
String tm = Options.get("audio.recording.trim");
if (tm.equals("peak")) {
autoTrimSamplePeak();
autoTrimSamplePeak(true);
} else if (tm.equals("fft")) {
autoTrimSampleFFT();
autoTrimSampleFFT(true);
}
if (Options.getBoolean("process.sphinx")) {
recognise();
}
}
}
public static final int FFTBuckets = 1024;
public double[][] getFFTProfile() {
double[] real = new double[FFTBuckets];
double[] imag = new double[FFTBuckets];
Sample[] samples = getProcessedAudioData();
int slices = (samples.length / FFTBuckets) + 1;
double[][] out = new double[slices][];
int slice = 0;
for (int i = 0; i < samples.length; i += FFTBuckets) {
for (int j = 0; j < FFTBuckets; j++) {
if (i + j < samples.length) {
real[j] = samples[i+j].getMono();
imag[j] = 0;
} else {
real[j] = 0;
imag[j] = 0;
}
}
out[slice++] = FFT.fft(real, imag, true);
}
return out;
public void autoTrimSampleFFT() {
autoTrimSampleFFT(false);
}
public void autoTrimSampleFFT() {
public void autoTrimSampleFFT(boolean useRaw) {
crossStartOffset = -1;
crossEndOffset = -1;
Sample[] samples = getProcessedAudioData();
if (samples == null) return;
double[][] samples;
if (useRaw) {
samples = getRawAudioData();
} else {
samples = getProcessedAudioData();
}
if (samples == null) {
System.err.println("Error: loading data failed!");
return;
}
int blocks = samples.length / 4096 + 1;
@@ -242,7 +232,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
for (int j = 0; j < 4096; j++) {
if (i + j < samples.length) {
real[j] = samples[i+j].getMono();
real[j] = (samples[i+j][LEFT] + samples[i+j][RIGHT]) / 2d;
imag[j] = 0;
} else {
real[j] = 0;
@@ -268,6 +258,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
block++;
}
int limit = Options.getInteger("audio.recording.trim.fft");
// Find first block with > 1 intensity and subtract one.
@@ -304,14 +295,26 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
if (endOffset <= startOffset) endOffset = startOffset + 4096;
if (endOffset < 0) endOffset = 0;
if (endOffset >= samples.length) endOffset = samples.length;
updateCrossings();
updateCrossings(useRaw);
intens = null;
samples = null;
System.gc();
}
public void autoTrimSamplePeak() {
autoTrimSamplePeak(false);
}
public void autoTrimSamplePeak(boolean useRaw) {
crossStartOffset = -1;
crossEndOffset = -1;
Sample[] samples = getProcessedAudioData();
double[][] samples;
if (useRaw) {
samples = getRawAudioData();
} else {
samples = getProcessedAudioData();
}
if (samples == null) return;
double noiseFloor = AudiobookRecorder.window.getNoiseFloor();
noiseFloor *= 1.1;
@@ -319,7 +322,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
// Find start
for (int i = 0; i < samples.length; i++) {
startOffset = i;
if (Math.abs(samples[i].getMono()) > noiseFloor) {
if (Math.abs((samples[i][LEFT] + samples[i][RIGHT])/2d) > noiseFloor) {
startOffset --;
if (startOffset < 0) startOffset = 0;
break;
@@ -334,7 +337,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
for (int i = samples.length-1; i >= 0; i--) {
endOffset = i;
if (Math.abs(samples[i].getMono()) > noiseFloor) {
if (Math.abs((samples[i][LEFT] + samples[i][RIGHT])/2d) > noiseFloor) {
endOffset ++;
if (endOffset >= samples.length-1) endOffset = samples.length-1;
break;
@@ -350,7 +353,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
if (startOffset < 0) startOffset = 0;
if (endOffset >= samples.length) endOffset = samples.length-1;
updateCrossings();
updateCrossings(useRaw);
}
public String getId() {
@@ -434,19 +437,31 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
public void updateCrossings() {
updateStartCrossing();
updateEndCrossing();
updateCrossings(false);
}
public void updateCrossings(boolean useRaw) {
updateStartCrossing(useRaw);
updateEndCrossing(useRaw);
}
public void updateStartCrossing() {
updateStartCrossing(false);
}
public void updateStartCrossing(boolean useRaw) {
if (crossStartOffset == -1) {
crossStartOffset = findNearestZeroCrossing(startOffset, 4096);
crossStartOffset = findNearestZeroCrossing(useRaw, startOffset, 4096);
}
}
public void updateEndCrossing() {
updateEndCrossing(false);
}
public void updateEndCrossing(boolean useRaw) {
if (crossEndOffset == -1) {
crossEndOffset = findNearestZeroCrossing(endOffset, 4096);
crossEndOffset = findNearestZeroCrossing(useRaw, endOffset, 4096);
}
}
@@ -566,6 +581,8 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
public void clearCache() {
audioData = null;
processedAudio = null;
storedFormat = null;
storedLength = -1;
}
public boolean lockedInCache() {
@@ -573,7 +590,16 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
public int findNearestZeroCrossing(int pos, int range) {
Sample[] data = getProcessedAudioData();
return findNearestZeroCrossing(false, pos, range);
}
public int findNearestZeroCrossing(boolean useRaw, int pos, int range) {
double[][] data = null;
if (useRaw) {
data = getRawAudioData();
} else {
data = getProcessedAudioData();
}
if (data == null) return 0;
if (data.length == 0) return 0;
@@ -583,19 +609,19 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
int backwards = pos;
int forwards = pos;
double backwardsPrev = data[backwards].getMono();
double forwardsPrev = data[forwards].getMono();
double backwardsPrev = (data[backwards][LEFT] + data[backwards][RIGHT]) / 2d;
double forwardsPrev = (data[forwards][LEFT] + data[forwards][RIGHT]) / 2d;
while (backwards > 0 || forwards < data.length-2) {
if (forwards < data.length-2) forwards++;
if (backwards > 0) backwards--;
if (backwardsPrev >= 0 && data[backwards].getMono() < 0) { // Found one!
if (backwardsPrev >= 0 && ((data[backwards][LEFT] + data[backwards][RIGHT]) / 2d) < 0) { // Found one!
return backwards;
}
if (forwardsPrev < 0 && data[forwards].getMono() >= 0) {
if (forwardsPrev < 0 && ((data[forwards][LEFT] + data[forwards][RIGHT]) / 2d) >= 0) {
return forwards;
}
@@ -604,8 +630,8 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
return pos;
}
backwardsPrev = data[backwards].getMono();
forwardsPrev = data[forwards].getMono();
backwardsPrev = (data[backwards][LEFT] + data[backwards][RIGHT]) / 2d;
forwardsPrev = (data[forwards][LEFT] + data[forwards][RIGHT]) / 2d;
}
return pos;
}
@@ -647,17 +673,26 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
public double getPeakValue() {
return getPeakValue(false);
}
public double getPeakValue(boolean useRaw) {
double oldGain = gain;
gain = 1.0d;
Sample[] samples = getProcessedAudioData();
double[][] samples = null;
if (useRaw) {
samples = getRawAudioData();
} else {
samples = getProcessedAudioData();
}
gain = oldGain;
if (samples == null) {
return 0;
}
double ms = 0;
for (int i = 0; i < samples.length; i++) {
if (Math.abs(samples[i].getMono()) > ms) {
ms = Math.abs(samples[i].getMono());
if (Math.abs((samples[i][LEFT] + samples[i][RIGHT]) / 2d) > ms) {
ms = Math.abs((samples[i][LEFT] + samples[i][RIGHT]) / 2d);
}
}
return ms;
@@ -860,14 +895,14 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
AudiobookRecorder.window.updateWaveform();
}
public Sample[] getDoubleDataS16LE(AudioInputStream s, AudioFormat format) throws IOException {
public double[][] getDoubleDataS16LE(AudioInputStream s, AudioFormat format) throws IOException {
long len = s.getFrameLength();
int frameSize = format.getFrameSize();
int chans = format.getChannels();
int bytes = frameSize / chans;
byte[] frame = new byte[frameSize];
Sample[] samples = new Sample[(int)len];
double[][] samples = new double[(int)len][2];
for (long fno = 0; fno < len; fno++) {
@@ -881,27 +916,29 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
int right = (rh << 8) | rl;
if ((left & 0x8000) == 0x8000) left |= 0xFFFF0000;
if ((right & 0x8000) == 0x8000) right |= 0xFFFF0000;
samples[(int)fno] = new Sample((double)left / 32768d, (double)right / 32768d);
samples[(int)fno][LEFT] = (double)left / 32768d;
samples[(int)fno][RIGHT] = (double)right / 32768d;
} else {
int l = frame[0] >= 0 ? frame[0] : 256 + frame[0];
int h = frame[1] >= 0 ? frame[1] : 256 + frame[1];
int mono = (h << 8) | l;
if ((mono & 0x8000) == 0x8000) mono |= 0xFFFF0000;
samples[(int)fno] = new Sample((double)mono / 32768d);
samples[(int)fno][LEFT] = (double)mono / 32768d;
samples[(int)fno][RIGHT] = (double)mono / 32768d;
}
}
return samples;
}
public Sample[] getDoubleDataS24LE(AudioInputStream s, AudioFormat format) throws IOException {
public double[][] getDoubleDataS24LE(AudioInputStream s, AudioFormat format) throws IOException {
long len = s.getFrameLength();
int frameSize = format.getFrameSize();
int chans = format.getChannels();
int bytes = frameSize / chans;
byte[] frame = new byte[frameSize];
Sample[] samples = new Sample[(int)len];
double[][] samples = new double[(int)len][2];
for (long fno = 0; fno < len; fno++) {
@@ -918,14 +955,16 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
int right = (rh << 16) | (rm << 8) | rl;
if ((left & 0x800000) == 0x800000) left |= 0xFF000000;
if ((right & 0x800000) == 0x800000) right |= 0xFF000000;
samples[(int)fno] = new Sample((double)left / 8388608d, (double)right / 8388608d);
samples[(int)fno][LEFT] = (double)left / 8388608d;
samples[(int)fno][RIGHT] = (double)right / 8388608d;
} else {
int l = frame[0] >= 0 ? frame[0] : 256 + frame[0];
int m = frame[1] >= 0 ? frame[1] : 256 + frame[1];
int h = frame[2] >= 0 ? frame[2] : 256 + frame[2];
int mono = (h << 16) | (m << 8) | l;
if ((mono & 0x800000) == 0x800000) mono |= 0xFF000000;
samples[(int)fno] = new Sample((double)mono / 8388608d);
samples[(int)fno][LEFT] = (double)mono / 8388608d;
samples[(int)fno][RIGHT] = (double)mono / 8388608d;
}
}
@@ -933,14 +972,20 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
public void loadFile() {
if (audioData != null) return;
if (audioData != null) {
return;
}
File f = getFile();
try {
if (!f.exists()) {
System.err.println("TODO: Race condition: wav file doesn't exist yet");
return;
}
AudioInputStream s = AudioSystem.getAudioInputStream(f);
AudioFormat format = getAudioFormat();
Sample[] samples = null;
double[][] samples = null;
switch (format.getSampleSizeInBits()) {
case 16:
@@ -956,17 +1001,24 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
audioData = samples;
CacheManager.addToCache(this);
} catch (Exception e) {
e.printStackTrace();
}
}
public Sample[] getProcessedAudioData() {
synchronized public double[][] getRawAudioData() {
loadFile();
return audioData;
}
synchronized public double[][] getProcessedAudioData() {
loadFile();
if (processedAudio != null) return processedAudio;
if (audioData == null) return null;
processedAudio = new Sample[audioData.length];
processedAudio = new double[audioData.length][2];
for (int i = 0; i < audioData.length; i++) {
processedAudio[i] = new Sample(audioData[i].left, audioData[i].right);
processedAudio[i][LEFT] = audioData[i][LEFT];
processedAudio[i][RIGHT] = audioData[i][RIGHT];
}
// Add processing in here.
@@ -992,43 +1044,45 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
// Add final master gain stage
for (int i = 0; i < processedAudio.length; i++) {
processedAudio[i].left = processedAudio[i].left * gain;
processedAudio[i].right = processedAudio[i].right * gain;
processedAudio[i][LEFT] *= gain;
processedAudio[i][RIGHT] *= gain;
}
return processedAudio;
}
public Sample[] getDoubleAudioData() {
public double[][] getDoubleAudioData() {
return getProcessedAudioData();
}
public Sample[] getCroppedAudioData() {
Sample[] inSamples = getDoubleAudioData();
public double[][] getCroppedAudioData() {
double[][] inSamples = getDoubleAudioData();
if (inSamples == null) return null;
updateCrossings();
int length = crossEndOffset - crossStartOffset;
Sample[] samples = new Sample[length];
double[][] samples = new double[length][2];
for (int i = 0; i < length; i++) {
samples[i] = inSamples[crossStartOffset + i];
samples[i][LEFT] = inSamples[crossStartOffset + i][LEFT];
samples[i][RIGHT] = inSamples[crossStartOffset + i][RIGHT];
}
return samples;
}
public byte[] getPCMData() {
Sample[] croppedData = getCroppedAudioData();
double[][] croppedData = getCroppedAudioData();
if (croppedData == null) return null;
int length = croppedData.length;
byte[] pcmData = new byte[length * 4];
for (int i = 0; i < length; i++) {
double sd = croppedData[i].left * 32768d;
double sd = croppedData[i][LEFT] * 32768d;
int si = (int)sd;
if (si > 32767) si = 32767;
if (si < -32768) si = -32768;
pcmData[i * 4] = (byte)(si & 0xFF);
pcmData[(i * 4) + 1] = (byte)((si & 0xFF00) >> 8);
sd = croppedData[i].right * 32768d;
sd = croppedData[i][RIGHT] * 32768d;
si = (int)sd;
if (si > 32767) si = 32767;
if (si < -32768) si = -32768;

View File

@@ -63,4 +63,17 @@ public class Utils {
}
}
}
static long millis = System.currentTimeMillis();
public static void report(String tag) {
long t = System.currentTimeMillis();
long d = t - millis;
millis = t;
System.err.println(String.format("%10d - %10s : %8d | %8d | %8d", d, tag,
Runtime.getRuntime().totalMemory(),
Runtime.getRuntime().maxMemory(),
Runtime.getRuntime().freeMemory()
));
}
}

View File

@@ -9,7 +9,7 @@ import javax.sound.sampled.*;
public class Waveform extends JPanel implements MouseListener, MouseMotionListener {
Sample[] samples = null;
double[][] samples = null;
int leftMarker = 0;
int rightMarker = 0;
@@ -92,7 +92,7 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
double lmax = 0;
for (int o = 0; o < step; o++) {
double sample = samples[offset + (n * step) + o].getMono();
double sample = (samples[offset + (n * step) + o][Sentence.LEFT] + samples[offset + (n * step) + o][Sentence.RIGHT]) / 2d;
if (sample >= 0) {
have += sample;
hcnt++;
@@ -189,7 +189,7 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
repaint();
}
public void setData(Sample[] s) {
public void setData(double[][] s) {
samples = s;
playMarker = 0;
repaint();