最近在做一个普通话纠正练习软件,里面要有一个录音的功能。关于音频的东西之前没怎么接触,所以在网上找了一些相关资料。由于.net窗体程序和mmsystem.h的头文件各种报错,所以最后决定用DirectSound来实现。如果是MFC程序的话,可能有mmsystem.h会更方便点。。。
主要参考了http://www.cnblogs.com/stg609/archive/2008/10/24/1318931.html 这个人的博客。
总体思路是:
- 设置wave文件的格式//这里要了解一下波形捕捉的基本知识,比如说采样率什么的。
- 捕获录音设备
- 创建捕获缓冲区
- 创建wave文件
- 创建缓冲区通知
- 开始录音
- 新开一条线程捕获波形
- 把捕获到的写入wave文件
- 停止录音,关闭缓冲区和新线程,把文件长度,数据长度写入文件
参考的博客上写的是C#代码,所以转到C++.net上会有各种奇怪的格式错误等等,所以了解了不少C#和托管C++的类型转换。
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1
#pragma
once
2
3 #define iNotifyNum 16
4 typedef unsigned char byte ;
5
6 using namespace System;
7 using namespace System::IO;
8 using namespace System::Threading;
9 using namespace Microsoft::DirectX;
10 using namespace Microsoft::DirectX::DirectSound;
11
12 enum RecordMode{Recording, Stop, Pause};
13
14 public ref class SoundRecord
15 {
16 public :
17 SoundRecord(); // 构造函数
18 ~ SoundRecord(); // 析构函数
19 void recordingStart(String ^ path); // 开始录音
20 void recordingStop(); // 停止录音
21 void recordingPause(); // 暂停录音
22 void recordingResume(); // 恢复录音
23 RecordMode recordMode;
24
25 private :
26 Notify ^ myNotify; // 缓冲区提示事件
27 FileStream ^ fsWav; // 保存的文件流
28 int iBufferOffset ; // 本次数据起始点, 上一次数据的终点。
29 int iSampleSize; // 所采集到的数据大小
30 int iNotifySize; // 通知所在区域大小
31 int iBufferSize; // 缓冲区大小
32 BinaryWriter ^ mWriter;
33 Microsoft::DirectX::DirectSound::Capture ^ capture; // 捕捉设备对象
34 CaptureBuffer ^ capturebuffer; // 捕捉缓冲区
35 AutoResetEvent ^ notifyevent;
36 Thread ^ notifythread ;
37 WaveFormat mWavFormat; // PCM格式
38
39 WaveFormat SetWaveFormat();
40 void CreateWaveFile(String ^ strFileName);
41 bool CreateCaputerDevice();
42 void CreateCaptureBuffer();
43 void CreateNotification();
44 void RecoData();
45 void RecordCapturedData();
46 };
2
3 #define iNotifyNum 16
4 typedef unsigned char byte ;
5
6 using namespace System;
7 using namespace System::IO;
8 using namespace System::Threading;
9 using namespace Microsoft::DirectX;
10 using namespace Microsoft::DirectX::DirectSound;
11
12 enum RecordMode{Recording, Stop, Pause};
13
14 public ref class SoundRecord
15 {
16 public :
17 SoundRecord(); // 构造函数
18 ~ SoundRecord(); // 析构函数
19 void recordingStart(String ^ path); // 开始录音
20 void recordingStop(); // 停止录音
21 void recordingPause(); // 暂停录音
22 void recordingResume(); // 恢复录音
23 RecordMode recordMode;
24
25 private :
26 Notify ^ myNotify; // 缓冲区提示事件
27 FileStream ^ fsWav; // 保存的文件流
28 int iBufferOffset ; // 本次数据起始点, 上一次数据的终点。
29 int iSampleSize; // 所采集到的数据大小
30 int iNotifySize; // 通知所在区域大小
31 int iBufferSize; // 缓冲区大小
32 BinaryWriter ^ mWriter;
33 Microsoft::DirectX::DirectSound::Capture ^ capture; // 捕捉设备对象
34 CaptureBuffer ^ capturebuffer; // 捕捉缓冲区
35 AutoResetEvent ^ notifyevent;
36 Thread ^ notifythread ;
37 WaveFormat mWavFormat; // PCM格式
38
39 WaveFormat SetWaveFormat();
40 void CreateWaveFile(String ^ strFileName);
41 bool CreateCaputerDevice();
42 void CreateCaptureBuffer();
43 void CreateNotification();
44 void RecoData();
45 void RecordCapturedData();
46 };