#include "speex/speex.h"
class CSpeexAudioCodec;
class CSpeexEncode
{
public:
CSpeexEncode(void);
virtual ~CSpeexEncode(void);
public:
CSpeexAudioCodec * m_pSpeexCodec;
private:
SpeexBits m_bits;
void * m_EncStat;
spx_int32_t m_framesize;
spx_int32_t m_BitRates;
char * m_audioBuffer ;
public:
void Encode(short * pSrcData,INT dwLength);
};
#include "StdAfx.h"
#include ".\speexaudiocodec.h"
extern "C" __declspec(dllexport) IAudioCodec * CreateCodec()
{
CSpeexAudioCodec * pILBC = new CSpeexAudioCodec();
return pILBC;
}
CSpeexAudioCodec::CSpeexAudioCodec(void)
{
m_SpeexEncode = NULL;
m_SpeexDecode = NULL;
m_SampleSize = 960;
}
CSpeexAudioCodec::~CSpeexAudioCodec(void)
{
if(m_SpeexEncode != NULL) delete m_SpeexEncode;
if(m_SpeexDecode != NULL) delete m_SpeexDecode;
}
//得到这个声音编码其的ID
LPCSTR CSpeexAudioCodec::GetCodecID()
{
static CString CODEC_ID = "SPEX";
return CODEC_ID;
}
//初始化一个声音的编码器.注意声音的输入dwChannelCount通道个数 dwBits为Sample的大小
BOOL CSpeexAudioCodec::InitialCodec(DWORD dwChannelCount,DWORD dwBits,DWORD dwSampleSize)
{
m_SampleSize = dwSampleSize;
return TRUE;
}
//设置编码后的声音的回调函数
void CSpeexAudioCodec::SetStreamCallBack(IAVStreamCallBack * pStreamCallBack)
{
m_pAVStreamCallback = pStreamCallBack;
}
//开始编码
BOOL CSpeexAudioCodec::Encode(BYTE * lpAudio,DWORD dwDataLength)
{
if(m_SpeexEncode == NULL){
m_SpeexEncode = new CSpeexEncode();
m_SpeexEncode->m_pSpeexCodec = this;
}
m_SpeexEncode->Encode((SHORT *)lpAudio,dwDataLength/2);
return 1;
}
//开始解码
BOOL CSpeexAudioCodec::Decode(BYTE * lpAudio,DWORD dwDataLength)
{
if(m_SpeexDecode == NULL){
m_SpeexDecode = new CSpeexDecode();
m_SpeexDecode->m_pSpeexCodec = this;
}
m_SpeexDecode->Decode((CHAR *)lpAudio,dwDataLength);
return 1;
}
//释放这个编码器
void CSpeexAudioCodec::Release()
{
delete this;
}
#pragma once
#include "../avcodecmanage/iavcodecmanage.h"
#include "SpeexEncode.h"
#include "SpeexDecode.h"
class CSpeexAudioCodec : public IAudioCodec
{
public:
CSpeexAudioCodec(void);
virtual ~CSpeexAudioCodec(void);
public:
IAVStreamCallBack * m_pAVStreamCallback;
CSpeexEncode * m_SpeexEncode;
CSpeexDecode * m_SpeexDecode;
DWORD m_SampleSize;
public:
//得到这个声音编码其的ID
virtual LPCSTR GetCodecID();
//初始化一个声音的编码器.注意声音的输入dwChannelCount通道个数 dwBits为Sample的大小
virtual BOOL InitialCodec(DWORD dwChannelCount,DWORD dwBits,DWORD dwSampleSize = 960);
//设置编码后的声音的回调函数
virtual void SetStreamCallBack(IAVStreamCallBack * pStreamCallBack);
//开始编码
virtual BOOL Encode(BYTE * lpAudio,DWORD dwDataLength);
//开始解码
virtual BOOL Decode(BYTE * lpAudio,DWORD dwDataLength);
//释放这个编码器
virtual void Release();
};
#include "StdAfx.h"
#include ".\speexaudiocodec.h"
extern "C" __declspec(dllexport) IAudioCodec * CreateCodec()
{
CSpeexAudioCodec * pILBC = new CSpeexAudioCodec();
return pILBC;
}
CSpeexAudioCodec::CSpeexAudioCodec(void)
{
m_SpeexEncode = NULL;
m_SpeexDecode = NULL;
m_SampleSize = 960;
}
CSpeexAudioCodec::~CSpeexAudioCodec(void)
{
if(m_SpeexEncode != NULL) delete m_SpeexEncode;
if(m_SpeexDecode != NULL) delete m_SpeexDecode;
}
//得到这个声音编码其的ID
LPCSTR CSpeexAudioCodec::GetCodecID()
{
static CString CODEC_ID = "SPEX";
return CODEC_ID;
}
//初始化一个声音的编码器.注意声音的输入dwChannelCount通道个数 dwBits为Sample的大小
BOOL CSpeexAudioCodec::InitialCodec(DWORD dwChannelCount,DWORD dwBits,DWORD dwSampleSize)
{
m_SampleSize = dwSampleSize;
return TRUE;
}
//设置编码后的声音的回调函数
void CSpeexAudioCodec::SetStreamCallBack(IAVStreamCallBack * pStreamCallBack)
{
m_pAVStreamCallback = pStreamCallBack;
}
//开始编码
BOOL CSpeexAudioCodec::Encode(BYTE * lpAudio,DWORD dwDataLength)
{
if(m_SpeexEncode == NULL){
m_SpeexEncode = new CSpeexEncode();
m_SpeexEncode->m_pSpeexCodec = this;
}
m_SpeexEncode->Encode((SHORT *)lpAudio,dwDataLength/2);
return 1;
}
//开始解码
BOOL CSpeexAudioCodec::Decode(BYTE * lpAudio,DWORD dwDataLength)
{
if(m_SpeexDecode == NULL){
m_SpeexDecode = new CSpeexDecode();
m_SpeexDecode->m_pSpeexCodec = this;
}
m_SpeexDecode->Decode((CHAR *)lpAudio,dwDataLength);
return 1;
}
//释放这个编码器
void CSpeexAudioCodec::Release()
{
delete this;
}
#pragma once #include "speex/speex.h" class CSpeexAudioCodec; class CSpeexEncode { public: CSpeexEncode(void); virtual ~CSpeexEncode(void); public: CSpeexAudioCodec * m_pSpeexCodec; private: SpeexBits m_bits; void * m_EncStat; spx_int32_t m_framesize; spx_int32_t m_BitRates; char * m_audioBuffer ; public: void Encode(short * pSrcData,INT dwLength); };
#include "StdAfx.h"
#include ".\speexencode.h"
#include "SpeexAudioCodec.h"
#include "WStdAfx.h"
#include "mfc_foundation.h"
#define SPEEX_BUFFER_ENCODE_LENGTH 2048
CSpeexEncode::CSpeexEncode(void)
{
m_audioBuffer = (char *)malloc(SPEEX_BUFFER_ENCODE_LENGTH);
WProfile profile((LPCSTR)(GetModuleDirectory()+"\\audio.ini"));
m_BitRates = profile.GetInt("Audio","bitrates");
if(m_BitRates == 0) m_BitRates = 8*1024;
speex_bits_init(&m_bits);
//int iMode = profile.GetInt("Audio","mode");
//if(iMode == 0)
{
m_EncStat = speex_encoder_init(&speex_nb_mode);
}
speex_encoder_ctl(m_EncStat,SPEEX_GET_FRAME_SIZE,&m_framesize);
speex_encoder_ctl(m_EncStat,SPEEX_SET_BITRATE,&m_BitRates);
spx_int32_t iQuality = (spx_int32_t)profile.GetInt("Audio","quality");
if(iQuality == 0) iQuality = 8;
speex_encoder_ctl(m_EncStat,SPEEX_SET_QUALITY,&iQuality);
spx_int32_t rate = 8000;
speex_encoder_ctl(m_EncStat, SPEEX_SET_SAMPLING_RATE, &rate);
}
CSpeexEncode::~CSpeexEncode(void)
{
speex_bits_destroy(&m_bits);
speex_encoder_destroy(m_EncStat);
free(m_audioBuffer);
}
void CSpeexEncode::Encode(short * pSrcData,INT dwLength)
{
int iFrameCount = dwLength/m_framesize;
CHAR * pBuffer = m_audioBuffer;
int iTotalSize = 0;
for(int i=0; i<iFrameCount; i++)
{
speex_bits_reset(&m_bits);
speex_encode_int(m_EncStat, pSrcData, &m_bits);
int nbBytes = speex_bits_write(&m_bits, pBuffer, SPEEX_BUFFER_ENCODE_LENGTH-iTotalSize);
iTotalSize += nbBytes;
pBuffer += nbBytes;
pSrcData += m_framesize;
}
m_pSpeexCodec->m_pAVStreamCallback->OnEncodeOutStream(m_audioBuffer,iTotalSize,'SPEX');
}