This project has moved and is read-only. For the latest updates, please go here.

VolumeMeter from PCM buffer

Jun 11, 2014 at 2:45 PM

I want to show VolumeMeter using PCM data (comes as 16-bit PCM data).

So, how I can do that?

I'm using BufferedWaveProvider as source.
var vuWaveFormat = new WaveFormat(SampleRate, BitsPerSample, Channels);
var bufferedWaveProvider = new BufferedWaveProvider(vuWaveFormat);
Next step - WaveChannel (present in samples, can be used to change volume, not very important for me but good)
var waveChannel = new SampleChannel(bufferedWaveProvider, true);
waveChannel.Volume = GetVolumeFromPercent(100); // Db from percent
Next - MeteringSampleProvider
var postVolumeMeter = new MeteringSampleProvider(waveChannel);
postVolumeMeter.StreamVolume += OnPostVolumeMeter;
Next - audio output to DirectSound.
audioOut = new DirectSoundOut();
//audioOut.Volume = 0;

All worked and made by sample. That's great.

But I don't need any audio output really. So, something like Null Renderer required. Or, how just to made VolumeMeter using PCM data from buffer using any other way?
Jun 12, 2014 at 12:36 PM
you need something to pull the audio through the signal chain. Probably easiest is to call Read on your postVolumeMeter manually.

set up like this:
var bufferedWaveProvder = new BufferedWaveProvider(vuWaveFormat)
var meter = new MeteringSampleProvider(bufferedWaveProvider.ToSampleProvider())
then when you add data to bufferedWaveProvider, call read on meter into a temporary buffer:
bufferedWaveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
meter.Read(tempReadBuffer, 0, e.BytesRecorded / 2);
the number of samples to ask for must be the number of samples requested or else bufferedWaveProvider will send some extra samples. So I divide bytes by two, assuming you're starting with 16 bit audio
Jul 3, 2014 at 6:25 PM
Thank you!