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-06 17:15:34

C#开发和使用

2009-08-27 17:47:21

c#皮肤

2009-09-11 11:27:38

AttributeUsC# Attribut

2024-04-15 16:11:33

C#HTTP请求.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

2009-08-31 17:47:43

C#接口使用

2009-09-04 15:45:29

C#缓存流

2009-08-18 09:37:14

C#枚举类型

2009-08-21 09:14:47

C# Excel CO

2009-08-25 15:59:28

C#串口操作

2009-08-26 17:28:48

C# DateTime

2009-08-18 17:29:02

C#使用指针

2009-08-19 14:26:58

C# JavaScri

2009-09-01 09:16:57

C#使用SharpZi

2009-08-20 13:23:28

C#使用Crystal
点赞
收藏

51CTO技术栈公众号