Skip to content

Commit 87722bb

Browse files
committed
Added sleep in print thread to reduce CPU usage
1 parent 40b9c7b commit 87722bb

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import org.sela.exception.FileException;
1616

1717
public class Player {
18-
private List<WavFrame> wavFrames;
19-
private Decoder decoder;
18+
private final List<WavFrame> wavFrames;
19+
private final Decoder decoder;
2020

2121
public Player(final File inputFile) throws IOException, FileException {
2222
decoder = new Decoder(inputFile, null);
@@ -34,9 +34,9 @@ public void play() throws LineUnavailableException, InterruptedException {
3434
line.open(af, 2048 * decoder.selaFile.getChannels());
3535
line.start();
3636

37-
//Prepare print thread
38-
PlayProgress progress = new PlayProgress(wavFrames.size());
39-
Thread printThread = new Thread(new ProgressPrinter(progress));
37+
// Prepare print thread
38+
final PlayProgress progress = new PlayProgress(wavFrames.size());
39+
final Thread printThread = new Thread(new ProgressPrinter(progress));
4040
printThread.start();
4141

4242
// Output wave form repeatedly
@@ -52,18 +52,25 @@ public void play() throws LineUnavailableException, InterruptedException {
5252
}
5353
}
5454

55-
// A separate thread for printing is required since audio lags when we print as well as play audio on single thread on Windows.
55+
// A separate thread for printing is required since audio lags when we print as
56+
// well as play audio on single thread on Windows.
5657
class ProgressPrinter implements Runnable {
57-
private PlayProgress progress;
58-
public ProgressPrinter(PlayProgress progress) {
58+
private final PlayProgress progress;
59+
60+
public ProgressPrinter(final PlayProgress progress) {
5961
this.progress = progress;
6062
}
6163

6264
public void run() {
6365
while (progress.currentFrameNumber < progress.totalFrameCount) {
6466
printProgress(progress.currentFrameNumber, progress.totalFrameCount);
67+
try {
68+
Thread.sleep(200);
69+
} catch (InterruptedException e) {
70+
e.printStackTrace();
71+
}
6572
}
66-
printProgress(progress.currentFrameNumber, progress.totalFrameCount); //Print one last time to make it 100%
73+
printProgress(progress.currentFrameNumber, progress.totalFrameCount); // Print one last time to make it 100%
6774
}
6875

6976
private void printProgress(final long current, final long total) {
@@ -77,12 +84,13 @@ private void printProgress(final long current, final long total) {
7784
}
7885
}
7986

80-
// Data Class for keeping track of progress. Will be shared between audio thread and print thread
87+
// Data Class for keeping track of progress. Will be shared between audio thread
88+
// and print thread
8189
class PlayProgress {
8290
public volatile int currentFrameNumber;
8391
public final int totalFrameCount;
8492

85-
public PlayProgress(int totalFrameCount) {
93+
public PlayProgress(final int totalFrameCount) {
8694
currentFrameNumber = 0;
8795
this.totalFrameCount = totalFrameCount;
8896
}

0 commit comments

Comments
 (0)