This project is read-only.

Playing WriteableBufferingSource speeds up after 5 sec

Feb 18, 2015 at 5:46 PM
Edited Feb 18, 2015 at 5:47 PM
Hello, the answer on post Play a Byte Array helped me a lot of how to buffer a music, but I am having another problem....
I am playing from a PCM, the first 5 seconds of the music it play right, but after that, it plays twice as fast

Here what I have done:
I have a player class with a event handler, that when the program receives a byte from a location, it adds to a queue, and a thread to add from this queue to the buffer.

The music I am getting from is from spotify,using libspotifydotnet, the CSCore.Codecs.RAW.RawDataReader plays correctly, BUT I cant keep adding more data to the stream while playing, OR CAN I???!!

Here is what i think is the relevant code
        //Main program
        WasapiOut soundOut = new WasapiOut();
        public static WriteableBufferingSource source;
        private static Queue<byte[]> _q = new Queue<byte[]>();

        source = new WriteableBufferingSource(new CSCore.WaveFormat(Session.format.sample_rate, 16, Session.format.channels, CSCore.AudioEncoding.Pcm));
        source.FillWithZeros = false;
        byte[] buffer = null;
        while (!_interrupt && !_complete)
            if (_q.Count > 0)
                buffer = _q.Dequeue();

                source.Write(buffer, 0, buffer.Length);
                System.Console.WriteLine("Buffer write {0} bytes", buffer.Length);
Feb 19, 2015 at 11:27 PM
Well, the WriteableBufferingSource uses a fixed buffer size. If the buffer is full, you have to read from the buffer first in order to free the used buffer. If there is some space to write data to, you can write new data to the buffer. The big advantage of this is that you have a high memory usage.
Check the Length property of the WriteableBufferingSource if the return value of the Write method is < buffer.Length.
To solve your problem, you could wait until the soundout reads from the buffer, specify a bigger buffersize or implement your own bufferingsource.
Feb 20, 2015 at 11:27 AM
Edited Feb 20, 2015 at 12:42 PM
Yes, it is writing less than the buffer size, do we have a way to increase the buffer size or use a variable buffer size????

I cant get the available space in the buffer, length and position always return "-1"

Any ideas ???

Thanks :)

edit: Hehehe found a way to increase the buffer size, I used
CSCore.WaveFormat waveFormat = new CSCore.WaveFormat(Session.format.sample_rate, 16, Session.format.channels, CSCore.AudioEncoding.Pcm);

source = new WriteableBufferingSource(waveFormat, waveFormat.BytesPerSecond * 240);
Does it means I have a 240seconds buffer??? 240 is too much?? I can have any problem with it???