- 注册时间
- 2010-10-22
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 2292
- 在线时间
- 小时
|
楼主 |
发表于 2011-1-6 11:07:02
|
显示全部楼层
然后,我们用src包中的测试文件mpg123_to_wav.c,过程:先将某mp3文件解码,得到PCM数据及相关采样信息,再通过sndfile(已包含此头文件)进行打包成WAV文件(此步骤略显轻松)。
然后,我们用WinHex打开生成的这个my.wav文件,看看它的头部是什么
从最左边可以看出,这个wav文件有43,416,620bytes =0x 02967C24 +8 bytes (注意是小端模式)
SampleRate=44100 =0x 0000AC44
从第41字节开始的4字节为PCM数据块的大小=0x 02967C00 bytes=43,416,620bytes -44
从第45字节开始存放PCM数据.....
所以,也不需要sndfile.h,注释掉- /*
- mpg123_to_wav.c
-
- copyright 2007 by the mpg123 project - free software under the terms of the LGPL 2.1
- see COPYING and AUTHORS files in distribution or http://mpg123.org
- initially written by Nicholas Humfrey
- */
-
- #include <stdio.h>
- #include <string.h>
- #include "mpg123.h"
- //#include "sndfile.h"
-
-
-
-
- void usage()
- {
- printf("Usage: mpg123_to_wav <input> <output>\n");
- exit(99);
- }
-
- void cleanup(mpg123_handle *mh)
- {
- /* It's really to late for error checks here;-) */
- mpg123_close(mh);
- mpg123_delete(mh);
- mpg123_exit();
- }
-
- int main(int argc, char *argv[])
- {
- //SNDFILE* sndfile = NULL;
- //SF_INFO sfinfo;
- mpg123_handle *mh = NULL;
- unsigned char* buffer = NULL;
- size_t buffer_size = 0;
- size_t done = 0;
- int channels = 0, encoding = 0;
- long rate = 0;
- int err = MPG123_OK;
-
-
- off_t samples = 0;
-
- if (argc!=3) usage();
- printf( "Input file: %s\n", argv[1]);
- printf( "Output file: %s\n", argv[2]);
-
- err = mpg123_init();
- if( err != MPG123_OK || (mh = mpg123_new(NULL, &err)) == NULL
- /* Let mpg123 work with the file, that excludes MPG123_NEED_MORE messages. */
- || mpg123_open(mh, argv[1]) != MPG123_OK
- /* Peek into track and get first output format. */
- || mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK )
-
- {
- fprintf( stderr, "Trouble with mpg123: %s\n",
- mh==NULL ? mpg123_plain_strerror(err) : mpg123_strerror(mh) );
- cleanup(mh);
- return -1;
- }
-
- if(encoding != MPG123_ENC_SIGNED_16)
- { /* Signed 16 is the default output format anyways; it would actually by only different if we forced it.
- So this check is here just for this explanation. */
- cleanup(mh);
- fprintf(stderr, "Bad encoding: 0x%x!\n", encoding);
- return -2;
- }
- /* Ensure that this output format will not change (it could, when we allow it). */
- mpg123_format_none(mh);
- mpg123_format(mh, rate, channels, encoding);
-
- /* Buffer could be almost any size here, mpg123_outblock() is just some recommendation.
- Important, especially for sndfile writing, is that the size is a multiple of sample size. */
- buffer_size = mpg123_outblock( mh );
-
- buffer = malloc( buffer_size );
-
- //bzero(&sfinfo, sizeof(sfinfo) );
- //**************************************************
- //memset(&sfinfo,'\0', sizeof(sfinfo));
-
- //sfinfo.samplerate = rate;
- //sfinfo.channels = channels;
- //sfinfo.format = SF_FORMAT_WAV|SF_FORMAT_PCM_16;
- printf("Creating 16bit WAV with %i channels and %liHz.\n", channels, rate);
-
- printf("%d\n",buffer_size);
-
- //sndfile = sf_open(argv[2], SFM_WRITE, &sfinfo);
- //if(sndfile == NULL){ fprintf(stderr, "Cannot open output file!\n"); cleanup(mh); return -2; }
-
- do
- {
- //printf("%d\n",buffer_size);
- //printf("%d\n",MPG123_OK);
- err = mpg123_read( mh, buffer, buffer_size, &done );
- //通过mpg123_read得到done字节数的PCM数据到buffer中,buffer_size仅共参考,无太大意义。
- //每循环一次,解码块指针后移,buffer为对应PCM数据,直到文件尾。
- //printf("Good\n");
- //system("Pause");
-
- //sf_write_short( sndfile, (short*)buffer, done/sizeof(short) );
- samples += done/sizeof(short);
- /* We are not in feeder mode, so MPG123_OK, MPG123_ERR and MPG123_NEW_FORMAT are the only possibilities.
- We do not handle a new format, MPG123_DONE is the end... so abort on anything not MPG123_OK. */
- } while (err==MPG123_OK);
-
- if(err != MPG123_DONE)
- fprintf( stderr, "Warning: Decoding ended prematurely because: %s\n",
- err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err) );
-
- //sf_close( sndfile );
-
- samples /= channels;
- printf("%li samples written.\n", (long)samples);
- cleanup(mh);
- return 0;
- }
复制代码
|
|