yansheng.wang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
MP3帧体的大小由MPEG版本号、比特率、抽样率和填充位4个因素确定。计算公式为:
帧大小= ((MPEG版本号== 1?144:72) * 比特率)/抽样率 + 填充位
位率为64kbps,采样频率为44.1kHz,padding(帧长调节)为1时,帧长为210字节。

位率为128kbps,采样频率为44.1kHz,padding(帧长调节)为0时,帧长为418字节。


以下是简单示例:

发火

#include <stdio.h>
#include <stdlib.h>

#include "lame.h"

#define BUF_SIZE 512
#define INBUF_SIZE 4096
#define MP3BUF_SIZE (int)(1.25 * BUF_SIZE) + 7200

short pcm_l[INBUF_SIZE];
short pcm_r[INBUF_SIZE];
unsigned char mp3_buf[MP3BUF_SIZE];

int main()
{
	FILE * mp3fp = fopen("爱的天国.mp3", "rb");
	if (!mp3fp)
	{
		printf("打开mp3文件失败");
		return -1;
	}

	FILE * wavfp = fopen("out1.pcm", "wb");
	if (!wavfp)
	{
		printf("创建wav文件失败");
		return -1;
	}

// 	fseek(mp3fp, 0, 2);
// 	long filelen = ftell(mp3fp);
// 	rewind(mp3fp);


// 	unsigned char * mp3buf = new unsigned char[filelen];
// 	int mp3len = fread(mp3buf, 1, filelen, mp3fp);

	////
	hip_t hip = hip_decode_init();
	if (!hip)
	{
		printf("创建mp3解码失败");
		return -1;
	}
	mp3data_struct mp3str;
	
	int samples;
	int mp3_bytes;
	int write_bytes;

	while ((mp3_bytes = fread(mp3_buf, 1, 210, mp3fp)) > 0)
	{
		samples = hip_decode(hip, mp3_buf, 210, pcm_l, pcm_r);
		if (samples > 0)
		{
			write_bytes = fwrite(pcm_l, sizeof(short), samples, wavfp);
		}
	}
	hip_decode_exit(hip);
	fclose(wavfp);

	return 0;
}


posted on 2012-07-16 18:01  小小程序员001  阅读(1721)  评论(0编辑  收藏  举报