Audio level meter for NAudio recording

余生颓废 提交于 2020-01-02 08:09:06

问题


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);
     }
}

回答1:


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.




回答2:


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];


来源:https://stackoverflow.com/questions/12556520/audio-level-meter-for-naudio-recording

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!