到目前为止,我写道:
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);
}
}
}
此部分用于通过ASIO捕捉和播放吉他的声音。到目前为止没问题。
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);
}
问题出现在方法addInputToStream
中,声音放在.wav文件中,但我有很多&#34;音频寄生虫&#34;在文件中。很烦人。
我在调用方法时尝试更改i
变量,但未使用Convert.ToSingle
但结果相同。
我怀疑WaveFormat
的{{1}}有问题,但我不知道自己错过了什么。
你们中的任何人都有线索吗?
答案 0 :(得分:0)
您需要创建一个WaveFileWriter并将样本写出来。这很简单,但花了我一段时间才得到。 (注意:我没有设置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();
}
}