Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Streaming with ohLibSpotify and NAudio
06-02-2014, 06:32 PM
Post: #7
RE: Streaming with ohLibSpotify and NAudio
It's pretty hard to guess, but here are some ideas:

1. Allocate the byte array once up-front and then re-use it. Lots of high-frequency allocations like this triggers lots of garbage collection, and that can stall your managed code long enough to cause stutters. You'll need to make it big enough, and then correctly handle both when spotify gives you too little data (copy only what it gives you into the array, then call AddSamples with the size you copied) or too much (copy only what fits in the array, call AddSamples with that size, then return the number of frames you actually copied).

2. Detect your stutters and report them to libspotify. In principle it should compensate by streaming further ahead. This might be tricky without writing your own WaveProvider, since none of your code is running at the time the stutter happens, but you can detect it after-the-fact - when you enter GetAudioBufferStats or MusicDelivery, if the BufferedWaveProvider.BufferedBytes is 0, there has been a stutter. Check the libspotify docs. I think it wants you to set stutters to the number of underruns since it last called GetAudioBufferStats.

3. The implementation you've posted for GetAudioBufferStats doesn't look quite right. When libspotify calls this, it wants to know about the state of your buffer right now, not the state it was in when it last called MusicDelivery. Look at it like this - you have a buffer of audio data. It is constantly draining away as you play the audio. Periodically libspotify calls MusicDelivery to 'top up' your buffer. In between that, it calls GetAudioBufferStats, possibly several times, to check up on the state of the buffer, to figure out how soon it should call MusicDelivery again (and to arrange for data to stream across the Internet at the correct rate to achieve that). You have two problems: one is that you are querying the buffer state during MusicDelivery, and that value will be out of date by the time GetAudioBufferStats is called. The other is that your implementation of SetAudioBufferStats throws away the value anyway, and your implementation of GetAudioBufferStats just calls the base class implementation, which does nothing useful. You might find it helpful to read up on C# "out" parameters - I think you might have misunderstood what they do.
Visit this user's website Find all posts by this user

Messages In This Thread
RE: Streaming with ohLibSpotify and NAudio - andreww - 06-02-2014 06:32 PM

Forum Jump: