2011-01-27 15 views
4

私は、hereというコードを使用してマイクからオーディオを送信することができました。NAudioを使用してオーディオを再生する

しかし、NAudioを使用してこれを行うことができませんでした。ネットワーク経由で送信するバイトを変換し戻すために

G711.Encode_aLaw 
G711.Decode_uLaw 

CodeProjectのからのコードは、次のようなをエンコードし、デコードするための明示的なコードを有しています。

上記のCodeProjectアプリケーション用のNAudioのサンプルコードを入手することはできますか?

答えて

0

ここでは、NAudio、マイク入力、スピーカー出力、u-LawまたはA-Lawエンコーディングを使用して書いたクイックC#コンソールアプリケーションを紹介します。 NAudio.Codecs名前空間には、A-Lawとu-Lawのエンコーダとデコーダが含まれています。

このプログラムはではありません。はネットワーク経由でデータを送信します(難しいことではありません)。私はあなたにそれを残すでしょう。代わりに、 "送信者"スレッドと "受信者"スレッドが含まれています。

マイクDataAvailableイベントハンドラは、バイトバッファをキューにドロップするだけです(バッファのコピーが作成されます。イベントから実際のバッファを保持したくない)。 「Sender」スレッドは、キューに入れられたバッファを取得し、PCMデータをg.711に変換し、それを第2のキューにドロップする。この「第2のキューに落ちる」部分は、特定のアプリケーション用のリモートUDP宛先に送信する部分です。

"Receiver"スレッドは、2番目のキューからデータを読み取り、PCMに変換して、WaveOut(スピーカー)デバイスで使用されているBufferedWaveProviderに送ります。この入力は、ネットワーク接続されたアプリケーションのUDPソケット受信で置き換えます。

このプログラムは、PCMの入出力(マイクとスピーカー)が同じWaveFormatを使用していることを保証します。それは、ネットワーク接続されたエンドポイントにとっても必要なことです。

とにかく、動作します。コードはここにあります。私はあまり詳細には行きません。何が起こっているのかを理解するのに役立つ多くのコメントがあります:

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; 
    } 
} 
関連する問題