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.

  1. struct RIFF_HEADER
  2. {
  3. TCHAR szRiffID[4]; // 'R','I','F','F'
  4. DWORD dwRiffSize;
  5. TCHAR szRiffFormat[4]; // 'W','A','V','E'
  6. };

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:

  1. struct WAVE_FORMAT
  2. {
  3. WORD wFormatTag;
  4. WORD wChannels;
  5. DWORD dwSamplesPerSec;
  6. DWORD dwAvgBytesPerSec;
  7. WORD wBlockAlign;
  8. WORD wBitsPerSample;
  9. };
  10. struct FMT_BLOCK
  11. {
  12. TCHAR szFmtID[4]; // 'f','m','t',' ' please note the
  13. // space character at the fourth location.
  14. DWORD dwFmtSize;
  15. WAVE_FORMAT wavFormat;
  16. };

Lastly, you describe the data block that contains the actual waveform data:

  1. struct DATA_BLOCK
  2. {
  3. TCHAR szDataID[4]; // 'd','a','t','a'
  4. DWORD dwDataSize;
  5. };

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:

  1. struct NOTE_CHUNK
  2. {
  3. TCHAR ID[4]; // 'note'
  4. long chunkSize;
  5. long dwIdentifier;
  6. TCHAR dwText[];
  7. };

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:

  1. Takes a wave file and reverses it (stores the reversed wave in the destination file).
  2. 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.
  3. Shows how you can do duplex operation (read and write) at the same time using just one wave handling class.
  4. And so on. The sample should get you started in building lots of fun wave applications.
  5. 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:

  1. It does not try to teach coding.
  2. 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.
  3. 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.
原创粉丝点击