听取麦克风输入的音频 [英] Hearing the Incoming audio from mic

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

问题描述

我只是想听到我说的话用n音讯话筒,这是我到目前为止的代码,但问题是我不能听到任何声音。 。任何帮助,将不胜感激。

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.BufferMilliseconds waveOut.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.NET 为C#。

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

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

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