Reputation: 31
I´m recording audio to a WAV file using NAudio library. I want to be able to update a ProgressBar to the value of audio level/volume as the sample data arrives.
public WaveIn Recorder_NAudio;
public WaveFileWriter Writer_NAudio;
public void record()
{
Recorder_NAudio = new WaveIn();
Recorder_NAudio.WaveFormat = new NAudio.Wave.WaveFormat(sampleRate, 1);
Writer_NAudio = new WaveFileWriter(File.Open(tempPath, FileMode.Create), Recorder_NAudio.WaveFormat);
Recorder_NAudio.DataAvailable += new EventHandler<WaveInEventArgs>(Recorder2_DataAvailable);
Recorder_NAudio.StartRecording();
}
void Recorder2_DataAvailable(object sender, WaveInEventArgs e)
{
if (Writer_NAudio != null) if (Writer_NAudio.CanWrite)
{
Writer_NAudio.WriteData(e.Buffer, 0, e.BytesRecorded);
// I need help to create this function
ProgressBar1.value = GetVolumeFromBytes(e.buffer);
}
}
Upvotes: 3
Views: 11018
Reputation: 29
I wrote a control that does everything you want to control can be found here.
For use with NAudio is sufficient to define a device as:
Private NAudio.CoreAudioApi.MMDevice device;
then at any time you know the value of the audio levels of the following values:
Master :
device.AudioMeterInformation.MasterPeakValue;
Left :
device.AudioMeterInformation.PeakValues[0];
Right :
device.AudioMeterInformation.PeakValues[1];
Upvotes: -1
Reputation: 49522
For a Windows Forms example, have a look at the NAudioDemo source code and in particular the AudioPlaybackPanel
class, which uses a MeteringSampleProvider
and subscribes to its StreamVolume
event to set the properties on a custom volume meter control (included in NAudio)
For a WPF example, look at voicerecorder.codeplex.com which uses a similar technique and the volume metering is described in this article.
Upvotes: 2