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
It’s much more advisable to use NAudio’s
BufferedWaveProviderinstead 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.