speexcodec.cpp 3.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
 *  Copyright (C) 2005 Savoir-Faire Linux inc.
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Jerome Oufella <jerome.oufella@savoirfairelinux.com> 
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "audiocodec.h"
#include <cstdio>
#include <speex/speex.h>

25

26
27
28
class Speex : public AudioCodec{
public:
	Speex(int payload=0)
29
30
31
32
33
34
35
 	: AudioCodec(payload, "speex"),
        _speexModePtr(NULL),
        _speex_dec_bits(),
        _speex_enc_bits(),
        _speex_dec_state(),
        _speex_enc_state(),
        _speex_frame_size()
36
37
38
	{
  	  _clockRate = 8000;
  	  _channel = 1;
39
40
	  _bitrate = 0;
	  _bandwidth = 0; 
41
42
  	  initSpeex();
	}
43
44
45

        Speex( const Speex& );
        Speex& operator=(const Speex&);
46
47
48
49
50
51
52
53
54
55
56
57
58

	void initSpeex() {
	/*
  	  if (_clockRate < 16000 ) {
    		_speexModePtr = &speex_nb_mode;
  	  } else if (_clockRate < 32000) {
    		_speexModePtr = &speex_wb_mode;
  	  } else {
    		_speexModePtr = &speex_uwb_mode;
  	  }
	*/
 	  // 8000 HZ --> Narrow-band mode
 	  // TODO Manage the other modes
59
 	  _speexModePtr = &speex_nb_mode; 
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

	// Init the decoder struct
  	  speex_bits_init(&_speex_dec_bits);
  	  _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() 
	{
  	  terminateSpeex();
	}

	void terminateSpeex() {
	// Destroy the decoder struct
  	  speex_bits_destroy(&_speex_dec_bits);
  	  speex_decoder_destroy(_speex_dec_state);
  	  _speex_dec_state = 0;

	// Destroy the encoder struct
  	  speex_bits_destroy(&_speex_enc_bits);
  	  speex_encoder_destroy(_speex_enc_state);
  	  _speex_enc_state = 0;
	}

	virtual int codecDecode (short *dst, unsigned char *src, unsigned int size) 
	{
    	  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; 
	}

	virtual int codecEncode (unsigned char *dst, short *src, unsigned int size) 
	{
  	  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);
  	  int nbBytes = speex_bits_write(&_speex_enc_bits, (char*)dst, size); 
  	  return nbBytes;
	}

private:
	const SpeexMode* _speexModePtr;
  	SpeexBits  _speex_dec_bits;
  	SpeexBits  _speex_enc_bits;
  	void *_speex_dec_state;
  	void *_speex_enc_state;
  	int _speex_frame_size;
};

// the class factories
extern "C" AudioCodec* create() {
    return new Speex(110);
}

extern "C" void destroy(AudioCodec* a) {
    delete a;
}