someoneunimportant
someoneunimportant

Reputation: 29

NAudio Asio Record Playback and Save (noise only)

I'm trying to pass the audio signal I get from the MIDI keyboard with Asio through my sound card, save it and write it to a file. I am aware that there are examples of this on the site, I have read all of them, but unfortunately, they could not solve my problem.

My problem is that I listen quite clearly while recording the audio, but when I open the file I recorded, all I hear is noise. I've been struggling for days I couldn't find any solution. Any help will be appreciated. Here is my code:

void asioStartPlaying(object sender, EventArgs e)
       {
           

           var asioOut = new AsioOut(2);
           BufferedWaveProvider wavprov = new BufferedWaveProvider(new WaveFormat(48000, 16, 1));
           asioOut.AudioAvailable += new EventHandler<AsioAudioAvailableEventArgs>(asio_DataAvailable);
           //asioOut.PlaybackStopped += new EventHandler<StoppedEventArgs>(asio_StoppedRecording);
           asioOut.InitRecordAndPlayback(wavprov, 1, 0);
           asioOut.Play();
           

           AsioRecordPath = filepath.Text;

           asioFile = new WaveFileWriter(GetAsioFileName(), wavprov.WaveFormat);

           button1.Enabled = false;
       }

void asio_DataAvailable(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);
           }
           
           asioFile.Write(buf, 0, e.SamplesPerBuffer * 4);
           asioFile.Flush();

           
           e.WrittenToOutputBuffers = true;
       }

Upvotes: 0

Views: 208

Answers (1)

someoneunimportant
someoneunimportant

Reputation: 29

GetAsInterleavedSamples() method helped. It creates a float array and using this array while writing to 'asioFile' solved the issue. Mark Heath has a great blog that I often visit, I say go check it out. https://markheath.net/post/asio-recording-in-naudio

Upvotes: 1

Related Questions