stephen / nodetunes

AirTunes v2 Server implementation
215 stars 64 forks source link

Use a buffered stream to improve playback quality #9

Closed asakusuma closed 9 years ago

asakusuma commented 10 years ago

Uses a buffered stream instead of a passthrough stream. Prevents gaps in the audio and this warning:

Didn't have any audio data in callback (buffer underflow)

stephen commented 10 years ago

Thanks for the PR!

(Please forgive my ignorance here, ) would you mind explaining how this prevents the audio gaps?

asakusuma commented 10 years ago

No worries. So the original output stream is just a passthrough stream, which literally just takes input bytes as they come and immediately outputs them. This means that if the connection gets momentarily slow or anything happens that causes the input audio to have a gap, the output audio will also have a gap, which causes the speaker buffer to underflow and not have any audio to play. My PR just adds a buffered stream, which essentially keeps the audio buffer ahead of schedule so that if there's a gap in the input audio, the stream is still outputting audio to the speaker, provided the gap is not larger than the amount of data buffered.

Edit: I've been testing this by airplaying from my iPhone to my MacBook, and I haven't heard any gaps or seen the warning at all anymore.

stephen commented 10 years ago

Hmm, if I'm understanding this correctly, then the bufferstream should be delayed more (to build up a small buffer) than the current passthrough stream is, correct?

Were you previously experiencing severe gaps in the audio? I've only seem the buffer underflow warning when pausing my music output, at which point it would complain because no new audio would come through.

asakusuma commented 10 years ago

Correct. From what I understand, AirPlay is designed to start with a 2 second buffer: http://en.wikipedia.org/wiki/AirPlay#Protocols

I was experiencing gaps in the audio. I wouldn't call them severe, but they were obviously noticeable and consistent.