Compare commits

...

4 Commits

18 changed files with 252 additions and 187 deletions

View File

@@ -1 +1 @@
version=0.1.7
version=0.1.8

View File

@@ -25,11 +25,11 @@ 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;
if (absSampleLeft > ceiling) {
gain -= attack;
@@ -48,8 +48,8 @@ public class AGC implements Effect {
}
}
samples[i].left *= gain;
samples[i].right *= gain;
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

@@ -92,6 +92,7 @@ public class AudiobookRecorder extends JFrame {
JSpinner gainPercent;
JCheckBox locked;
JCheckBox attention;
JCheckBox rawAudio;
JButtonSpacePlay reprocessAudioFFT;
JButtonSpacePlay reprocessAudioPeak;
@@ -298,6 +299,7 @@ public class AudiobookRecorder extends JFrame {
window = this;
try {
String clsname = "com.jtattoo.plaf.hifi.HiFiLookAndFeel";
UIManager.setLookAndFeel(clsname);
@@ -312,8 +314,6 @@ public class AudiobookRecorder extends JFrame {
Method mth = cls.getMethod("setCurrentTheme", cArg);
mth.invoke(cls, p);
} catch (Exception e) {
e.printStackTrace();
}
@@ -457,6 +457,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);
@@ -1943,14 +1948,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 +2120,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,9 +2649,13 @@ public class AudiobookRecorder extends JFrame {
public void updateWaveform() {
if (selectedSentence != null) {
if (rawAudio.isSelected()) {
sampleWaveform.setData(selectedSentence.getRawAudioData());
} else {
sampleWaveform.setData(selectedSentence.getDoubleAudioData());
}
}
}
public void loadEffects() {
effects = new TreeMap<String,EffectGroup>();
@@ -2715,6 +2724,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 +2829,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 +2869,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;

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

@@ -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;
}
}
for (DelayLineStore d : delayLines) {
Sample[] subSamples = new Sample[samples.length];
double[][] subSamples = new double[samples.length][2];
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];
}
for (DelayLineStore d : delayLines) {
for (int i = 0; i < samples.length; i++) {
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

@@ -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,13 +58,17 @@ 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;
@@ -134,7 +138,6 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
}
public Sentence() {
super("");
id = UUID.randomUUID().toString();
@@ -173,19 +176,21 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
try {
Thread.sleep(10);
} catch (Exception e) {
e.printStackTrace();
}
}
audioData = null;
// processedAudio = null;
storedFormat = null;
storedLength = -1;
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();
@@ -195,40 +200,19 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
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(boolean useRaw) {
crossStartOffset = -1;
crossEndOffset = -1;
Sample[] samples = getProcessedAudioData();
double[][] samples;
if (useRaw) {
samples = getRawAudioData();
} else {
samples = getProcessedAudioData();
}
if (samples == null) return;
int blocks = samples.length / 4096 + 1;
@@ -242,7 +226,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;
@@ -309,9 +293,18 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
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 +312,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 +327,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;
@@ -565,7 +558,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
public void clearCache() {
audioData = null;
processedAudio = null;
// processedAudio = null;
}
public boolean lockedInCache() {
@@ -573,7 +566,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
public int findNearestZeroCrossing(int pos, int range) {
Sample[] data = getProcessedAudioData();
double[][] data = getProcessedAudioData();
if (data == null) return 0;
if (data.length == 0) return 0;
@@ -583,19 +576,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 +597,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;
}
@@ -649,15 +642,15 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
public double getPeakValue() {
double oldGain = gain;
gain = 1.0d;
Sample[] samples = getProcessedAudioData();
double[][] 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 +853,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 +874,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 +913,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;
}
}
@@ -940,7 +937,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
AudioInputStream s = AudioSystem.getAudioInputStream(f);
AudioFormat format = getAudioFormat();
Sample[] samples = null;
double[][] samples = null;
switch (format.getSampleSizeInBits()) {
case 16:
@@ -959,14 +956,20 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
}
}
public Sample[] getProcessedAudioData() {
synchronized public double[][] getRawAudioData() {
loadFile();
if (processedAudio != null) return processedAudio;
return audioData;
}
synchronized public double[][] getProcessedAudioData() {
loadFile();
// if (processedAudio != null) return processedAudio;
if (audioData == null) return null;
processedAudio = new Sample[audioData.length];
double[][] 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 +995,44 @@ 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

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