简体   繁体   English

用于NAudio录音的音频电平表

[英]Audio level meter for NAudio recording

I´m recording audio to a WAV file using NAudio library. 我正在使用NAudio库将音频录制到WAV文件。 I want to be able to update a ProgressBar to the value of audio level/volume as the sample data arrives. 我希望能够在示例数据到达时将ProgressBar更新为音频级别/音量的值。

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

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) 对于Windows窗体示例,请查看NAudioDemo源代码,特别是AudioPlaybackPanel类,它使用MeteringSampleProvider并订阅其StreamVolume事件以设置自定义音量计控件(包含在NAudio中)的属性

For a WPF example, look at voicerecorder.codeplex.com which uses a similar technique and the volume metering is described in this article . 对于WPF示例,请查看使用类似技术的voicerecorder.codeplex.com本文介绍了音量计量。

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: 与NAudio一起使用足以将设备定义为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM