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. 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. Very irritating.
I tried to change the i
variable when I call the method, to not used Convert.ToSingle
but same result.
I suspect a problem with the WaveFormat
of waveWriter
but I don't know what I'm missing.
Anyone of you have a clue ?
You need to create a WaveFileWriter and write the samples out. 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).