Record the input from NAudio WaveIn and output it to NAudio WaveOut

I want to receive input from a microphone device through NAudio.WaveIn, and then output this exact input to the output device through NAudio.WaveOut.

How can I do it?

+6
source share
2 answers

Here is the code that worked for me:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using NAudio.Wave; using NAudio.CoreAudioApi; namespace WindowsFormsApplication1 { public partial class Form4 : Form { private BufferedWaveProvider bwp; WaveIn wi; WaveOut wo; public Form4() { InitializeComponent(); wo = new WaveOut(); wi = new WaveIn(); wi.DataAvailable += new EventHandler<WaveInEventArgs>(wi_DataAvailable); bwp = new BufferedWaveProvider(wi.WaveFormat); bwp.DiscardOnBufferOverflow = true; wo.Init(bwp); wi.StartRecording(); wo.Play(); } void wi_DataAvailable(object sender, WaveInEventArgs e) { bwp.AddSamples(e.Buffer, 0, e.BytesRecorded); } } } 
+12
source

A better way would be to use a BufferedWaveProvider as input to WaveOut. Then, in the DataAvailable WaveIn callback, feed the data written to the BufferedWaveProvider

 void DataAvailable(object sender, WaveInEventArgs args) { bufferedWaveProvider.AddSamples(args.Buffer, 0, args.BytesRecorded); } 

You need to know that the default buffer size will lead to a noticeable delay, so if you were hoping for a low latency, you might need to experiment a bit with the buffer sizes to see how much you can get it.

+4
source

Source: https://habr.com/ru/post/886087/


All Articles