⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0,1410,21300,00.html

📁 C++builder学习资料C++builder
💻 HTML
📖 第 1 页 / 共 2 页
字号:
			<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>  

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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>  

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 &lt;mmreg.h&gt;</font>

<font color="990099">#include&lt;string&gt;</font>

<font color="990099">#include&lt;fstream&gt;</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 &amp;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">&quot;Unable to open file &quot;</font>);

        s += filename;

        <b>throw</b> s;

    }



    file.read (csID, 4);

    <b>if</b> (std::string (csID) != <font color="#9933CC">&quot;RIFF&quot;</font>){

        std::string s (<font color="#9933CC">&quot; does not have a valid RIFF ID.&quot;</font>);

        s = filename + s;

        <b>throw</b> s;

    }



    file.read ((<b>char</b>*)&amp;fsize, 4);

    file.read (csID, 4);

    <b>if</b> (std::string (csID) != <font color="#9933CC">&quot;WAVE&quot;</font>){

        std::string s (<font color="#9933CC">&quot; is not a valid WAVE form-type.&quot;</font>);

        s = filename + s;

        <b>throw</b> s;

    }



    file.read (csID, 4);

    <b>if</b> (std::string (csID) != <font color="#9933CC">&quot;fmt &quot;</font>){

        std::string s (<font color="#9933CC">&quot; does not have a valid wave-form chunk ID.&quot;</font>);

        s = filename + s;

        <b>throw</b> s;

    }



    file.read ((<b>char</b>*)&amp;wfxsize, 4);

    file.read ((<b>char</b>*)pwfx, wfxsize);

    <b>if</b> (wfm ().wFormatTag != WAVE_FORMAT_PCM){

        std::string s (<font color="#9933CC">&quot; is not of type PCM wave format.&quot;</font>);

        s = filename + s;

        <b>throw</b> s;

    }



    file.read (csID, 4);

    <b>if</b> (std::string (csID) != <font color="#9933CC">&quot;data&quot;</font>){

        std::string s (<font color="#9933CC">&quot; does not have a valid data chunk ID.&quot;</font>);

        s = filename + s;

        <b>throw</b> s;

    }



    file.read ((<b>char</b>*)&amp;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> 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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 + -