From e979dd755a05288c548b5921e623b4317928ffc9 Mon Sep 17 00:00:00 2001 From: Alexandre Savard <alexandre.savard@savoirfairelinux.net> Date: Thu, 12 Feb 2009 10:21:51 -0500 Subject: [PATCH] libspeexdsp added --- src/audio/codecs/Makefile.am | 2 +- src/audio/codecs/alaw.cpp | 2 ++ src/audio/codecs/gsmcodec.cpp | 3 ++ src/audio/codecs/speexcodec.cpp | 53 ++++++++++++++++++++++++++------- src/audio/codecs/ulaw.cpp | 4 ++- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/audio/codecs/Makefile.am b/src/audio/codecs/Makefile.am index 0f790f478a..6f8b16559c 100644 --- a/src/audio/codecs/Makefile.am +++ b/src/audio/codecs/Makefile.am @@ -14,7 +14,7 @@ SPEEX_LIB = libcodec_speex.so libcodec_speex_so_SOURCES = speexcodec.cpp libcodec_speex_so_CFLAGS = -fPIC -g -Wall libcodec_speex_so_CXXFLAGS = -fPIC -g -Wall -libcodec_speex_so_LDFLAGS = --shared -lc -lspeex +libcodec_speex_so_LDFLAGS = --shared -lc -lspeex -lspeexdsp -lm INSTALL_SPEEX_RULE = install-libcodec_speex_so endif diff --git a/src/audio/codecs/alaw.cpp b/src/audio/codecs/alaw.cpp index d8b69d0359..807d518bab 100644 --- a/src/audio/codecs/alaw.cpp +++ b/src/audio/codecs/alaw.cpp @@ -36,6 +36,7 @@ public: virtual ~Alaw(){} virtual int codecDecode (short *dst, unsigned char *src, unsigned int size) { + // _debug("Decoded by alaw \n"); int16* end = dst+size; while(dst<end) *dst++ = ALawDecode(*src++); @@ -44,6 +45,7 @@ public: virtual int codecEncode (unsigned char *dst, short *src, unsigned int size) { + // _debug("Encoded by alaw \n"); size >>= 1; uint8* end = dst+size; while(dst<end) diff --git a/src/audio/codecs/gsmcodec.cpp b/src/audio/codecs/gsmcodec.cpp index 1ab85f2388..896b3a0009 100644 --- a/src/audio/codecs/gsmcodec.cpp +++ b/src/audio/codecs/gsmcodec.cpp @@ -52,6 +52,7 @@ public: } virtual int codecDecode (short * dst, unsigned char * src, unsigned int size){ + // _debug("Decoded by gsm \n"); (void)size; if(gsm_decode(_decode_gsmhandle, (gsm_byte*)src, (gsm_signal*)dst) < 0) printf("ERROR: gsm_decode\n"); @@ -59,6 +60,8 @@ public: } virtual int codecEncode (unsigned char * dst, short * src, unsigned int size){ + + // _debug("Encoded by gsm \n"); (void)size; gsm_encode(_encode_gsmhandle, (gsm_signal*)src, (gsm_byte*) dst); return 33; diff --git a/src/audio/codecs/speexcodec.cpp b/src/audio/codecs/speexcodec.cpp index f89d73b938..1400e0c206 100644 --- a/src/audio/codecs/speexcodec.cpp +++ b/src/audio/codecs/speexcodec.cpp @@ -21,6 +21,7 @@ #include "audiocodec.h" #include <cstdio> #include <speex/speex.h> +#include <speex/speex_preprocess.h> class Speex : public AudioCodec{ @@ -32,7 +33,8 @@ public: _speex_enc_bits(), _speex_dec_state(), _speex_enc_state(), - _speex_frame_size() + _speex_frame_size(), + _preprocess_state() { _clockRate = 8000; _channel = 1; @@ -44,7 +46,23 @@ public: Speex( const Speex& ); Speex& operator=(const Speex&); - void initSpeex() { + void initSpeex() { + int temp = 1; + int temp10 = 10; + int db = -10; + + int *enable; + enable = &temp; + + int *quality; + quality = &temp10; + + int *complex; + complex = &temp10; + + int *attenuation; + attenuation = &db; + /* if (_clockRate < 16000 ) { _speexModePtr = &speex_nb_mode; @@ -60,14 +78,27 @@ public: // Init the decoder struct speex_bits_init(&_speex_dec_bits); - _speex_dec_state = speex_decoder_init(_speexModePtr); - - // Init the encoder struct + _speex_dec_state = speex_decoder_init(_speexModePtr); + + // Init the encoder struct speex_bits_init(&_speex_enc_bits); _speex_enc_state = speex_encoder_init(_speexModePtr); - - speex_decoder_ctl(_speex_dec_state, SPEEX_GET_FRAME_SIZE, &_speex_frame_size); - } + speex_encoder_ctl(_speex_enc_state, SPEEX_SET_VAD, enable); + speex_encoder_ctl(_speex_enc_state, SPEEX_SET_DTX, enable); + speex_encoder_ctl(_speex_enc_state, SPEEX_SET_VBR_QUALITY, quality); + speex_encoder_ctl(_speex_enc_state, SPEEX_SET_COMPLEXITY, complex); + + // Init the decoder struct + speex_decoder_ctl(_speex_dec_state, SPEEX_GET_FRAME_SIZE, &_speex_frame_size); + + // Init the preprocess struct + _preprocess_state = speex_preprocess_state_init(_speex_frame_size,_clockRate); + speex_preprocess_ctl(_preprocess_state, SPEEX_PREPROCESS_SET_DENOISE, enable); + speex_preprocess_ctl(_preprocess_state, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, attenuation); + speex_preprocess_ctl(_preprocess_state, SPEEX_PREPROCESS_SET_VAD, enable); + speex_preprocess_ctl(_preprocess_state, SPEEX_PREPROCESS_SET_AGC, enable); + + } ~Speex() { @@ -91,6 +122,7 @@ public: int ratio = 320 / _speex_frame_size; speex_bits_read_from(&_speex_dec_bits, (char*)src, size); speex_decode_int(_speex_dec_state, &_speex_dec_bits, dst); + return _speex_frame_size * ratio; } @@ -98,8 +130,8 @@ public: { speex_bits_reset(&_speex_enc_bits); speex_encoder_ctl(_speex_enc_state,SPEEX_SET_SAMPLING_RATE,&_clockRate); - - speex_encode_int(_speex_enc_state, src, &_speex_enc_bits); + speex_preprocess_run(_preprocess_state, src); + speex_encode_int(_speex_enc_state, src, &_speex_enc_bits); int nbBytes = speex_bits_write(&_speex_enc_bits, (char*)dst, size); return nbBytes; } @@ -111,6 +143,7 @@ private: void *_speex_dec_state; void *_speex_enc_state; int _speex_frame_size; + SpeexPreprocessState *_preprocess_state; }; // the class factories diff --git a/src/audio/codecs/ulaw.cpp b/src/audio/codecs/ulaw.cpp index 2a53c81a04..6d44bff13c 100644 --- a/src/audio/codecs/ulaw.cpp +++ b/src/audio/codecs/ulaw.cpp @@ -36,6 +36,7 @@ public: } virtual int codecDecode (short *dst, unsigned char *src, unsigned int size) { + // _debug("Decoded by ulaw"); int16* end = dst+size; while(dst<end) *dst++ = ULawDecode(*src++); @@ -43,7 +44,8 @@ public: } virtual int codecEncode (unsigned char *dst, short *src, unsigned int size) { - size >>= 1; + // _debug("Encoded by ulaw \n"); + size >>= 1; uint8* end = dst+size; while(dst<end) *dst++ = ULawEncode(*src++); -- GitLab