成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#中DirectSound錄音的使用

開發 后端
本文介紹了C#中DirectSound錄音的原理、類型和代碼等。

一.聲卡錄音的基本原理

為了實現一個錄音的基本過程,至少需要以下對象的支持:

1.   錄音設備,對我們的PC設備就是聲卡。這個錄音設備可以進行的操作應該有開始和關閉。

2.   緩沖區,也就是錄制的聲音放在哪里的問題。

二.DirectSound錄音的描述模型 (我裝的是directx_dec2005_redist.exe)

DirectSound錄音的支持類

ØCapture,設備對象,可以看作是聲卡的描述。

ØCaptureBuffer,緩沖區對象,存放錄入的音頻數據。

ØNotify,事件通知對象,由于錄音是一個長時間的過程,因此使用一個緩沖隊列(多個緩沖區)接收數據,每當一個緩沖區滿的時候,系統使用這個對象通知應用程序取走這個緩沖區,并繼續錄音。

以上三個對象是進行錄音操作的主要對象,由于在C++中對DirectSound的操作DirectX幫助文檔中已經有很詳細的說明,這里就不再贅述了。本文是針對Managed Code。除了以上三個主要的DirectSound類,還需要以下幾個輔助類。

ØWaveFormat,描述了進行錄制的聲音波形的格式,例如采樣率,單聲道還是立體聲,每個采樣點的長度等等。

ØThread,線程類,由于錄音的過程是需要不斷處理緩沖區滿的事件,因此新建一個線程對此進行單獨處理。

ØAutoResetEvent,通知的事件,當緩沖區滿的時候,使用該事件作為通知事件。

三.DirectSound錄音代碼解析(SoundRecord類)

  1.  using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Text;  
  4.    
  5.  using System.Windows.Forms;  
  6.  using System.IO;  
  7.  using System.Threading;  
  8.  using Microsoft.DirectX;  
  9.  using Microsoft.DirectX.DirectSound;  
  10.    
  11.  namespace DirectSoundTest  
  12.  {  
  13.      class SoundRecord  
  14.      {  
  15.          public const int cNotifyNum = 16;       // 緩沖隊列的數目  
  16.          private int mNextCaptureOffset = 0;      // 該次錄音緩沖區的起始點  
  17.          private int mSampleCount = 0;            // 錄制的樣本數目  
  18.          private int mNotifySize = 0;             // 每次通知大小  
  19.          private int mBufferSize = 0;             // 緩沖隊列大小  
  20.          private string mFileName = string.Empty;     // 文件名  
  21.          private FileStream mWaveFile = null;         // 文件流  
  22.          private BinaryWriter mWriter = null;         // 寫文件  
  23.          private Capture mCapDev = null;              // 音頻捕捉設備  
  24.          private CaptureBuffer mRecBuffer = null;     // 緩沖區對象  
  25.          private Notify mNotify = null;               // 消息通知對象  
  26.          private WaveFormat mWavFormat;                       // 錄音的格式  
  27.          private Thread mNotifyThread = null;                 // 處理緩沖區消息的線程  
  28.          private AutoResetEvent mNotificationEvent = null;    // 通知事件  
  29.    
  30.    
  31.    
  32.          /**//// < summary>  
  33.          /// 構造函數,設定錄音設備,設定錄音格式.  
  34.          /// < /summary>  
  35.          public SoundRecord()  
  36.          {  
  37.             // 初始化音頻捕捉設備  
  38.              InitCaptureDevice();  
  39.              // 設定錄音格式  
  40.             mWavFormat = CreateWaveFormat();  
  41.         }  
  42.    
  43.          /**//// < summary>  
  44.          /// 設定錄音結束后保存的文件,包括路徑  
  45.          /// < /summary>  
  46.          /// < param name="filename">保存wav文件的路徑名< /param>  
  47.          public void SetFileName(string filename)  
  48.          {  
  49.              mFileName = filename;  
  50.          }  
  51.    
  52.          /**//// < summary>  
  53.          /// 開始錄音  
  54.          /// < /summary>  
  55.          public void RecStart()  
  56.          {  
  57.              // 創建錄音文件  
  58.              CreateSoundFile();  
  59.    
  60.              // 創建一個錄音緩沖區,并開始錄音  
  61.              CreateCaptureBuffer();  
  62.    
  63.              // 建立通知消息,當緩沖區滿的時候處理方法  
  64.              InitNotifications();  
  65.              mRecBuffer.Start(true);  
  66.         }  
  67.    
  68.          /**//// < summary>  
  69.          /// 停止錄音  
  70.          /// < /summary>  
  71.         public void RecStop()  
  72.          {  
  73.              // 關閉通知消息  
  74.              if (null != mNotificationEvent)  
  75.                  mNotificationEvent.Set();  
  76.    
  77.              // 停止錄音  
  78.              mRecBuffer.Stop();  
  79.              // 寫入緩沖區最后的數據  
  80.              RecordCapturedData();  
  81.    
  82.              // 回寫長度信息  
  83.              mWriter.Seek(4, SeekOrigin.Begin);  
  84.              mWriter.Write((int)(mSampleCount + 36));   // 寫文件長度  
  85.              mWriter.Seek(40, SeekOrigin.Begin);  
  86.              mWriter.Write(mSampleCount);                // 寫數據長度  
  87.              mWriter.Close();  
  88.              mWaveFile.Close();  
  89.              mWriter = null;  
  90.              mWaveFile = null;  
  91.          }  
  92.    
  93.          //4.內部調用函數  
  94.    
  95.          /**//// < summary>  
  96.          /// 初始化錄音設備,此處使用主錄音設備.  
  97.          /// < /summary>  
  98.          /// < returns>調用成功返回true,否則返回false< /returns>  
  99.         private bool InitCaptureDevice()  
  100.         {  
  101.             // 獲取默認音頻捕捉設備  
  102.             CaptureDevicesCollection devices = new CaptureDevicesCollection();  // 枚舉音頻捕捉設備  
  103.  
  104.             Guid deviceGuid = Guid.Empty;                                       // 音頻捕捉設備的ID  
  105.             if (devices.Count > 0)  
  106.                 deviceGuid = devices[0].DriverGuid;  
  107.             else 
  108.             {  
  109.                 MessageBox.Show("系統中沒有音頻捕捉設備");  
  110.                 return false;  
  111.             }  
  112.  
  113.             // 用指定的捕捉設備創建Capture對象  
  114.             try 
  115.             {  
  116.                 mCapDev = new Capture(deviceGuid);  
  117.             }  
  118.             catch (DirectXException e)  
  119.             {  
  120.                 MessageBox.Show(e.ToString());  
  121.                 return false;  
  122.             }  
  123.             return true;  
  124.         }  
  125.  
  126.         /**//// < summary>  
  127.         /// 創建錄音格式,此處使用16bit,16KHz,Mono的錄音格式  
  128.         /// < /summary>  
  129.         /// < returns>WaveFormat結構體< /returns>  
  130.         private WaveFormat CreateWaveFormat()  
  131.         {  
  132.             WaveFormat format = new WaveFormat();  
  133.             format.FormatTag = WaveFormatTag.Pcm;   // PCM  
  134.             format.SamplesPerSecond = 16000;        // 16KHz  
  135.             format.BitsPerSample = 16;              // 16Bit  
  136.             format.Channels = 1;                    // Mono  
  137.             format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));  
  138.             format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;  
  139.             return format;  
  140.         }  
  141.         /**//// < summary>  
  142.         /// 創建錄音使用的緩沖區  
  143.         /// < /summary>  
  144.         private void CreateCaptureBuffer()  
  145.         {  
  146.             // 緩沖區的描述對象  
  147.             CaptureBufferDescription bufferdescription = new CaptureBufferDescription();  
  148.  
  149.             if (null != mNotify)  
  150.             {  
  151.                 mNotify.Dispose();  
  152.                 mNotify = null;  
  153.             }  
  154.             if (null != mRecBuffer)  
  155.             {  
  156.                 mRecBuffer.Dispose();  
  157.                 mRecBuffer = null;  
  158.             }  
  159.  
  160.             // 設定通知的大小,默認為1s鐘  
  161.  
  162.             mNotifySize = (1024 > mWavFormat.AverageBytesPerSecond / 8) ? 1024 : (mWavFormat.AverageBytesPerSecond / 8);  
  163.             mNotifySize -= mNotifySize % mWavFormat.BlockAlign;  
  164.  
  165.  
  166.             // 設定緩沖區大小  
  167.             mBufferSize = mNotifySize * cNotifyNum;  
  168.             // 創建緩沖區描述              
  169.             bufferdescription.BufferBytes = mBufferSize;  
  170.             bufferdescription.Format = mWavFormat;           // 錄音格式  
  171.  
  172.             // 創建緩沖區           
  173.             mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev);  
  174.             mNextCaptureOffset = 0;  
  175.         }  
  176.  
  177.         /**//// < summary>  
  178.         /// 初始化通知事件,將原緩沖區分成16個緩沖隊列,在每個緩沖隊列的結束點設定通知點.  
  179.         /// < /summary>  
  180.         /// < returns>是否成功< /returns>  
  181.         private bool InitNotifications()  
  182.         {  
  183.             if (null == mRecBuffer)  
  184.             {  
  185.                 MessageBox.Show("未創建錄音緩沖區");  
  186.                 return false;  
  187.  
  188.             }  
  189.             // 創建一個通知事件,當緩沖隊列滿了就激發該事件.  
  190.             mNotificationEvent = new AutoResetEvent(false);  
  191.             // 創建一個線程管理緩沖區事件  
  192.             if (null == mNotifyThread)  
  193.             {  
  194.                 mNotifyThread = new Thread(new ThreadStart(WaitThread));  
  195.                 mNotifyThread.Start();  
  196.             }  
  197.  
  198.             // 設定通知的位置  
  199.             BufferPositionNotify[] PositionNotify = new BufferPositionNotify[cNotifyNum + 1];  
  200.             for (int i = 0; i <  cNotifyNum; i++)  
  201.             {  
  202.                 PositionNotify[i].Offset = (mNotifySize * i) + mNotifySize - 1;  
  203.                 PositionNotify[i].EventNotifyHandle = mNotificationEvent.Handle;  
  204.             }  
  205.  
  206.             mNotify = new Notify(mRecBuffer);  
  207.             mNotify.SetNotificationPositions(PositionNotify, cNotifyNum);  
  208.             return true;  
  209.         }  
  210.  
  211.         /**//// < summary>  
  212.         /// 將錄制的數據寫入wav文件  
  213.         /// < /summary>  
  214.         private void RecordCapturedData()  
  215.         {  
  216.             byte[] CaptureData = null;  
  217.             int ReadPos;  
  218.             int CapturePos;  
  219.             int LockSize;  
  220.             mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos);  
  221.             LockSize = ReadPos - mNextCaptureOffset;  
  222.             if (LockSize <  0)  
  223.                 LockSize += mBufferSize;  
  224.  
  225.             // 對齊緩沖區邊界,實際上由于開始設定完整,這個操作是多余的.  
  226.             LockSize -= (LockSize % mNotifySize);  
  227.             if (0 == LockSize)  
  228.                 return;  
  229.             // 讀取緩沖區內的數據  
  230.             CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);  
  231.  
  232.             // 寫入Wav文件  
  233.             mWriter.Write(CaptureData, 0, CaptureData.Length);  
  234.  
  235.             // 更新已經錄制的數據長度.  
  236.             mSampleCount += CaptureData.Length;  
  237.             // 移動錄制數據的起始點,通知消息只負責指示產生消息的位置,并不記錄上次錄制的位置  
  238.             mNextCaptureOffset += CaptureData.Length;  
  239.             mNextCaptureOffset %= mBufferSize; // Circular buffer  
  240.         }  
  241.  
  242.  
  243.         /**//// < summary>  
  244.         /// 接收緩沖區滿消息的處理線程  
  245.         /// < /summary>  
  246.         private void WaitThread()  
  247.         {  
  248.             while (true)  
  249.             {  
  250.                 // 等待緩沖區的通知消息  
  251.                 mNotificationEvent.WaitOne(Timeout.Infinite, true);  
  252.                 // 錄制數據  
  253.                 RecordCapturedData();  
  254.             }  
  255.         }  
  256.  
  257.         /**//// < summary>  
  258.         /// 創建保存的波形文件,并寫入必要的文件頭.  
  259.         /// < /summary>  
  260.         private void CreateSoundFile()  
  261.         {  
  262.             /**//**************************************************************************  
  263.          Here is where the file will be created. A  
  264.          wave file is a RIFF file, which has chunks  
  265.         of data that describe what the file contains.  
  266.          A wave RIFF file is put together like this:  
  267.          The 12 byte RIFF chunk is constructed like this:  
  268.          Bytes 0 - 3 :  'R' 'I' 'F' 'F'  
  269.          Bytes 4 - 7 :  Length of file, minus the first 8 bytes of the RIFF description.  
  270.                            (4 bytes for "WAVE" + 24 bytes for format chunk length +  
  271.                            8 bytes for data chunk description + actual sample data size.)  
  272.           Bytes 8 - 11: 'W' 'A' 'V' 'E'  
  273.           The 24 byte FORMAT chunk is constructed like this:  
  274.           Bytes 0 - 3 : 'f' 'm' 't' ' '  
  275.           Bytes 4 - 7 : The format chunk length. This is always 16.  
  276.           Bytes 8 - 9 : File padding. Always 1.  
  277.           Bytes 10- 11: Number of channels. Either 1 for mono,  or 2 for stereo.  
  278.           Bytes 12- 15: Sample rate.  
  279.           Bytes 16- 19: Number of bytes per second.  
  280.           Bytes 20- 21: Bytes per sample. 1 for 8 bit mono, 2 for 8 bit stereo or  
  281.           16 bit mono, 4 for 16 bit stereo.  
  282.           Bytes 22- 23: Number of bits per sample.  
  283.           The DATA chunk is constructed like this:  
  284.           Bytes 0 - 3 : 'd' 'a' 't' 'a'  
  285.           Bytes 4 - 7 : Length of data, in bytes.            
  286.           Bytes 8 -: Actual sample data.  
  287.           ***************************************************************************/ 
  288.             // Open up the wave file for writing.  
  289.             mWaveFile = new FileStream(mFileName, FileMode.Create);  
  290.             mWriter = new BinaryWriter(mWaveFile);  
  291.             // Set up file with RIFF chunk info.              
  292.             char[] ChunkRiff = { 'R''I''F''F' };  
  293.             char[] ChunkType = { 'W''A''V''E' };  
  294.             char[] ChunkFmt = { 'f''m''t'' ' };  
  295.             char[] ChunkData = { 'd''a''t''a' };  
  296.             short shPad = 1;                // File padding  
  297.             int nFormatChunkLength = 0x10;  // Format chunk length.  
  298.             int nLength = 0;                // File length, minus first 8 bytes of RIFF description. This will be filled in later.  
  299.             short shBytesPerSample = 0;     // Bytes per sample.  
  300.  
  301.             // 一個樣本點的字節數目  
  302.             if (8 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels)  
  303.                 shBytesPerSample = 1;  
  304.             else if ((8 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels) || (16 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels))  
  305.                 shBytesPerSample = 2;  
  306.             else if (16 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels)  
  307.                 shBytesPerSample = 4;  
  308.  
  309.             // RIFF 塊  
  310.             mWriter.Write(ChunkRiff);  
  311.             mWriter.Write(nLength);  
  312.             mWriter.Write(ChunkType);  
  313.             // WAVE塊  
  314.             mWriter.Write(ChunkFmt);  
  315.             mWriter.Write(nFormatChunkLength);  
  316.             mWriter.Write(shPad);  
  317.             mWriter.Write(mWavFormat.Channels);  
  318.             mWriter.Write(mWavFormat.SamplesPerSecond);  
  319.             mWriter.Write(mWavFormat.AverageBytesPerSecond);  
  320.             mWriter.Write(shBytesPerSample);  
  321.             mWriter.Write(mWavFormat.BitsPerSample);  
  322.  
  323.             // 數據塊  
  324.             mWriter.Write(ChunkData);  
  325.             mWriter.Write((int)0);   // The sample length will be written in later.  
  326.         }  
  327.     }  

四、DirectSound錄音外部窗體調用方式

聲明部分:

  1. private SoundRecord recorder = null;    // 錄音 

窗體構造函數:

  1. recorder = new SoundRecord(); 

啟動錄音按鈕:

  1. private void btnStart_Click(object sender, System.EventArgs e)  
  2. {  
  3.     //  
  4.     // 錄音設置  
  5.     //  
  6.     string wavfile = null;  
  7.     wavfile = “test.wav”;  
  8.     recorder.SetFileName(wavfile);  
  9.     recorder.RecStart();  

中止錄音按鈕:

  1. private void btnStop_Click(object sender, System.EventArgs e)  
  2. {  
  3.     recorder.RecStop();  
  4.     recorder = null;  

五、需要添加的外部引用文件

在系統的System32目錄下添加以下兩個引用文件,如果沒有,在DirectX的開發包內可以找到。

Microsoft.DirectX.dll

Microsoft.DirectX.DirectSound.dll

【編輯推薦】

  1. C# winForm自定義鼠標樣式的兩種方法
  2. C#自定義消息框的設置圖解
  3. 掌握C#自定義泛型類:從初始化說起
  4. C#存儲過程的循序漸進
  5. 存儲過程的優勢及其調用方法介紹
責任編輯:book05 來源: cnblogs
相關推薦

2021-01-20 05:53:25

C# ValueTupleTuple

2020-05-22 07:00:00

C#用戶注釋編程語言

2024-04-16 12:13:07

usingC#開發

2009-08-27 17:47:21

c#皮膚

2009-09-11 11:27:38

AttributeUsC# Attribut

2009-08-06 17:15:34

C#開發和使用

2024-04-15 16:11:33

C#HTTP請求.NET

2009-08-03 16:39:56

C# Assembly

2009-01-19 10:26:02

C#Namespace.NET

2009-09-11 11:33:58

C# WinForm控Attribute

2024-12-02 00:53:45

IDisposabl工具接口

2024-06-24 08:33:06

2009-08-31 17:47:43

C#接口使用

2009-09-04 15:45:29

C#緩存流

2009-08-25 17:15:50

C#隱藏C#重寫C#重載

2009-08-18 09:37:14

C#枚舉類型

2009-08-25 15:59:28

C#串口操作

2009-08-26 17:28:48

C# DateTime

2009-08-21 09:14:47

C# Excel CO

2009-08-25 16:49:44

C#使用if語句
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 99久久精品免费视频 | 久久99精品久久久久久青青日本 | 一区二区三区视频在线 | 精品国产1区2区3区 一区二区手机在线 | 国产在线高清 | 成人在线免费观看 | 欧洲一级毛片 | 在线小视频 | 国产激情视频 | 国产亚洲www | 69av网| 日韩中文在线观看 | 999久久久 | 黄网站在线观看 | 久久国产一区二区三区 | 亚洲一区久久 | 国产乱码精品一区二区三区中文 | 射欧美| 成人区精品一区二区婷婷 | 999久久久免费精品国产 | 视频国产一区 | 午夜精品久久久久久久久久久久 | 日韩视频在线免费观看 | 日韩福利在线观看 | 日韩中文字幕在线观看 | 国产成人免费一区二区60岁 | 中文字幕av网址 | 中文字幕高清av | 精品免费av| 天天操综合网站 | 国产精品久久久一区二区三区 | 黑人中文字幕一区二区三区 | 高清欧美性猛交xxxx黑人猛交 | 亚洲欧美国产毛片在线 | 九九精品在线 | 欧洲视频一区二区 | 欧美成人综合 | 91久久久久久久久久久 | 欧美在线国产精品 | 精品久久久久国产 | 99热国产精品|