Skip to content

Commit 40b9c7b

Browse files
committed
Fixes in progress printing infrastructure
1 parent 04ee986 commit 40b9c7b

File tree

4 files changed

+61
-29
lines changed

4 files changed

+61
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Java rewrite of the original [sela project](https://github.com/sahaRatul/sela).
77
![Decoder](https://cloud.githubusercontent.com/assets/12273725/8868418/cbb6a1dc-31f5-11e5-91f6-8290766baa34.png)
88

99
### Build requirements
10-
- Open/Oracle JDK 8/13
10+
- Open/Oracle JDK >=11
1111
- Maven
1212

1313
### Build instructions
@@ -27,4 +27,4 @@ Java rewrite of the original [sela project](https://github.com/sahaRatul/sela).
2727
- [Paper on shorten, the original open source lossless codec](ftp://svr-ftp.eng.cam.ac.uk/pub/reports/robinson_tr156.ps.Z)
2828
- ISO/IEC 14496 Part 3, Subpart 11 (Audio Lossless Coding)
2929

30-
### License : MIT
30+
### License : MIT

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<artifactId>sela</artifactId>
66
<version>2.0</version>
77
<properties>
8-
<maven.compiler.source>13</maven.compiler.source>
9-
<maven.compiler.target>13</maven.compiler.target>
8+
<maven.compiler.source>11</maven.compiler.source>
9+
<maven.compiler.target>11</maven.compiler.target>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1111
</properties>
1212
<dependencies>

src/main/java/org/sela/App.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,42 @@
1111

1212
public final class App {
1313
public static void main(final String[] args) {
14-
System.out.println("\u001B[1mSimplE Lossless Audio. Released under MIT license\u001B[0m");
14+
System.out.println("SimplE Lossless Audio. Released under MIT license");
1515
if (args.length < 2) {
1616
printUsage();
1717
} else {
1818
try {
1919
parseCommandLineArgs(args);
2020
} catch (final Exception e) {
21-
System.err.println("\u001B[1m" + e.getMessage() + ". Aborting...\u001B[0m");
21+
System.err.println(e.getMessage() + ". Aborting...");
2222
e.printStackTrace();
2323
}
2424
}
2525
}
2626

2727
private static void parseCommandLineArgs(final String[] args)
28-
throws IOException, FileException, LineUnavailableException {
28+
throws IOException, FileException, LineUnavailableException, InterruptedException {
2929
if (args[0].equals("-e") && args.length == 3) {
3030
final File inputFile = new File(args[1]);
3131
final File outputFile = new File(args[2]);
32-
System.out.println("\u001B[1mEncoding: \u001B[0m" + inputFile.getAbsolutePath());
32+
System.out.println("Encoding: " + inputFile.getAbsolutePath());
3333
encodeFile(inputFile, outputFile);
3434
} else if (args[0].equals("-d") && args.length == 3) {
3535
final File inputFile = new File(args[1]);
3636
final File outputFile = new File(args[2]);
37-
System.out.println("\u001B[1mDecoding: \u001B[0m" + inputFile.getAbsolutePath());
37+
System.out.println("Decoding: " + inputFile.getAbsolutePath());
3838
decodeFile(inputFile, outputFile);
3939
} else if (args[0].equals("-p") && args.length == 2) {
4040
final File inputFile = new File(args[1]);
41-
System.out.println("\u001B[1mPlaying: \u001B[0m" + inputFile.getAbsolutePath());
41+
System.out.println("Playing: " + inputFile.getAbsolutePath());
4242
playFile(inputFile);
4343
System.out.println("");
4444
} else {
45-
System.out.println("Invalid arguments..");
45+
System.out.println("Invalid arguments...");
4646
printUsage();
4747
return;
4848
}
49-
System.out.println("\u001B[1mDone\u001B[0m");
49+
System.out.println("Done");
5050
}
5151

5252
private static void encodeFile(final File inputFile, final File outputFile) throws IOException, FileException {
@@ -61,22 +61,22 @@ private static void decodeFile(final File inputFile, final File outputFile) thro
6161
wavFile.writeToStream();
6262
}
6363

64-
private static void playFile(final File inputFile) throws IOException, FileException, LineUnavailableException {
64+
private static void playFile(final File inputFile) throws IOException, FileException, LineUnavailableException, InterruptedException {
6565
final Player selaPlayer = new Player(inputFile);
6666
selaPlayer.play();
6767
}
6868

6969
private static void printUsage() {
7070
System.out.println("");
71-
System.out.println("\u001B[1mUsage:\u001B[0m");
71+
System.out.println("Usage:");
7272
System.out.println("");
73-
System.out.println("\u001B[1mEncoding a file:\u001B[0m");
73+
System.out.println("Encoding a file:");
7474
System.out.println("java -jar sela.jar -e path/to/input.wav path/to/output.sela");
7575
System.out.println("");
76-
System.out.println("\u001B[1mDecoding a file:\u001B[0m");
76+
System.out.println("Decoding a file:");
7777
System.out.println("java -jar sela.jar -d path/to/input.sela path/to/output.wav");
7878
System.out.println("");
79-
System.out.println("\u001B[1mPlaying a file:\u001B[0m");
79+
System.out.println("Playing a file:");
8080
System.out.println("java -jar sela.jar -p path/to/input.sela");
8181
System.out.println("");
8282
}

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

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,7 @@ public Player(final File inputFile) throws IOException, FileException {
2323
wavFrames = decoder.processFrames();
2424
}
2525

26-
private static void printProgress(final long current, final long total) {
27-
final StringBuilder string = new StringBuilder(140);
28-
final int percent = (int) (current * 100 / total);
29-
string.append('\r').append(String.format("%d%% [", percent))
30-
.append(String.join("", Collections.nCopies(percent / 2, "="))).append("\u001B[1m>\u001B[0m")
31-
.append(String.join("", Collections.nCopies(50 - (percent / 2), " "))).append(']').append(" (")
32-
.append(current).append('/').append(total).append(')');
33-
System.out.print(string);
34-
}
35-
36-
public void play() throws LineUnavailableException {
26+
public void play() throws LineUnavailableException, InterruptedException {
3727
// Select audio format parameters
3828
final AudioFormat af = new AudioFormat(decoder.selaFile.getSampleRate(), decoder.selaFile.getBitsPerSample(),
3929
decoder.selaFile.getChannels(), true, false);
@@ -44,14 +34,56 @@ public void play() throws LineUnavailableException {
4434
line.open(af, 2048 * decoder.selaFile.getChannels());
4535
line.start();
4636

37+
//Prepare print thread
38+
PlayProgress progress = new PlayProgress(wavFrames.size());
39+
Thread printThread = new Thread(new ProgressPrinter(progress));
40+
printThread.start();
41+
4742
// Output wave form repeatedly
4843
for (int i = 0; i < wavFrames.size(); i++) {
4944
final byte[] bytes = wavFrames.get(i).getDemuxedShortSamplesInByteArray();
5045
line.write(bytes, 0, bytes.length);
51-
Player.printProgress((i + 1), wavFrames.size());
46+
progress.currentFrameNumber++;
5247
}
48+
printThread.join();
5349
line.drain();
5450
line.stop();
5551
line.close();
5652
}
53+
}
54+
55+
// A separate thread for printing is required since audio lags when we print as well as play audio on single thread on Windows.
56+
class ProgressPrinter implements Runnable {
57+
private PlayProgress progress;
58+
public ProgressPrinter(PlayProgress progress) {
59+
this.progress = progress;
60+
}
61+
62+
public void run() {
63+
while (progress.currentFrameNumber < progress.totalFrameCount) {
64+
printProgress(progress.currentFrameNumber, progress.totalFrameCount);
65+
}
66+
printProgress(progress.currentFrameNumber, progress.totalFrameCount); //Print one last time to make it 100%
67+
}
68+
69+
private void printProgress(final long current, final long total) {
70+
final StringBuilder string = new StringBuilder(140);
71+
final int percent = (int) (current * 100 / total);
72+
string.append('\r').append(String.format("%d%% [", percent))
73+
.append(String.join("", Collections.nCopies(percent / 2, "="))).append(">")
74+
.append(String.join("", Collections.nCopies(50 - (percent / 2), " "))).append(']').append(" (")
75+
.append(current).append('/').append(total).append(')');
76+
System.out.print(string);
77+
}
78+
}
79+
80+
// Data Class for keeping track of progress. Will be shared between audio thread and print thread
81+
class PlayProgress {
82+
public volatile int currentFrameNumber;
83+
public final int totalFrameCount;
84+
85+
public PlayProgress(int totalFrameCount) {
86+
currentFrameNumber = 0;
87+
this.totalFrameCount = totalFrameCount;
88+
}
5789
}

0 commit comments

Comments
 (0)