[speex-dev] C++ wrapper for speex

Tom Grandgent tgrand at canvaslink.com
Tue May 25 07:44:37 PDT 2004


I recently wrote some wrapper classes for the Speex encoder and decoder 
for use with my VoIP program.  I think they're pretty high level and 
easy to use.  They handle all buffer allocation internally to make life 
easy.  Here's a simple little example that happens to use VAD:

// buf is float[] or short int[], buflen is multiple of frame size
// (there is a getFrameSize() method for both classes)

// Encode some audio
tgAudioEncoder enc(16000);  // you could do 8000 instead
enc.encode(buf, buflen);
unsigned char *compressed = enc.getOutput();
int clen = enc.getOutputLen();
int speechDetected = enc.getSpeechDetected();

// Decode it
tgAudioDecoder dec(16000);
int decodedSamples = dec.decode(compressed, clen);
memcpy(buf, dec.getOutput(), decodedSamples * sizeof(short int));

Anyway, that's the basic idea.  I don't think it could get much 
easier. :)  Now, this is only raw encoding and decoding of the audio 
and doesn't have any kind of container support (like OGG), so you 
can't use it with speexenc/speexdec for example.  Also, there is 
error checking which I have omitted from the example above.

You may use the code however you want.  I only ask that you notify 
me of any problems you find with it.  You can download it at:


(This name only reflects the purpose of the classes in my project.)

Oh and by the way, should you wish to compile this, you will need 
a 1.1.x version of Speex since it relies on the Speex preprocessor.  
And you will also have to remove the getAGCLevel() method or 
implement SPEEX_PREPROCESS_GET_AGC_GAIN, something I added to 
Speex for my own use.

Good luck,


Ronald Madrid (mfcguru at yahoo.com) wrote:
> hi sirs,
> i am only recently studying speex and i'm looking for
> a C++ wrapper for speex that has higer-level
> implementations for the Encode and Decode functions,
> to sort of base my initial understaning of how the
> speex API work.
> i have in fact created my own, following on the
> instructions in the speex codec manual, but i havn't
> really made any positive progress. (a lot of people
> have already done it and i supposed i could do it
> also, but i'm really confused now with all my failed
> attempts.)
> i'm particularly interested with real-time audio and i
> have an event handler that captures audio samples like
> the ff:
> void CRecordSound::OnSoundData(WPARAM wParam, LPARAM
> lParam)
> {
> 	LPWAVEHDR lpHdr = (LPWAVEHDR) lParam;
> 	if(lpHdr)
> 	{					
> 		WAVEHDR* pSpeexHdr = new WAVEHDR;
> 		BYTE * pSound = new BYTE[lpHdr->dwBufferLength];			
> 		memcpy(pSound,lpHdr->lpData,lpHdr->dwBufferLength);
> 		pSpeexHdr->lpData = (char*)pSound; 
> 		pSpeexHdr->dwBufferLength = lpHdr->dwBufferLength; 
> 		pSpeexHdr->dwFlags = 0L; 
> 		pSpeexHdr->dwLoops = 0L;
> 		m_Player->PostThreadMessage(WM_PLAYSOUND_PLAYBLOCK,
> GetCurrentThreadId(),(LPARAM) lpHdr);					
> 	}
> }
> this handler would simply capture the voice data and
> immidiately play it as it is, and as far as that is
> concern, it is working.
> my speex case study is simple. i just want to encode
> the raw data recieved by the handler and immidiately
> decode it before posting WM_PLAYSOUND_PLAYBLOCK, and
> to supply the decoded data as appropriate.
> i would sincerely appreciate any offer of help,
> advices, some real source code, or whatever graces
> that would help me advance away from this ignorance. 
> thank you to all of you and for the wonderful codec
> that is speex.
> ronald c. madrid

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'speex-dev-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.

More information about the Speex-dev mailing list