PCM Audio and Wave Files 2
来源:互联网 发布:python爬微博数据 编辑:程序博客网 时间:2024/06/08 01:59
When a device needs to reproduce the stored stereo audio (or any multi-channel audio), it will process the left and right channels (or however many channels there are) simultaneously. This collective piece of information is called asample frame.
[clip_image008.gif]
Note: Although the example shows just stereo audio, it is the same principle for 3-channel, 5-channel, and so forth. So, for 3-channel audio, the layout would beleft, right, center, left, right, center, and so on.
So far, you have covered the very basics of PCM audio and how it is represented in a wave file. It is time to take a look at some code and see how you can use C++ to manage wave files. Start by laying out the structures for the different chunks of a wave file.
The first chunk is the riff header chunk and can be represented as follows. You use a TCHAR that is defined as a normal ASCII char or as a wide character depending upon whether the UNICODE directive has been set on your compiler.
- struct RIFF_HEADER
- {
- TCHAR szRiffID[4]; // 'R','I','F','F'
- DWORD dwRiffSize;
- TCHAR szRiffFormat[4]; // 'W','A','V','E'
- };
I guess it is self explanatory. The second chunk is the fmt chunk. It describes the properties of the wave file, such as bits per sample, number of channels, and the like. You can use a helper structure to neatly represent the chunk as:
- struct WAVE_FORMAT
- {
- WORD wFormatTag;
- WORD wChannels;
- DWORD dwSamplesPerSec;
- DWORD dwAvgBytesPerSec;
- WORD wBlockAlign;
- WORD wBitsPerSample;
- };
- struct FMT_BLOCK
- {
- TCHAR szFmtID[4]; // 'f','m','t',' ' please note the
- // space character at the fourth location.
- DWORD dwFmtSize;
- WAVE_FORMAT wavFormat;
- };
Lastly, you describe the data block that contains the actual waveform data:
- struct DATA_BLOCK
- {
- TCHAR szDataID[4]; // 'd','a','t','a'
- DWORD dwDataSize;
- };
That's it. That's all you need to describe a wave form. Of course, there a lot of optional chunks that you can have (they should be before the data block and after the fmt block). Just as an example, here is an optional chunk that you could use:
Note Chunk, used to store "comments" about the wave data:
- struct NOTE_CHUNK
- {
- TCHAR ID[4]; // 'note'
- long chunkSize;
- long dwIdentifier;
- TCHAR dwText[];
- };
Please note that I used "long" here. You can use a DWORD or long interchangeably.
I have put together a small application that will help you understand a lot about how to use C++ to work with wave files. Here is what the source code does:
- Takes a wave file and reverses it (stores the reversed wave in the destination file).
- Shows how you can use memory mapped files to handle wave files efficiently. Wave files tend to become huge beasts; using a memory-mapped file could turn out to be a life saver.
- Shows how you can do duplex operation (read and write) at the same time using just one wave handling class.
- And so on. The sample should get you started in building lots of fun wave applications.
- Shows how you can use "progressive" writes, so you can terminate the app halfway through and you will still have a "valid" wave file. You can use this feature to immediately write a voice recording app almost effortlessly. Give it a try.
What the source code does not try to do:
- It does not try to teach coding.
- It leaves a lot of room for optimizations. The code just shows you one way of working with wave files to reverse them; it will take a LONG time to reverse a big wave file.
- A better approach would be to use an intermediate buffer to swap chunks of data in memory, but I leave that as an exercise for you.
- PCM Audio and Wave Files 2
- PCM Audio and Wave Files
- PCM Audio and Wave Files
- Audio PCM
- Audio PCM
- Wave Files
- 音频编解码·格式篇(1)Wave PCM audio format(WAV)
- 音频编解码·格式篇(1)Wave PCM audio format(WAV)
- 音频编解码·格式篇(1)Wave PCM audio format(WAV)
- 音频编解码·格式篇(1)Wave PCM audio format(WAV)
- 解决can't play not PCM-coded WAVE-files的问题
- WAVE PCM 声音文件格式
- WAVE PCM soundfile format
- WAVE PCM 声音文件格式
- WAVE PCM soundfile format
- WAVE PCM 文件剖析
- WAVE PCM 声音文件格式
- WAVE PCM soundfile format
- 测试领域的发展和学习
- Magento Paypal Web Payment Standard(paypal标准)付款如何设置
- Sqlite 主键自动创建
- yum 命令配置及使用
- 手游爆发“平台战”启幕
- PCM Audio and Wave Files 2
- 划分算法(快速排序的根基)
- 任务处理——最优化问题
- 正式学习第一天,XML的相关内容
- php/遇到的问题
- Magento订单状态详解(一):Magento订单基本流程 http://www.php-source.com/thread-20100-1-1.html
- 如何快速判断一个数是不是 2 的乘方?
- java装饰者模式
- Magento SEO指南之代码优化 http://www.php-source.com/thread-5606-1-1.html