-
-
Notifications
You must be signed in to change notification settings - Fork 35
Description
The cal() method seems to be at the core of StreamPlayer, and it's the one I really want to understand.
How often is it called? Can you tell me the call frequency, or the time between each call, in a typical case, given a normal sample rate of 44100 Hz or whatever your choice.
There's an inner loop, which starts with
java-stream-player/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java
Lines 833 to 834 in d32a46a
while ((nBytesRead != -1) && status != Status.STOPPED && status != Status.NOT_SPECIFIED | |
&& status != Status.SEEKING) { |
How often does this execute? Frequency or time between iterations, in a typical case?
Yet an inner loop: I suspect it would be easier to read as a while-loop, rather than a for-loop. The most important work of the loop is actually in the for-clause, which is unusual and hard to read. I'm referring to "nBytesRead = audioInputStream.read(audioDataBuffer.array(), totalRead, toRead)".
java-stream-player/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java
Lines 846 to 852 in d32a46a
for (; toRead > 0 && (nBytesRead = audioInputStream.read(audioDataBuffer.array(), totalRead, | |
toRead)) != -1; toRead -= nBytesRead, totalRead += nBytesRead) | |
// Check for under run | |
if (outlet.getSourceDataLine().available() >= outlet.getSourceDataLine().getBufferSize()) | |
logger.info(() -> "Underrun> Available=" + outlet.getSourceDataLine().available() | |
+ " , SourceDataLineBuffer=" + outlet.getSourceDataLine().getBufferSize()); |
Then comes a part which I think is important for performance. In a not-so-good way.
java-stream-player/src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java
Lines 872 to 882 in d32a46a
listeners.forEach(listener -> { | |
if (audioInputStream instanceof PropertiesContainer) { | |
// Pass audio parameters such as instant | |
// bit rate, ... | |
listener.progress(nEncodedBytes, outlet.getSourceDataLine().getMicrosecondPosition(), | |
trimBuffer, ((PropertiesContainer) audioInputStream).properties()); | |
} else | |
// Pass audio parameters | |
listener.progress(nEncodedBytes, outlet.getSourceDataLine().getMicrosecondPosition(), | |
trimBuffer, emptyMap); | |
}); |
This is executed for every iteration of the inter loop we are in. The listener operations may take a long time. Do the listeners really have to be called for each iteration of the loop? The iteration frequency is controlled by audio properties in some way I'm asking about above, but the listeners generally deal with graphics. Maybe graphics don't have to update as often.
Compare with the following from JavaFx MediaPlayer:
@Override public void onAudioSpectrumEvent(final AudioSpectrumEvent evt) {
Platform.runLater(() -> {
AudioSpectrumListener listener = getAudioSpectrumListener();
if (listener != null) {
listener.spectrumDataUpdate(evt.getTimestamp(),
evt.getDuration(),
magnitudes = evt.getSource().getMagnitudes(magnitudes),
phases = evt.getSource().getPhases(phases));
}
});
}
Regrettably, JavaFx MediaPlayer only allows listeners to spectrum events, but the principle could be applied i a more general way. There seem to be some event generator for spectrum events, that run with a different (more slow, I assume) than the audio loop frequency.