diff --git a/src/main/java/org/sela/App.java b/src/main/java/org/sela/App.java index 39c34a7..7cec87d 100644 --- a/src/main/java/org/sela/App.java +++ b/src/main/java/org/sela/App.java @@ -10,33 +10,34 @@ import org.sela.exception.FileException; public final class App { - public static void main(String[] args) { + public static void main(final String[] args) { System.out.println("\u001B[1mSimplE Lossless Audio. Released under MIT license\u001B[0m"); if (args.length < 2) { printUsage(); } else { try { parseCommandLineArgs(args); - } catch (Exception e) { + } catch (final Exception e) { System.err.println("\u001B[1m" + e.getMessage() + ". Aborting...\u001B[0m"); e.printStackTrace(); } } } - private static void parseCommandLineArgs(String[] args) throws IOException, FileException, LineUnavailableException { + private static void parseCommandLineArgs(final String[] args) + throws IOException, FileException, LineUnavailableException { if (args[0].equals("-e") && args.length == 3) { - File inputFile = new File(args[1]); - File outputFile = new File(args[2]); + final File inputFile = new File(args[1]); + final File outputFile = new File(args[2]); System.out.println("\u001B[1mEncoding: \u001B[0m" + inputFile.getAbsolutePath()); encodeFile(inputFile, outputFile); } else if (args[0].equals("-d") && args.length == 3) { - File inputFile = new File(args[1]); - File outputFile = new File(args[2]); + final File inputFile = new File(args[1]); + final File outputFile = new File(args[2]); System.out.println("\u001B[1mDecoding: \u001B[0m" + inputFile.getAbsolutePath()); decodeFile(inputFile, outputFile); } else if (args[0].equals("-p") && args.length == 2) { - File inputFile = new File(args[1]); + final File inputFile = new File(args[1]); System.out.println("\u001B[1mPlaying: \u001B[0m" + inputFile.getAbsolutePath()); playFile(inputFile); System.out.println(""); @@ -48,20 +49,20 @@ private static void parseCommandLineArgs(String[] args) throws IOException, File System.out.println("\u001B[1mDone\u001B[0m"); } - private static void encodeFile(File inputFile, File outputFile) throws IOException, FileException { - Encoder selaEncoder = new Encoder(inputFile, outputFile); - SelaFile selaFile = selaEncoder.process(); + private static void encodeFile(final File inputFile, final File outputFile) throws IOException, FileException { + final Encoder selaEncoder = new Encoder(inputFile, outputFile); + final SelaFile selaFile = selaEncoder.process(); selaFile.writeToStream(); } - private static void decodeFile(File inputFile, File outputFile) throws IOException, FileException { - Decoder selaDecoder = new Decoder(inputFile, outputFile); - WavFile wavFile = selaDecoder.process(); + private static void decodeFile(final File inputFile, final File outputFile) throws IOException, FileException { + final Decoder selaDecoder = new Decoder(inputFile, outputFile); + final WavFile wavFile = selaDecoder.process(); wavFile.writeToStream(); } - private static void playFile(File inputFile) throws IOException, FileException, LineUnavailableException { - Player selaPlayer = new Player(inputFile); + private static void playFile(final File inputFile) throws IOException, FileException, LineUnavailableException { + final Player selaPlayer = new Player(inputFile); selaPlayer.play(); } diff --git a/src/main/java/org/sela/Decoder.java b/src/main/java/org/sela/Decoder.java index 527834b..0767173 100644 --- a/src/main/java/org/sela/Decoder.java +++ b/src/main/java/org/sela/Decoder.java @@ -16,9 +16,9 @@ public class Decoder { protected WavFile wavFile; protected SelaFile selaFile; - private File outputFile; + private final File outputFile; - public Decoder(File inputFile, File outputFile) throws FileNotFoundException { + public Decoder(final File inputFile, final File outputFile) throws FileNotFoundException { this.selaFile = new SelaFile(new FileInputStream(inputFile)); this.outputFile = outputFile; } @@ -29,11 +29,11 @@ private void readFrames() throws IOException, FileException { protected List processFrames() throws IOException, FileException { readFrames(); - + // Decode frames in parallel - List wavFrames = selaFile.getFrames().parallelStream().map(x -> new FrameDecoder(x).process()) + final List wavFrames = selaFile.getFrames().parallelStream().map(x -> new FrameDecoder(x).process()) .collect(Collectors.toList()); - + // Sort decoded samples Collections.sort(wavFrames); @@ -41,9 +41,10 @@ protected List processFrames() throws IOException, FileException { } public WavFile process() throws IOException, FileException { - List wavFrames = processFrames(); + final List wavFrames = processFrames(); - WavFile wavFile = new WavFile(selaFile.getSampleRate(), selaFile.getBitsPerSample(), selaFile.getChannels(), wavFrames, new FileOutputStream(outputFile)); + final WavFile wavFile = new WavFile(selaFile.getSampleRate(), selaFile.getBitsPerSample(), + selaFile.getChannels(), wavFrames, new FileOutputStream(outputFile)); this.wavFile = wavFile; return wavFile; } diff --git a/src/main/java/org/sela/Encoder.java b/src/main/java/org/sela/Encoder.java index 378a380..aa78f54 100644 --- a/src/main/java/org/sela/Encoder.java +++ b/src/main/java/org/sela/Encoder.java @@ -13,24 +13,24 @@ import org.sela.exception.*; public class Encoder { - private WavFile wavFile; - private File outputFile; + private final WavFile wavFile; + private final File outputFile; private List wavFrames; private int frameCount; private final int samplePerSubFrame = 2048; - public Encoder(File inputFile, File outputFile) throws FileException, IOException { + public Encoder(final File inputFile, final File outputFile) throws FileException, IOException { wavFile = new WavFile(inputFile); this.outputFile = outputFile; } private void readSamples() { - long sampleCount = wavFile.getSampleCount(); + final long sampleCount = wavFile.getSampleCount(); frameCount = (int) Math.ceil((double) sampleCount / (samplePerSubFrame * wavFile.getNumChannels())); wavFrames = new ArrayList<>(frameCount); for (int i = 0; i < frameCount; i++) { - int[][] samples = new int[wavFile.getNumChannels()][samplePerSubFrame]; + final int[][] samples = new int[wavFile.getNumChannels()][samplePerSubFrame]; wavFile.readFrames(samples, samplePerSubFrame); wavFrames.add(new WavFrame(i, samples)); } @@ -40,7 +40,7 @@ public SelaFile process() throws IOException { readSamples(); // Encode samples in parallel - List frames = wavFrames.parallelStream() + final List frames = wavFrames.parallelStream() .map(x -> (new FrameEncoder(x)).process()).collect(Collectors.toList()); // Sort encoded frames diff --git a/src/main/java/org/sela/Player.java b/src/main/java/org/sela/Player.java index f58f284..55e02a8 100644 --- a/src/main/java/org/sela/Player.java +++ b/src/main/java/org/sela/Player.java @@ -15,32 +15,29 @@ import org.sela.exception.FileException; public class Player extends Decoder { - List wavFrames; + private List wavFrames; - public Player(File inputFile) throws IOException, FileException { + public Player(final File inputFile) throws IOException, FileException { super(inputFile, null); wavFrames = super.processFrames(); } - private static void printProgress(long current, long total) { - StringBuilder string = new StringBuilder(140); - int percent = (int) (current * 100 / total); - string.append('\r') - .append(String.format("%d%% [", percent)) - .append(String.join("", Collections.nCopies(percent / 2, "="))) - .append("\u001B[1m>\u001B[0m") - .append(String.join("", Collections.nCopies(50 - (percent / 2), " "))) - .append(']') - .append(" (").append(current).append('/').append(total).append(')'); + private static void printProgress(final long current, final long total) { + final StringBuilder string = new StringBuilder(140); + final int percent = (int) (current * 100 / total); + string.append('\r').append(String.format("%d%% [", percent)) + .append(String.join("", Collections.nCopies(percent / 2, "="))).append("\u001B[1m>\u001B[0m") + .append(String.join("", Collections.nCopies(50 - (percent / 2), " "))).append(']').append(" (") + .append(current).append('/').append(total).append(')'); System.out.print(string); } public void play() throws LineUnavailableException { // Select audio format parameters - AudioFormat af = new AudioFormat(super.selaFile.getSampleRate(), super.selaFile.getBitsPerSample(), + final AudioFormat af = new AudioFormat(super.selaFile.getSampleRate(), super.selaFile.getBitsPerSample(), super.selaFile.getChannels(), true, false); - DataLine.Info info = new DataLine.Info(SourceDataLine.class, af); - SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); + final DataLine.Info info = new DataLine.Info(SourceDataLine.class, af); + final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); // Prepare audio output line.open(af, 2048 * super.selaFile.getChannels()); @@ -48,7 +45,7 @@ public void play() throws LineUnavailableException { // Output wave form repeatedly for (int i = 0; i < wavFrames.size(); i++) { - byte[] bytes = wavFrames.get(i).getDemuxedShortSamplesInByteArray(); + final byte[] bytes = wavFrames.get(i).getDemuxedShortSamplesInByteArray(); line.write(bytes, 0, bytes.length); Player.printProgress((i + 1), wavFrames.size()); } diff --git a/src/main/java/org/sela/data/Chunk.java b/src/main/java/org/sela/data/Chunk.java index e88a454..eb96d73 100644 --- a/src/main/java/org/sela/data/Chunk.java +++ b/src/main/java/org/sela/data/Chunk.java @@ -20,17 +20,17 @@ else if(!format.equals("WAVE")) { } } - public void write(ByteBuffer buffer) { - buffer.put((byte)'R'); - buffer.put((byte)'I'); - buffer.put((byte)'F'); - buffer.put((byte)'F'); + public void write(final ByteBuffer buffer) { + buffer.put((byte) 'R'); + buffer.put((byte) 'I'); + buffer.put((byte) 'F'); + buffer.put((byte) 'F'); buffer.putInt(chunkSize); - buffer.put((byte)'W'); - buffer.put((byte)'A'); - buffer.put((byte)'V'); - buffer.put((byte)'E'); - for (SubChunk subChunk : subChunks) { + buffer.put((byte) 'W'); + buffer.put((byte) 'A'); + buffer.put((byte) 'V'); + buffer.put((byte) 'E'); + for (final SubChunk subChunk : subChunks) { subChunk.write(buffer); } } diff --git a/src/main/java/org/sela/data/Frame.java b/src/main/java/org/sela/data/Frame.java index 30f3602..71f265f 100644 --- a/src/main/java/org/sela/data/Frame.java +++ b/src/main/java/org/sela/data/Frame.java @@ -6,14 +6,14 @@ public final class Frame implements Comparable { public final int syncWord = 0xAA55FF00; public ArrayList subFrames; - private int index; // For internal sorting, not to be written to output + private final int index; // For internal sorting, not to be written to output - public Frame(ArrayList subFrames, int index) { + public Frame(final ArrayList subFrames, final int index) { this.subFrames = subFrames; this.index = index; } - public Frame(int index) { + public Frame(final int index) { this.index = index; } @@ -22,21 +22,21 @@ public int getIndex() { } @Override - public int compareTo(Frame frame) { + public int compareTo(final Frame frame) { return this.index - frame.index; } public int getByteCount() { int count = 4; - for (SubFrame subFrame : subFrames) { + for (final SubFrame subFrame : subFrames) { count += subFrame.getByteCount(); } return count; } - public void write(ByteBuffer buffer) { + public void write(final ByteBuffer buffer) { buffer.putInt(syncWord); - for (SubFrame subFrame : subFrames) { + for (final SubFrame subFrame : subFrames) { subFrame.write(buffer); } } diff --git a/src/main/java/org/sela/data/LpcDecodedData.java b/src/main/java/org/sela/data/LpcDecodedData.java index 5a63f1b..c2c9c12 100644 --- a/src/main/java/org/sela/data/LpcDecodedData.java +++ b/src/main/java/org/sela/data/LpcDecodedData.java @@ -3,7 +3,7 @@ public final class LpcDecodedData { public int[] samples; - public LpcDecodedData(int[] samples) { + public LpcDecodedData(final int[] samples) { this.samples = samples; } } \ No newline at end of file diff --git a/src/main/java/org/sela/data/LpcEncodedData.java b/src/main/java/org/sela/data/LpcEncodedData.java index 33e81d8..39be384 100644 --- a/src/main/java/org/sela/data/LpcEncodedData.java +++ b/src/main/java/org/sela/data/LpcEncodedData.java @@ -5,7 +5,8 @@ public final class LpcEncodedData { public int[] quantizedReflectionCoefficients; public int[] residues; - public LpcEncodedData(byte optimalLpcOrder, int[] quantizedReflectionCoefficients, int[] residues) { + public LpcEncodedData(final byte optimalLpcOrder, final int[] quantizedReflectionCoefficients, + final int[] residues) { this.optimalLpcOrder = optimalLpcOrder; this.quantizedReflectionCoefficients = quantizedReflectionCoefficients; this.residues = residues; diff --git a/src/main/java/org/sela/data/RiceDecodedData.java b/src/main/java/org/sela/data/RiceDecodedData.java index 128d4a3..4075304 100644 --- a/src/main/java/org/sela/data/RiceDecodedData.java +++ b/src/main/java/org/sela/data/RiceDecodedData.java @@ -3,7 +3,7 @@ public final class RiceDecodedData { public int[] decodedData; - public RiceDecodedData(int[] decodedData) { + public RiceDecodedData(final int[] decodedData) { this.decodedData = decodedData; } } \ No newline at end of file diff --git a/src/main/java/org/sela/data/RiceEncodedData.java b/src/main/java/org/sela/data/RiceEncodedData.java index 1d06ada..14c319b 100644 --- a/src/main/java/org/sela/data/RiceEncodedData.java +++ b/src/main/java/org/sela/data/RiceEncodedData.java @@ -5,7 +5,7 @@ public final class RiceEncodedData { public int dataCount; public int[] encodedData; - public RiceEncodedData(int optimumRiceParam, int dataCount, int[] encodedData) { + public RiceEncodedData(final int optimumRiceParam, final int dataCount, final int[] encodedData) { this.optimumRiceParam = optimumRiceParam; this.dataCount = dataCount; this.encodedData = encodedData; diff --git a/src/main/java/org/sela/data/SelaFile.java b/src/main/java/org/sela/data/SelaFile.java index 4a45f08..364b92b 100644 --- a/src/main/java/org/sela/data/SelaFile.java +++ b/src/main/java/org/sela/data/SelaFile.java @@ -24,7 +24,8 @@ public final class SelaFile { DataOutputStream outputStream; DataInputStream inputStream; - public SelaFile(int sampleRate, short bitsPerSample, byte channels, List frames, FileOutputStream fos) { + public SelaFile(final int sampleRate, final short bitsPerSample, final byte channels, final List frames, + final FileOutputStream fos) { this.sampleRate = sampleRate; this.bitsPerSample = bitsPerSample; this.channels = channels; @@ -33,24 +34,24 @@ public SelaFile(int sampleRate, short bitsPerSample, byte channels, List this.outputStream = new DataOutputStream(new BufferedOutputStream(fos)); } - public SelaFile(FileInputStream fis) { + public SelaFile(final FileInputStream fis) { this.inputStream = new DataInputStream(new BufferedInputStream(fis)); } - private void write(ByteBuffer buffer) { + private void write(final ByteBuffer buffer) { buffer.put(magicNumber); buffer.putInt(sampleRate); buffer.putShort(bitsPerSample); buffer.put(channels); buffer.putInt(numFrames); - for (Frame frame : frames) { + for (final Frame frame : frames) { frame.write(buffer); } } - private void read(ByteBuffer buffer) throws FileException { - byte[] magicNumber = new byte[4]; + private void read(final ByteBuffer buffer) throws FileException { + final byte[] magicNumber = new byte[4]; for (int i = 0; i < magicNumber.length; i++) { magicNumber[i] = buffer.get(); } @@ -66,42 +67,43 @@ private void read(ByteBuffer buffer) throws FileException { // Read frames for (int i = 0; i < numFrames; i++) { // Read SyncWord - int sync = buffer.getInt(); + final int sync = buffer.getInt(); if (sync != 0xAA55FF00) { break; } - Frame frame = new Frame(i); + final Frame frame = new Frame(i); frame.subFrames = new ArrayList<>(2); // Read subframes for (int j = 0; j < channels; j++) { // Get channel - byte subFrameChannel = buffer.get(); + final byte subFrameChannel = buffer.get(); // Get Reflection data - byte reflectionCoefficientRiceParam = buffer.get(); - short reflectionCoefficientRequiredInts = buffer.getShort(); - byte optimumLpcOrder = buffer.get(); - int[] encodedReflectionCoefficients = new int[reflectionCoefficientRequiredInts]; + final byte reflectionCoefficientRiceParam = buffer.get(); + final short reflectionCoefficientRequiredInts = buffer.getShort(); + final byte optimumLpcOrder = buffer.get(); + final int[] encodedReflectionCoefficients = new int[reflectionCoefficientRequiredInts]; for (int k = 0; k < encodedReflectionCoefficients.length; k++) { encodedReflectionCoefficients[k] = buffer.getInt(); } // Get Residue data - byte residueRiceParam = buffer.get(); - short residueRequiredInts = buffer.getShort(); - short samplesPerChannel = buffer.getShort(); - int[] encodedResidues = new int[residueRequiredInts]; + final byte residueRiceParam = buffer.get(); + final short residueRequiredInts = buffer.getShort(); + final short samplesPerChannel = buffer.getShort(); + final int[] encodedResidues = new int[residueRequiredInts]; for (int k = 0; k < encodedResidues.length; k++) { encodedResidues[k] = buffer.getInt(); } // Generate subframes - RiceEncodedData reflectionData = new RiceEncodedData(reflectionCoefficientRiceParam, optimumLpcOrder, - encodedReflectionCoefficients); - RiceEncodedData residueData = new RiceEncodedData(residueRiceParam, samplesPerChannel, encodedResidues); - SubFrame subFrame = new SubFrame(subFrameChannel, reflectionData, residueData); + final RiceEncodedData reflectionData = new RiceEncodedData(reflectionCoefficientRiceParam, + optimumLpcOrder, encodedReflectionCoefficients); + final RiceEncodedData residueData = new RiceEncodedData(residueRiceParam, samplesPerChannel, + encodedResidues); + final SubFrame subFrame = new SubFrame(subFrameChannel, reflectionData, residueData); frame.subFrames.add(subFrame); } frames.add(frame); @@ -113,10 +115,10 @@ public void readFromStream() throws IOException, FileException { throw new FileException("inputStream is null"); } - byte[] inputBytes = new byte[inputStream.available()]; + final byte[] inputBytes = new byte[inputStream.available()]; inputStream.read(inputBytes); - ByteBuffer buffer = ByteBuffer.wrap(inputBytes); + final ByteBuffer buffer = ByteBuffer.wrap(inputBytes); buffer.order(ByteOrder.LITTLE_ENDIAN); read(buffer); @@ -128,11 +130,11 @@ public void writeToStream() throws IOException, FileException { throw new FileException("outputStream is null"); } int byteCount = 15; - for (Frame frame : frames) { + for (final Frame frame : frames) { byteCount += frame.getByteCount(); } - ByteBuffer buffer = ByteBuffer.allocate(byteCount); + final ByteBuffer buffer = ByteBuffer.allocate(byteCount); buffer.order(ByteOrder.LITTLE_ENDIAN); write(buffer); diff --git a/src/main/java/org/sela/data/SubChunk.java b/src/main/java/org/sela/data/SubChunk.java index f2cc44e..6733fc8 100644 --- a/src/main/java/org/sela/data/SubChunk.java +++ b/src/main/java/org/sela/data/SubChunk.java @@ -7,10 +7,10 @@ public class SubChunk { protected int subChunkSize; protected byte[] subChunkData; - public void write(ByteBuffer buffer) { - char[] chars = new char[4]; + public void write(final ByteBuffer buffer) { + final char[] chars = new char[4]; subChunkId.getChars(0, 4, chars, 0); - for (char c : chars) { + for (final char c : chars) { buffer.put((byte)c); } buffer.putInt(subChunkSize); diff --git a/src/main/java/org/sela/data/SubFrame.java b/src/main/java/org/sela/data/SubFrame.java index 82d71cd..a07c8b8 100644 --- a/src/main/java/org/sela/data/SubFrame.java +++ b/src/main/java/org/sela/data/SubFrame.java @@ -18,7 +18,7 @@ public final class SubFrame { public short samplesPerChannel; public int[] encodedResidues; - public SubFrame(byte channel, RiceEncodedData reflectionData, RiceEncodedData residueData) { + public SubFrame(final byte channel, final RiceEncodedData reflectionData, final RiceEncodedData residueData) { this.channel = channel; this.reflectionCoefficientRiceParam = (byte) reflectionData.optimumRiceParam; @@ -36,20 +36,20 @@ public int getByteCount() { return 10 + (4 * (encodedReflectionCoefficients.length + encodedResidues.length)); } - public void write(ByteBuffer buffer) { + public void write(final ByteBuffer buffer) { buffer.put(channel); - + buffer.put(reflectionCoefficientRiceParam); buffer.putShort(reflectionCoefficientRequiredInts); buffer.put(optimumLpcOrder); - for (int i : encodedReflectionCoefficients) { + for (final int i : encodedReflectionCoefficients) { buffer.putInt(i); } buffer.put(residueRiceParam); buffer.putShort(residueRequiredInts); buffer.putShort(samplesPerChannel); - for (int i : encodedResidues) { + for (final int i : encodedResidues) { buffer.putInt(i); } } diff --git a/src/main/java/org/sela/data/WavFile.java b/src/main/java/org/sela/data/WavFile.java index 66df396..f489252 100644 --- a/src/main/java/org/sela/data/WavFile.java +++ b/src/main/java/org/sela/data/WavFile.java @@ -27,13 +27,14 @@ public class WavFile { private DataOutputStream outputStream; - public WavFile(File inputFile) throws IOException, FileException { + public WavFile(final File inputFile) throws IOException, FileException { this.inputFile = inputFile; this.readOffset = 0; this.read(); } - public WavFile(int sampleRate, short bitsPerSample, byte channels, List frames, FileOutputStream fos) { + public WavFile(final int sampleRate, final short bitsPerSample, final byte channels, final List frames, + final FileOutputStream fos) { this.outputStream = new DataOutputStream(new BufferedOutputStream(fos)); this.frames = frames; createChunk(); @@ -42,9 +43,10 @@ public WavFile(int sampleRate, short bitsPerSample, byte channels, List x.subChunkId.equals("fmt ")).findFirst().get(); + final SubChunk subChunk = chunk.subChunks.stream().filter(x -> x.subChunkId.equals("fmt ")).findFirst().get(); if (subChunk == null) { throw new FileException("fmt subchunk not found in wav"); } - int subChunkIndex = chunk.subChunks.indexOf(subChunk); + final int subChunkIndex = chunk.subChunks.indexOf(subChunk); - ByteBuffer buffer = ByteBuffer.wrap(subChunk.subChunkData); + final ByteBuffer buffer = ByteBuffer.wrap(subChunk.subChunkData); buffer.order(ByteOrder.LITTLE_ENDIAN); - FormatSubChunk formatSubChunk = new FormatSubChunk(); + final FormatSubChunk formatSubChunk = new FormatSubChunk(); formatSubChunk.subChunkId = subChunk.subChunkId; formatSubChunk.subChunkSize = subChunk.subChunkSize; formatSubChunk.subChunkData = subChunk.subChunkData; @@ -122,11 +124,11 @@ private void generateFormatSubChunk() throws FileException { chunk.subChunks.set(subChunkIndex, formatSubChunk); } - private void createFormatSubChunk(int sampleRate, byte channels, short bitsPerSample) { - SubChunk subChunk = new SubChunk(); + private void createFormatSubChunk(final int sampleRate, final byte channels, final short bitsPerSample) { + final SubChunk subChunk = new SubChunk(); subChunk.subChunkId = "fmt "; subChunk.subChunkSize = 16; - ByteBuffer buffer = ByteBuffer.allocate(16); + final ByteBuffer buffer = ByteBuffer.allocate(16); buffer.order(ByteOrder.LITTLE_ENDIAN); buffer.putShort((short) 1); // AudioFormat buffer.putShort((short) channels); // Channels @@ -140,26 +142,26 @@ private void createFormatSubChunk(int sampleRate, byte channels, short bitsPerSa private void generateDataChunk() throws FileException { // Get Data subChunk - SubChunk subChunk = chunk.subChunks.stream().filter(x -> x.subChunkId.equals("data")).findFirst().get(); + final SubChunk subChunk = chunk.subChunks.stream().filter(x -> x.subChunkId.equals("data")).findFirst().get(); if (subChunk == null) { throw new FileException("data subchunk not found in wav"); } - int subChunkIndex = chunk.subChunks.indexOf(subChunk); + final int subChunkIndex = chunk.subChunks.indexOf(subChunk); // Get fmt subChunk - FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.stream() + final FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.stream() .filter(x -> x.subChunkId.equals("fmt ")).findFirst().get(); - DataSubChunk dataSubChunk = new DataSubChunk(); + final DataSubChunk dataSubChunk = new DataSubChunk(); - ByteBuffer buffer = ByteBuffer.wrap(subChunk.subChunkData); + final ByteBuffer buffer = ByteBuffer.wrap(subChunk.subChunkData); buffer.order(ByteOrder.LITTLE_ENDIAN); dataSubChunk.subChunkId = subChunk.subChunkId; dataSubChunk.subChunkSize = subChunk.subChunkSize; dataSubChunk.subChunkData = subChunk.subChunkData; - int bytesPerSample = formatSubChunk.bitsPerSample / 8; - int sampleCount = dataSubChunk.subChunkSize / bytesPerSample; + final int bytesPerSample = formatSubChunk.bitsPerSample / 8; + final int sampleCount = dataSubChunk.subChunkSize / bytesPerSample; dataSubChunk.samples = new int[sampleCount]; for (int i = 0; i < sampleCount; i++) { dataSubChunk.samples[i] = buffer.getShort(); @@ -169,10 +171,10 @@ private void generateDataChunk() throws FileException { } private void createDataChunk() { - SubChunk subChunk = new SubChunk(); + final SubChunk subChunk = new SubChunk(); subChunk.subChunkId = "data"; subChunk.subChunkSize = 0; - for (WavFrame frame : frames) { + for (final WavFrame frame : frames) { subChunk.subChunkSize += frame.getSizeInBytes(); } subChunk.subChunkData = new byte[0]; @@ -180,12 +182,12 @@ private void createDataChunk() { } private void demuxSamples() { - FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.stream() + final FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.stream() .filter(x -> x.subChunkId.equals("fmt ")).findFirst().get(); - DataSubChunk dataSubChunk = (DataSubChunk) chunk.subChunks.stream().filter(x -> x.subChunkId.equals("data")) - .findFirst().get(); + final DataSubChunk dataSubChunk = (DataSubChunk) chunk.subChunks.stream() + .filter(x -> x.subChunkId.equals("data")).findFirst().get(); - int[][] demuxedSamples = new int[formatSubChunk.numChannels][dataSubChunk.samples.length + final int[][] demuxedSamples = new int[formatSubChunk.numChannels][dataSubChunk.samples.length / formatSubChunk.numChannels]; for (int i = 0; i < demuxedSamples.length; i++) { for (int j = 0; j < demuxedSamples[i].length; j++) { @@ -199,17 +201,17 @@ private void demuxSamples() { } public short getNumChannels() { - FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.get(0); + final FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.get(0); return formatSubChunk.numChannels; } public int getSampleRate() { - FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.get(0); + final FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.get(0); return formatSubChunk.sampleRate; } public short getBitsPerSample() { - FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.get(0); + final FormatSubChunk formatSubChunk = (FormatSubChunk) chunk.subChunks.get(0); return formatSubChunk.bitsPerSample; } @@ -236,11 +238,11 @@ public void writeToStream() throws FileException, IOException { throw new FileException("outputStream is null"); } int byteCount = 44; - for (WavFrame frame : frames) { + for (final WavFrame frame : frames) { byteCount += frame.getSizeInBytes(); } - ByteBuffer buffer = ByteBuffer.allocate(byteCount); + final ByteBuffer buffer = ByteBuffer.allocate(byteCount); buffer.order(ByteOrder.LITTLE_ENDIAN); // Write chunk and subchunks @@ -256,8 +258,8 @@ public void writeToStream() throws FileException, IOException { outputStream.close(); } - public void readFrames(int[][] output, int samplesPerChannel) { - int readLimit = (demuxedSamples[0].length - readOffset) > samplesPerChannel ? samplesPerChannel + public void readFrames(final int[][] output, final int samplesPerChannel) { + final int readLimit = (demuxedSamples[0].length - readOffset) > samplesPerChannel ? samplesPerChannel : (demuxedSamples[0].length - readOffset); for (int i = 0; i < demuxedSamples.length; i++) { for (int j = 0; j < readLimit; j++) { diff --git a/src/main/java/org/sela/data/WavFrame.java b/src/main/java/org/sela/data/WavFrame.java index b12a02d..89d948a 100644 --- a/src/main/java/org/sela/data/WavFrame.java +++ b/src/main/java/org/sela/data/WavFrame.java @@ -4,10 +4,10 @@ import java.nio.ByteOrder; public class WavFrame implements Comparable { - private int index; + private final int index; public int[][] samples; - public WavFrame(int index, int[][] samples) { + public WavFrame(final int index, final int[][] samples) { this.index = index; this.samples = samples; } @@ -17,26 +17,26 @@ public int getIndex() { } @Override - public int compareTo(WavFrame frame) { + public int compareTo(final WavFrame frame) { return this.index - frame.index; } public int getSizeInBytes() { - return samples.length * samples[0].length * 2; //Assuming 16 bit samples + return samples.length * samples[0].length * 2; // Assuming 16 bit samples } public byte[] getDemuxedShortSamplesInByteArray() { - //Demux - short[] demuxed = new short[samples.length * samples[0].length]; + // Demux + final short[] demuxed = new short[samples.length * samples[0].length]; for (int i = 0; i < samples.length; i++) { - for(int j = 0; j < samples[i].length; j++) { + for (int j = 0; j < samples[i].length; j++) { demuxed[j * samples.length + i] = (short) samples[i][j]; } } // Write to buffer - byte[] bytes = new byte[demuxed.length * 2]; - ByteBuffer buffer = ByteBuffer.wrap(bytes); + final byte[] bytes = new byte[demuxed.length * 2]; + final ByteBuffer buffer = ByteBuffer.wrap(bytes); buffer.order(ByteOrder.LITTLE_ENDIAN); for (int i = 0; i < demuxed.length; i++) { buffer.putShort(demuxed[i]); diff --git a/src/main/java/org/sela/frame/FrameDecoder.java b/src/main/java/org/sela/frame/FrameDecoder.java index fe4e324..0ffa354 100644 --- a/src/main/java/org/sela/frame/FrameDecoder.java +++ b/src/main/java/org/sela/frame/FrameDecoder.java @@ -5,33 +5,33 @@ import org.sela.rice.RiceDecoder; public final class FrameDecoder { - private Frame frame; + private final Frame frame; - public FrameDecoder(Frame frame) { + public FrameDecoder(final Frame frame) { this.frame = frame; } public WavFrame process() { - int[][] samples = new int[frame.subFrames.size()][]; + final int[][] samples = new int[frame.subFrames.size()][]; // Foreach subFrame - for (SubFrame subFrame : frame.subFrames) { + for (final SubFrame subFrame : frame.subFrames) { // Stage 1 - Extract data from subFrame - byte channel = subFrame.channel; - byte optimumLpcOrder = subFrame.optimumLpcOrder; - RiceEncodedData reflectionData = new RiceEncodedData(subFrame.reflectionCoefficientRiceParam, + final byte channel = subFrame.channel; + final byte optimumLpcOrder = subFrame.optimumLpcOrder; + final RiceEncodedData reflectionData = new RiceEncodedData(subFrame.reflectionCoefficientRiceParam, subFrame.optimumLpcOrder, subFrame.encodedReflectionCoefficients); - RiceEncodedData residueData = new RiceEncodedData(subFrame.residueRiceParam, subFrame.samplesPerChannel, - subFrame.encodedResidues); + final RiceEncodedData residueData = new RiceEncodedData(subFrame.residueRiceParam, + subFrame.samplesPerChannel, subFrame.encodedResidues); // Stage 2 - Decompress data - RiceDecodedData decodedReflectionData = (new RiceDecoder(reflectionData)).process(); - RiceDecodedData decodedResidueData = (new RiceDecoder(residueData)).process(); + final RiceDecodedData decodedReflectionData = (new RiceDecoder(reflectionData)).process(); + final RiceDecodedData decodedResidueData = (new RiceDecoder(residueData)).process(); // Stage 3 - Generate Samples - LpcEncodedData encodedData = new LpcEncodedData(optimumLpcOrder, decodedReflectionData.decodedData, + final LpcEncodedData encodedData = new LpcEncodedData(optimumLpcOrder, decodedReflectionData.decodedData, decodedResidueData.decodedData); - LpcDecodedData decoded = (new SampleGenerator(encodedData)).process(); + final LpcDecodedData decoded = (new SampleGenerator(encodedData)).process(); samples[channel] = decoded.samples; } return new WavFrame(frame.getIndex(), samples); diff --git a/src/main/java/org/sela/frame/FrameEncoder.java b/src/main/java/org/sela/frame/FrameEncoder.java index c9cbeed..1ee3735 100644 --- a/src/main/java/org/sela/frame/FrameEncoder.java +++ b/src/main/java/org/sela/frame/FrameEncoder.java @@ -7,30 +7,30 @@ import org.sela.rice.*; public final class FrameEncoder { - private WavFrame wavFrame; + private final WavFrame wavFrame; - public FrameEncoder(WavFrame wavFrame) { + public FrameEncoder(final WavFrame wavFrame) { this.wavFrame = wavFrame; } public Frame process() { - ArrayList subFrames = new ArrayList<>(wavFrame.samples.length); + final ArrayList subFrames = new ArrayList<>(wavFrame.samples.length); // Foreach channel for (byte i = 0; i < wavFrame.samples.length; i++) { // Stage 1 - Generate residues and reflection coefficients - ResidueGenerator residueGenerator = new ResidueGenerator(new LpcDecodedData(wavFrame.samples[i])); - LpcEncodedData residues = residueGenerator.process(); + final ResidueGenerator residueGenerator = new ResidueGenerator(new LpcDecodedData(wavFrame.samples[i])); + final LpcEncodedData residues = residueGenerator.process(); // Stage 2 - Compress residues and reflection coefficients - RiceEncoder reflectionRiceEncoder = new RiceEncoder( + final RiceEncoder reflectionRiceEncoder = new RiceEncoder( new RiceDecodedData(residues.quantizedReflectionCoefficients)); - RiceEncoder residueRiceEncoder = new RiceEncoder(new RiceDecodedData(residues.residues)); - RiceEncodedData reflectionData = reflectionRiceEncoder.process(); - RiceEncodedData residueData = residueRiceEncoder.process(); + final RiceEncoder residueRiceEncoder = new RiceEncoder(new RiceDecodedData(residues.residues)); + final RiceEncodedData reflectionData = reflectionRiceEncoder.process(); + final RiceEncodedData residueData = residueRiceEncoder.process(); // Stage 3 - Generate Subframes - SubFrame subFrame = new SubFrame(i, reflectionData, residueData); + final SubFrame subFrame = new SubFrame(i, reflectionData, residueData); subFrames.add(subFrame); } diff --git a/src/main/java/org/sela/lpc/LinearPredictionBase.java b/src/main/java/org/sela/lpc/LinearPredictionBase.java index 5223b6a..a53feb7 100644 --- a/src/main/java/org/sela/lpc/LinearPredictionBase.java +++ b/src/main/java/org/sela/lpc/LinearPredictionBase.java @@ -15,7 +15,7 @@ public LinearPredictionBase() { optimalLpcOrder = 1; } - public LinearPredictionBase(int[] quantizedReflectionCoefficients, byte optimalLpcOrder) { + public LinearPredictionBase(final int[] quantizedReflectionCoefficients, final byte optimalLpcOrder) { this.reflectionCoefficients = new double[maxLpcOrder]; this.quantizedReflectionCoefficients = quantizedReflectionCoefficients; this.optimalLpcOrder = optimalLpcOrder; @@ -36,17 +36,17 @@ protected void dequantizeReflectionCoefficients() { protected void generatelinearPredictionCoefficients() { linearPredictionCoefficients = new long[optimalLpcOrder + 1]; - double[][] linearPredictionCoefficientMatrix = new double[optimalLpcOrder][optimalLpcOrder]; - double[] lpcTmp = new double[optimalLpcOrder]; - long correction = (long) 1 << correctionFactor; + final double[][] linearPredictionCoefficientMatrix = new double[optimalLpcOrder][optimalLpcOrder]; + final double[] lpcTmp = new double[optimalLpcOrder]; + final long correction = (long) 1 << correctionFactor; // Generate LPC matrix for (int i = 0; i < optimalLpcOrder; i++) { lpcTmp[i] = reflectionCoefficients[i]; - int i2 = i >> 1; + final int i2 = i >> 1; int j = 0; for (j = 0; j < i2; j++) { - double tmp = lpcTmp[j]; + final double tmp = lpcTmp[j]; lpcTmp[j] += reflectionCoefficients[i] * lpcTmp[i - 1 - j]; lpcTmp[i - 1 - j] += reflectionCoefficients[i] * tmp; } diff --git a/src/main/java/org/sela/lpc/ResidueGenerator.java b/src/main/java/org/sela/lpc/ResidueGenerator.java index 64bc72e..ad5f874 100644 --- a/src/main/java/org/sela/lpc/ResidueGenerator.java +++ b/src/main/java/org/sela/lpc/ResidueGenerator.java @@ -4,15 +4,15 @@ import org.sela.data.LpcEncodedData; public final class ResidueGenerator extends LinearPredictionBase { - private int[] samples; - private int[] residues; - private double[] quantizedSamples; - private double[] autocorrelationFactors; + private final int[] samples; + private final int[] residues; + private final double[] quantizedSamples; + private final double[] autocorrelationFactors; private final int maxShort = 32767; private final double sqrt2 = 1.4142135623730950488016887242096; - public ResidueGenerator(LpcDecodedData data) { + public ResidueGenerator(final LpcDecodedData data) { this.samples = data.samples; this.quantizedSamples = new double[samples.length]; this.autocorrelationFactors = new double[super.maxLpcOrder + 1]; @@ -51,7 +51,7 @@ private void generateAutoCorrelation() { private void generateReflectionCoefficients() { double error; - double[][] gen = new double[2][super.maxLpcOrder]; + final double[][] gen = new double[2][super.maxLpcOrder]; for (int i = 0; i < super.maxLpcOrder; i++) { gen[0][i] = gen[1][i] = autocorrelationFactors[i + 1]; @@ -97,7 +97,7 @@ private void quantizeReflectionCoefficients() { } private void generateResidues() { - long correction = (long) 1 << (super.correctionFactor - 1); + final long correction = (long) 1 << (super.correctionFactor - 1); residues[0] = samples[0]; diff --git a/src/main/java/org/sela/lpc/SampleGenerator.java b/src/main/java/org/sela/lpc/SampleGenerator.java index 0b60710..2cb0a9f 100644 --- a/src/main/java/org/sela/lpc/SampleGenerator.java +++ b/src/main/java/org/sela/lpc/SampleGenerator.java @@ -3,17 +3,17 @@ import org.sela.data.*; public final class SampleGenerator extends LinearPredictionBase { - private int[] residues; - private int[] samples; + private final int[] residues; + private final int[] samples; - public SampleGenerator(LpcEncodedData encodedData) { + public SampleGenerator(final LpcEncodedData encodedData) { super(encodedData.quantizedReflectionCoefficients, encodedData.optimalLpcOrder); this.residues = encodedData.residues; this.samples = new int[residues.length]; } private void generateSamples() { - long correction = (long) 1 << (super.correctionFactor - 1); + final long correction = (long) 1 << (super.correctionFactor - 1); samples[0] = residues[0]; diff --git a/src/main/java/org/sela/rice/RiceDecoder.java b/src/main/java/org/sela/rice/RiceDecoder.java index 2407368..35acb60 100644 --- a/src/main/java/org/sela/rice/RiceDecoder.java +++ b/src/main/java/org/sela/rice/RiceDecoder.java @@ -3,14 +3,14 @@ import org.sela.data.*; public final class RiceDecoder { - private int[] input; + private final int[] input; private int[] bitInput; - private int dataCount; - private int optimumRiceParam; - private long[] unsignedOutput; - private int[] output; + private final int dataCount; + private final int optimumRiceParam; + private final long[] unsignedOutput; + private final int[] output; - public RiceDecoder(RiceEncodedData encodedData) { + public RiceDecoder(final RiceEncodedData encodedData) { this.input = encodedData.encodedData; this.dataCount = encodedData.dataCount; this.optimumRiceParam = encodedData.optimumRiceParam; diff --git a/src/main/java/org/sela/rice/RiceEncoder.java b/src/main/java/org/sela/rice/RiceEncoder.java index 2c99d8b..5b99ddd 100644 --- a/src/main/java/org/sela/rice/RiceEncoder.java +++ b/src/main/java/org/sela/rice/RiceEncoder.java @@ -6,16 +6,16 @@ import org.sela.data.*; public final class RiceEncoder { - private int[] input; - private long[] unsignedInput; + private final int[] input; + private final long[] unsignedInput; private int[] output; private int[] bitOutput; - private ArrayList bitSizes; + private final ArrayList bitSizes; private int optimumRiceParam; private int requiredBits; private final int maxRiceParam = 20; - public RiceEncoder(RiceDecodedData data) { + public RiceEncoder(final RiceDecodedData data) { this.input = data.decodedData; this.unsignedInput = new long[input.length]; this.bitSizes = new ArrayList(maxRiceParam);