C# DirectSound录音类

一.声卡录音的基本原理 

为了实现一个录音的基本过程,至少需要以下对象的支持: 

1.   录音设备,对我们的PC设备就是声卡。这个录音设备可以进行的操作应该有开始和关闭。 

2.   缓冲区,也就是录制的声音放在哪里的问题。


二.DirectSound对录音的描述模型 

1.   DirectSound对录音的支持类 

     Capture,设备对象,可以看作是声卡的描述。 

     CaptureBuffer,缓冲区对象,存放录入的音频数据。 

     Notify,事件通知对象,由于录音是一个长时间的过程,因此使用一个缓冲队列(多个缓冲区)接收数据,每当一个缓冲区满的时候,系统使用这个对象通知应用程序取走这个缓冲区,并继续录音。

以上三个对象是进行录音操作的主要对象,由于在C++中对DirectSound的操作DirectX帮助文档中已经有很详细的说明,这里就不再赘述了。本文是针对Managed Code。除了以上三个主要的DirectSound类,还需要以下几个辅助类。 

     WaveFormat,描述了进行录制的声音波形的格式,例如采样率,单声道还是立体声,每个采样点的长度等等。 

     Thread,线程类,由于录音的过程是需要不断处理缓冲区满的事件,因此新建一个线程对此进行单独处理。

     AutoResetEvent,通知的事件,当缓冲区满的时候,使用该事件作为通知事件。


  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using System.IO;
  5using System.Windows.Forms;
  6using System.Threading;
  7using Microsoft.DirectX;
  8using Microsoft.DirectX.DirectSound;
  9namespace pcd.DirectSound
 10{
 11    /// <summary>
 12    /// 录音
 13    /// </summary>

 14    public class SoundRecord
 15    {
 16        //SoundRecord的成员数据
 17        public const int cNotifyNum = 16;       // 缓冲队列的数目
 18        private int mNextCaptureOffset = 0;      // 该次录音缓冲区的起始点
 19        private int mSampleCount = 0;            // 录制的样本数目
 20        private int mNotifySize = 0;             // 每次通知大小 
 21        private int mBufferSize = 0;             // 缓冲队列大小
 22        private string mFileName = string.Empty;     // 文件名 
 23        private FileStream mWaveFile = null;         // 文件流 
 24        private BinaryWriter mWriter = null;         // 写文件
 25        private Capture mCapDev = null;              // 音频捕捉设备 
 26        private CaptureBuffer mRecBuffer = null;     // 缓冲区对象 
 27        private Notify mNotify = null;               // 消息通知对象
 28        private WaveFormat mWavFormat;                       // 录音的格式 
 29        private Thread mNotifyThread = null;                 // 处理缓冲区消息的线程 
 30        private AutoResetEvent mNotificationEvent = null;    // 通知事件 
 31
 32        构造函数 构造函数
 33        /// <summary> 
 34        /// 构造函数,设定录音设备,设定录音格式. 
 35        /// </summary> 

 36        public SoundRecord()
 37        {
 38            // 初始化音频捕捉设备 
 39            InitCaptureDevice();
 40            // 设定录音格式 
 41            mWavFormat = CreateWaveFormat();
 42        }

 43        #endregion

 44
 45        公开方法 公开方法
 46        /// <summary> 
 47        /// 设定录音结束后保存的文件,包括路径 
 48        /// </summary> 
 49        /// <param name="filename">保存wav文件的路径名</param> 

 50        public void SetFileName(string filename)
 51        {
 52            mFileName = filename;
 53        }

 54        /// <summary> 
 55        /// 开始录音 
 56        /// </summary> 

 57        public void RecStart()
 58        {
 59
 60            // 创建录音文件 
 61            CreateSoundFile();
 62            // 创建一个录音缓冲区,并开始录音 
 63            CreateCaptureBuffer();
 64            // 建立通知消息,当缓冲区满的时候处理方法 
 65            InitNotifications();
 66            mRecBuffer.Start(true);
 67        }

 68
 69        /// <summary> 
 70        /// 停止录音 
 71        /// </summary>

 72        public void RecStop()
 73        {
 74            try
 75            {
 76                // 关闭通知消息
 77                if (null != mNotificationEvent)
 78                    mNotificationEvent.Set();
 79                // 停止录音
 80                mRecBuffer.Stop();
 81                // 写入缓冲区最后的数据 
 82                RecordCapturedData();
 83                // 回写长度信息 
 84                mWriter.Seek(4, SeekOrigin.Begin);
 85                mWriter.Write((int)(mSampleCount + 36));   // 写文件长度 
 86                mWriter.Seek(40, SeekOrigin.Begin);
 87                mWriter.Write(mSampleCount);                // 写数据长度 
 88                mWriter.Close();
 89                mWaveFile.Close();
 90                mWriter = null;
 91                mWaveFile = null;
 92            }

 93            catch
 94            { }
 95        }

 96        #endregion

 97
 98        内部调用函数 内部调用函数
 99        /// <summary> 
100        /// 初始化录音设备,此处使用主录音设备. 
101        /// </summary> 
102        /// <returns>调用成功返回true,否则返回false</returns>

103        private bool InitCaptureDevice()
104        {
105            // 获取默认音频捕捉设备
106            CaptureDevicesCollection devices = new CaptureDevicesCollection();  // 枚举音频捕捉设备 
107            Guid deviceGuid = Guid.Empty;                                       // 音频捕捉设备的ID
108            if (devices.Count > 0)
109                deviceGuid = devices[0].DriverGuid;
110            else
111            {
112                MessageBox.Show("系统中没有音频捕捉设备");
113                return false;
114            }

115            // 用指定的捕捉设备创建Capture对象 
116            try
117            {
118                mCapDev = new Capture(deviceGuid);
119            }

120            catch (DirectXException e)
121            {
122                MessageBox.Show(e.ToString());
123                return false;
124            }

125            return true;
126        }

127        /// <summary> 
128
129        /// 创建录音格式,此处使用16bit,16KHz,Mono的录音格式
130        /// </summary> 
131        /// <returns>WaveFormat结构体</returns> 

132        private WaveFormat CreateWaveFormat()
133        {
134            WaveFormat format = new WaveFormat();
135            format.FormatTag = WaveFormatTag.Pcm;   // PCM 
136            format.SamplesPerSecond = 16000;        // 16KHz 
137            format.BitsPerSample = 16;              // 16Bit 
138            format.Channels = 1;                    // Mono
139            format.BlockAlign = (short)(format.Channels * (format.BitsPerSample / 8));
140            format.AverageBytesPerSecond = format.BlockAlign * format.SamplesPerSecond;
141            return format;
142        }

143        /// <summary>
144        /// 创建录音使用的缓冲区 
145        /// </summary> 

146        private void CreateCaptureBuffer()
147        {
148            // 缓冲区的描述对象 
149            CaptureBufferDescription bufferdescription = new CaptureBufferDescription();
150            if (null != mNotify)
151            {
152                mNotify.Dispose();
153                mNotify = null;
154            }

155            if (null != mRecBuffer)
156            {
157                mRecBuffer.Dispose();
158                mRecBuffer = null;
159            }

160            // 设定通知的大小,默认为1s钟 
161            mNotifySize = (1024 > mWavFormat.AverageBytesPerSecond / 8? 1024 : (mWavFormat.AverageBytesPerSecond / 8);
162            mNotifySize -= mNotifySize % mWavFormat.BlockAlign;
163            // 设定缓冲区大小 
164            mBufferSize = mNotifySize * cNotifyNum;
165            // 创建缓冲区描述  
166            bufferdescription.BufferBytes = mBufferSize;
167            bufferdescription.Format = mWavFormat;     // 录音格式
168            // 创建缓冲区 
169            mRecBuffer = new CaptureBuffer(bufferdescription, mCapDev);
170            mNextCaptureOffset = 0;
171        }

172        /// <summary>
173
174        /// 初始化通知事件,将原缓冲区分成16个缓冲队列,在每个缓冲队列的结束点设定通知点.
175        /// </summary> 
176        /// <returns>是否成功</returns> 

177        private bool InitNotifications()
178        {
179            if (null == mRecBuffer)
180            {
181                MessageBox.Show("未创建录音缓冲区");
182                return false;
183            }

184            // 创建一个通知事件,当缓冲队列满了就激发该事件. 
185            mNotificationEvent = new AutoResetEvent(false);
186            // 创建一个线程管理缓冲区事件 
187            if (null == mNotifyThread)
188            {
189                mNotifyThread = new Thread(new ThreadStart(WaitThread));
190                mNotifyThread.Start();
191            }

192            // 设定通知的位置 
193            BufferPositionNotify[] PositionNotify = new BufferPositionNotify[cNotifyNum + 1];
194            for (int i = 0; i < cNotifyNum; i++)
195            {
196                PositionNotify[i].Offset = (mNotifySize * i) + mNotifySize - 1;
197                PositionNotify[i].EventNotifyHandle = mNotificationEvent.Handle;
198
199            }

200            mNotify = new Notify(mRecBuffer);
201            mNotify.SetNotificationPositions(PositionNotify, cNotifyNum);
202            return true;
203        }

204        /// <summary> 
205        /// 将录制的数据写入wav文件 
206        /// </summary> 

207        private void RecordCapturedData()
208        {
209            byte[] CaptureData = null;
210            int ReadPos;
211            int CapturePos;
212            int LockSize;
213            mRecBuffer.GetCurrentPosition(out CapturePos, out ReadPos);
214            LockSize = ReadPos - mNextCaptureOffset;
215            if (LockSize < 0)
216                LockSize += mBufferSize;
217            // 对齐缓冲区边界,实际上由于开始设定完整,这个操作是多余的. 
218            LockSize -= (LockSize % mNotifySize);
219            if (0 == LockSize)
220                return;
221
222            // 读取缓冲区内的数据 
223            CaptureData = (byte[])mRecBuffer.Read(mNextCaptureOffset, typeof(byte), LockFlag.None, LockSize);
224            // 写入Wav文件
225            mWriter.Write(CaptureData, 0, CaptureData.Length);
226            // 更新已经录制的数据长度. 
227            mSampleCount += CaptureData.Length;
228            // 移动录制数据的起始点,通知消息只负责指示产生消息的位置,并不记录上次录制的位置 
229            mNextCaptureOffset += CaptureData.Length;
230            mNextCaptureOffset %= mBufferSize; // Circular buffer
231        }

232
233        /// <summary> 
234        /// 接收缓冲区满消息的处理线程 
235        /// </summary> 

236        private void WaitThread()
237        {
238            while (true)
239            {
240                // 等待缓冲区的通知消息 
241                mNotificationEvent.WaitOne(Timeout.Infinite, true);
242                // 录制数据 
243                RecordCapturedData();
244            }

245        }

246
247        /// <summary> 
248        /// 创建保存的波形文件,并写入必要的文件头. 
249        /// </summary> 

250        private void CreateSoundFile()
251        {
252            /**************************************************************************
253
254         Here is where the file will be created. A 
255         wave file is a RIFF file, which has chunks 
256         of data that describe what the file contains. 
257         A wave RIFF file is put together like this:
258         The 12 byte RIFF chunk is constructed like this: 
259         Bytes 0 - 3 :  'R' 'I' 'F' 'F'
260         Bytes 4 - 7 :  Length of file, minus the first 8 bytes of the RIFF description. 
261                           (4 bytes for "WAVE" + 24 bytes for format chunk length + 
262                           8 bytes for data chunk description + actual sample data size.) 
263          Bytes 8 - 11: 'W' 'A' 'V' 'E'
264          The 24 byte FORMAT chunk is constructed like this: 
265          Bytes 0 - 3 : 'f' 'm' 't' ' ' 
266          Bytes 4 - 7 : The format chunk length. This is always 16.
267          Bytes 8 - 9 : File padding. Always 1. 
268          Bytes 10- 11: Number of channels. Either 1 for mono,  or 2 for stereo. 
269          Bytes 12- 15: Sample rate. 
270          Bytes 16- 19: Number of bytes per second. 
271          Bytes 20- 21: Bytes per sample. 1 for 8 bit mono, 2 for 8 bit stereo or 
272                          16 bit mono, 4 for 16 bit stereo. 
273          Bytes 22- 23: Number of bits per sample.
274          The DATA chunk is constructed like this: 
275          Bytes 0 - 3 : 'd' 'a' 't' 'a' 
276          Bytes 4 - 7 : Length of data, in bytes. 
277          Bytes 8 -: Actual sample data. 
278                    ***************************************************************************/

279
280            // Open up the wave file for writing. 
281            mWaveFile = new FileStream(mFileName, FileMode.Create);
282            mWriter = new BinaryWriter(mWaveFile);
283            // Set up file with RIFF chunk info. 
284            char[] ChunkRiff = 'R''I''F''F' };
285            char[] ChunkType = 'W''A''V''E' };
286            char[] ChunkFmt = 'f''m''t'' ' };
287            char[] ChunkData = 'd''a''t''a' };
288            short shPad = 1;                // File padding 
289            int nFormatChunkLength = 0x10;  // Format chunk length. 
290            int nLength = 0;                // File length, minus first 8 bytes of RIFF description. This will be filled in later. 
291            short shBytesPerSample = 0;     // Bytes per sample.
292            // 一个样本点的字节数目 
293            if (8 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels)
294                shBytesPerSample = 1;
295            else if ((8 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels) || (16 == mWavFormat.BitsPerSample && 1 == mWavFormat.Channels))
296                shBytesPerSample = 2;
297            else if (16 == mWavFormat.BitsPerSample && 2 == mWavFormat.Channels)
298                shBytesPerSample = 4;
299
300            // RIFF 块 
301            mWriter.Write(ChunkRiff);
302            mWriter.Write(nLength);
303            mWriter.Write(ChunkType);
304            // WAVE块 
305            mWriter.Write(ChunkFmt);
306            mWriter.Write(nFormatChunkLength);
307            mWriter.Write(shPad);
308            mWriter.Write(mWavFormat.Channels);
309            mWriter.Write(mWavFormat.SamplesPerSecond);
310            mWriter.Write(mWavFormat.AverageBytesPerSecond);
311            mWriter.Write(shBytesPerSample);
312            mWriter.Write(mWavFormat.BitsPerSample);
313            // 数据块 
314            mWriter.Write(ChunkData);
315            mWriter.Write((int)0);   // The sample length will be written in later. 
316        }

317        #endregion

318    }
319
320}
321

外部窗体调用方式

声明部分:

private SoundRecord recorder = null;    // 录音 

窗体构造函数:

recorder = new SoundRecord(); 

启动录音按钮:

private void btnStart_Click(object sender, System.EventArgs e) 

    // 

    // 录音设置 

    // 

    string wavfile = null; 

    wavfile = “test.wav”; 

    recorder.SetFileName(wavfile); 

    recorder.RecStart(); 

中止录音按钮:

private void btnStop_Click(object sender, System.EventArgs e) 

{

    recorder.RecStop(); 

    recorder = null;

}
原文地址:https://www.cnblogs.com/top5/p/1635072.html