从麦克风听到传入音频

本文关键字:音频 麦克风 | 更新日期: 2023-09-27 18:01:12

我只想听听我用NAudio对麦克风说的话,这是我到目前为止的代码,但问题是我什么都听不见。任何帮助都将不胜感激。

public partial class frmMain : Form
    {
        private WaveIn waveIn; // Gets an audio from microphone
        private WaveOut waveOut; // Sends audio to speaker
        private BufferedWaveProvider waveProvider; // Gets an audio from stream
        public frmMain()
        {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            waveOut = new WaveOut();
            waveIn = new WaveIn();
            waveProvider = new BufferedWaveProvider(waveIn.WaveFormat);
            waveOut.Init(waveProvider);             
            waveIn.DataAvailable += waveIn_DataAvailable;
            waveOut.Play();            
        }
        private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            waveProvider.Read(e.Buffer, 0, e.BytesRecorded);
        }
        private void btnStop_Click(object sender, EventArgs e)
        {
            waveIn.StopRecording();
            waveIn.Dispose();
        }
        private void btnStart_Click(object sender, EventArgs e)
        {
            waveIn.StartRecording();
        }
    }

我将在网络编程中使用这个场景,在这个场景中,我将数据从麦克风发送到插座,然后在客户端,BufferedWaveProvider将读取数据,然后将其发送到扬声器。如果什么是更好的方法,请发表一些评论。

TIA

从麦克风听到传入音频

承诺的示例代码。代码用于具有两个按钮(名为StartBtn和StopBtn(的表单。

public partial class Form1 : Form
{
    private WaveIn waveIn = null;
    private BufferedWaveProvider waveProvider = null;
    private WaveOut waveOut = null;
    public Form1()
    {
        InitializeComponent();
    }
    private void StartBtn_Click(object sender, EventArgs e)
    {
        if (waveIn != null)
            return;
        // create wave input from mic
        waveIn = new WaveIn(this.Handle);
        waveIn.BufferMilliseconds = 25;
        waveIn.RecordingStopped += waveIn_RecordingStopped;
        waveIn.DataAvailable += waveIn_DataAvailable;
        // create wave provider
        waveProvider = new BufferedWaveProvider(waveIn.WaveFormat);
        // create wave output to speakers
        waveOut = new WaveOut();
        waveOut.DesiredLatency = 100;
        waveOut.Init(waveProvider);
        waveOut.PlaybackStopped += wavePlayer_PlaybackStopped;
        // start recording and playback
        waveIn.StartRecording();
        waveOut.Play();
    }
    void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        // add received data to waveProvider buffer
        if (waveProvider != null)
            waveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
    }
    private void StopBtn_Click(object sender, EventArgs e)
    {
        if (waveIn != null)
            waveIn.StopRecording();
    }
    void waveIn_RecordingStopped(object sender, StoppedEventArgs e)
    {
        // stop playback
        if (waveOut != null)
            waveOut.Stop();
        // dispose of wave input
        if (waveIn != null)
        {
            waveIn.Dispose();
            waveIn = null;
        }
        // drop wave provider
        waveProvider = null;
    }
    void wavePlayer_PlaybackStopped(object sender, StoppedEventArgs e)
    {
        // stop recording
        if (waveIn != null)
            waveIn.StopRecording();
        // dispose of wave output
        if (waveOut != null)
        {
            waveOut.Dispose();
            waveOut = null;
        }
    }
}

特别注意waveIn.BufferMillisecondswaveOut.DesiredLatency设置,以减少滞后时间。

为了压缩网络传输的数据,我建议使用不同的库来处理数据块。您可能需要调整BufferMilliseconds值以减少开销并获得更好的压缩比。

OpusCodec看起来是一个合理的选择,使用了针对C#的Opus.NET。