使用NAudio发送播放音频

使用NAudio发送播放音频,audio,microphone,naudio,Audio,Microphone,Naudio,我已经设法使用找到的代码从麦克风发送音频 然而,我一直无法使用 CodeProject中的代码具有明确的编码和解码代码,例如: G711.Encode_aLaw G711.Decode_uLaw 翻译并返回通过网络发送的字节 有没有可能为上面的CodeProject应用程序获取一些NAudio的示例代码?这是一个我使用NAudio、麦克风输入、扬声器输出以及u-Law或a-Law编码编写的快速C#Console应用程序。NAudio.Codecs名称空间包含A定律和u定律编码器和解码器 这个程

我已经设法使用找到的代码从麦克风发送音频

然而,我一直无法使用

CodeProject中的代码具有明确的编码和解码代码,例如:

G711.Encode_aLaw
G711.Decode_uLaw
翻译并返回通过网络发送的字节

有没有可能为上面的CodeProject应用程序获取一些NAudio的示例代码?

这是一个我使用NAudio、麦克风输入、扬声器输出以及u-Law或a-Law编码编写的快速C#Console应用程序。
NAudio.Codecs
名称空间包含A定律和u定律编码器和解码器

这个程序不通过网络发送数据(这并不难,我只是不想在这里做)。我把这个留给你。相反,它包含一个“发送方”线程和一个“接收方”线程

麦克风
DataAvailable
事件处理程序只是将字节缓冲区放入队列中(它制作缓冲区的副本-您不想保留事件中的实际缓冲区)。“发送方”线程获取排队的缓冲区,将PCM数据转换为g.711并将其放入第二个队列。这个“放入第二个队列”部分是您为特定应用程序发送到远程UDP目的地的地方

“Receiver”线程从第二个队列读取数据,将其转换回PCM,并将其提供给WaveOut(扬声器)设备正在使用的
BufferedWaveProvider
。您可以将此输入替换为网络应用程序的UDP套接字接收

请注意,该程序保证PCM输入和输出(麦克风和扬声器)使用相同的
波形。对于联网端点,您也必须这样做

不管怎样,它是有效的。这是代码。我不想谈太多细节。有很多评论试图帮助理解发生了什么:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NAudio.Wave;
using NAudio.Codecs;

namespace G711MicStream
{
    class Program
    {
        delegate byte EncoderMethod( short _raw );
        delegate short DecoderMethod( byte _encoded );

        // Change these to their ALaw equivalent if you want.
        static EncoderMethod Encoder = MuLawEncoder.LinearToMuLawSample;
        static DecoderMethod Decoder = MuLawDecoder.MuLawToLinearSample;



        static void Main(string[] args)
        {
            // Fire off our Sender thread.
            Thread sender = new Thread(new ThreadStart(Sender));
            sender.Start();

            // And receiver...
            Thread receiver = new Thread(new ThreadStart(Receiver));
            receiver.Start();

            // We're going to try for 16-bit PCM, 8KHz sampling, 1 channel.
            // This should align nicely with u-law
            CommonFormat = new WaveFormat(16000, 16, 1);

            // Prep the input.
            IWaveIn wavein = new WaveInEvent();
            wavein.WaveFormat = CommonFormat;
            wavein.DataAvailable += new EventHandler<WaveInEventArgs>(wavein_DataAvailable);
            wavein.StartRecording();

            // Prep the output.  The Provider gets the same formatting.
            WaveOut waveout = new WaveOut();
            OutProvider = new BufferedWaveProvider(CommonFormat);
            waveout.Init(OutProvider);
            waveout.Play();


            // Now we can just run until the user hits the <X> button.
            Console.WriteLine("Running g.711 audio test.  Hit <X> to quit.");
            for( ; ; )
            {
                Thread.Sleep(100);
                if( !Console.KeyAvailable ) continue;
                ConsoleKeyInfo info = Console.ReadKey(false);
                if( (info.Modifiers & ConsoleModifiers.Alt) != 0 ) continue;
                if( (info.Modifiers & ConsoleModifiers.Control) != 0 ) continue;

                // Quit looping on non-Alt, non-Ctrl X
                if( info.Key == ConsoleKey.X ) break;                
            }

            Console.WriteLine("Stopping...");

            // Shut down the mic and kick the thread semaphore (without putting
            // anything in the queue).  This will (eventually) stop the thread
            // (which also signals the receiver thread to stop).
            wavein.StopRecording();
            try{ wavein.Dispose(); } catch(Exception){}
            SenderKick.Release();

            // Wait for both threads to exit.
            sender.Join();
            receiver.Join();

            // And close down the output.
            waveout.Stop();
            try{ waveout.Dispose(); } catch(Exception) {}

            // Sleep a little.  This seems to be accepted practice when shutting
            // down these audio components.
            Thread.Sleep(500);
        }


        /// <summary>
        /// Grabs the mic data and just queues it up for the Sender.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void  wavein_DataAvailable(object sender, WaveInEventArgs e)
        {
            // Create a local copy buffer.
            byte [] buffer = new byte [e.BytesRecorded];
            System.Buffer.BlockCopy(e.Buffer, 0, buffer, 0, e.BytesRecorded);

            // Drop it into the queue.  We'll need to lock for this.
            Lock.WaitOne();
            SenderQueue.AddLast(buffer);
            Lock.ReleaseMutex();

            // and kick the thread.
            SenderKick.Release();
        }


        static
        void
        Sender()
        {
            // Holds the data from the DataAvailable event.
            byte [] qbuffer = null;

            for( ; ; )
            {
                // Wait for a 'kick'...
                SenderKick.WaitOne();

                // Lock...
                Lock.WaitOne();
                bool dataavailable = ( SenderQueue.Count != 0 );
                if( dataavailable )
                {
                    qbuffer = SenderQueue.First.Value;
                    SenderQueue.RemoveFirst();
                }
                Lock.ReleaseMutex();

                // If the queue was empty on a kick, then that's our signal to
                // exit.
                if( !dataavailable ) break;

                // Convert each 16-bit PCM sample to its 1-byte u-law equivalent.
                int numsamples = qbuffer.Length / sizeof(short);
                byte [] g711buff = new byte [numsamples];

                // I like unsafe for this kind of stuff!
                unsafe
                {
                    fixed( byte * inbytes = &qbuffer[0] )
                    fixed( byte * outbytes = &g711buff[0] )
                    {
                        // Recast input buffer to short[]
                        short * buff = (short *)inbytes;

                        // And loop over the samples.  Since both input and
                        // output are 16-bit, we can use the same index.
                        for( int index = 0; index < numsamples; ++index )
                        {
                            outbytes[index] = Encoder(buff[index]);
                        }
                    }
                }

                // This gets passed off to the reciver.  We'll queue it for now.
                Lock.WaitOne();
                ReceiverQueue.AddLast(g711buff);
                Lock.ReleaseMutex();
                ReceiverKick.Release();
            }

            // Log it.  We'll also kick the receiver (with no queue addition)
            // to force it to exit.
            Console.WriteLine("Sender: Exiting.");
            ReceiverKick.Release();
        }

        static
        void
        Receiver()
        {
            byte [] qbuffer = null;
            for( ; ; )
            {
                // Wait for a 'kick'...
                ReceiverKick.WaitOne();

                // Lock...
                Lock.WaitOne();
                bool dataavailable = ( ReceiverQueue.Count != 0 );
                if( dataavailable )
                {
                    qbuffer = ReceiverQueue.First.Value;
                    ReceiverQueue.RemoveFirst();
                }
                Lock.ReleaseMutex();

                // Exit on kick with no data.
                if( !dataavailable ) break;

                // As above, but we convert in reverse, from 1-byte u-law
                // samples to 2-byte PCM samples.
                int numsamples = qbuffer.Length;
                byte [] outbuff = new byte [qbuffer.Length * 2];
                unsafe
                {
                    fixed( byte * inbytes = &qbuffer[0] )
                    fixed( byte * outbytes = &outbuff[0] )
                    {
                        // Recast the output to short[]
                        short * outpcm = (short *)outbytes;

                        // And loop over the u-las samples.
                        for( int index = 0; index < numsamples; ++index )
                        {
                            outpcm[index] = Decoder(inbytes[index]);
                        }
                    }
                }

                // And write the output buffer to the Provider buffer for the
                // WaveOut devices.
                OutProvider.AddSamples(outbuff, 0, outbuff.Length);
            }

            Console.Write("Receiver: Exiting.");
        }


        /// <summary>Lock for the sender queue.</summary>
        static Mutex Lock = new Mutex();

        static WaveFormat CommonFormat;

        /// <summary>"Kick" semaphore for the sender queue.</summary>
        static Semaphore SenderKick = new Semaphore(0, int.MaxValue);
        /// <summary>Queue of byte buffers from the DataAvailable event.</summary>
        static LinkedList<byte []> SenderQueue = new LinkedList<byte[]>();

        static Semaphore ReceiverKick = new Semaphore(0, int.MaxValue);
        static LinkedList<byte []> ReceiverQueue = new LinkedList<byte[]>();

        /// <summary>WaveProvider for the output.</summary>
        static BufferedWaveProvider OutProvider;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用NAudio.波;
使用NAudio.codec;
命名空间G711MicStream
{
班级计划
{
委托字节编码方法(短_raw);
委托短解码方法(字节编码);
//如果需要,请将这些更改为其ALaw等效值。
静态编码器方法编码器=MuLawEncoder.LinearToMuLawSample;
静态解码方法解码器=MuLawDecoder.MuLawToLinearSample;
静态void Main(字符串[]参数)
{
//发射我们的发送线程。
线程发送器=新线程(新线程开始(发送器));
sender.Start();
//和接收器。。。
线程接收器=新线程(新线程开始(接收器));
receiver.Start();
//我们将尝试16位PCM,8KHz采样,1通道。
//这应该与u定律很好地一致
CommonFormat=新波形(16000,16,1);
//准备输入。
IWaveIn-wavein=新的WaveInEvent();
wavein.WaveFormat=通用格式;
wavein.DataAvailable+=新的事件处理程序(wavein_DataAvailable);
wavein.StartRecording();
//准备输出。提供程序获得相同的格式。
WaveOut WaveOut=新的WaveOut();
OutProvider=新的BufferedWaveProvider(CommonFormat);
waveout.Init(输出提供程序);
waveout.Play();
//现在我们可以一直运行,直到用户点击按钮。
Console.WriteLine(“运行g.711音频测试。点击退出”);
对于(;;)
{
睡眠(100);
如果(!Console.KeyAvailable)继续;
ConsoleKeyInfo=Console.ReadKey(false);
如果((info.Modifiers&ConsoleModifiers.Alt)!=0)继续;
如果((info.Modifiers&ConsoleModifiers.Control)!=0)继续;
//退出非Alt、非Ctrl X上的循环
如果(info.Key==ConsoleKey.X)中断;
}
控制台。写入线(“停止…”);
//关闭麦克风并踢线程信号灯(不放
//这将(最终)停止线程
//(这也向接收器线程发出停止信号)。
wavein.StopRecording();
请尝试{wavein.Dispose();}捕获(异常){}
SenderKick.Release();
//等待两个线程退出。
sender.Join();
receiver.Join();
//并关闭输出。
waveout.Stop();
请尝试{waveout.Dispose();}捕获(异常){}
//睡一会儿。这似乎是在睡觉时被接受的习惯
//下载这些音频组件。
睡眠(500);
}
/// 
///抓取麦克风数据并将其排队等待发件人。
/// 
/// 
/// 
静态无效wavein_数据可用(对象发送方,WaveInEventArgs e)
{
//创建本地副本缓冲区。
字节[]缓冲区=新字节[e.BytesRecorded];
System.Buffer.BlockCopy(e.Buffer,0,Buffer,0,e.BytesRecorded);
//把它放到队列中。我们需要为此锁定。
Lock.WaitOne();
SenderQueue.AddLast(缓冲区);
Lock.ReleaseMutex();
//踢线。
SenderKick.Release();
}
静止的
无效的
发送者()
{
//保存DataAvailable事件中的数据。
字节[]qbuffer=null;
对于(;;)
{
//等待一个“踢”。。。
SenderKick.WaitOne();
//锁定。。。
Lock.WaitOne();
bool dataavailable=(SenderQueue.Count!=0);
如果(数据可用)
{
qbuffer=SenderQueue.First.Value;
SenderQueue.Rem