[英]How to save an audio capture in a wav file using ASIO and Naudio?
So far, I wrote that : 到目前为止,我写道:
private AsioOut ASIODriver;
private BufferedWaveProvider buffer;
String[] drivernames = AsioOut.GetDriverNames();
ASIODriver = new AsioOut(drivernames[1]);
buffer = new BufferedWaveProvider(new WaveFormat(sampleRate, 16, 2));
ASIODriver.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(ASIODriver_AudioAvailable);
ASIODriver.InitRecordAndPlayback(buffer, 2, sampleRate);
ASIODriver.Play();
private void ASIODriver_AudioAvailable(object sender, AsioAudioAvailableEventArgs e)
{
byte[] buf = new byte[e.SamplesPerBuffer * 4];
for (int i = 0; i < e.InputBuffers.Length; i++)
{
Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer * 4);
Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer * 4);
if (recorderOn && i == 1)
{
recorder.addInputToStream(buf);
}
}
}
This part is used to capture and playback the sound from a guitar with ASIO. 此部分用于使用ASIO捕获和播放吉他的声音。 No problems so far. 到目前为止没有问题。
public void addInputToStream(byte[] buffer)
{
byte[] sample = new byte[buffer.Length / 4];
if (waveWriter == null) waveWriter = new WaveFileWriter(@"C:\Temp\Test0001.wav", new WaveFormat(44100, 16, 1));
for (int i = 0; i < buffer.Length; i = i + 4)
{
sample[i/4] = (byte)Convert.ToSingle(buffer[i] + buffer[i + 1] + buffer[i + 2] + buffer[i + 3]);
}
waveWriter.Write(sample, 0, sample.Length);
}
The problem appears in the method addInputToStream
, the sound is well put in the .wav file but I have a lot of "audio parasites" in the file. 问题出现在方法addInputToStream
,声音很好地放在.wav文件中,但是文件中有很多“音频寄生虫”。 Very irritating. 很烦人。 I tried to change the i
variable when I call the method, to not used Convert.ToSingle
but same result. 我在尝试调用方法时尝试将i
变量更改为未使用Convert.ToSingle
但结果相同。
I suspect a problem with the WaveFormat
of waveWriter
but I don't know what I'm missing. 我怀疑waveWriter
的WaveFormat
存在问题,但我不知道自己缺少什么。 Anyone of you have a clue ? 有人知道吗?
You need to create a WaveFileWriter and write the samples out. 您需要创建一个WaveFileWriter并将示例写出来。 This is pretty simple, but took me a while to get also. 这很简单,但花了我一段时间才获得。 (note: I didn't setup the waveprovider, but you have that part already). (注意:我没有设置waveprovider,但是您已经拥有了该部分)。
class AsioSample
{
private NAudio.Wave.AsioOut _audioDevice;
public WaveFileWriter AsioWaveFileWriter;
public AsioSample()
{
//this is set for one channel
AsioWaveFileWriter = new WaveFileWriter("sample.wav", new WaveFormat(48000, 1));
_audioDevice = new NAudio.Wave.AsioOut(2);//or whatever
_audioDevice.InitRecordAndPlayback(_waveProvider, 1, 48000);
_audioDevice.AudioAvailable += new EventHandler<NAudio.Wave.AsioAudioAvailableEventArgs>(OnAudioAvailable);
_audioDevice.Play();
}
private unsafe void OnAudioAvailable(object sender, NAudio.Wave.AsioAudioAvailableEventArgs e)
{
var floatsamples = new float[e.SamplesPerBuffer * e.InputBuffers.Length];
e.GetAsInterleavedSamples(floatsamples);
AsioWaveFileWriter.WriteSamples(floatsamples, 0, floatsamples.Length);
AsioWaveFileWriter.Flush();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.