📄 0,1410,21300,00.html
字号:
<td align="left" valign="middle">
<font color="Black">The actual PCM data</font>
</td>
</tr>
</table>
</center>
</td>
</tr>
</table>
<p>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr align="left" valign="top">
<td align="left">
<a name="TheWaveFormatStructure">The WaveFormat Structure</a>
</td>
<td align="right">
<a href="#TableOfContents">TOC</a>
</td>
</tr>
<tr align="left" valign="top">
<td bgcolor="White" align="left" colspan="2">
<br>
The WaveFormat Structure, is a <b>struct</b> that contains the format of the PCM data that is stored by a Waveform Audio File. Windows gives you this structure in the mmreg.h header file. The structure looks as follows:
<pre><font size="-1">
<b>typedef</b> <b>struct</b> {
WORD wFormatTag;
WORD nChannels;
DWORD nSamplesPerSec;
DWORD nAvgBytesPerSec;
WORD nBlockAlign;
WORD wBitsPerSample;
WORD cbSize;
} WAVEFORMATEX;
Members:
wFormatTag
Waveform-audio format type. Format tags are registered with Microsoft Corporation for many compression
algorithms. A complete list of format tags can be found in the MMREG.H header file.
nChannels
Number of channels in the waveform-audio data. Monaural data uses one channel and stereo data uses two
channels.
nSamplesPerSec
Sample rate, in samples per second (hertz), that each channel should be played or recorded. If wFormatTag is
WAVE_FORMAT_PCM, then common values for nSamplesPerSec are 8.0 kHz, 11.025 kHz, 22.05 kHz, and 44.1 kHz. For
non-PCM formats, this member must be computed according to the manufacturer's specification of the format
tag.
nAvgBytesPerSec
Required average data-transfer rate, in bytes per second, for the format tag. If wFormatTag is
WAVE_FORMAT_PCM, nAvgBytesPerSec should be equal to the product of nSamplesPerSec and nBlockAlign. For
non-PCM formats, this member must be computed according to the manufacturer's specification of the format
tag.
Playback and record software can estimate buffer sizes by using the nAvgBytesPerSec member.
nBlockAlign
Block alignment, in bytes. The block alignment is the minimum atomic unit of data for the wFormatTag format
type. If wFormatTag is WAVE_FORMAT_PCM, nBlockAlign should be equal to the product of nChannels and
wBitsPerSample divided by 8 (bits per byte). For non-PCM formats, this member must be computed according to
the manufacturer's specification of the format tag.
Playback and record software must process a multiple of nBlockAlign bytes of data at a time. Data written
and read from a device must always start at the beginning of a block. For example, it is illegal to start
playback of PCM data in the middle of a sample (that is, on a non-block-aligned boundary).
wBitsPerSample
Bits per sample for the wFormatTag format type. If wFormatTag is WAVE_FORMAT_PCM, then wBitsPerSample should
be equal to 8 or 16. For non-PCM formats, this member must be set according to the manufacturer's
specification of the format tag. Note that some compression schemes cannot define a value for
wBitsPerSample, so this member can be zero.
cbSize
Size, in bytes, of extra format information appended to the end of the WAVEFORMATEX structure. This
information can be used by non-PCM formats to store extra attributes for the wFormatTag. If no extra
information is required by the wFormatTag, this member must be set to zero. Note that for WAVE_FORMAT_PCM
formats (and only WAVE_FORMAT_PCM formats), this member is ignored.
</font></pre>
</td>
</tr>
</table>
<p>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr align="left" valign="top">
<td align="left">
<a name="ReadingTheWavefromAudioFile">Reading the Waveform Audio File</a>
</td>
<td align="right">
<a href="#TableOfContents">TOC</a>
</td>
</tr>
<tr align="left" valign="top">
<td bgcolor="White" align="left" colspan="2">
<br>
Now that we know how the file is structured, and know all about the WaveFormat structure, all we have to do is read the file in. The following code does just that.
<pre><font size="2">
<font color="990099">#include <mmreg.h></font>
<font color="990099">#include<string></font>
<font color="990099">#include<fstream></font>
<font color="#003399"><i>/*--------------------------------------------------------------------------------**
** This function takes in a filename, a pointer to a WAVEFORMATEX structure that **
** will be set, and a char* databuf, which will be allocated and filled with the **
** PCM data in the file filename. **
** **
** On error it will throw a string with a proper error announcement **
**--------------------------------------------------------------------------------*/</i></font>
<b>void</b> OpenWaveFile (<b>const</b> std::string &filename, WAVEFORMATEX *pwfx, <b>char</b>* databuf){
std::ifstream file;
<b>char</b> csID[4];
<b>long</b> fsize;
<b>long</b> wfxsize;
<b>long</b> datasize;
file.open (filename.c_str (), std::ios_base::binary);
<b>if</b> (file == NULL){
std::string s (<font color="#9933CC">"Unable to open file "</font>);
s += filename;
<b>throw</b> s;
}
file.read (csID, 4);
<b>if</b> (std::string (csID) != <font color="#9933CC">"RIFF"</font>){
std::string s (<font color="#9933CC">" does not have a valid RIFF ID."</font>);
s = filename + s;
<b>throw</b> s;
}
file.read ((<b>char</b>*)&fsize, 4);
file.read (csID, 4);
<b>if</b> (std::string (csID) != <font color="#9933CC">"WAVE"</font>){
std::string s (<font color="#9933CC">" is not a valid WAVE form-type."</font>);
s = filename + s;
<b>throw</b> s;
}
file.read (csID, 4);
<b>if</b> (std::string (csID) != <font color="#9933CC">"fmt "</font>){
std::string s (<font color="#9933CC">" does not have a valid wave-form chunk ID."</font>);
s = filename + s;
<b>throw</b> s;
}
file.read ((<b>char</b>*)&wfxsize, 4);
file.read ((<b>char</b>*)pwfx, wfxsize);
<b>if</b> (wfm ().wFormatTag != WAVE_FORMAT_PCM){
std::string s (<font color="#9933CC">" is not of type PCM wave format."</font>);
s = filename + s;
<b>throw</b> s;
}
file.read (csID, 4);
<b>if</b> (std::string (csID) != <font color="#9933CC">"data"</font>){
std::string s (<font color="#9933CC">" does not have a valid data chunk ID."</font>);
s = filename + s;
<b>throw</b> s;
}
file.read ((<b>char</b>*)&datasize, 4);
databuf = <b>new</b> <b>char</b> [datasize];
file.read (databuf, datasize);
}</font><font size="1">
</font></pre>
</td>
</tr>
</table>
<p>
<table border="0" cellspacing="0" cellpadding="2" width="100%">
<tr align="left" valign="top">
<td align="left">
<a name="Conclusion">Conclusion</a>
</td>
<td align="right">
<a href="#TableOfContents">TOC</a>
</td>
</tr>
<tr align="left" valign="top">
<td bgcolor="White" align="left" colspan="2">
<br>
Thats all there is to a it. You now have a buffer with all your PCM data in it and a WaveFormat structure that tells you what you need to know about the PCM data. You can now take the PCM data and play it though the sound device.<br>
<br>
Here is a list of all the main points in this article:
<ul type="disc">
<li>Waveform Audio is in RIFF.</li>
<li>There are 3 "chunks" in a waveform audio file. The three "chunks" are the "RIFF", "fmt ", and "data" sections</li>
<li>The WaveFormat structure contains all the PCM format information.</li>
<li>The "data" section contains all the PCM data.</li>
</ul>
</td>
</tr>
</table>
<p>
</table>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -