-
Notifications
You must be signed in to change notification settings - Fork 7
/
VoiceCodec_Speex.cpp
129 lines (106 loc) · 3.53 KB
/
VoiceCodec_Speex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
125
126
127
128
129
#include "VoiceCodec_Speex.h"
#define ARRAYSIZE(p) (sizeof(p)/sizeof(p[0]))
const size_t VoiceCodec_Speex::ENCODED_FRAMESIZE[] = {6, 6, 15, 15, 20, 20, 28, 28, 38, 38, 38};
VoiceCodec_Speex::VoiceCodec_Speex(size_t quality) {
m_encoder = speex_encoder_init(&speex_nb_mode);
m_decoder = speex_decoder_init(&speex_nb_mode);
speex_bits_init(&m_bits);
// Transformation
quality = (quality - 1) * 2;
m_encodedBytes = ENCODED_FRAMESIZE[quality];
speex_encoder_ctl(m_encoder, SPEEX_SET_QUALITY, &quality);
bool postFilter = true; // Set the perceptual enhancement on
speex_decoder_ctl(m_decoder, SPEEX_SET_ENH, &postFilter);
size_t sampleRate = SAMPLERATE;
speex_encoder_ctl(m_encoder, SPEEX_SET_SAMPLING_RATE, &sampleRate);
speex_decoder_ctl(m_decoder, SPEEX_SET_SAMPLING_RATE, &sampleRate);
}
VoiceCodec_Speex::~VoiceCodec_Speex() {
speex_encoder_destroy(m_encoder);
speex_encoder_destroy(m_decoder);
speex_bits_destroy(&m_bits);
}
// We need reset on new client connected?
void VoiceCodec_Speex::ChangeQuality(size_t quality) {
// Transformation
quality = (quality - 1) * 2;
m_encodedBytes = ENCODED_FRAMESIZE[quality];
speex_encoder_ctl(m_encoder, SPEEX_SET_QUALITY, &quality);
speex_encoder_ctl(m_encoder, SPEEX_RESET_STATE, nullptr);
}
void VoiceCodec_Speex::ResetState() {
speex_encoder_ctl(m_encoder, SPEEX_RESET_STATE, nullptr);
speex_decoder_ctl(m_decoder, SPEEX_RESET_STATE, nullptr);
}
size_t VoiceCodec_Speex::Encode(const int16_t *rawSamples, size_t rawSampleCount, uint8_t *encodedBytes, size_t maxEncodedBytes) {
if (rawSamples == nullptr) {
return 0;
}
if (rawSampleCount == 0) {
return 0;
}
// TODO: add kick message for this
if (rawSampleCount % FRAMESIZE != 0) {
return 0;
}
if (encodedBytes == nullptr) {
return 0;
}
if (maxEncodedBytes == 0) {
return 0;
}
size_t encodedBytesCount = 0;
size_t curRawSample = 0;
float rawFrame[FRAMESIZE];
while (rawSampleCount != 0) {
// TODO: add save for unencoded samples
if (encodedBytesCount + m_encodedBytes > maxEncodedBytes) {
return 0;
}
for (size_t i = 0; i < ARRAYSIZE(rawFrame); i++) {
rawFrame[i] = rawSamples[curRawSample];
curRawSample++;
rawSampleCount--;
}
speex_bits_reset(&m_bits);
speex_encode(m_encoder, rawFrame, &m_bits);
encodedBytesCount += speex_bits_write(&m_bits, (char *)&encodedBytes[encodedBytesCount], m_encodedBytes);
}
return encodedBytesCount;
}
size_t VoiceCodec_Speex::Decode(const uint8_t *encodedBytes, size_t encodedBytesCount, int16_t *rawSamples, size_t maxRawSamples) {
if (encodedBytes == nullptr) {
return 0;
}
if (encodedBytesCount == 0) {
return 0;
}
// TODO: add kick message for this
if (encodedBytesCount % m_encodedBytes != 0) {
return 0;
}
if (rawSamples == nullptr) {
return 0;
}
if (maxRawSamples == 0) {
return 0;
}
size_t decodedRawSamples = 0;
size_t curEncodedBytePos = 0;
float rawFrame[FRAMESIZE];
while (encodedBytesCount != 0) {
// TODO: add save? or kick?
if (decodedRawSamples + ARRAYSIZE(rawFrame)*sizeof(int16_t) > maxRawSamples) {
return 0;
}
// Need speex_bits_reset?
speex_bits_read_from(&m_bits, (char *)&encodedBytes[curEncodedBytePos], m_encodedBytes);
speex_decode(m_decoder, &m_bits, rawFrame);
for (size_t i = 0; i < ARRAYSIZE(rawFrame); i++) {
rawSamples[decodedRawSamples++] = (int16_t)rawFrame[i];
}
encodedBytesCount -= m_encodedBytes;
curEncodedBytePos += m_encodedBytes;
}
return decodedRawSamples;
}