听到来自麦克风的传入音频 [英] Hearing the Incoming audio from mic

查看:30
本文介绍了听到来自麦克风的传入音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i just want to hear what i say to microphone using NAudio and this is my code so far but the problem is i can't hear anything. any help would be appreciated.

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 将读取数据,然后将其发送到扬声器.如果有更好的方法,请发表评论.

i will use this scenario in network programming on which i send the data from microphone to the socket then on the client side the BufferedWaveProvider will read the data then send it to the speaker. Please put also some comment if what is the better way to do it.

TIA

推荐答案

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

Sample code as promised. Code is for a form with two buttons (named StartBtn and 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 设置以减少延迟时间.

Note especially the waveIn.BufferMilliseconds and waveOut.DesiredLatency settings to reduce the lag times.

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

For compressing the data for network transmission I suggest using a different library to process the data blocks. You might need to tune the BufferMilliseconds value to reduce the overheads and get better compression ratios.

Opus 编解码器 看起来是一个合理的选择,带有 Opus.NET 用于 C#.

The Opus Codec looks like a reasonable option, with Opus.NET for C#.

这篇关于听到来自麦克风的传入音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆