pcm转aac

#include 
#include 
#include "faac.h"

int main()
{
	int n_read_len = 0;
	FILE *fp = NULL;
	FILE *aac_fp = NULL;
	int ret = 0;
	
	unsigned long  sampleRate     = 8000;     //编码采样率
    unsigned int   numChannels    = 1;        //编码声道数
    unsigned long  inputSample    = 0;        //输入样本大小,在打开编码器时会得到此值
    unsigned long  maxOutputBytes = 0;        //最大输出,编码后的输出数据大小不会高于这个值,也是打开编码器时获得
    unsigned int   mPCMBitSize    = 16;       //pcm位深,用于计算一帧pcm大小

    int mPCMBufferSize = 0;       //一帧PCM缓存大小
    int mCountSize = 0;           //计算缓存大小
    char* mPCMBuffer;             //PCM缓存
	unsigned char* aacData;
	
    faacEncHandle encoder;                //faac编码器句柄
    faacEncConfigurationPtr config;       //faac设置类
	
	//open
	encoder = faacEncOpen(sampleRate, numChannels, &inputSample, &maxOutputBytes);
	if(encoder == NULL)
	{
		printf("[ERROR] Failed to call faacEncOpen()\n");
		goto ERR;
	}
	
	//faacEncSetConfiguration
	config = faacEncGetCurrentConfiguration(encoder);   //获取当前编码器的设置句柄  
    config->aacObjectType = LOW;                        //设置AAC类型
    config->useLfe = 0;                                 //是否允许一个声道为低频通道
    config->useTns = 1;                                 //是否使用瞬时噪声定形滤波器(具体作用不是很清楚)
    config->allowMidside = 0;                           
    config->outputFormat =1;                            // RAW_STREAM = 0, ADTS_STREAM=1  (ADTS可以实现单帧单独解码,raw由于缺少头无法单帧解码,因此无法做实时传输)
    config->bitRate =0;                                 //设置比特率
    config->inputFormat = FAAC_INPUT_16BIT;  //设置输入PCM格式
    faacEncSetConfiguration(encoder, config);   //应用设置
	
	
	mPCMBufferSize = inputSample * mPCMBitSize / 8;
    mPCMBuffer = new char[mPCMBufferSize];
	aacData = new unsigned char[maxOutputBytes];   //编码后输出数据(也就是AAC数据)存放位置
	
	fp = fopen("test.pcm", "r");
	if(!fp)
	{
		printf("[ERROR] Failed to call fopen test.pcm\n");
		goto ERR;
	}
	
	aac_fp = fopen("test.aac", "w+");
	if(!fp)
	{
		printf("[ERROR] Failed to call fopen test.aac\n");
		goto ERR;
	}
	
	
	while(1)
	{
		n_read_len = fread(mPCMBuffer, 1, mPCMBufferSize, fp);
		if(n_read_len <= 0)
		{
			printf("======== aac enc end ========\n");
			break;
		}
		
		ret = faacEncEncode(encoder, (int32_t *)mPCMBuffer, inputSample, aacData, maxOutputBytes);  
		
        //ret为0时不代表编码失败,而是编码速度较慢,导致缓存还未完全flush,可用一个循环继续调用编码接口,当 ret>0 时表示编码成功,且返回值为编码后数据长度
        while (ret == 0)
        {
            ret = faacEncEncode(encoder, (int32_t *)mPCMBuffer, inputSample, aacData, maxOutputBytes);
        }

        if (ret > 0)
        {
            printf("encode voice success, aac data len: %d\n", ret);
			fwrite(aacData, 1, ret, aac_fp);
            
        }
        else
        {
            printf("encode failed!!\n");
        }
		
	}
	
ERR:
	fclose(fp);
	fclose(aac_fp);
	
	if(encoder != NULL)
	{
		faacEncClose(encoder);
		encoder = NULL;
	}

	if(NULL != mPCMBuffer)
	{
		delete[] mPCMBuffer;
		mPCMBuffer = NULL;
	}	
	
	if(NULL != aacData)
	{
		delete[] aacData;
		aacData = NULL;
	}	
	
	return 0;
}


你可能感兴趣的:(pcm,aac,c语言)