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

📄 main.c

📁 the author is LiFeng
💻 C
字号:
/*
* This source code is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*       
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* File Name: main.c							
*
* Reference:
*
* Author: Li Feng,  fli_linux@yahoo.com.cn                                                 
*
* Description:
*
* 	
* 
* History:
* 02/23/2005  Li Feng    Created
*  
*
*CodeReview Log:
* 
*/
#include <stdio.h>
#include <string.h>
#include "typedef.h"
#include "libmp3dec.h"

typedef signed char         INT8;
typedef signed short        INT16;
typedef signed int          INT32;
typedef unsigned char       UINT8;
typedef unsigned short      UINT16;
typedef unsigned int        UINT32;


#define MP2_FRAME_SIZE 216
#define PCM_FRAME_SIZE 2048*8

#define WAVE_FORMAT_PCM 1

#pragma pack(push,1)

typedef struct _riffchunk
{
	UINT32 fcc;
	UINT32  cb;
} RIFFCHUNK, *LPRIFFCHUNK;

typedef struct _rifflist 
{
	UINT32 fcc;
	UINT32  cb;
	UINT32 fccListType;
} RIFFLIST , *LPRIFFLIST;

#define RIFFROUND(cb) ((cb) + ((cb)&1))

#define RIFFNEXT(pChunk) (LPRIFFCHUNK)((LPBYTE)(pChunk) \
						+ sizeof(RIFFCHUNK) \
						+ RIFFROUND(((LPRIFFCHUNK)pChunk)->cb))
typedef struct 
{
	UINT32 fcc;
	UINT32 cb;
	UINT16  wFormatTag; 
	UINT16  nChannels; 
	UINT32 nSamplesPerSec; 
	UINT32 nAvgBytesPerSec; 
	UINT16  nBlockAlign; 
	UINT16  wBitsPerSample; 
}WAVEFORM;

#pragma pack(pop)

const UINT32 WAVE_HEAD_LEN=44;
static UINT8 head[44];	

void SetWaveHead(UINT32 dwDataLen, UINT32 nSampleRate, UINT32 nChannel)
{
	RIFFCHUNK *pch;
	RIFFLIST  *priff;
	WAVEFORM *pwave;

	memset(&head,0x00,WAVE_HEAD_LEN);
	priff=(RIFFLIST*)head;
	priff->fcc=SWAP32('RIFF');
	if(dwDataLen)
		priff->cb=dwDataLen+WAVE_HEAD_LEN-sizeof(RIFFCHUNK);
	priff->fccListType=SWAP32('WAVE');
	pwave=(WAVEFORM*)(priff+1);
	pwave->fcc=SWAP32('fmt ');
	pwave->cb=sizeof(WAVEFORM)-sizeof(RIFFCHUNK);
	pwave->wFormatTag=WAVE_FORMAT_PCM;
	pwave->nChannels=nChannel;
	pwave->nSamplesPerSec=nSampleRate;
	pwave->nAvgBytesPerSec=pwave->nSamplesPerSec*nChannel*2;
	pwave->nBlockAlign=nChannel*2;
	pwave->wBitsPerSample=16;
	pch=(RIFFCHUNK*)(pwave+1);
	pch->fcc=SWAP32('data');
	if(dwDataLen)
	{
		pch->cb = dwDataLen;
		//memcpy(dwDataLen,&WAVE_HEAD_LEN,sizeof(UINT32));
	}
}


#define BUF_SIZE 1024

int main(int argc, char **argv)
{
	FILE *fpInput;
	FILE *fpOutput;
	signed short data[PCM_FRAME_SIZE];
	unsigned char mpa_data[1792];
	int nRead = 0;
	int nDataLen;
	UINT32 nFileLen = 0;
	HMP3DEC hDec;
	uint32_t nFrameSize;
	uint32_t nFrameLen;
	uint32_t nSampleRate;
	uint32_t nChannels;
	
	if(argc<3)
		return 0;
	
	fpInput = fopen(argv[1], "rb");
	fpOutput= fopen(argv[2], "wb");
	if(fpInput==NULL ||fpOutput==NULL)
	{
		printf("open file error\n");
		goto err;
	}

	fwrite(head, 1, WAVE_HEAD_LEN, fpOutput);
	hDec = MP3_decode_init();
	fread(mpa_data, 1, 4, fpInput);
	nFrameSize = MP3_GetFrameSize(*(uint32_t*)mpa_data);
	if(nFrameSize==0)
	{
		goto err;
	}
	MP3_GetAudioInfo(&nSampleRate, &nChannels, *(uint32_t*)mpa_data);

	while((nRead = fread(mpa_data+4, 1, nFrameSize-4, fpInput))==nFrameSize-4)
	{
		nFrameLen = MP3_decode_frame(hDec, data, &nDataLen, mpa_data, nFrameSize);
		if(nDataLen>0)
		{
			fwrite(data, 1, nDataLen, fpOutput);
			nFileLen += nDataLen;
		}
		if(fread(mpa_data, 1, 4, fpInput) != 4)
			break;
		nFrameSize = MP3_GetFrameSize(*(uint32_t*)mpa_data);
	}
	MP3_decode_close(hDec);
	SetWaveHead(nFileLen, nSampleRate, nChannels);
	fseek(fpOutput, 0, SEEK_SET);
	fwrite(head, 1, WAVE_HEAD_LEN, fpOutput);

err:

	if(fpInput)
	{
		fclose(fpInput);
	}
	if(fpOutput)
	{
		fclose(fpOutput);
	}
	return 0;
}









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -