1
/*---------------------------------------------------------------------------*\
4
AUTHOR......: David Rowe
5
DATE CREATED: 21/8/2010
7
Codec2 fully quantised encoder and decoder functions. If you want use
8
codec2, the codec2_xxx functions are for you.
10
\*---------------------------------------------------------------------------*/
13
Copyright (C) 2010 David Rowe
17
This program is free software; you can redistribute it and/or modify
18
it under the terms of the GNU Lesser General Public License version 2.1, as
19
published by the Free Software Foundation. This program is
20
distributed in the hope that it will be useful, but WITHOUT ANY
21
WARRANTY; without even the implied warranty of MERCHANTABILITY or
22
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
23
License for more details.
25
You should have received a copy of the GNU Lesser General Public License
26
along with this program; if not, see <http://www.gnu.org/licenses/>.
43
#include "postfilter.h"
45
#include "codec2_internal.h"
47
/*---------------------------------------------------------------------------*\
51
\*---------------------------------------------------------------------------*/
53
/*---------------------------------------------------------------------------*\
55
FUNCTION....: codec2_create
56
AUTHOR......: David Rowe
57
DATE CREATED: 21/8/2010
59
Create and initialise an instance of the codec. Returns a pointer
60
to the codec states or NULL on failure. One set of states is
61
sufficient for a full duuplex codec (i.e. an encoder and decoder).
62
You don't need separate states for encoders and decoders. See
63
c2enc.c and c2dec.c for examples.
65
\*---------------------------------------------------------------------------*/
72
c2 = (CODEC2*)malloc(sizeof(CODEC2));
78
c2->hpf_states[0] = c2->hpf_states[1] = 0.0;
81
make_analysis_window(c2->w,c2->W);
82
make_synthesis_window(c2->Pn);
88
for(l=1; l<MAX_AMP; l++)
89
c2->prev_model.A[l] = 0.0;
90
c2->prev_model.Wo = TWO_PI/P_MAX;
91
c2->prev_model.L = PI/c2->prev_model.Wo;
92
c2->prev_model.voiced = 0;
94
for(i=0; i<LPC_ORD; i++) {
95
c2->prev_lsps[i] = i*PI/(LPC_ORD+1);
99
c2->nlp = nlp_create();
100
if (c2->nlp == NULL) {
108
/*---------------------------------------------------------------------------*\
110
FUNCTION....: codec2_create
111
AUTHOR......: David Rowe
112
DATE CREATED: 21/8/2010
114
Destroy an instance of the codec.
116
\*---------------------------------------------------------------------------*/
118
void codec2_destroy(void *codec2_state)
122
assert(codec2_state != NULL);
123
c2 = (CODEC2*)codec2_state;
124
nlp_destroy(c2->nlp);
128
/*---------------------------------------------------------------------------*\
130
FUNCTION....: codec2_encode
131
AUTHOR......: David Rowe
132
DATE CREATED: 21/8/2010
134
Encodes 160 speech samples (20ms of speech) into 51 bits.
136
The codec2 algorithm actually operates internally on 10ms (80
137
sample) frames, so we run the encoding algorithm twice. On the
138
first frame we just send the voicing bit. One the second frame we
139
send all model parameters.
141
The bit allocation is:
144
--------------------------------------
145
Harmonic magnitudes (LSPs) 36
146
Low frequency LPC correction 1
148
Wo (fundamental frequnecy) 7
149
Voicing (10ms update) 2
152
\*---------------------------------------------------------------------------*/
154
void codec2_encode(void *codec2_state, unsigned char * bits, short speech[])
158
int voiced1, voiced2;
159
int lsp_indexes[LPC_ORD];
163
unsigned int nbit = 0;
165
assert(codec2_state != NULL);
166
c2 = (CODEC2*)codec2_state;
168
/* first 10ms analysis frame - we just want voicing */
170
analyse_one_frame(c2, &model, speech);
171
voiced1 = model.voiced;
173
/* second 10ms analysis frame */
175
analyse_one_frame(c2, &model, &speech[N]);
176
voiced2 = model.voiced;
178
Wo_index = encode_Wo(model.Wo);
179
encode_amplitudes(lsp_indexes,
184
memset(bits, '\0', ((CODEC2_BITS_PER_FRAME + 7) / 8));
185
pack(bits, &nbit, Wo_index, WO_BITS);
186
for(i=0; i<LPC_ORD; i++) {
187
pack(bits, &nbit, lsp_indexes[i], lsp_bits(i));
189
pack(bits, &nbit, energy_index, E_BITS);
190
pack(bits, &nbit, voiced1, 1);
191
pack(bits, &nbit, voiced2, 1);
193
assert(nbit == CODEC2_BITS_PER_FRAME);
196
/*---------------------------------------------------------------------------*\
198
FUNCTION....: codec2_decode
199
AUTHOR......: David Rowe
200
DATE CREATED: 21/8/2010
202
Decodes frames of 51 bits into 160 samples (20ms) of speech.
204
\*---------------------------------------------------------------------------*/
206
void codec2_decode(void *codec2_state, short speech[],
207
const unsigned char * bits)
211
int voiced1, voiced2;
212
int lsp_indexes[LPC_ORD];
218
float ak_interp[LPC_ORD+1];
220
unsigned int nbit = 0;
223
assert(codec2_state != NULL);
224
c2 = (CODEC2*)codec2_state;
226
/* unpack bit stream to integer codes */
228
Wo_index = unpack(bits, &nbit, WO_BITS);
229
for(i=0; i<LPC_ORD; i++) {
230
lsp_indexes[i] = unpack(bits, &nbit, lsp_bits(i));
232
energy_index = unpack(bits, &nbit, E_BITS);
233
voiced1 = unpack(bits, &nbit, 1);
234
voiced2 = unpack(bits, &nbit, 1);
235
assert(nbit == CODEC2_BITS_PER_FRAME);
237
/* decode integer codes to model parameters */
239
model.Wo = decode_Wo(Wo_index);
240
model.L = PI/model.Wo;
241
memset(&model.A, 0, (model.L+1)*sizeof(model.A[0]));
242
decode_amplitudes(&model,
249
model.voiced = voiced2;
250
model_interp.voiced = voiced1;
251
model_interp.Wo = P_MAX/2;
252
memset(&model_interp.A, 0, MAX_AMP*sizeof(model_interp.A[0]));
254
/* interpolate middle frame's model parameters for adjacent frames */
256
interpolate_lsp(&model_interp, &c2->prev_model, &model,
257
c2->prev_lsps, c2->prev_energy, lsps, energy, ak_interp);
258
apply_lpc_correction(&model_interp);
260
/* synthesis two 10ms frames */
262
synthesise_one_frame(c2, speech, &model_interp, ak_interp);
263
synthesise_one_frame(c2, &speech[N], &model, ak);
265
/* update memories (decode states) for next time */
267
memcpy(&c2->prev_model, &model, sizeof(MODEL));
268
memcpy(c2->prev_lsps, lsps, sizeof(lsps));
269
c2->prev_energy = energy;
272
/*---------------------------------------------------------------------------*\
274
FUNCTION....: synthesise_one_frame()
275
AUTHOR......: David Rowe
276
DATE CREATED: 23/8/2010
278
Synthesise 80 speech samples (10ms) from model parameters.
280
\*---------------------------------------------------------------------------*/
282
void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model, float ak[])
286
phase_synth_zero_order(model, ak, &c2->ex_phase, LPC_ORD);
287
postfilter(model, &c2->bg_est);
288
synthesise(c2->Sn_, model, c2->Pn, 1);
291
if (c2->Sn_[i] > 32767.0)
293
else if (c2->Sn_[i] < -32767.0)
296
speech[i] = c2->Sn_[i];
301
/*---------------------------------------------------------------------------*\
303
FUNCTION....: analyse_one_frame()
304
AUTHOR......: David Rowe
305
DATE CREATED: 23/8/2010
307
Extract sinusoidal model parameters from 80 speech samples (10ms of
310
\*---------------------------------------------------------------------------*/
312
void analyse_one_frame(CODEC2 *c2, MODEL *model, short speech[])
320
/* Read input speech */
323
c2->Sn[i] = c2->Sn[i+N];
325
c2->Sn[i+M-N] = speech[i];
327
dft_speech(Sw, c2->Sn, c2->w);
331
nlp(c2->nlp,c2->Sn,N,M,P_MIN,P_MAX,&pitch,Sw,&c2->prev_Wo);
332
model->Wo = TWO_PI/pitch;
333
model->L = PI/model->Wo;
335
/* estimate model parameters */
337
two_stage_pitch_refinement(model, Sw);
338
estimate_amplitudes(model, Sw, c2->W);
339
est_voicing_mbe(model, Sw, c2->W, Sw_, Ew, c2->prev_Wo);
341
c2->prev_Wo = model->Wo;