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

VolumeMeter from PCM buffer

Jun 11, 2014 at 1:45 PM
Hi

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.Init(postVolumeMeter);
//audioOut.Volume = 0;

audioOut.Play();
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?
Coordinator
Jun 12, 2014 at 11:36 AM
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 5:25 PM
Thank you!