[Speex-dev] Fw: problem in encoder/decoder
Eid
eidmahmoud at gmail.com
Sun Oct 9 05:21:46 PDT 2005
-------Original Message-------
From: Eid
Date: 10/09/05 14:07:44
To: speex-dev at xiph.org
Subject: problem in encoder/decoder
hi all,
I'm developing an app to broadcast voice over web. i tried to use speex and
download ver 1.04 documentation and use it to develop encoding and decoding
but after decoding voice is damaged i attach my encode/decode function.
BOOL EncodeSpeex(CVoiceMsg *pMsg)
{
SpeexBits bits;
void* enc_state;
// Initialize.
speex_bits_init(&bits);
enc_state = speex_encoder_init(&speex_nb_mode);
int frame_size = 0;
// get frame size.
speex_encoder_ctl(enc_state,SPEEX_GET_FRAME_SIZE,&frame_size);
int nbBytes = 0;
//unsigned char *input_frame,*encodedstr;
unsigned char *encodedstr;
float* input_frame;//approx of frame size
input_frame = new float[frame_size];
encodedstr = new unsigned char[MAX_FRAME_BYTES];
ZeroMemory(input_frame,frame_size);
ZeroMemory(encodedstr,MAX_FRAME_BYTES);
int pos = 0;
unsigned char*result;
result = new unsigned char[pMsg->m_nBufferSize];
ZeroMemory(result,pMsg->m_nBufferSize);
int encoded_bytes =0;
int complexity=4;
int bitrate =3;
speex_encoder_ctl(enc_state, SPEEX_SET_COMPLEXITY, &complexity);
//speex_encoder_ctl(enc_state, SPEEX_SET_SAMPLING_RATE, &rate);
speex_encoder_ctl(enc_state, SPEEX_SET_BITRATE, &bitrate);
int tmp=1;
// speex_encoder_ctl(enc_state, SPEEX_SET_VAD, &tmp);
while(pos<pMsg->m_nBufferSize && encoded_bytes<pMsg->m_nBufferSize)
{
if((pMsg->m_nBufferSize-pos)>frame_size)
{
CopyMemory(input_frame,pMsg->m_pBuffer+pos,frame_size);
pos += frame_size;
}
else
{
CopyMemory(input_frame,pMsg->m_pBuffer+pos,pMsg->m_nBufferSize-pos);
pos+=(pMsg->m_nBufferSize-pos);
}
//encode input frame
speex_bits_reset(&bits);
speex_encode(enc_state,input_frame,&bits);
nbBytes = speex_bits_write(&bits,(char*)encodedstr,MAX_FRAME_BYTES);
unsigned char lenbytes[1];
lenbytes[0]=nbBytes;
CopyMemory(result+encoded_bytes ,lenbytes,1);
encoded_bytes++;
CopyMemory(result+encoded_bytes ,encodedstr,nbBytes);
encoded_bytes+= nbBytes;
}
// save again to message buffer
if(pMsg->m_pBuffer != NULL)
{
delete pMsg->m_pBuffer;
pMsg->m_pBuffer = NULL;
}
pMsg->m_pBuffer = new UCHAR[encoded_bytes];
CopyMemory(pMsg->m_pBuffer,result,encoded_bytes);
pMsg->m_nBufferSize = encoded_bytes;
// destroy and free resources...
speex_bits_destroy(&bits);
speex_encoder_destroy(enc_state);
delete[] result;
delete[] input_frame ;
delete[] encodedstr;
result = NULL;
input_frame = NULL;
encodedstr = NULL;
return TRUE;
}
where pMsg->m_pBuffer is an UCHAR buffer containing recorded speech. and at
the end of encode it contains the encoded bytes.
BOOL DecodeSpeex(CVoiceMsg *pMsg)
{
SpeexBits bits;
void* dec_state;
// Initialize.
speex_bits_init(&bits);
dec_state = speex_decoder_init(&speex_nb_mode);
int frame_size = 0;
// get frame size.
speex_decoder_ctl(dec_state,SPEEX_GET_FRAME_SIZE,&frame_size);
int nbBytes = 0;
unsigned char *input_frame;
short *output_frame;
input_frame = new unsigned char[frame_size];
int enh = 1;
speex_decoder_ctl(dec_state,SPEEX_SET_ENH,&enh);
unsigned char*result;
int scale=25;
int size= max(pMsg->m_nBufferSize*scale
frame_size*scale*(pMsg->m_nBufferSize/frame_size)) + frame_size*scale;
result = new unsigned char[size];
ZeroMemory(result,size);
output_frame = new short[frame_size];
ZeroMemory(output_frame,frame_size);
float* out_float;
out_float = new float[frame_size];
ZeroMemory(out_float,frame_size);
int encoded_bytes =0;
int pos=0;
while(pos<pMsg->m_nBufferSize && encoded_bytes<size)
{
nbBytes = pMsg->m_pBuffer[pos];
if(nbBytes>frame_size)
nbBytes = frame_size;
if((pMsg->m_nBufferSize-pos)<nbBytes)
nbBytes = pMsg->m_nBufferSize-pos;
pos++;// skip lenght byte.
CopyMemory(input_frame,pMsg->m_pBuffer+pos,nbBytes);
//encode input frame.
speex_bits_read_from(&bits,(char*)input_frame,nbBytes);
speex_decode(dec_state,&bits,out_float);
//speex_decode_int(dec_state,&bits,output_frame);
//output_frame =(short*) out_float;
for(int i=0;i<frame_size;i++)
output_frame[i] = out_float[i];
pos += nbBytes;
CopyMemory(result+encoded_bytes,output_frame,frame_size);
encoded_bytes+= frame_size;
}
speex_bits_destroy(&bits);
speex_decoder_destroy(dec_state);
// save again to message buffer
if(pMsg->m_pBuffer != NULL)
{
delete pMsg->m_pBuffer;
pMsg->m_pBuffer = NULL;
}
pMsg->m_pBuffer = new UCHAR[encoded_bytes];
CopyMemory(pMsg->m_pBuffer,result,encoded_bytes);
pMsg->m_nBufferSize = encoded_bytes;
pMsg->m_nSampleSize = pMsg->m_nBufferSize / 2;//wfx2.nBlockAlign;
delete[] result;
delete[] output_frame ;
delete[] input_frame;
result = NULL;
input_frame = NULL;
output_frame = NULL;
return TRUE;
}
More information about the Speex-dev
mailing list