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

📄 mp3dec2wav.c

📁 一个将mp3/mp2文件解压成wav文件的源码
💻 C
字号:

#include <windows.h>
//#include "mpglibdll.h"

// wfz_added headerfile-----
//#include <fcntl.h>
//#include <sys/types.h>
//#include <sys/stat.h>
//#include <io.h>
#include <stdio.h>
#include "audio.h"
#include "mpglib\mpg123.h"



//HINSTANCE    hmpglibdll;
//INITMP3      InitMP3;
//EXITMP3      ExitMP3;
//DECODEMP3    decodeMP3;

#define NO_DLL_FOUND -1

char buf[16384];
struct mpstr mp;

extern long freqs[9]; // wfz_added

int main (void)
{
	int size;
	char out[8192];
	int len,ret;
	FILE *pFileRead;
 	audio_file *aufile;

	char mp3FileName[100];
	char wavFileName[100];
	int samplesperframe;
	int bytespersample;

	int samplerate   = 44100;
	int channels     = 2;
	int outputFormat = FAAD_FMT_16BIT;  //only support 16bit now, otherwise error
	int fileType     =  OUTPUT_WAV;
	int channelMask  = 0;
	BOOL firstbuf = TRUE;
	//------print log info-----
	FILE *pLogFile = fopen("d:\\Mp3toWav.txt","wa");
	char log[150];


	switch(outputFormat)
	{
	case FAAD_FMT_16BIT:
		bytespersample = 2;
		break;
	case FAAD_FMT_24BIT:
		bytespersample = 3;
		break;
	case FAAD_FMT_32BIT:
		bytespersample = 4;
		break;
	default:
		bytespersample = 2;
	}
	strcpy(mp3FileName, "phonexb.mp2");
	strcpy(wavFileName, "result.wav");

//------
	// Open file for input: 
	if( (pFileRead = fopen( mp3FileName, "rb" )) == NULL )
	{
		perror( "open jsb.mp3 failed on input file" );
		exit( 1 );
	}
	// Open file for output: 
	aufile = open_audio_file(wavFileName, samplerate, channels,
		outputFormat, fileType, channelMask);
	
	if(aufile->sndfile  == NULL)
	{
		perror( "Open failed on output file" );
		exit( 1 );
	}
//-------


	InitMP3(&mp);

	while( !feof(pFileRead) )
	{
		len = fread(buf, sizeof(char), 10000, pFileRead);
		if(len <= 0)
			break;
		ret = decodeMP3(&mp,buf,len,out,8192,&size);
		if(firstbuf)
		{
			aufile->samplerate = freqs[mp.fr.sampling_frequency]; 
			firstbuf = FALSE;
		}
		while(ret == MP3_OK)
		{
			samplesperframe = size/bytespersample;
       		write_audio_file(aufile, out, samplesperframe, 0);
			if(samplesperframe != 2304) // 1152 * 2 channels = 2304
			{
				sprintf(log, " samples of current frame is %d \n", samplesperframe);
				fwrite(log, 1, strlen(log), pLogFile); 
			}
			ret = decodeMP3(&mp,NULL,0,out,8192,&size);
		}
	}

	//aufile->samplerate = mp.fr.sampling_frequency; //save the real samplerate
	ExitMP3(&mp);
	
	fclose(pFileRead);
	fclose(pLogFile);
    close_audio_file(aufile);

	return (0);
}

⌨️ 快捷键说明

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