Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8215583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:50:21+00:00 2026-06-07T11:50:21+00:00

I’m trying to combine NAudio with Reactive Extentions, and I’m having an issue getting

  • 0

I’m trying to combine NAudio with Reactive Extentions, and I’m having an issue getting NAudio to play the audio.

The following is my code so far:

 public class WaveOutPlayer : IDisposable
{
    WaveOut wavOut = new WaveOut(WaveCallbackInfo.FunctionCallback());

    public WaveOutPlayer(int device, int sampleRate, int channels, IStereoSource source)
    {
        var provider = new WavProv(source, sampleRate, channels);
        provider.SetWaveFormat(sampleRate,channels);

        wavOut.Init(provider);
    }

    private class WavProv : WaveProvider32
    {
        AutoResetEvent are = new AutoResetEvent(false);
        ConcurrentQueue<float> queue = new ConcurrentQueue<float>();

        public WavProv(IStereoSource source, int sampleRate, int channels)
        {
            source.ChannelLeft
           .Zip(source.ChannelRight, (ls, rs) => new double[] { ls, rs })      //one sample from each channel
           .SelectMany(samps => samps)                                         //convert to samples array l,r,l,r,l
           .Buffer(sampleRate * channels * 1)                                     //buffer samplerate*channels*2 seconds
           .Select(x => x.ToArray())                                            // to observable of chunks
           .Do(x => { are.Set(); })
           .SubscribeOn(NewThreadScheduler.Default)
           .Subscribe(data =>
           {
               //queue.Enqueue((float)data);
               data.ToList().ForEach((x) => queue.Enqueue((float)x));
           });

        }

        public override int Read(float[] buffer, int offset, int sampleCount) 
        {
            int itemsRead;

            if (!queue.Any())                                                    //No data in the queue
            {
                //are.WaitOne();
                buffer = Enumerable.Repeat(0.0f, sampleCount).ToArray();        //Wait for some data
                itemsRead = sampleCount;
            }
            else
            {

                //number of items to read is lower of  samplecount or items in queue
                int itemsToRead = (queue.Count() > sampleCount) ? sampleCount : queue.Count();

                for (itemsRead = 0; itemsRead < itemsToRead; itemsRead++)
                {
                    float res;
                    if(queue.TryDequeue(out res))
                        buffer[itemsRead + offset] = res;       //add items from queue to buffer
                }
            }
            Console.WriteLine("Requested:{0}, Read: {1}",sampleCount, itemsRead);
            return itemsRead;
        }
    }

    public void Play()
    {
        wavOut.Play();
    }

    public void Dispose()
    {
        wavOut.Dispose();
    }
}

The Read method is being called, and the Console.WriteLine is showing I’m always providing enough data. Incidently, If I slow down the production of signal, such that I need to occasionally provide an all zeros buffer, (code not present currently) then I get a ‘clicking’ sound.

Is there any other issues/gotchas that I’ve missed?

e.g. Is the amplitude range between 0-1 only, or does it support the full range of float?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:50:23+00:00Added an answer on June 7, 2026 at 11:50 am

    It’s much more advisable to use NAudio’s BufferedWaveProvider instead of the whole reset event / queue. It was designed for scenarios like these.

    You will encounter breaks when the player’s buffer runs out abruptly – the data is not streamed fast enough.

    Secondly, let me clear up the function signature:

    int Read(float[] buffer, int offset, int sampleCount)

    You will be supplied with a buffer, an offset in the buffer, and a chunk size. The value you returned indicates the number of items you could supply for this Read operation.
    You are not required to supply an exact number of bytes as corresponds to the samples requested.

    So if you wanted to supply silence, don’t construct and return a buffer of zeros, and instead simply return 0 as the read length.

    Finally, if you don’t want an abrupt cut off, you can slowly decrease the amplitude of the last set of samples – it can be as simple as multiplying by (N - n) / N (n ϵ [0, N]). Cut off due to a buffer underrun is an entirely different issue.

    N.B.

    Let me add that your present solution is a not a good way to do this in production. Using a lot of objects in between will eventually lead to a lot of latency issues when garbage collection comes knocking. Mark Heath, the author of NAudio, has used a lot of tricks to minimize Garbage collection, like reusing buffers, EventArgs and the like, so it would be unjust if it went to waste.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.