Switch to Sample object instead of double, and refactor effects for whole sampleset processing
This commit is contained in:
@@ -25,27 +25,36 @@ public class AGC implements Effect {
|
|||||||
return getName();
|
return getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double process(double sample) {
|
public void process(Sample[] samples) {
|
||||||
double absSample = Math.abs(sample) * gain;
|
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;
|
||||||
|
|
||||||
if (absSample > ceiling) {
|
if (absSampleLeft > ceiling) {
|
||||||
gain -= attack;
|
gain -= attack;
|
||||||
if (gain < 0) gain = 0;
|
if (gain < 0) gain = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (absSample < ceiling) {
|
if (absSampleRight > ceiling) {
|
||||||
|
gain -= attack;
|
||||||
|
if (gain < 0) gain = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((absSampleLeft < ceiling) && (absSampleRight < ceiling)) {
|
||||||
gain += decay;
|
gain += decay;
|
||||||
if (gain > limit) {
|
if (gain > limit) {
|
||||||
gain = limit;
|
gain = limit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sample *= gain;
|
samples[i].left *= gain;
|
||||||
|
samples[i].right *= gain;
|
||||||
return sample;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init(double sr) {
|
public void init(double sr) {
|
||||||
|
gain = 1d;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dump() {
|
public void dump() {
|
||||||
|
|||||||
@@ -19,8 +19,11 @@ public class Amplifier implements Effect {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double process(double sample) {
|
public void process(Sample[] samples) {
|
||||||
return sample * gain;
|
for (int i = 0; i < samples.length; i++) {
|
||||||
|
samples[i].left *= gain;
|
||||||
|
samples[i].right *= gain;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getGain() {
|
public double getGain() {
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class AudiobookRecorder extends JFrame {
|
|||||||
|
|
||||||
// Settings - tweakable
|
// Settings - tweakable
|
||||||
|
|
||||||
public static final int PLAYBACK_CHUNK_SIZE = 256; // Was 1024
|
public static final int PLAYBACK_CHUNK_SIZE = 16384;
|
||||||
|
|
||||||
public static final String SPHINX_MODEL = "resource:/edu/cmu/sphinx/models/en-us/en-us";
|
public static final String SPHINX_MODEL = "resource:/edu/cmu/sphinx/models/en-us/en-us";
|
||||||
|
|
||||||
@@ -1943,14 +1943,14 @@ public class AudiobookRecorder extends JFrame {
|
|||||||
|
|
||||||
public double getNoiseFloor() {
|
public double getNoiseFloor() {
|
||||||
if (roomNoise == null) return 0;
|
if (roomNoise == null) return 0;
|
||||||
double[] samples = roomNoise.getDoubleAudioData();
|
Sample[] samples = roomNoise.getDoubleAudioData();
|
||||||
if (samples == null) {
|
if (samples == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
double ms = 0;
|
double ms = 0;
|
||||||
for (int i = 0; i < samples.length; i++) {
|
for (int i = 0; i < samples.length; i++) {
|
||||||
if (Math.abs(samples[i]) > ms) {
|
if (Math.abs(samples[i].getMono()) > ms) {
|
||||||
ms = Math.abs(samples[i]);
|
ms = Math.abs(samples[i].getMono());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2001,7 +2001,7 @@ public class AudiobookRecorder extends JFrame {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
AudioFormat sampleformat = s.getAudioFormat();
|
AudioFormat sampleformat = s.getAudioFormat();
|
||||||
AudioFormat format = new AudioFormat(sampleformat.getSampleRate(), 16, 1, true, false);
|
AudioFormat format = new AudioFormat(sampleformat.getSampleRate(), 16, 2, true, false);
|
||||||
|
|
||||||
play = AudioSystem.getSourceDataLine(format, Options.getPlaybackMixer());
|
play = AudioSystem.getSourceDataLine(format, Options.getPlaybackMixer());
|
||||||
play.open(format);
|
play.open(format);
|
||||||
@@ -2095,7 +2095,7 @@ public class AudiobookRecorder extends JFrame {
|
|||||||
try {
|
try {
|
||||||
|
|
||||||
AudioFormat sampleformat = s.getAudioFormat();
|
AudioFormat sampleformat = s.getAudioFormat();
|
||||||
AudioFormat format = new AudioFormat(sampleformat.getSampleRate(), 16, 1, true, false);
|
AudioFormat format = new AudioFormat(sampleformat.getSampleRate(), 16, 2, true, false);
|
||||||
play = AudioSystem.getSourceDataLine(format, Options.getPlaybackMixer());
|
play = AudioSystem.getSourceDataLine(format, Options.getPlaybackMixer());
|
||||||
play.open(format);
|
play.open(format);
|
||||||
play.start();
|
play.start();
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ public class Biquad implements Effect {
|
|||||||
int type;
|
int type;
|
||||||
double a0, a1, a2, b1, b2;
|
double a0, a1, a2, b1, b2;
|
||||||
double Fc, Q, peakGain;
|
double Fc, Q, peakGain;
|
||||||
double z1, z2;
|
double lz1, lz2;
|
||||||
|
double rz1, rz2;
|
||||||
double sampleFrequency;
|
double sampleFrequency;
|
||||||
|
|
||||||
public Biquad() {
|
public Biquad() {
|
||||||
@@ -45,15 +46,19 @@ public class Biquad implements Effect {
|
|||||||
Fc = 440d;
|
Fc = 440d;
|
||||||
Q = 0.707d;
|
Q = 0.707d;
|
||||||
peakGain = 0.0d;
|
peakGain = 0.0d;
|
||||||
z1 = 0.0d;
|
lz1 = 0.0d;
|
||||||
z2 = 0.0d;
|
lz2 = 0.0d;
|
||||||
|
rz1 = 0.0d;
|
||||||
|
rz2 = 0.0d;
|
||||||
sampleFrequency = 44100d;
|
sampleFrequency = 44100d;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Biquad(int type, double Fc, double Q, double peakGainDB) {
|
public Biquad(int type, double Fc, double Q, double peakGainDB) {
|
||||||
setBiquad(type, Fc, Q, peakGainDB);
|
setBiquad(type, Fc, Q, peakGainDB);
|
||||||
z1 = 0.0;
|
lz1 = 0.0;
|
||||||
z2 = 0.0;
|
lz2 = 0.0;
|
||||||
|
rz1 = 0.0;
|
||||||
|
rz2 = 0.0;
|
||||||
sampleFrequency = 44100d;
|
sampleFrequency = 44100d;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,17 +89,33 @@ public class Biquad implements Effect {
|
|||||||
setPeakGain(peakGainDB);
|
setPeakGain(peakGainDB);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double process(double in) {
|
public void process(Sample[] samples) {
|
||||||
double out = in * a0 + z1;
|
lz1 = 0d;
|
||||||
z1 = in * a1 + z2 - b1 * out;
|
lz2 = 0d;
|
||||||
z2 = in * a2 - b2 * out;
|
rz1 = 0d;
|
||||||
return out;
|
rz2 = 0d;
|
||||||
|
for (Sample in : samples) {
|
||||||
|
double lout = in.left * a0 + lz1;
|
||||||
|
|
||||||
|
lz1 = in.left * a1 + lz2 - b1 * lout;
|
||||||
|
lz2 = in.left * a2 - b2 * lout;
|
||||||
|
|
||||||
|
double rout = in.right * a0 + rz1;
|
||||||
|
|
||||||
|
rz1 = in.right * a1 + rz2 - b1 * rout;
|
||||||
|
rz2 = in.right * a2 - b2 * rout;
|
||||||
|
|
||||||
|
in.left = lout;
|
||||||
|
in.right = rout;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init(double sf) {
|
public void init(double sf) {
|
||||||
sampleFrequency = sf;
|
sampleFrequency = sf;
|
||||||
z1 = 0d;
|
lz1 = 0d;
|
||||||
z2 = 0d;
|
lz2 = 0d;
|
||||||
|
rz1 = 0d;
|
||||||
|
rz2 = 0d;
|
||||||
calcBiquad();
|
calcBiquad();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,10 +19,13 @@ public class Clipping implements Effect {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double process(double sample) {
|
public void process(Sample[] samples) {
|
||||||
if (sample > clip) return clip;
|
for (Sample sample : samples) {
|
||||||
if (sample < -clip) return -clip;
|
if (sample.left > clip) sample.left = clip;
|
||||||
return sample;
|
if (sample.left < -clip) sample.left = -clip;
|
||||||
|
if (sample.right > clip) sample.right = clip;
|
||||||
|
if (sample.right < -clip) sample.right = -clip;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getClip() {
|
public double getClip() {
|
||||||
|
|||||||
@@ -14,23 +14,52 @@ public class DelayLine implements Effect {
|
|||||||
return "Delay Line (" + delayLines.size() + " lines)";
|
return "Delay Line (" + delayLines.size() + " lines)";
|
||||||
}
|
}
|
||||||
|
|
||||||
public double process(double sample) {
|
public void process(Sample[] samples) {
|
||||||
double s = sample;
|
Sample[] savedSamples = new Sample[samples.length];
|
||||||
for (DelayLineStore d : delayLines) {
|
for (int i = 0; i < samples.length; i++) {
|
||||||
double echo = d.pass(sample);
|
savedSamples[i] = new Sample(samples[i].left, samples[i].right);
|
||||||
s = mix(s, echo);
|
|
||||||
}
|
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
double mix(double a, double b) {
|
for (DelayLineStore d : delayLines) {
|
||||||
if ((a < 0) && (b < 0)) {
|
Sample[] subSamples = new Sample[samples.length];
|
||||||
return (a + b) - (a * b);
|
for (int i = 0; i < samples.length; i++) {
|
||||||
|
subSamples[i] = new Sample(savedSamples[i].left, savedSamples[i].right);
|
||||||
}
|
}
|
||||||
if ((a > 0) && (b > 0)) {
|
|
||||||
return (a + b) - (a * b);
|
d.process(subSamples);
|
||||||
|
|
||||||
|
for (int i = 0; i < subSamples.length; i++) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
return a + b;
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Sample mix(Sample a, Sample b) {
|
||||||
|
Sample out = new Sample(0, 0);
|
||||||
|
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
out.left = a.left + b.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);
|
||||||
|
} else {
|
||||||
|
out.right = a.right + b.right;
|
||||||
|
}
|
||||||
|
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DelayLineStore addDelayLine(int samples, double gain) {
|
public DelayLineStore addDelayLine(int samples, double gain) {
|
||||||
@@ -59,6 +88,4 @@ public class DelayLine implements Effect {
|
|||||||
s.init(sf);
|
s.init(sf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,42 +4,29 @@ import java.util.concurrent.ArrayBlockingQueue;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class DelayLineStore {
|
public class DelayLineStore {
|
||||||
ArrayBlockingQueue<Double> fifo;
|
|
||||||
double gain;
|
double gain;
|
||||||
int numSamples;
|
int numSamples;
|
||||||
|
|
||||||
ArrayList<Effect> effects;
|
ArrayList<Effect> effects;
|
||||||
|
|
||||||
public DelayLineStore(int s, double g) {
|
public DelayLineStore(int s, double g) {
|
||||||
fifo = new ArrayBlockingQueue<Double>(s);
|
|
||||||
for (int i = 0; i < s; i++) {
|
|
||||||
fifo.add(0d);
|
|
||||||
}
|
|
||||||
numSamples = s;
|
numSamples = s;
|
||||||
gain = g;
|
gain = g;
|
||||||
effects = new ArrayList<Effect>();
|
effects = new ArrayList<Effect>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double pass(double s) {
|
public void process(Sample[] samples) {
|
||||||
try {
|
|
||||||
for (Effect e : effects) {
|
for (Effect e : effects) {
|
||||||
s = e.process(s);
|
e.process(samples);
|
||||||
}
|
}
|
||||||
double v = s * gain;
|
|
||||||
double t = fifo.poll();
|
for (Sample sample : samples) {
|
||||||
fifo.add(v);
|
sample.left *= gain;
|
||||||
return t;
|
sample.right *= gain;
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return 0d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSamples(int s) {
|
public void setSamples(int s) {
|
||||||
fifo = new ArrayBlockingQueue<Double>(s);
|
|
||||||
for (int i = 0; i < s; i++) {
|
|
||||||
fifo.add(0d);
|
|
||||||
}
|
|
||||||
numSamples = s;
|
numSamples = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,13 +42,6 @@ public class DelayLineStore {
|
|||||||
return gain;
|
return gain;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void purge() {
|
|
||||||
fifo.clear();
|
|
||||||
for (int i = 0; i < numSamples; i++) {
|
|
||||||
fifo.add(0d);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addEffect(Effect e) {
|
public void addEffect(Effect e) {
|
||||||
effects.add(e);
|
effects.add(e);
|
||||||
}
|
}
|
||||||
@@ -70,7 +50,6 @@ public class DelayLineStore {
|
|||||||
for (Effect e : effects) {
|
for (Effect e : effects) {
|
||||||
e.init(sf);
|
e.init(sf);
|
||||||
}
|
}
|
||||||
purge();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void dump() {
|
public void dump() {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package uk.co.majenko.audiobookrecorder;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public interface Effect {
|
public interface Effect {
|
||||||
public double process(double sample);
|
public void process(Sample[] samples);
|
||||||
public String getName();
|
public String getName();
|
||||||
public ArrayList<Effect> getChildEffects();
|
public ArrayList<Effect> getChildEffects();
|
||||||
public void dump();
|
public void dump();
|
||||||
|
|||||||
@@ -16,12 +16,10 @@ public class EffectGroup implements Effect {
|
|||||||
effects = new ArrayList<Effect>();
|
effects = new ArrayList<Effect>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double process(double sample) {
|
public void process(Sample[] samples) {
|
||||||
double out = sample;
|
|
||||||
for (Effect e : effects) {
|
for (Effect e : effects) {
|
||||||
out = e.process(out);
|
e.process(samples);
|
||||||
}
|
}
|
||||||
return out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String n) {
|
public void setName(String n) {
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ public class LFO implements Effect {
|
|||||||
phase = p;
|
phase = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double process(double sample) {
|
public void process(Sample[] samples) {
|
||||||
|
for (Sample sample : samples) {
|
||||||
double v = Math.sin(phase);
|
double v = Math.sin(phase);
|
||||||
phase += sampleStep;
|
phase += sampleStep;
|
||||||
if (phase > (Math.PI * 2d)) {
|
if (phase > (Math.PI * 2d)) {
|
||||||
@@ -37,9 +38,9 @@ public class LFO implements Effect {
|
|||||||
v *= depth;
|
v *= depth;
|
||||||
|
|
||||||
// Apply it to the sample
|
// Apply it to the sample
|
||||||
sample += (sample * v);
|
sample.left += (sample.left * v);
|
||||||
|
sample.right += (sample.right * v);
|
||||||
return sample;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() { return "Low Frequency Oscillator (" + frequency + " Hz, " + (depth * 100d) + "%)"; }
|
public String getName() { return "Low Frequency Oscillator (" + frequency + " Hz, " + (depth * 100d) + "%)"; }
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package uk.co.majenko.audiobookrecorder;
|
package uk.co.majenko.audiobookrecorder;
|
||||||
|
|
||||||
public class Sample {
|
public class Sample {
|
||||||
double left;
|
public double left;
|
||||||
double right;
|
public double right;
|
||||||
|
|
||||||
public Sample(double m) {
|
public Sample(double m) {
|
||||||
left = m;
|
left = m;
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
AudioFormat storedFormat = null;
|
AudioFormat storedFormat = null;
|
||||||
double storedLength = -1d;
|
double storedLength = -1d;
|
||||||
|
|
||||||
double[] audioData = null;
|
Sample[] audioData = null;
|
||||||
|
|
||||||
RecordingThread recordingThread;
|
RecordingThread recordingThread;
|
||||||
|
|
||||||
@@ -198,7 +198,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
double[] real = new double[FFTBuckets];
|
double[] real = new double[FFTBuckets];
|
||||||
double[] imag = new double[FFTBuckets];
|
double[] imag = new double[FFTBuckets];
|
||||||
|
|
||||||
double[] samples = getProcessedAudioData();
|
Sample[] samples = getProcessedAudioData();
|
||||||
int slices = (samples.length / FFTBuckets) + 1;
|
int slices = (samples.length / FFTBuckets) + 1;
|
||||||
|
|
||||||
double[][] out = new double[slices][];
|
double[][] out = new double[slices][];
|
||||||
@@ -208,7 +208,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
for (int i = 0; i < samples.length; i += FFTBuckets) {
|
for (int i = 0; i < samples.length; i += FFTBuckets) {
|
||||||
for (int j = 0; j < FFTBuckets; j++) {
|
for (int j = 0; j < FFTBuckets; j++) {
|
||||||
if (i + j < samples.length) {
|
if (i + j < samples.length) {
|
||||||
real[j] = samples[i+j];
|
real[j] = samples[i+j].getMono();
|
||||||
imag[j] = 0;
|
imag[j] = 0;
|
||||||
} else {
|
} else {
|
||||||
real[j] = 0;
|
real[j] = 0;
|
||||||
@@ -227,7 +227,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
public void autoTrimSampleFFT() {
|
public void autoTrimSampleFFT() {
|
||||||
crossStartOffset = -1;
|
crossStartOffset = -1;
|
||||||
crossEndOffset = -1;
|
crossEndOffset = -1;
|
||||||
double[] samples = getProcessedAudioData();
|
Sample[] samples = getProcessedAudioData();
|
||||||
if (samples == null) return;
|
if (samples == null) return;
|
||||||
|
|
||||||
int blocks = samples.length / 4096 + 1;
|
int blocks = samples.length / 4096 + 1;
|
||||||
@@ -241,7 +241,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
|
|
||||||
for (int j = 0; j < 4096; j++) {
|
for (int j = 0; j < 4096; j++) {
|
||||||
if (i + j < samples.length) {
|
if (i + j < samples.length) {
|
||||||
real[j] = samples[i+j];
|
real[j] = samples[i+j].getMono();
|
||||||
imag[j] = 0;
|
imag[j] = 0;
|
||||||
} else {
|
} else {
|
||||||
real[j] = 0;
|
real[j] = 0;
|
||||||
@@ -310,7 +310,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
public void autoTrimSamplePeak() {
|
public void autoTrimSamplePeak() {
|
||||||
crossStartOffset = -1;
|
crossStartOffset = -1;
|
||||||
crossEndOffset = -1;
|
crossEndOffset = -1;
|
||||||
double[] samples = getProcessedAudioData();
|
Sample[] samples = getProcessedAudioData();
|
||||||
if (samples == null) return;
|
if (samples == null) return;
|
||||||
double noiseFloor = AudiobookRecorder.window.getNoiseFloor();
|
double noiseFloor = AudiobookRecorder.window.getNoiseFloor();
|
||||||
noiseFloor *= 1.1;
|
noiseFloor *= 1.1;
|
||||||
@@ -318,7 +318,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
// Find start
|
// Find start
|
||||||
for (int i = 0; i < samples.length; i++) {
|
for (int i = 0; i < samples.length; i++) {
|
||||||
startOffset = i;
|
startOffset = i;
|
||||||
if (Math.abs(samples[i]) > noiseFloor) {
|
if (Math.abs(samples[i].getMono()) > noiseFloor) {
|
||||||
startOffset --;
|
startOffset --;
|
||||||
if (startOffset < 0) startOffset = 0;
|
if (startOffset < 0) startOffset = 0;
|
||||||
break;
|
break;
|
||||||
@@ -333,7 +333,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
|
|
||||||
for (int i = samples.length-1; i >= 0; i--) {
|
for (int i = samples.length-1; i >= 0; i--) {
|
||||||
endOffset = i;
|
endOffset = i;
|
||||||
if (Math.abs(samples[i]) > noiseFloor) {
|
if (Math.abs(samples[i].getMono()) > noiseFloor) {
|
||||||
endOffset ++;
|
endOffset ++;
|
||||||
if (endOffset >= samples.length-1) endOffset = samples.length-1;
|
if (endOffset >= samples.length-1) endOffset = samples.length-1;
|
||||||
break;
|
break;
|
||||||
@@ -567,7 +567,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public int findNearestZeroCrossing(int pos, int range) {
|
public int findNearestZeroCrossing(int pos, int range) {
|
||||||
double[] data = getProcessedAudioData();
|
Sample[] data = getProcessedAudioData();
|
||||||
if (data == null) return 0;
|
if (data == null) return 0;
|
||||||
if (data.length == 0) return 0;
|
if (data.length == 0) return 0;
|
||||||
|
|
||||||
@@ -577,19 +577,19 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
int backwards = pos;
|
int backwards = pos;
|
||||||
int forwards = pos;
|
int forwards = pos;
|
||||||
|
|
||||||
double backwardsPrev = data[backwards];
|
double backwardsPrev = data[backwards].getMono();
|
||||||
double forwardsPrev = data[forwards];
|
double forwardsPrev = data[forwards].getMono();
|
||||||
|
|
||||||
while (backwards > 0 || forwards < data.length-2) {
|
while (backwards > 0 || forwards < data.length-2) {
|
||||||
|
|
||||||
if (forwards < data.length-2) forwards++;
|
if (forwards < data.length-2) forwards++;
|
||||||
if (backwards > 0) backwards--;
|
if (backwards > 0) backwards--;
|
||||||
|
|
||||||
if (backwardsPrev >= 0 && data[backwards] < 0) { // Found one!
|
if (backwardsPrev >= 0 && data[backwards].getMono() < 0) { // Found one!
|
||||||
return backwards;
|
return backwards;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (forwardsPrev < 0 && data[forwards] >= 0) {
|
if (forwardsPrev < 0 && data[forwards].getMono() >= 0) {
|
||||||
return forwards;
|
return forwards;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -598,8 +598,8 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
backwardsPrev = data[backwards];
|
backwardsPrev = data[backwards].getMono();
|
||||||
forwardsPrev = data[forwards];
|
forwardsPrev = data[forwards].getMono();
|
||||||
}
|
}
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
@@ -643,15 +643,15 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
public double getPeakValue() {
|
public double getPeakValue() {
|
||||||
double oldGain = gain;
|
double oldGain = gain;
|
||||||
gain = 1.0d;
|
gain = 1.0d;
|
||||||
double[] samples = getProcessedAudioData();
|
Sample[] samples = getProcessedAudioData();
|
||||||
gain = oldGain;
|
gain = oldGain;
|
||||||
if (samples == null) {
|
if (samples == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
double ms = 0;
|
double ms = 0;
|
||||||
for (int i = 0; i < samples.length; i++) {
|
for (int i = 0; i < samples.length; i++) {
|
||||||
if (Math.abs(samples[i]) > ms) {
|
if (Math.abs(samples[i].getMono()) > ms) {
|
||||||
ms = Math.abs(samples[i]);
|
ms = Math.abs(samples[i].getMono());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ms;
|
return ms;
|
||||||
@@ -851,19 +851,18 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
AudiobookRecorder.window.updateWaveform();
|
AudiobookRecorder.window.updateWaveform();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double[] getDoubleDataS16LE(AudioInputStream s, AudioFormat format) throws IOException {
|
public Sample[] getDoubleDataS16LE(AudioInputStream s, AudioFormat format) throws IOException {
|
||||||
long len = s.getFrameLength();
|
long len = s.getFrameLength();
|
||||||
int frameSize = format.getFrameSize();
|
int frameSize = format.getFrameSize();
|
||||||
int chans = format.getChannels();
|
int chans = format.getChannels();
|
||||||
int bytes = frameSize / chans;
|
int bytes = frameSize / chans;
|
||||||
|
|
||||||
byte[] frame = new byte[frameSize];
|
byte[] frame = new byte[frameSize];
|
||||||
double[] samples = new double[(int)len];
|
Sample[] samples = new Sample[(int)len];
|
||||||
|
|
||||||
for (long fno = 0; fno < len; fno++) {
|
for (long fno = 0; fno < len; fno++) {
|
||||||
|
|
||||||
s.read(frame);
|
s.read(frame);
|
||||||
int sample = 0;
|
|
||||||
if (chans == 2) { // Stereo
|
if (chans == 2) { // Stereo
|
||||||
int ll = frame[0] >= 0 ? frame[0] : 256 + frame[0];
|
int ll = frame[0] >= 0 ? frame[0] : 256 + frame[0];
|
||||||
int lh = frame[1] >= 0 ? frame[1] : 256 + frame[1];
|
int lh = frame[1] >= 0 ? frame[1] : 256 + frame[1];
|
||||||
@@ -873,27 +872,27 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
int right = (rh << 8) | rl;
|
int right = (rh << 8) | rl;
|
||||||
if ((left & 0x8000) == 0x8000) left |= 0xFFFF0000;
|
if ((left & 0x8000) == 0x8000) left |= 0xFFFF0000;
|
||||||
if ((right & 0x8000) == 0x8000) right |= 0xFFFF0000;
|
if ((right & 0x8000) == 0x8000) right |= 0xFFFF0000;
|
||||||
sample = (left + right) / 2;
|
samples[(int)fno] = new Sample((double)left / 32768d, (double)right / 32768d);
|
||||||
} else {
|
} else {
|
||||||
int l = frame[0] >= 0 ? frame[0] : 256 + frame[0];
|
int l = frame[0] >= 0 ? frame[0] : 256 + frame[0];
|
||||||
int h = frame[1] >= 0 ? frame[1] : 256 + frame[1];
|
int h = frame[1] >= 0 ? frame[1] : 256 + frame[1];
|
||||||
sample = (h << 8) | l;
|
int mono = (h << 8) | l;
|
||||||
if ((sample & 0x8000) == 0x8000) sample |= 0xFFFF0000;
|
if ((mono & 0x8000) == 0x8000) mono |= 0xFFFF0000;
|
||||||
|
samples[(int)fno] = new Sample((double)mono / 32768d);
|
||||||
}
|
}
|
||||||
samples[(int)fno] = (double)sample / 32768d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return samples;
|
return samples;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double[] getDoubleDataS24LE(AudioInputStream s, AudioFormat format) throws IOException {
|
public Sample[] getDoubleDataS24LE(AudioInputStream s, AudioFormat format) throws IOException {
|
||||||
long len = s.getFrameLength();
|
long len = s.getFrameLength();
|
||||||
int frameSize = format.getFrameSize();
|
int frameSize = format.getFrameSize();
|
||||||
int chans = format.getChannels();
|
int chans = format.getChannels();
|
||||||
int bytes = frameSize / chans;
|
int bytes = frameSize / chans;
|
||||||
|
|
||||||
byte[] frame = new byte[frameSize];
|
byte[] frame = new byte[frameSize];
|
||||||
double[] samples = new double[(int)len];
|
Sample[] samples = new Sample[(int)len];
|
||||||
|
|
||||||
for (long fno = 0; fno < len; fno++) {
|
for (long fno = 0; fno < len; fno++) {
|
||||||
|
|
||||||
@@ -910,15 +909,15 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
int right = (rh << 16) | (rm << 8) | rl;
|
int right = (rh << 16) | (rm << 8) | rl;
|
||||||
if ((left & 0x800000) == 0x800000) left |= 0xFF000000;
|
if ((left & 0x800000) == 0x800000) left |= 0xFF000000;
|
||||||
if ((right & 0x800000) == 0x800000) right |= 0xFF000000;
|
if ((right & 0x800000) == 0x800000) right |= 0xFF000000;
|
||||||
sample = (left + right) / 2;
|
samples[(int)fno] = new Sample((double)left / 8388608d, (double)right / 8388608d);
|
||||||
} else {
|
} else {
|
||||||
int l = frame[0] >= 0 ? frame[0] : 256 + frame[0];
|
int l = frame[0] >= 0 ? frame[0] : 256 + frame[0];
|
||||||
int m = frame[1] >= 0 ? frame[1] : 256 + frame[1];
|
int m = frame[1] >= 0 ? frame[1] : 256 + frame[1];
|
||||||
int h = frame[2] >= 0 ? frame[2] : 256 + frame[2];
|
int h = frame[2] >= 0 ? frame[2] : 256 + frame[2];
|
||||||
sample = (h << 16) | (m << 8) | l;
|
int mono = (h << 16) | (m << 8) | l;
|
||||||
if ((sample & 0x800000) == 0x800000) sample |= 0xFF000000;
|
if ((mono & 0x800000) == 0x800000) mono |= 0xFF000000;
|
||||||
|
samples[(int)fno] = new Sample((double)mono / 8388608d);
|
||||||
}
|
}
|
||||||
samples[(int)fno] = (double)sample / 8388608d;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return samples;
|
return samples;
|
||||||
@@ -932,7 +931,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
AudioInputStream s = AudioSystem.getAudioInputStream(f);
|
AudioInputStream s = AudioSystem.getAudioInputStream(f);
|
||||||
AudioFormat format = getAudioFormat();
|
AudioFormat format = getAudioFormat();
|
||||||
|
|
||||||
double[] samples = null;
|
Sample[] samples = null;
|
||||||
|
|
||||||
switch (format.getSampleSizeInBits()) {
|
switch (format.getSampleSizeInBits()) {
|
||||||
case 16:
|
case 16:
|
||||||
@@ -951,12 +950,12 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public double[] getProcessedAudioData() {
|
public Sample[] getProcessedAudioData() {
|
||||||
loadFile();
|
loadFile();
|
||||||
if (audioData == null) return null;
|
if (audioData == null) return null;
|
||||||
double[] samples = new double[audioData.length];
|
Sample[] samples = new Sample[audioData.length];
|
||||||
for (int i = 0; i < audioData.length; i++) {
|
for (int i = 0; i < audioData.length; i++) {
|
||||||
samples[i] = audioData[i];
|
samples[i] = new Sample(audioData[i].left, audioData[i].right);
|
||||||
}
|
}
|
||||||
// Add processing in here.
|
// Add processing in here.
|
||||||
|
|
||||||
@@ -966,9 +965,7 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
|
|
||||||
if (eff != null) {
|
if (eff != null) {
|
||||||
eff.init(getAudioFormat().getFrameRate());
|
eff.init(getAudioFormat().getFrameRate());
|
||||||
for (int i = 0; i < samples.length; i++) {
|
eff.process(samples);
|
||||||
samples[i] = eff.process(samples[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (effectChain != null) {
|
if (effectChain != null) {
|
||||||
@@ -977,32 +974,31 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
eff = AudiobookRecorder.window.effects.get(effectChain);
|
eff = AudiobookRecorder.window.effects.get(effectChain);
|
||||||
if (eff != null) {
|
if (eff != null) {
|
||||||
eff.init(getAudioFormat().getFrameRate());
|
eff.init(getAudioFormat().getFrameRate());
|
||||||
for (int i = 0; i < samples.length; i++) {
|
eff.process(samples);
|
||||||
samples[i] = eff.process(samples[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add final master gain stage
|
// Add final master gain stage
|
||||||
for (int i = 0; i < samples.length; i++) {
|
for (int i = 0; i < samples.length; i++) {
|
||||||
samples[i] = samples[i] * gain;
|
samples[i].left = samples[i].left * gain;
|
||||||
|
samples[i].right = samples[i].right * gain;
|
||||||
}
|
}
|
||||||
return samples;
|
return samples;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double[] getDoubleAudioData() {
|
public Sample[] getDoubleAudioData() {
|
||||||
return getProcessedAudioData();
|
return getProcessedAudioData();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double[] getCroppedAudioData() {
|
public Sample[] getCroppedAudioData() {
|
||||||
double[] inSamples = getDoubleAudioData();
|
Sample[] inSamples = getDoubleAudioData();
|
||||||
if (inSamples == null) return null;
|
if (inSamples == null) return null;
|
||||||
updateCrossings();
|
updateCrossings();
|
||||||
|
|
||||||
int length = crossEndOffset - crossStartOffset;
|
int length = crossEndOffset - crossStartOffset;
|
||||||
|
|
||||||
double[] samples = new double[length];
|
Sample[] samples = new Sample[length];
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
samples[i] = inSamples[crossStartOffset + i];
|
samples[i] = inSamples[crossStartOffset + i];
|
||||||
}
|
}
|
||||||
@@ -1010,17 +1006,23 @@ public class Sentence extends DefaultMutableTreeNode implements Cacheable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public byte[] getPCMData() {
|
public byte[] getPCMData() {
|
||||||
double[] croppedData = getCroppedAudioData();
|
Sample[] croppedData = getCroppedAudioData();
|
||||||
if (croppedData == null) return null;
|
if (croppedData == null) return null;
|
||||||
int length = croppedData.length;
|
int length = croppedData.length;
|
||||||
byte[] pcmData = new byte[length * 2];
|
byte[] pcmData = new byte[length * 4];
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
double sd = croppedData[i] * 32768d;
|
double sd = croppedData[i].left * 32768d;
|
||||||
int si = (int)sd;
|
int si = (int)sd;
|
||||||
if (si > 32767) si = 32767;
|
if (si > 32767) si = 32767;
|
||||||
if (si < -32768) si = -32768;
|
if (si < -32768) si = -32768;
|
||||||
pcmData[i * 2] = (byte)(si & 0xFF);
|
pcmData[i * 4] = (byte)(si & 0xFF);
|
||||||
pcmData[(i * 2) + 1] = (byte)((si & 0xFF00) >> 8);
|
pcmData[(i * 4) + 1] = (byte)((si & 0xFF00) >> 8);
|
||||||
|
sd = croppedData[i].right * 32768d;
|
||||||
|
si = (int)sd;
|
||||||
|
if (si > 32767) si = 32767;
|
||||||
|
if (si < -32768) si = -32768;
|
||||||
|
pcmData[(i * 4) + 2] = (byte)(si & 0xFF);
|
||||||
|
pcmData[(i * 4) + 3] = (byte)((si & 0xFF00) >> 8);
|
||||||
}
|
}
|
||||||
return pcmData;
|
return pcmData;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import javax.sound.sampled.*;
|
|||||||
|
|
||||||
public class Waveform extends JPanel implements MouseListener, MouseMotionListener {
|
public class Waveform extends JPanel implements MouseListener, MouseMotionListener {
|
||||||
|
|
||||||
double[] samples = null;
|
Sample[] samples = null;
|
||||||
|
|
||||||
int leftMarker = 0;
|
int leftMarker = 0;
|
||||||
int rightMarker = 0;
|
int rightMarker = 0;
|
||||||
@@ -92,7 +92,7 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
double lmax = 0;
|
double lmax = 0;
|
||||||
|
|
||||||
for (int o = 0; o < step; o++) {
|
for (int o = 0; o < step; o++) {
|
||||||
double sample = samples[offset + (n * step) + o];
|
double sample = samples[offset + (n * step) + o].getMono();
|
||||||
if (sample >= 0) {
|
if (sample >= 0) {
|
||||||
have += sample;
|
have += sample;
|
||||||
hcnt++;
|
hcnt++;
|
||||||
@@ -189,7 +189,7 @@ public class Waveform extends JPanel implements MouseListener, MouseMotionListen
|
|||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setData(double[] s) {
|
public void setData(Sample[] s) {
|
||||||
samples = s;
|
samples = s;
|
||||||
playMarker = 0;
|
playMarker = 0;
|
||||||
repaint();
|
repaint();
|
||||||
|
|||||||
Reference in New Issue
Block a user