📄 将wav格式压缩成mp3-c-c++-华夏名网资讯中心 虚拟主机,域名注册,双线虚拟主机,服务器租赁,b style=colorblack;background-color#ff66ff为-b7万用户提供服务.htm
字号:
*beConfig,<BR> PDWORD dwSamples, PDWORD dwBufferSize,
PHBE_STREAM phbeStream);<BR>extern "C" __declspec(dllexport)
BE_ERR beEncodeChunk(HBE_STREAM
hbeStream,<BR> DWORD nSamples, PSHORT pSamples, PBYTE
pOutput, PDWORD pdwOutput);<BR>extern "C" __declspec(dllexport)
BE_ERR beDeinitStream(HBE_STREAM
hbeStream,<BR> PBYTE pOutput, PDWORD pdwOutput); extern
"C" __declspec(dllexport) BE_ERR beCloseStream(HBE_STREAM
hbeStream);<BR>extern "C" __declspec(dllexport)
VOID beVersion(BE_VERSION
*beVersion);<BR><BR>#endif<BR>#pragma pack(pop)<BR>#endif<BR><BR>As you can
see in the header above, you have to add #define _BLADEDLL into
your .cpp file before including the header.<BR><BR>Below, you’ll find the code
of a little application which takes a <B
style="COLOR: black; BACKGROUND-COLOR: #99ff99">wav</B> file in input and
encode the file to <B style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.
I don’t give more explanations because the code is very straightforward and
commented. It is not very elegant but it’s just to show how to use the
DLL.<BR><BR>File
Format.h<BR>//---------------------------------------------------------------------------<BR>#ifndef
Format_H<BR>#define
Format_H<BR>//---------------------------------------------------------------------------<BR>struct
FormatChunk<BR>{<BR> char chunkID[4];<BR> long chunkSize;<BR> short wFormatTag;<BR> unsigned
short wChannels;<BR> unsigned
long dwSamplesPerSec;<BR> unsigned
long dwAvgBytesPerSec;<BR> unsigned
short wBlockAlign;<BR> unsigned
short wBitsPerSample;<BR> // Note: there may
be additional fields here, depending upon wFormatTag.<BR>};<BR><BR>// This is
the start ID of a Wave file<BR>// must contains ’RIFF’ and ’WAVE’<BR>char
startID[12];<BR><BR>// contains the chunk id (’data’, ’cue ’ ...) and the
chunk size<BR>struct
Chunk<BR>{<BR> char chunkID[4];<BR> long chunkSize;<BR>};<BR><BR>//
a pointer to the samples in the data chunk<BR>unsigned
char *WaveformData;<BR><BR>//---------------------------------------------------------------------------<BR>#endif<BR><BR><BR>File
Unit1_H.h<BR>//---------------------------------------------------------------------------<BR>#ifndef
Unit1H<BR>#define
Unit1H<BR>//---------------------------------------------------------------------------<BR>#include
<Classes.hpp><BR>#include <Controls.hpp><BR>#include
<StdCtrls.hpp><BR>#include <Forms.hpp><BR>#include
<Dialogs.hpp><BR>//---------------------------------------------------------------------------<BR>class
TForm1 : public TForm<BR>{<BR>__published: // IDE-managed
Components<BR> TOpenDialog
*OpenDialog1;<BR> TEdit
*FileEdit;<BR> TLabel
*Label1;<BR> TButton
*Browse;<BR> TButton
*Encode;<BR> void __fastcall BrowseClick(TObject
*Sender);<BR> void __fastcall EncodeClick(TObject
*Sender);<BR>private: // User
declarations<BR> AnsiString
OutputFileName;<BR>public: // User
declarations<BR> __fastcall TForm1(TComponent*
Owner);<BR>};<BR>//---------------------------------------------------------------------------<BR>extern
PACKAGE TForm1
*Form1;<BR>//---------------------------------------------------------------------------<BR>#endif<BR><BR><BR>Fiel
Unit1.cpp<BR>//---------------------------------------------------------------------------<BR>#include
<vcl.h><BR>#pragma hdrstop<BR><BR>#include <fstream><BR>#include
<iostream><BR>#include "Unit1.h"<BR><BR>#define
_BLADEDLL //
Don’t forget it<BR>#include "lame_enc.h"<BR>#include
"format.h"<BR>//---------------------------------------------------------------------------<BR>#pragma
package(smart_init)<BR>#pragma resource "*.dfm"<BR>TForm1
*Form1;<BR>//---------------------------------------------------------------------------<BR>__fastcall
TForm1::TForm1(TComponent*
Owner)<BR> :
TForm(Owner)<BR>{<BR>}<BR>//---------------------------------------------------------------------------<BR>void
__fastcall TForm1::BrowseClick(TObject
*Sender)<BR>{<BR> OpenDialog1->InitialDir =
ExtractFileDir(Application->ExeName);<BR> if(OpenDialog1->Execute())<BR> {<BR> FileEdit->Text
=
OpenDialog1->FileName;<BR> OutputFileName
= ChangeFileExt(OpenDialog1->FileName, ".<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>");<BR> }<BR>}<BR>//---------------------------------------------------------------------------<BR>void
__fastcall TForm1::EncodeClick(TObject
*Sender)<BR>{<BR> if(FileEdit->Text ==
"")<BR> return;<BR> std::ifstream
fin(FileEdit->Text.c_str(),
std::ios::binary);<BR> if(!fin)<BR> return;<BR> //
read the 12 character in front of the
file<BR> fin.read((char*)&startID,
sizeof(startID));<BR><BR> // get the format
chunk<BR> FormatChunk
fc;<BR> fin.read((char*)&fc,
sizeof(FormatChunk));<BR> // the first chunk MUST be
the format chunk<BR> if(strncmp(fc.chunkID, "fmt ", 4)
!=
0)<BR> {<BR> Application->MessageBox("This
is not a valid Wave
file",<BR> "Wav2Mp3
ERROR",
MB_OK);<BR> return;<BR> }<BR> if(fc.wFormatTag!=1)<BR> {<BR> Application->MessageBox("Cannot
handle compressed Wave
file",<BR> "Wav2Mp3
ERROR",
MB_OK);<BR> return;<BR> }<BR> //
initialization of <B style="COLOR: white; BACKGROUND-COLOR: #880000">Mp3</B>
encoder<BR> BE_CONFIG
bc;<BR> bc.dwConfig =
BE_CONFIG_MP3;<BR> // 32000, 44100 and 48000 are the
only sample rate authorized<BR> // due to encoding
limitations<BR> if(fc.dwSamplesPerSec == 32000 ||
fc.dwSamplesPerSec == 44100
||<BR> fc.dwSamplesPerSec
== 48000)<BR> bc.format.<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.dwSampleRate =
fc.dwSamplesPerSec;<BR> else<BR> {<BR> Application->MessageBox("Unsuported
sample
rate",<BR> "Wav2Mp3
ERROR",
MB_OK);<BR> return;<BR> }<BR> if(fc.wChannels
== 1)<BR> bc.format.<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.byMode =
BE_MP3_MODE_MONO;<BR> else<BR> bc.format.<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.byMode =
BE_MP3_MODE_STEREO;<BR> // the resulting file length
depends on this parameter<BR> // higher the bitrate,
better the result<BR> bc.format.<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.wBitrate =
192;<BR> bc.format.<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.bCopyright =
false;<BR> bc.format.<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.bCRC =
false;<BR> bc.format.<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.bOriginal =
false;<BR> bc.format.<B
style="COLOR: white; BACKGROUND-COLOR: #880000">mp3</B>.bPrivate =
false;<BR> // skip extra formatchunk parameter, if
any<BR> if(sizeof(FormatChunk) < int(8 +
fc.chunkSize))<BR> {<BR> char
c;<BR> for(int i=0; i< int(8
+ fc.chunkSize - sizeof(FormatChunk));
i++)<BR> fin.get(c);<BR> }<BR> //
get next chunk<BR> Chunk
chunk;<BR> fin.read((char*)&chunk,
sizeof(Chunk));<BR> // check if it’s the data
chunk<BR> while(strncmp(chunk.chunkID, "data", 4) !=
0)<BR> {<BR> char
c;<BR> for(int i=0;
i<chunk.chunkSize;
i++)<BR> fin.get(c);<BR> fin.read((char*)&chunk,sizeof(Chunk));<BR> }<BR> //
process with the encoding<BR> DWORD
dwNumberOfSamples;<BR> DWORD
dwOutputBufferLength;<BR> HBE_STREAM
hStream;<BR> if(beInitStream(&bc,
&dwNumberOfSamples,
&dwOutputBufferLength,<BR> &hStream)
!=
BE_ERR_SUCCESSFUL)<BR> {<BR> Application->MessageBox("Cannot
perform
compression",<BR> "Wav2Mp3
ERROR",
MB_OK);<BR> return;<BR> }<BR> std::ofstream
fout(OutputFileName.c_str(),
std::ios::binary);<BR> char *Mp3Buffer = new
char[dwOutputBufferLength];<BR> SHORT *InputBuffer =
new SHORT[dwNumberOfSamples]; // SHORT =
short = 16 bits<BR><BR> int
nSamplesPerformed=0;<BR> DWORD
dwNumberOfSamplesEncoded;<BR> while(nSamplesPerformed
<
chunk.chunkSize)<BR> {<BR> fin.read((char*)InputBuffer,
dwNumberOfSamples *
2);<BR> nSamplesPerformed +=
dwNumberOfSamples *
2;<BR> if(beEncodeChunk(hStream,
dwNumberOfSamples,
InputBuffer,<BR> (BYTE*)Mp3Buffer,
&dwNumberOfSamplesEncoded) !=
BE_ERR_SUCCESSFUL)<BR> {<BR> Application->MessageBox("Cannot
perform
compression",<BR> "Wav2Mp3
ERROR",
MB_OK);<BR> return;<BR> }<BR> fout.write(Mp3Buffer,
dwNumberOfSamplesEncoded);<BR> }<BR> beDeinitStream(hStream,
(BYTE*)Mp3Buffer,
&dwNumberOfSamplesEncoded);<BR> beCloseStream(hStream);<BR><BR> delete
Mp3Buffer;<BR> delete
InputBuffer;<BR> return;<BR>}
<P><BR><BR> </P>
<LI
style="MARGIN-TOP: 20px; PADDING-LEFT: 10px; MARGIN-LEFT: 0px; LINE-HEIGHT: 18px; TEXT-ALIGN: left"><BR>
<P>以上内容由 <A href="http://www.sudu.cn/">华夏名网</A> 搜集整理,如转载请注明原文出处,并保留这一部分内容。</P>
<TABLE style="BORDER-COLLAPSE: collapse" borderColor=#333333 cellSpacing=0
cellPadding=4 width="98%" border=1>
<TBODY>
<TR>
<TD bgColor=#eeeeee> “华夏名网” http://www.sudu.cn 和 http://www.bigwww.com
是成都飞数科技有限公司的网络服务品牌,专业经营虚拟主机,域名注册,VPS,服务器租用业务。公司创建于2002年,经过6年的高速发展,“华夏名网”已经成为我国一家知名的互联网服务提供商,被国外权威机构webhosting.info评价<B
style="COLOR: black; BACKGROUND-COLOR: #ff66ff">为</B>25大IDC服务商之一。</TD></TR></TBODY></TABLE>
<P>华夏名网网址导航: <A href="http://www.sudu.cn/webhosting">虚拟主机</A> <A
href="http://www.sudu.cn/webhosting
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -