问题
I am trying to create an app that allows me to record a wav file everytime the input volume is greater than a given volume.
I have the code to record the sound off a button but i would like to automate it, my code below:
public partial class Form1 : Form
{
private WaveIn waveIn;
private WaveFileWriter writer;
String outputFilename = @"c:\test.wav";
public Form1()
{
InitializeComponent();
int sampleRate = 22000;
int channels = 1;
waveIn = new WaveIn();
waveIn.WaveFormat = new WaveFormat(sampleRate, channels);
waveIn.DeviceNumber = 0;
waveIn.DataAvailable += new EventHandler<WaveInEventArgs>(
waveIn_DataAvailable);
writer = new WaveFileWriter(outputFilename, waveIn.WaveFormat);
waveIn.StartRecording();
}
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
writer.WriteData(e.Buffer, 0, e.BytesRecorded);
}
private void button1_Click(object sender, EventArgs e)
{
waveIn.StopRecording();
waveIn.Dispose();
writer.Close();
}
}
回答1:
in waveIn_DataAvailable
you can examine each sample by looking at the bytes in e.Buffer
. (Assuming you recorded in 16 bit, each pair of bytes is one sample - use BitConverter.ToInt16). If any sample goes above the threshold you specified then you can write with writer.WriteData
.
To switch off recording, you would probably want to check that a certain number of 'silent' samples had passed.
来源:https://stackoverflow.com/questions/8480556/start-recording-wave-using-naudio-when-sound-input-reachs-a-certain-level