自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#中DirectSound錄音的使用

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

一.聲卡錄音的基本原理

為了實(shí)現(xiàn)一個(gè)錄音的基本過(guò)程,至少需要以下對(duì)象的支持:

1.   錄音設(shè)備,對(duì)我們的PC設(shè)備就是聲卡。這個(gè)錄音設(shè)備可以進(jìn)行的操作應(yīng)該有開(kāi)始和關(guān)閉。

2.   緩沖區(qū),也就是錄制的聲音放在哪里的問(wèn)題。

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

DirectSound錄音的支持類

ØCapture,設(shè)備對(duì)象,可以看作是聲卡的描述。

ØCaptureBuffer,緩沖區(qū)對(duì)象,存放錄入的音頻數(shù)據(jù)。

ØNotify,事件通知對(duì)象,由于錄音是一個(gè)長(zhǎng)時(shí)間的過(guò)程,因此使用一個(gè)緩沖隊(duì)列(多個(gè)緩沖區(qū))接收數(shù)據(jù),每當(dāng)一個(gè)緩沖區(qū)滿的時(shí)候,系統(tǒng)使用這個(gè)對(duì)象通知應(yīng)用程序取走這個(gè)緩沖區(qū),并繼續(xù)錄音。

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

ØWaveFormat,描述了進(jìn)行錄制的聲音波形的格式,例如采樣率,單聲道還是立體聲,每個(gè)采樣點(diǎn)的長(zhǎng)度等等。

ØThread,線程類,由于錄音的過(guò)程是需要不斷處理緩沖區(qū)滿的事件,因此新建一個(gè)線程對(duì)此進(jìn)行單獨(dú)處理。

ØAutoResetEvent,通知的事件,當(dāng)緩沖區(qū)滿的時(shí)候,使用該事件作為通知事件。

三.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;       // 緩沖隊(duì)列的數(shù)目  
  16.          private int mNextCaptureOffset = 0;      // 該次錄音緩沖區(qū)的起始點(diǎn)  
  17.          private int mSampleCount = 0;            // 錄制的樣本數(shù)目  
  18.          private int mNotifySize = 0;             // 每次通知大小  
  19.          private int mBufferSize = 0;             // 緩沖隊(duì)列大小  
  20.          private string mFileName = string.Empty;     // 文件名  
  21.          private FileStream mWaveFile = null;         // 文件流  
  22.          private BinaryWriter mWriter = null;         // 寫(xiě)文件  
  23.          private Capture mCapDev = null;              // 音頻捕捉設(shè)備  
  24.          private CaptureBuffer mRecBuffer = null;     // 緩沖區(qū)對(duì)象  
  25.          private Notify mNotify = null;               // 消息通知對(duì)象  
  26.          private WaveFormat mWavFormat;                       // 錄音的格式  
  27.          private Thread mNotifyThread = null;                 // 處理緩沖區(qū)消息的線程  
  28.          private AutoResetEvent mNotificationEvent = null;    // 通知事件  
  29.    
  30.    
  31.    
  32.          /**//// < summary>  
  33.          /// 構(gòu)造函數(shù),設(shè)定錄音設(shè)備,設(shè)定錄音格式.  
  34.          /// < /summary>  
  35.          public SoundRecord()  
  36.          {  
  37.             // 初始化音頻捕捉設(shè)備  
  38.              InitCaptureDevice();  
  39.              // 設(shè)定錄音格式  
  40.             mWavFormat = CreateWaveFormat();  
  41.         }  
  42.    
  43.          /**//// < summary>  
  44.          /// 設(shè)定錄音結(jié)束后保存的文件,包括路徑  
  45.          /// < /summary>  
  46.          /// < param name="filename">保存wav文件的路徑名< /param>  
  47.          public void SetFileName(string filename)  
  48.          {  
  49.              mFileName = filename;  
  50.          }  
  51.    
  52.          /**//// < summary>  
  53.          /// 開(kāi)始錄音  
  54.          /// < /summary>  
  55.          public void RecStart()  
  56.          {  
  57.              // 創(chuàng)建錄音文件  
  58.              CreateSoundFile();  
  59.    
  60.              // 創(chuàng)建一個(gè)錄音緩沖區(qū),并開(kāi)始錄音  
  61.              CreateCaptureBuffer();  
  62.    
  63.              // 建立通知消息,當(dāng)緩沖區(qū)滿的時(shí)候處理方法  
  64.              InitNotifications();  
  65.              mRecBuffer.Start(true);  
  66.         }  
  67.    
  68.          /**//// < summary>  
  69.          /// 停止錄音  
  70.          /// < /summary>  
  71.         public void RecStop()  
  72.          {  
  73.              // 關(guān)閉通知消息  
  74.              if (null != mNotificationEvent)  
  75.                  mNotificationEvent.Set();  
  76.    
  77.              // 停止錄音  
  78.              mRecBuffer.Stop();  
  79.              // 寫(xiě)入緩沖區(qū)最后的數(shù)據(jù)  
  80.              RecordCapturedData();  
  81.    
  82.              // 回寫(xiě)長(zhǎng)度信息  
  83.              mWriter.Seek(4, SeekOrigin.Begin);  
  84.              mWriter.Write((int)(mSampleCount + 36));   // 寫(xiě)文件長(zhǎng)度  
  85.              mWriter.Seek(40, SeekOrigin.Begin);  
  86.              mWriter.Write(mSampleCount);                // 寫(xiě)數(shù)據(jù)長(zhǎng)度  
  87.              mWriter.Close();  
  88.              mWaveFile.Close();  
  89.              mWriter = null;  
  90.              mWaveFile = null;  
  91.          }  
  92.    
  93.          //4.內(nèi)部調(diào)用函數(shù)  
  94.    
  95.          /**//// < summary>  
  96.          /// 初始化錄音設(shè)備,此處使用主錄音設(shè)備.  
  97.          /// < /summary>  
  98.          /// < returns>調(diào)用成功返回true,否則返回false< /returns>  
  99.         private bool InitCaptureDevice()  
  100.         {  
  101.             // 獲取默認(rèn)音頻捕捉設(shè)備  
  102.             CaptureDevicesCollection devices = new CaptureDevicesCollection();  // 枚舉音頻捕捉設(shè)備  
  103.  
  104.             Guid deviceGuid = Guid.Empty;                                       // 音頻捕捉設(shè)備的ID  
  105.             if (devices.Count > 0)  
  106.                 deviceGuid = devices[0].DriverGuid;  
  107.             else 
  108.             {  
  109.                 MessageBox.Show("系統(tǒng)中沒(méi)有音頻捕捉設(shè)備");  
  110.                 return false;  
  111.             }  
  112.  
  113.             // 用指定的捕捉設(shè)備創(chuàng)建Capture對(duì)象  
  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.         /// 創(chuàng)建錄音格式,此處使用16bit,16KHz,Mono的錄音格式  
  128.         /// < /summary>  
  129.         /// < returns>WaveFormat結(jié)構(gòu)體< /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.         /// 創(chuàng)建錄音使用的緩沖區(qū)  
  143.         /// < /summary>  
  144.         private void CreateCaptureBuffer()  
  145.         {  
  146.             // 緩沖區(qū)的描述對(duì)象  
  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.             // 設(shè)定通知的大小,默認(rèn)為1s鐘  
  161.  
  162.             mNotifySize = (1024 > mWavFormat.AverageBytesPerSecond / 8) ? 1024 : (mWavFormat.AverageBytesPerSecond / 8);  
  163.             mNotifySize -= mNotifySize % mWavFormat.BlockAlign;  
  164.  
  165.  
  166.             // 設(shè)定緩沖區(qū)大小  
  167.             mBufferSize = mNotifySize * cNotifyNum;  
  168.             // 創(chuàng)建緩沖區(qū)描述              
  169.             bufferdescription.BufferBytes = mBufferSize;  
  170.             bufferdescription.Format = mWavFormat;           // 錄音格式  
  171.  
  172.             // 創(chuàng)建緩沖區(qū)           
  173.             mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev);  
  174.             mNextCaptureOffset = 0;  
  175.         }  
  176.  
  177.         /**//// < summary>  
  178.         /// 初始化通知事件,將原緩沖區(qū)分成16個(gè)緩沖隊(duì)列,在每個(gè)緩沖隊(duì)列的結(jié)束點(diǎn)設(shè)定通知點(diǎn).  
  179.         /// < /summary>  
  180.         /// < returns>是否成功< /returns>  
  181.         private bool InitNotifications()  
  182.         {  
  183.             if (null == mRecBuffer)  
  184.             {  
  185.                 MessageBox.Show("未創(chuàng)建錄音緩沖區(qū)");  
  186.                 return false;  
  187.  
  188.             }  
  189.             // 創(chuàng)建一個(gè)通知事件,當(dāng)緩沖隊(duì)列滿了就激發(fā)該事件.  
  190.             mNotificationEvent = new AutoResetEvent(false);  
  191.             // 創(chuàng)建一個(gè)線程管理緩沖區(qū)事件  
  192.             if (null == mNotifyThread)  
  193.             {  
  194.                 mNotifyThread = new Thread(new ThreadStart(WaitThread));  
  195.                 mNotifyThread.Start();  
  196.             }  
  197.  
  198.             // 設(shè)定通知的位置  
  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.         /// 將錄制的數(shù)據(jù)寫(xiě)入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.             // 對(duì)齊緩沖區(qū)邊界,實(shí)際上由于開(kāi)始設(shè)定完整,這個(gè)操作是多余的.  
  226.             LockSize -= (LockSize % mNotifySize);  
  227.             if (0 == LockSize)  
  228.                 return;  
  229.             // 讀取緩沖區(qū)內(nèi)的數(shù)據(jù)  
  230.             CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);  
  231.  
  232.             // 寫(xiě)入Wav文件  
  233.             mWriter.Write(CaptureData, 0, CaptureData.Length);  
  234.  
  235.             // 更新已經(jīng)錄制的數(shù)據(jù)長(zhǎng)度.  
  236.             mSampleCount += CaptureData.Length;  
  237.             // 移動(dòng)錄制數(shù)據(jù)的起始點(diǎn),通知消息只負(fù)責(zé)指示產(chǎn)生消息的位置,并不記錄上次錄制的位置  
  238.             mNextCaptureOffset += CaptureData.Length;  
  239.             mNextCaptureOffset %= mBufferSize; // Circular buffer  
  240.         }  
  241.  
  242.  
  243.         /**//// < summary>  
  244.         /// 接收緩沖區(qū)滿消息的處理線程  
  245.         /// < /summary>  
  246.         private void WaitThread()  
  247.         {  
  248.             while (true)  
  249.             {  
  250.                 // 等待緩沖區(qū)的通知消息  
  251.                 mNotificationEvent.WaitOne(Timeout.Infinite, true);  
  252.                 // 錄制數(shù)據(jù)  
  253.                 RecordCapturedData();  
  254.             }  
  255.         }  
  256.  
  257.         /**//// < summary>  
  258.         /// 創(chuàng)建保存的波形文件,并寫(xiě)入必要的文件頭.  
  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.             // 一個(gè)樣本點(diǎn)的字節(jié)數(shù)目  
  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.             // 數(shù)據(jù)塊  
  324.             mWriter.Write(ChunkData);  
  325.             mWriter.Write((int)0);   // The sample length will be written in later.  
  326.         }  
  327.     }  

四、DirectSound錄音外部窗體調(diào)用方式

聲明部分:

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

窗體構(gòu)造函數(shù):

  1. recorder = new SoundRecord(); 

啟動(dòng)錄音按鈕:

  1. private void btnStart_Click(object sender, System.EventArgs e)  
  2. {  
  3.     //  
  4.     // 錄音設(shè)置  
  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;  

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

在系統(tǒng)的System32目錄下添加以下兩個(gè)引用文件,如果沒(méi)有,在DirectX的開(kāi)發(fā)包內(nèi)可以找到。

Microsoft.DirectX.dll

Microsoft.DirectX.DirectSound.dll

【編輯推薦】

  1. C# winForm自定義鼠標(biāo)樣式的兩種方法
  2. C#自定義消息框的設(shè)置圖解
  3. 掌握C#自定義泛型類:從初始化說(shuō)起
  4. C#存儲(chǔ)過(guò)程的循序漸進(jìn)
  5. 存儲(chǔ)過(guò)程的優(yōu)勢(shì)及其調(diào)用方法介紹
責(zé)任編輯:book05 來(lái)源: cnblogs
相關(guān)推薦

2021-01-20 05:53:25

C# ValueTupleTuple

2020-05-22 07:00:00

C#用戶注釋編程語(yǔ)言

2024-04-16 12:13:07

usingC#開(kāi)發(fā)

2009-08-06 17:15:34

C#開(kāi)發(fā)和使用

2009-08-27 17:47:21

c#皮膚

2009-09-11 11:27:38

AttributeUsC# Attribut

2024-04-15 16:11:33

C#HTTP請(qǐng)求.NET

2009-01-19 10:26:02

C#Namespace.NET

2009-08-03 16:39:56

C# Assembly

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-26 17:28:48

C# DateTime

2009-08-25 15:59:28

C#串口操作

2009-08-21 09:14:47

C# Excel CO

2009-08-18 09:37:14

C#枚舉類型

2009-08-19 14:26:58

C# JavaScri

2009-08-18 17:29:02

C#使用指針
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)