0

I am playing a PCM RAW format, and 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 have done so far

    //Main.cs
    WasapiOut soundOut = new WasapiOut();
    soundOut.Initialize(Player.source);
    soundOut.Play();

...

   //Player.cs
    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 written {0} bytes", buffer.Length);
            Thread.Sleep(10);
        }
    }

    //New data downloaded event
    private static void Session_OnAudioDataArrived(byte[] buffer)
    {
        if (!_interrupt && !_complete)
        {
            _q.Enqueue(buffer);
        }
    }
TLPNull
  • 475
  • 4
  • 12

2 Answers2

1

First, sorry for my english, i do the same, and i check that player is Stop, you need one of this fix:

     while (!_interrupt && !_complete)
        {
            if (_q.Count > 0)
            {
                buffer = _q.Dequeue();
                source.Write(buffer, 0, buffer.Length);

// Check is playing
    if (soundOut.PlaybackState == PlaybackState.Stopped)
                    soundOut.Play();

                System.Console.WriteLine("Buffer written {0} bytes", buffer.Length);
                Thread.Sleep(10);
            }
        }

Or put

FillWithZeros=true

-1

The WriteableBufferingSource uses a fixed buffer size. If the buffer is full it cant add new data, resolved incresing the buffer size.

CSCore.WaveFormat waveFormat = new CSCore.WaveFormat(Session.format.sample_rate, 16, Session.format.channels, CSCore.AudioEncoding.Pcm);

source = new WriteableBufferingSource(waveFormat, waveFormat.BytesPerSecond * 240);
TLPNull
  • 475
  • 4
  • 12