分享
 
 
 

将Wav格式压缩成Mp3

王朝other·作者佚名  2008-06-01
窄屏简体版  字體: |||超大  

Compressing Wav file to mp3

● 1. IntrodUCtion

First, I don't meant to give you informations about how to understand the mp3 algorithm. My goal is to eXPlain how to use an already existing encoder with BCB.

● 2. Choosing the mp3 encoder

There are tons of mp3 encoders. Some of them are free others are not. Some are fast but produce an awful result. Others are slow but with Excellent result and give a high audio quality. The ideal would be a free, reasonably fast encoder giving a high audio quality, all at the same time.

Enjoy! This pearl exists. But we have to look at it in the GNU world. There is a GNU project, called LAME, for Lame Ain't a Mp3 Encoder, under the GPL license. The official web site of the LAME project is http://www.mp3dev.org/mp3/

Moreover, as it is a GNU project, we have Access to the source and there is a version compiled for Win32 in a DLL.

Among all the other encoders, I want to quote two of them. The first, FRAUNHOFER, because it is a fast and excellent encoder : http://www.iis.fhg.de/ but it's not free though.

The second because it's a very fast encoder but the audio result is awful. So don't use it except if you are looking for a fast encoder. It's the encoder from Xing Tech : http://www.xingtech.com/

Note : The Lame encoder has a limitation. The sample rate must be 32000, 44100 or 48000.?

● 3. Some informations about the WAV format

A wav file is just a collection of chunks. There is a format chunk wich contains all the informations about the samples. For instance, the bitrate, the number of channels, if it's stereo or mono... There is also a chunk containing the data. In other Words, this chunk contains all the samples. In front of the file, there are 12 characters indicating that the file is a wav file.

The two chunks given above must be present in the file.

There could be other chunk but we just ignore them. They are not needed for our purpose. If you want to know more about wav file, take a look at http://www.wotsit.org/ for a complete description.

The format chunk :

struct FormatChunk

{

char chunkID[4];

long chunkSize;

short wFormatTag;

unsigned short wChannels;

unsigned long dwSamplesPerSec;

unsigned long dwAvgBytesPerSec;

unsigned short wBlockAlign;

unsigned short wBitsPerSample;

// Note: there may be additional fields here, depending upon wFormatTag.

};

Above, you can see the struct representing the format chunk. The chunkID is always "fmt " with an ending space (4 characters). It's the identification of the chunk. All other chunk have such an ID. The chunkSize parameter contains the number of bytes of the chunk, the ID and chunkSize excluded.

The format chunk must be the first chunk in the file.

The data chunk :

struct Chunk

{

char chunkID[4];

long chunkSize;

};

In the case of the data chunk, the chunkID contains "data". The chunkSize parameters contains the size of the raw data (samples). The data begins just after chunkSize.

In the case of the data chunk, the chunkID contains "data". The chunkSize parameters contains the size of the raw data (samples). The data begins just after chunkSize.

Dans le cas du bloc de données, chunkID contient "data". Le paramètre chunkSize contient la taille du bloc de données proprement dites. Celles-ci commencent juste après chunkSize.

So, when we read a wav file, all we have to do is :

- read the first 12 characters to check if it's a real wav file.

- read the format chunk in a struct similar to the formatChunk struct.

- skip the extra parameters in the format chunk, if any.

- find the data chunk, read the raw data and carry out with the encoding.

-skip all other chunks.

Donc, ce que nous devons faire est :

- lire les 12 premiers caractères pour déterminer si on est bien en présence d'un fichier wav.

- lire le bloc de format dans une structure similaire à la structure formatChunk.

- ignorer les caractères supplémentaires dans le bloc de format, s'il y en a.

- ignorer tous les blocs qui ne sont pas le bloc de données.

- trouver le bloc de données, lire ces données et lancer l'encodage.

● 4. Importing the DLL

The DLL used for the encoding is called lame_enc.dll.

Unfortunately, this DLL was build with VC 6 from Microsoft. If we just create a lib file from the DLL and try to import the library in BCB, we'll get an 'Unresolved external error' at link time for each function we'll try to use. Due to the declaration type, BCB is expecting a function name with a leading underscore and the function names doesn't have such a leading underscore.

To resolve this issue, we must, first, create a def file from our DLL. Open a console windows and type :

IMPDEF lame_enc.def lame_enc.dll

Open the lame_enc.def file with an editor (Notepad for instance) and modify it like this. This will create aliases for the functions :

LIBRARY LAME_ENC.DLL

EXPORTS

_beCloseStream = beCloseStream

_beDeinitStream = beDeinitStream

_beEncodeChunk = beEncodeChunk

_beInitStream = beInitStream

_beVersion = beVersion

_beWriteVBRHeader = beWriteVBRHeader

beCloseStream @4

beDeinitStream @3

beEncodeChunk @2

beInitStream @1

beVersion @5

beWriteVBRHeader @6

Now, we can create the lib file from our def file. We'll import that lib file in our project. To create the lib file, type :

implib lame_enc.lib lame_enc.def

● 5. The code

First, you have to import the libary in your project. Next, include the header file of the DLL into your unit. In the DLL header file, you have to add extern "C" in front of all exported function.

Here is the header with the moifications (lame_enc.h) :

/* bladedll.h

+++++++++++++++++++++++++++

+ Blade's Encoder DLL &n

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有