Skip to content

Commit

Permalink
final keyword added throughout file
Browse files Browse the repository at this point in the history
  • Loading branch information
sahaRatul committed Jan 8, 2020
1 parent bcda703 commit a40a9f5
Show file tree
Hide file tree
Showing 22 changed files with 197 additions and 193 deletions.
33 changes: 17 additions & 16 deletions src/main/java/org/sela/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand All @@ -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();
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/org/sela/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -29,21 +29,22 @@ private void readFrames() throws IOException, FileException {

protected List<WavFrame> processFrames() throws IOException, FileException {
readFrames();

// Decode frames in parallel
List<WavFrame> wavFrames = selaFile.getFrames().parallelStream().map(x -> new FrameDecoder(x).process())
final List<WavFrame> wavFrames = selaFile.getFrames().parallelStream().map(x -> new FrameDecoder(x).process())
.collect(Collectors.toList());

// Sort decoded samples
Collections.sort(wavFrames);

return wavFrames;
}

public WavFile process() throws IOException, FileException {
List<WavFrame> wavFrames = processFrames();
final List<WavFrame> 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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/sela/Encoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<WavFrame> 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));
}
Expand All @@ -40,7 +40,7 @@ public SelaFile process() throws IOException {
readSamples();

// Encode samples in parallel
List<Frame> frames = wavFrames.parallelStream()
final List<Frame> frames = wavFrames.parallelStream()
.map(x -> (new FrameEncoder(x)).process()).collect(Collectors.toList());

// Sort encoded frames
Expand Down
29 changes: 13 additions & 16 deletions src/main/java/org/sela/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,37 @@
import org.sela.exception.FileException;

public class Player extends Decoder {
List<WavFrame> wavFrames;
private List<WavFrame> 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());
line.start();

// 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());
}
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/sela/data/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/sela/data/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
public final class Frame implements Comparable<Frame> {
public final int syncWord = 0xAA55FF00;
public ArrayList<SubFrame> 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<SubFrame> subFrames, int index) {
public Frame(final ArrayList<SubFrame> subFrames, final int index) {
this.subFrames = subFrames;
this.index = index;
}

public Frame(int index) {
public Frame(final int index) {
this.index = index;
}

Expand All @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/sela/data/LpcDecodedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public final class LpcDecodedData {
public int[] samples;

public LpcDecodedData(int[] samples) {
public LpcDecodedData(final int[] samples) {
this.samples = samples;
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/sela/data/LpcEncodedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/sela/data/RiceDecodedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public final class RiceDecodedData {
public int[] decodedData;

public RiceDecodedData(int[] decodedData) {
public RiceDecodedData(final int[] decodedData) {
this.decodedData = decodedData;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/sela/data/RiceEncodedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit a40a9f5

Please sign in to comment.