windows faac将pcm转换成aac

faac 和fdk-aac是2个不同的音频转换库。

头文件:

#ifndef __PCM_TO_AAC__
#define __PCM_TO_AAC__

#include <faac.h>
#include <string>
#include <memory>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

class PCM_TO_AAC
{
public:
	PCM_TO_AAC();
	~PCM_TO_AAC();

public:
	bool Init(int nSamplerate/*采样率*/, int nChannel/*通道号*/, int nSampleBit/*采样位数*/);

public:
	//成功--返回aac数据大小,小于等于0时视为失败
	int ConvertProcess(char* pcmbuf/*待转化的PCM数据,该数据大小必须等于InputSample * 采样位数 / 8,否则转化会失败*/, int PcmSize/*待转化pcm数据大小,该数据大小必须等于InputSample * 采样位数 / 8*/, unsigned char* aacBuf/*aac数据缓存buf*/);

public:
	int GetPerPcmPacketSize() { return m_PCMBufSize; };

private:
	faacEncHandle m_hEncoder;

private:
	unsigned long m_nMaxOutputBytes;
	int m_nPCMSampleBit;
	int m_PCMBufSize;

private:
	char* m_PtrPCMBuf;
	char* m_PtrAACBuf;

private:
	faacEncConfigurationPtr m_pConfiguration;
};

#endif

源文件:

 

#include "PCMToAAC.h"

PCM_TO_AAC::PCM_TO_AAC()
{
    
}

PCM_TO_AAC::~PCM_TO_AAC()
{
    if (m_hEncoder){
        faacEncClose(m_hEncoder);
        m_hEncoder = 0;
    }

    if (m_PtrPCMBuf) {
        delete[] m_PtrPCMBuf;
        m_PtrPCMBuf = NULL;
    }
    
    if (m_PtrAACBuf) {
        delete[] m_PtrAACBuf;
        m_PtrAACBuf = NULL;
    }
}

bool PCM_TO_AAC::Init(int nSamplerate/*采样率*/, int nChannel/*通道号*/, int nSampleBit/*采样位数*/)
{
    m_nMaxOutputBytes = 0;
    m_nPCMSampleBit = nSampleBit;

    //
    unsigned long nInputSample = 0;
    m_hEncoder = faacEncOpen(nSamplerate, nChannel, &nInputSample, &m_nMaxOutputBytes);
    if (!m_hEncoder){
        return false;
    }

    m_PCMBufSize = nInputSample * m_nPCMSampleBit / 8;

    //
    try {
        m_PtrPCMBuf = new char[m_PCMBufSize];
    }
    catch (...) {}
    
    //
    try {
        m_PtrAACBuf = new char[m_nMaxOutputBytes];
    }
    catch (...) {}

    //
    m_pConfiguration = faacEncGetCurrentConfiguration(m_hEncoder);
    m_pConfiguration->inputFormat = FAAC_INPUT_16BIT;
    m_pConfiguration->outputFormat = 1; /* 0--RAW  1--ADTS header */
    m_pConfiguration->aacObjectType = LOW;
    m_pConfiguration->allowMidside = 0;
    m_pConfiguration->useTns = 1;

    faacEncSetConfiguration(m_hEncoder, m_pConfiguration);
    return true;
}

int PCM_TO_AAC::ConvertProcess(char* pcmbuf, int PcmSize, unsigned char* aacBuf)
{
    unsigned int inputSample = PcmSize / (m_nPCMSampleBit / 8);

    return faacEncEncode(m_hEncoder, (int*)pcmbuf, inputSample, aacBuf, m_nMaxOutputBytes);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值