This project has moved. For the latest updates, please go here.

Question about Universal Apps

Dec 22, 2015 at 8:09 PM
Hi friends,


I could not find a proper forum which I why I am asking here. I use the library in WPF without issues, but after porting to Universal Apps I get stutters in the audio. I've been fiddling with some of the parameters but I don't really know much about how this works.

Hope you can help. Thanks a lot
    public void Play(string uri)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);

        }
        private void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream responseStream = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(responseStream);

            var waveFormat = new WaveFormat(8000, 16, 1);
            var bufferedWaveProvider = new BufferedWaveProvider(waveFormat);
            bufferedWaveProvider.BufferDuration = TimeSpan.FromMilliseconds(200);

            var waveOut = new WasapiOutRT(AudioClientShareMode.Shared, 100);
            waveOut.Init(() => bufferedWaveProvider);
            waveOut.Play();

            byte[] buffer = new byte[800];
            while (true)
            {
                var sizeRead = streamRead.BaseStream.Read(buffer, 0, buffer.Length);
                if (sizeRead > 0 && bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes > sizeRead)
                {
                    bufferedWaveProvider.AddSamples(buffer, 0, sizeRead);
                }
            }
        }
Dec 22, 2015 at 8:09 PM
Just for comparison, here's the code I use in WPF.
var webRequest = WebRequest.Create(uri);

                using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (var responseStream = new StreamReader(webResponse.GetResponseStream()))
                    {
                        var waveFormat = new WaveFormat(8000, 16, 1);

                        var bufferedWaveProvider = new BufferedWaveProvider(waveFormat);
                        bufferedWaveProvider.BufferDuration = TimeSpan.FromMilliseconds(200);

                        var waveOut = new WaveOutEvent();
                        waveOut.DesiredLatency = 100;
                        waveOut.Init(bufferedWaveProvider);
                        waveOut.Play();

                        byte[] buffer = new byte[800];
                        while (true)
                        {
                            if (bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes > buffer.Length)
                            {
                                if (responseStream.Peek() > -1 && responseStream.BaseStream.Read(buffer, 0, buffer.Length) > 0)
                                {
                                    bufferedWaveProvider.AddSamples(buffer, 0, buffer.Length);
                                    await Task.Delay(1, cancellationToken.Token);
                                }
                            }

                        }
                    }
                }
Dec 22, 2015 at 10:29 PM
Edited Dec 22, 2015 at 10:32 PM
Solved!


After much fiddling I decided to try to play a wave file (16bit 8000hz PCM stream) from disk. The file played correctly but at twice the speed, then I changed the waveFormat to 4000hz on the WaveOut and it worked!

Here's the code.
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream responseStream = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(responseStream);

            var waveFormat = new WaveFormat(4000, 16, 1);
            var bufferedWaveProvider = new BufferedWaveProvider(waveFormat);
            bufferedWaveProvider.BufferDuration = TimeSpan.FromMilliseconds(1000);

            var waveOut = new WasapiOutRT(AudioClientShareMode.Shared, 100);
            waveOut.Init(() => bufferedWaveProvider);
            waveOut.Play();

            byte[] buffer = new byte[800];
            while (true)
            {
                if (bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes > buffer.Length)
                {
                    var bufferSize = streamRead.BaseStream.Read(buffer, 0, buffer.Length);
                    if (bufferSize > 0 && bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes > bufferSize)
                        bufferedWaveProvider.AddSamples(buffer, 0, bufferSize);
                }
            }
Please note that this provided correct audio for a 8000hz audio stream from disk and also a 8000hz network audio stream. If you have suggestions on improving this code, by all means. Thanks!
Marked as answer by Fegelein on 12/22/2015 at 3:29 PM
Coordinator
Dec 23, 2015 at 2:25 PM
More likely the correct WaveFormat should have been 8000,8,1 or maybe 8000,16,2, but glad to hear you got something working
Dec 23, 2015 at 5:52 PM
hi Mark,


I have just tried both and they don't work. I've also tried them in WPF with similar results. It strikes me as odd that the code is nearly identical now with regard to NAudio and I could only get it to work using 4kHz.

Since I am a simple library consumer I don't really care though. Would you like me to prepare a case using a RAW wav and two example projects? One for WPF and one for Universal Apps?