libfaac使用实例

来源:互联网 发布:淘宝店铺装潢 编辑:程序博客网 时间:2024/05/21 17:53
本文对采集的未压缩声音装换为aac格式
对应录制篇:http://blog.163.com/zhujiatc@126/blog/static/183463820130208928612/
代码实例用到上文生成的文件
最后生成aac文件


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream.h>
#include <time.h>
#include "faac.h"
#pragma comment(lib, "libfaac.lib")
typedef unsigned long   ULONG;
typedef unsigned int    UINT;
typedef unsigned char   BYTE;
typedef char            _TCHAR;
int main(int argc, _TCHAR* argv[])
{
    ULONG nSampleRate = 44100;  // 采样率
    UINT nChannels = 2;         // 声道数
    UINT nBit = 16;             // 单样本位数
    ULONG nInputSamples = 0; //输入样本数
    ULONG nMaxOutputBytes = 0; //输出所需最大空间
ULONG nMaxInputBytes=0;     //输入最大字节
    int nRet;
    faacEncHandle hEncoder; //aac句柄
    faacEncConfigurationPtr pConfiguration;//aac设置指针 

    BYTE* pbPCMBuffer;
    BYTE* pbAACBuffer;

    FILE* fpIn; // PCM file for input
    FILE* fpOut; // AAC file for output

    fpIn = fopen("myvoice", "rb");
    fpOut = fopen("out.aac", "wb");
if (fpIn==NULL)
{
printf("can't find myvoice!\n");
return 0;
}
    // (1) Open FAAC engine
    hEncoder = faacEncOpen(nSampleRate, nChannels, &nInputSamples, &nMaxOutputBytes);//初始化aac句柄,同时获取最大输入样本,及编码所需最小字节
nMaxInputBytes=nInputSamples*nBit/8;//计算最大输入字节,跟据最大输入样本数
printf("nInputSamples:%d nMaxInputBytes:%d nMaxOutputBytes:%d\n", nInputSamples, nMaxInputBytes,nMaxOutputBytes);
    if(hEncoder == NULL)
    {
        printf("[ERROR] Failed to call faacEncOpen()\n");
        return -1;
    }
    pbPCMBuffer = new BYTE [nMaxInputBytes];
    pbAACBuffer = new BYTE [nMaxOutputBytes];
    // (2.1) Get current encoding configuration
    pConfiguration = faacEncGetCurrentConfiguration(hEncoder);//获取配置结构指针
    pConfiguration->inputFormat = FAAC_INPUT_16BIT;
pConfiguration->outputFormat=1;
pConfiguration->useTns=true;
pConfiguration->useLfe=false;
pConfiguration->aacObjectType=LOW;
pConfiguration->shortctl=SHORTCTL_NORMAL;
pConfiguration->quantqual=100;
pConfiguration->bandWidth=0;
pConfiguration->bitRate=0;
    // (2.2) Set encoding configuration
    nRet = faacEncSetConfiguration(hEncoder, pConfiguration);//设置配置,根据不同设置,耗时不一样
int temp1=clock();
    while(true)
    {
nRet=0;
        nRet = fread(pbPCMBuffer, 1, nMaxInputBytes, fpIn);
if(nRet < 1)
        {
            break;
        }
        // 计算实际输入样本数,
        nInputSamples = nRet/ (nBit / 8);
        // (3) Encode
        nRet = faacEncEncode(hEncoder, (int*) pbPCMBuffer, nInputSamples, pbAACBuffer, nMaxOutputBytes);
if (nRet<1)
{
continue;
}
        fwrite(pbAACBuffer, 1, nRet, fpOut);
    }  
while((nRet=faacEncEncode(hEncoder, NULL, 0, pbAACBuffer, nMaxOutputBytes))>0)
{
fwrite(pbAACBuffer, 1, nRet, fpOut);
}
printf("usetime:%d\n",clock()-temp1);
    // (4) Close FAAC engine 关闭acc句柄
    nRet = faacEncClose(hEncoder);
    delete[] pbPCMBuffer;
    delete[] pbAACBuffer;
    fclose(fpIn);
    fclose(fpOut);
    system("PAUSE");
    return 0;
}
0 0
原创粉丝点击