Skip to content

Commit f892eab

Browse files
committed
Feedback comments from reddit implemented
1 parent 0f333e2 commit f892eab

File tree

4 files changed

+62
-58
lines changed

4 files changed

+62
-58
lines changed

src/main/java/org/sela/Player.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
import org.sela.data.WavFrame;
1515
import org.sela.exception.FileException;
1616

17-
public class Player extends Decoder {
17+
public class Player {
1818
private List<WavFrame> wavFrames;
19+
private Decoder decoder;
1920

2021
public Player(final File inputFile) throws IOException, FileException {
21-
super(inputFile, null);
22-
wavFrames = super.processFrames();
22+
decoder = new Decoder(inputFile, null);
23+
wavFrames = decoder.processFrames();
2324
}
2425

2526
private static void printProgress(final long current, final long total) {
@@ -34,13 +35,13 @@ private static void printProgress(final long current, final long total) {
3435

3536
public void play() throws LineUnavailableException {
3637
// Select audio format parameters
37-
final AudioFormat af = new AudioFormat(super.selaFile.getSampleRate(), super.selaFile.getBitsPerSample(),
38-
super.selaFile.getChannels(), true, false);
38+
final AudioFormat af = new AudioFormat(decoder.selaFile.getSampleRate(), decoder.selaFile.getBitsPerSample(),
39+
decoder.selaFile.getChannels(), true, false);
3940
final DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);
4041
final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
4142

4243
// Prepare audio output
43-
line.open(af, 2048 * super.selaFile.getChannels());
44+
line.open(af, 2048 * decoder.selaFile.getChannels());
4445
line.start();
4546

4647
// Output wave form repeatedly

src/main/java/org/sela/lpc/LinearPredictionBase.java renamed to src/main/java/org/sela/lpc/LinearPredictor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.sela.lpc;
22

3-
public class LinearPredictionBase {
3+
public class LinearPredictor {
44
protected double[] reflectionCoefficients;
55
protected int[] quantizedReflectionCoefficients;
66
protected long[] linearPredictionCoefficients;
@@ -10,12 +10,12 @@ public class LinearPredictionBase {
1010
protected final int maxLpcOrder = 100;
1111
protected final int correctionFactor = 35;
1212

13-
public LinearPredictionBase() {
13+
public LinearPredictor() {
1414
this.reflectionCoefficients = new double[maxLpcOrder];
1515
optimalLpcOrder = 1;
1616
}
1717

18-
public LinearPredictionBase(final int[] quantizedReflectionCoefficients, final byte optimalLpcOrder) {
18+
public LinearPredictor(final int[] quantizedReflectionCoefficients, final byte optimalLpcOrder) {
1919
this.reflectionCoefficients = new double[maxLpcOrder];
2020
this.quantizedReflectionCoefficients = quantizedReflectionCoefficients;
2121
this.optimalLpcOrder = optimalLpcOrder;

src/main/java/org/sela/lpc/ResidueGenerator.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
33
import org.sela.data.LpcDecodedData;
44
import org.sela.data.LpcEncodedData;
55

6-
public final class ResidueGenerator extends LinearPredictionBase {
6+
public final class ResidueGenerator {
77
private final int[] samples;
88
private final int[] residues;
99
private final double[] quantizedSamples;
1010
private final double[] autocorrelationFactors;
11+
private final LinearPredictor linearPredictor;
1112

1213
private final int maxShort = 32767;
1314
private final double sqrt2 = 1.4142135623730950488016887242096;
1415

1516
public ResidueGenerator(final LpcDecodedData data) {
17+
this.linearPredictor = new LinearPredictor();
1618
this.samples = data.samples;
1719
this.quantizedSamples = new double[samples.length];
18-
this.autocorrelationFactors = new double[super.maxLpcOrder + 1];
20+
this.autocorrelationFactors = new double[linearPredictor.maxLpcOrder + 1];
1921
this.residues = new int[samples.length];
2022
}
2123

@@ -35,86 +37,86 @@ private void generateAutoCorrelation() {
3537
mean = sum / quantizedSamples.length;
3638

3739
// Generate autocorrelation coefficients
38-
for (int i = 0; i <= super.maxLpcOrder; i++) {
40+
for (int i = 0; i <= linearPredictor.maxLpcOrder; i++) {
3941
autocorrelationFactors[i] = 0.0;
4042
for (int j = i; j < quantizedSamples.length; j++) {
4143
autocorrelationFactors[i] += (quantizedSamples[j] - mean) * (quantizedSamples[j - i] - mean);
4244
}
4345
}
4446

4547
// Normalise the coefficients
46-
for (int i = 1; i <= super.maxLpcOrder; i++) {
48+
for (int i = 1; i <= linearPredictor.maxLpcOrder; i++) {
4749
autocorrelationFactors[i] /= autocorrelationFactors[0];
4850
}
4951
autocorrelationFactors[0] = 1.0;
5052
}
5153

5254
private void generateReflectionCoefficients() {
5355
double error;
54-
final double[][] gen = new double[2][super.maxLpcOrder];
56+
final double[][] gen = new double[2][linearPredictor.maxLpcOrder];
5557

56-
for (int i = 0; i < super.maxLpcOrder; i++) {
58+
for (int i = 0; i < linearPredictor.maxLpcOrder; i++) {
5759
gen[0][i] = gen[1][i] = autocorrelationFactors[i + 1];
5860
}
5961

6062
error = autocorrelationFactors[0];
61-
super.reflectionCoefficients[0] = -gen[1][0] / error;
62-
error += gen[1][0] * super.reflectionCoefficients[0];
63+
linearPredictor.reflectionCoefficients[0] = -gen[1][0] / error;
64+
error += gen[1][0] * linearPredictor.reflectionCoefficients[0];
6365

64-
for (int i = 1; i < super.maxLpcOrder; i++) {
65-
for (int j = 0; j < super.maxLpcOrder - i; j++) {
66-
gen[1][j] = gen[1][j + 1] + super.reflectionCoefficients[i - 1] * gen[0][j];
67-
gen[0][j] = gen[1][j + 1] * super.reflectionCoefficients[i - 1] + gen[0][j];
66+
for (int i = 1; i < linearPredictor.maxLpcOrder; i++) {
67+
for (int j = 0; j < linearPredictor.maxLpcOrder - i; j++) {
68+
gen[1][j] = gen[1][j + 1] + linearPredictor.reflectionCoefficients[i - 1] * gen[0][j];
69+
gen[0][j] = gen[1][j + 1] * linearPredictor.reflectionCoefficients[i - 1] + gen[0][j];
6870
}
69-
super.reflectionCoefficients[i] = -gen[1][0] / error;
70-
error += gen[1][0] * super.reflectionCoefficients[i];
71+
linearPredictor.reflectionCoefficients[i] = -gen[1][0] / error;
72+
error += gen[1][0] * linearPredictor.reflectionCoefficients[i];
7173
}
7274
}
7375

7476
private void generateoptimalLpcOrder() {
75-
for (int i = super.maxLpcOrder - 1; i >= 0; i--) {
76-
if (Math.abs(super.reflectionCoefficients[i]) > 0.05) {
77-
super.optimalLpcOrder = (byte) (i + 1);
77+
for (int i = linearPredictor.maxLpcOrder - 1; i >= 0; i--) {
78+
if (Math.abs(linearPredictor.reflectionCoefficients[i]) > 0.05) {
79+
linearPredictor.optimalLpcOrder = (byte) (i + 1);
7880
break;
7981
}
8082
}
8183
}
8284

8385
private void quantizeReflectionCoefficients() {
84-
super.quantizedReflectionCoefficients = new int[super.optimalLpcOrder];
86+
linearPredictor.quantizedReflectionCoefficients = new int[linearPredictor.optimalLpcOrder];
8587

86-
if (super.quantizedReflectionCoefficients.length > 0) {
87-
super.quantizedReflectionCoefficients[0] = (int) Math
88-
.floor(64 * (-1 + (sqrt2 * Math.sqrt(super.reflectionCoefficients[0] + 1))));
88+
if (linearPredictor.quantizedReflectionCoefficients.length > 0) {
89+
linearPredictor.quantizedReflectionCoefficients[0] = (int) Math
90+
.floor(64 * (-1 + (sqrt2 * Math.sqrt(linearPredictor.reflectionCoefficients[0] + 1))));
8991
}
90-
if (super.quantizedReflectionCoefficients.length > 1) {
91-
super.quantizedReflectionCoefficients[1] = (int) Math
92-
.floor(64 * (-1 + (sqrt2 * Math.sqrt(-super.reflectionCoefficients[1] + 1))));
92+
if (linearPredictor.quantizedReflectionCoefficients.length > 1) {
93+
linearPredictor.quantizedReflectionCoefficients[1] = (int) Math
94+
.floor(64 * (-1 + (sqrt2 * Math.sqrt(-linearPredictor.reflectionCoefficients[1] + 1))));
9395
}
94-
for (int i = 2; i < super.optimalLpcOrder; i++) {
95-
super.quantizedReflectionCoefficients[i] = (int) Math.floor(64 * super.reflectionCoefficients[i]);
96+
for (int i = 2; i < linearPredictor.optimalLpcOrder; i++) {
97+
linearPredictor.quantizedReflectionCoefficients[i] = (int) Math.floor(64 * linearPredictor.reflectionCoefficients[i]);
9698
}
9799
}
98100

99101
private void generateResidues() {
100-
final long correction = (long) 1 << (super.correctionFactor - 1);
102+
final long correction = (long) 1 << (linearPredictor.correctionFactor - 1);
101103

102104
residues[0] = samples[0];
103105

104-
for (int i = 1; i <= super.optimalLpcOrder; i++) {
106+
for (int i = 1; i <= linearPredictor.optimalLpcOrder; i++) {
105107
long temp = correction;
106108
for (int j = 1; j <= i; j++) {
107-
temp += super.linearPredictionCoefficients[j] * samples[i - j];
109+
temp += linearPredictor.linearPredictionCoefficients[j] * samples[i - j];
108110
}
109-
residues[i] = samples[i] - (int) (temp >> super.correctionFactor);
111+
residues[i] = samples[i] - (int) (temp >> linearPredictor.correctionFactor);
110112
}
111113

112-
for (int i = super.optimalLpcOrder + 1; i < samples.length; i++) {
114+
for (int i = linearPredictor.optimalLpcOrder + 1; i < samples.length; i++) {
113115
long temp = correction;
114-
for (int j = 0; j <= super.optimalLpcOrder; j++) {
115-
temp += (super.linearPredictionCoefficients[j] * samples[i - j]);
116+
for (int j = 0; j <= linearPredictor.optimalLpcOrder; j++) {
117+
temp += (linearPredictor.linearPredictionCoefficients[j] * samples[i - j]);
116118
}
117-
residues[i] = samples[i] - (int) (temp >> super.correctionFactor);
119+
residues[i] = samples[i] - (int) (temp >> linearPredictor.correctionFactor);
118120
}
119121
}
120122

@@ -124,9 +126,9 @@ public LpcEncodedData process() {
124126
generateReflectionCoefficients();
125127
generateoptimalLpcOrder();
126128
quantizeReflectionCoefficients();
127-
super.dequantizeReflectionCoefficients();
128-
super.generatelinearPredictionCoefficients();
129+
linearPredictor.dequantizeReflectionCoefficients();
130+
linearPredictor.generatelinearPredictionCoefficients();
129131
generateResidues();
130-
return new LpcEncodedData(optimalLpcOrder, quantizedReflectionCoefficients, residues);
132+
return new LpcEncodedData(linearPredictor.optimalLpcOrder, linearPredictor.quantizedReflectionCoefficients, residues);
131133
}
132134
}

src/main/java/org/sela/lpc/SampleGenerator.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,41 @@
22

33
import org.sela.data.*;
44

5-
public final class SampleGenerator extends LinearPredictionBase {
5+
public final class SampleGenerator {
66
private final int[] residues;
77
private final int[] samples;
8+
private final LinearPredictor linearPredictor;
89

910
public SampleGenerator(final LpcEncodedData encodedData) {
10-
super(encodedData.quantizedReflectionCoefficients, encodedData.optimalLpcOrder);
11+
this.linearPredictor = new LinearPredictor(encodedData.quantizedReflectionCoefficients, encodedData.optimalLpcOrder);
1112
this.residues = encodedData.residues;
1213
this.samples = new int[residues.length];
1314
}
1415

1516
private void generateSamples() {
16-
final long correction = (long) 1 << (super.correctionFactor - 1);
17+
final long correction = (long) 1 << (linearPredictor.correctionFactor - 1);
1718

1819
samples[0] = residues[0];
1920

20-
for (int i = 1; i <= super.optimalLpcOrder; i++) {
21+
for (int i = 1; i <= linearPredictor.optimalLpcOrder; i++) {
2122
long temp = correction;
2223
for (int j = 1; j <= i; j++) {
23-
temp -= super.linearPredictionCoefficients[j] * samples[i - j];
24+
temp -= linearPredictor.linearPredictionCoefficients[j] * samples[i - j];
2425
}
25-
samples[i] = residues[i] - (int) (temp >> super.correctionFactor);
26+
samples[i] = residues[i] - (int) (temp >> linearPredictor.correctionFactor);
2627
}
2728

28-
for (int i = super.optimalLpcOrder + 1; i < residues.length; i++) {
29+
for (int i = linearPredictor.optimalLpcOrder + 1; i < residues.length; i++) {
2930
long temp = correction;
30-
for (int j = 0; j <= super.optimalLpcOrder; j++)
31-
temp -= (super.linearPredictionCoefficients[j] * samples[i - j]);
32-
samples[i] = residues[i] - (int) (temp >> super.correctionFactor);
31+
for (int j = 0; j <= linearPredictor.optimalLpcOrder; j++)
32+
temp -= (linearPredictor.linearPredictionCoefficients[j] * samples[i - j]);
33+
samples[i] = residues[i] - (int) (temp >> linearPredictor.correctionFactor);
3334
}
3435
}
3536

3637
public LpcDecodedData process() {
37-
super.dequantizeReflectionCoefficients();
38-
super.generatelinearPredictionCoefficients();
38+
linearPredictor.dequantizeReflectionCoefficients();
39+
linearPredictor.generatelinearPredictionCoefficients();
3940
generateSamples();
4041
return new LpcDecodedData(samples);
4142
}

0 commit comments

Comments
 (0)