hy,<br>recording and playback without speex is working perfectly<br clear="all"><br><br>Lp, Tim<br>+--------------------------+<br>| email: <a href="mailto:rico@gama.us">rico@gama.us</a> |<br>| www: <a href="http://gama.us">http://gama.us</a> |<br>
|--------------------------|<br>| tel: 00386 31 457 627 |<br>+--------------------------+<br><br>
<br><br><div class="gmail_quote">2009/9/2 Tim Rijavec <span dir="ltr"><<a href="mailto:rico@gama.us">rico@gama.us</a>></span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
hy,<br><br>here is my speex encoder/decoder .. the sampleRate i use is 16000 and quality,complexity are at 5.<br><br>can someone take a look in to the code and see if there is something that is making that robot voice <br>
<br>here is a link to colored and numbered code, same as below<br><a href="http://barvanjekode.gama.us/temp/1257361243.html" target="_blank">http://barvanjekode.gama.us/temp/1257361243.html</a><br><br>thanx.<br><br>code<br>
-----------------------<br>
#include "codec.h"<br><br>/////////////////////////////////////////////////////////////////<br>// Constants<br>/////////////////////////////////////////////////////////////////<br>#define Cvoicecodecrawbuffersize 16 * Ckilobyte<br>
<br>/////////////////////////////////////////////////////////////////<br>//<br>// TVoiceCodec<br>//<br>/////////////////////////////////////////////////////////////////<br>TVoiceCodec::TVoiceCodec() :<br>pMode( NULL ),<br>
pPreprocessorState( NULL ),<br>pCodecEncoderState( NULL ),<br>encoderQuality( 5 ),<br>encoderComplexity( 3 ),<br>encoderFrameSize( 0 ),<br>encodedFrameBytes( 0 ),<br>pCodecDecoderState( NULL ),<br>sampleRate( VOICE_SAMPLE_RATE ),<br>
decoderFrameSize( 0 )<br>{<br> /////////////////////////////////////////////////////////////////<br> // Prepare buffers<br> /////////////////////////////////////////////////////////////////<br> uint voicememsize = Cvoicecodecrawbuffersize + CODEC_MAX_BUFFER_SIZE;<br>
byte *voicemem = (byte*)pMemoryMng->Alloc(voicememsize);<br> memset( voicemem, 0, voicememsize );<br> <br> // raw voice input buffer<br> pCodecRawBuffer = voicemem;<br> pCodecRawBufferCur = pCodecRawBuffer;<br>
//<br> voicemem += Cvoicecodecrawbuffersize;<br> <br> // input/output voice buffer<br> pCodecBuffer = voicemem;<br>}<br><br><br>TVoiceCodec::~TVoiceCodec()<br>{<br> if ( pCodecEncoderState )<br> {<br> speex_preprocess_state_destroy( pPreprocessorState );<br>
speex_encoder_destroy( pCodecEncoderState );<br> speex_bits_destroy( &codecBits );<br> pCodecEncoderState = NULL;<br> }<br><br> if ( pCodecDecoderState )<br> {<br> speex_decoder_destroy( pCodecDecoderState );<br>
speex_bits_destroy( &codecBits );<br> pCodecDecoderState = NULL;<br> }<br>}<br><br><br>void TVoiceCodec::setSampleRate( int samplerate )<br>{<br> sampleRate = samplerate;<br><br> // determine the mode<br>
int modeID = -1;<br><br> // speex has limits [6000 - 48000]<br> if ( sampleRate > 48000 )<br> return;<br><br> if ( sampleRate > 25000 )<br> modeID = SPEEX_MODEID_UWB;<br> else if ( sampleRate > 12500 )<br>
modeID = SPEEX_MODEID_WB;<br> else if ( sampleRate >= 6000 )<br> modeID = SPEEX_MODEID_NB;<br> else<br> return;<br><br> // set up mode<br> pMode = speex_lib_get_mode( modeID );<br>}<br><br>void TVoiceCodec::setupEncoder( uint quality, uint complexity )<br>
{<br> encoderQuality = quality;<br> encoderComplexity = complexity;<br><br> // catch invalid sample rates resulting in invalid mode<br> if ( !pMode )<br> return;<br> //<br> assert( ( pCodecEncoderState == NULL ) && "encoder already initialized" );<br>
//<br> int enabled = 1;<br> int disabled = 0;<br> //<br> float highpass = 80;<br><br> // setup encoder<br> pCodecEncoderState = speex_encoder_init( pMode );<br> //<br> speex_encoder_ctl( pCodecEncoderState, SPEEX_SET_SAMPLING_RATE, &sampleRate );<br>
speex_encoder_ctl( pCodecEncoderState, SPEEX_SET_COMPLEXITY, &encoderComplexity );<br> speex_encoder_ctl( pCodecEncoderState, SPEEX_SET_QUALITY, &encoderQuality );<br> speex_encoder_ctl( pCodecEncoderState, SPEEX_SET_VBR, &disabled );<br>
speex_encoder_ctl( pCodecEncoderState, SPEEX_SET_DTX, &disabled );<br><br> // init encoder<br> speex_bits_init( &codecBits );<br> speex_bits_reset( &codecBits );<br><br> // determine encoded bytes per frame with spx_int16_t type, speex can work with float;s, but not in this engine<br>
speex_encode_int( pCodecEncoderState, (spx_int16_t*)pCodecBuffer, &codecBits );<br> encodedFrameBytes = (uint)speex_bits_nbytes( &codecBits );<br><br> // setup preprocessor<br> speex_encoder_ctl( pCodecEncoderState, SPEEX_GET_FRAME_SIZE, &encoderFrameSize );<br>
//<br> pPreprocessorState = speex_preprocess_state_init( encoderFrameSize, sampleRate );<br> //<br> speex_preprocess_ctl( pPreprocessorState, SPEEX_PREPROCESS_SET_DENOISE, &enabled );<br> speex_preprocess_ctl( pPreprocessorState, SPEEX_PREPROCESS_SET_VAD, &disabled );<br>
}<br><br>void TVoiceCodec::setupDecoder()<br>{<br> // catch invalid sample rates resulting in invalid mode<br> if ( !pMode )<br> return;<br> //<br> assert( ( pCodecDecoderState == NULL ) && "decoder already initialized" );<br>
//<br> int enabled = 1;<br> //<br> pCodecDecoderState = speex_decoder_init( pMode );<br> speex_decoder_ctl( pCodecDecoderState, SPEEX_SET_SAMPLING_RATE, &sampleRate );<br> speex_decoder_ctl( pCodecDecoderState, SPEEX_GET_FRAME_SIZE, &decoderFrameSize );<br>
speex_decoder_ctl( pCodecDecoderState, SPEEX_SET_ENH, &enabled );<br> //<br> speex_bits_init( &codecBits );<br>}<br><br>uint TVoiceCodec::encode( byte *rawsoundbuffer, uint length, char *bitbuffer, float gain )<br>
{<br> if( !pCodecEncoderState )<br> return 1;<br><br> // copy raw voice data to temporary codec raw voice data buffer<br> memcpy( pCodecRawBufferCur, rawsoundbuffer, length );<br> pCodecRawBufferCur += length;<br>
<br> // determine the size of data<br> uint buffersize = (int)( pCodecRawBufferCur - pCodecRawBuffer );<br><br> // do we have enough data for encoding?<br> if ( buffersize < encoderFrameSize )<br> {<br> //pLog->Add("codec: encoder: return: buffersize: %d: needtoencode %d", buffersize, encoderFrameSize);<br>
return 0;<br> }<br><br> int encodedbytes = 0;<br> int pos = 0;<br> // while we have enough data<br> while( (uint)( pCodecRawBufferCur - pCodecRawBuffer ) >= encoderFrameSize )<br> {<br> // copy data from temporary buffer to codec buffer<br>
memcpy( pCodecBuffer, pCodecRawBuffer, encoderFrameSize );<br><br> // move memory of temporary buffer<br> memmove( pCodecRawBuffer, pCodecRawBuffer + encoderFrameSize, buffersize - ( pos + encoderFrameSize ) );<br>
pCodecRawBufferCur -= encoderFrameSize;<br><br> // increase current pos<br> pos += encoderFrameSize;<br><br> //encode<br> speex_bits_reset( &codecBits );<br> //<br> speex_encode_int( pCodecEncoderState, (spx_int16_t*)pCodecBuffer, &codecBits );<br>
//<br> int nb = speex_bits_nbytes( &codecBits );<br> encodedbytes += speex_bits_write( &codecBits, &bitbuffer[encodedbytes], nb );<br> }<br><br> //pLog->Add("buffersize: %d: total left: %d enc buffersize: %d", length, inputBufferArray.Count(), encodedbytes );<br>
return encodedbytes;<br>}<br><br>uint TVoiceCodec::decode( char *bitbuffer, uint length, byte *samplearray, float gain )<br>{<br> // return if decoder is not initialized<br> if( !pCodecDecoderState )<br> return 0;<br>
<br> // return if there is not enough data to decode<br> if ( length < encodedFrameBytes )<br> return 0;<br><br> int buffersize = length;<br> uint decodedbytes = 0;<br> int pos = 0;<br>
//<br> while( (int)buffersize - (int)encodedFrameBytes >= 0 )<br> { <br> speex_bits_reset( &codecBits );<br> speex_bits_read_from( &codecBits, bitbuffer + pos, encodedFrameBytes );<br> //<br>
if( speex_decode_int( pCodecDecoderState, &codecBits, (spx_int16_t*)pCodecBuffer ) == 0 )<br> {<br> memcpy( &samplearray[decodedbytes], pCodecBuffer, decoderFrameSize );<br> decodedbytes += decoderFrameSize;<br>
}<br> //<br> pos += encodedFrameBytes;<br> buffersize -= encodedFrameBytes;<br> }<br><br> return decodedbytes;<br>}<br><br>
-----------------------<br>code<br><br clear="all">Lp, Tim<br>+--------------------------+<br>| email: <a href="mailto:rico@gama.us" target="_blank">rico@gama.us</a> |<br>| www: <a href="http://gama.us" target="_blank">http://gama.us</a> |<br>
|--------------------------|<br>| tel: 00386 31 457 627 |<br>+--------------------------+<br><br>
</blockquote></div><br>