~ubuntu-branches/ubuntu/trusty/gnuradio/trusty

« back to all changes in this revision

Viewing changes to .pc/update-codec2/gr-vocoder/lib/codec2/codec2.c

  • Committer: Package Import Robot
  • Author(s): A. Maitland Bottoms
  • Date: 2014-01-05 12:14:43 UTC
  • mfrom: (2.1.23 sid)
  • Revision ID: package-import@ubuntu.com-20140105121443-je18dkof6uhox808
Tags: 3.7.2.1-5
use correct compiler for unstable build

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*---------------------------------------------------------------------------*\
2
 
 
3
 
  FILE........: codec2.c
4
 
  AUTHOR......: David Rowe
5
 
  DATE CREATED: 21/8/2010
6
 
 
7
 
  Codec2 fully quantised encoder and decoder functions.  If you want use
8
 
  codec2, the codec2_xxx functions are for you.
9
 
 
10
 
\*---------------------------------------------------------------------------*/
11
 
 
12
 
/*
13
 
  Copyright (C) 2010 David Rowe
14
 
 
15
 
  All rights reserved.
16
 
 
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.
24
 
 
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/>.
27
 
*/
28
 
 
29
 
#include <assert.h>
30
 
#include <stdio.h>
31
 
#include <stdlib.h>
32
 
#include <string.h>
33
 
#include <math.h>
34
 
 
35
 
#include "defines.h"
36
 
#include "sine.h"
37
 
#include "nlp.h"
38
 
#include "dump.h"
39
 
#include "lpc.h"
40
 
#include "quantise.h"
41
 
#include "phase.h"
42
 
#include "interp.h"
43
 
#include "postfilter.h"
44
 
#include "codec2.h"
45
 
#include "codec2_internal.h"
46
 
 
47
 
/*---------------------------------------------------------------------------*\
48
 
 
49
 
                                FUNCTIONS
50
 
 
51
 
\*---------------------------------------------------------------------------*/
52
 
 
53
 
/*---------------------------------------------------------------------------*\
54
 
 
55
 
  FUNCTION....: codec2_create
56
 
  AUTHOR......: David Rowe
57
 
  DATE CREATED: 21/8/2010
58
 
 
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.
64
 
 
65
 
\*---------------------------------------------------------------------------*/
66
 
 
67
 
void *codec2_create()
68
 
{
69
 
    CODEC2 *c2;
70
 
    int     i,l;
71
 
 
72
 
    c2 = (CODEC2*)malloc(sizeof(CODEC2));
73
 
    if (c2 == NULL)
74
 
        return NULL;
75
 
 
76
 
    for(i=0; i<M; i++)
77
 
        c2->Sn[i] = 1.0;
78
 
    c2->hpf_states[0] = c2->hpf_states[1] = 0.0;
79
 
    for(i=0; i<2*N; i++)
80
 
        c2->Sn_[i] = 0;
81
 
    make_analysis_window(c2->w,c2->W);
82
 
    make_synthesis_window(c2->Pn);
83
 
    quantise_init();
84
 
    c2->prev_Wo = 0.0;
85
 
    c2->bg_est = 0.0;
86
 
    c2->ex_phase = 0.0;
87
 
 
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;
93
 
 
94
 
    for(i=0; i<LPC_ORD; i++) {
95
 
      c2->prev_lsps[i] = i*PI/(LPC_ORD+1);
96
 
    }
97
 
    c2->prev_energy = 1;
98
 
 
99
 
    c2->nlp = nlp_create();
100
 
    if (c2->nlp == NULL) {
101
 
        free (c2);
102
 
        return NULL;
103
 
    }
104
 
 
105
 
    return (void*)c2;
106
 
}
107
 
 
108
 
/*---------------------------------------------------------------------------*\
109
 
 
110
 
  FUNCTION....: codec2_create
111
 
  AUTHOR......: David Rowe
112
 
  DATE CREATED: 21/8/2010
113
 
 
114
 
  Destroy an instance of the codec.
115
 
 
116
 
\*---------------------------------------------------------------------------*/
117
 
 
118
 
void codec2_destroy(void *codec2_state)
119
 
{
120
 
    CODEC2 *c2;
121
 
 
122
 
    assert(codec2_state != NULL);
123
 
    c2 = (CODEC2*)codec2_state;
124
 
    nlp_destroy(c2->nlp);
125
 
    free(codec2_state);
126
 
}
127
 
 
128
 
/*---------------------------------------------------------------------------*\
129
 
 
130
 
  FUNCTION....: codec2_encode
131
 
  AUTHOR......: David Rowe
132
 
  DATE CREATED: 21/8/2010
133
 
 
134
 
  Encodes 160 speech samples (20ms of speech) into 51 bits.
135
 
 
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.
140
 
 
141
 
  The bit allocation is:
142
 
 
143
 
    Parameter                      bits/frame
144
 
    --------------------------------------
145
 
    Harmonic magnitudes (LSPs)     36
146
 
    Low frequency LPC correction    1
147
 
    Energy                          5
148
 
    Wo (fundamental frequnecy)      7
149
 
    Voicing (10ms update)           2
150
 
    TOTAL                          51
151
 
 
152
 
\*---------------------------------------------------------------------------*/
153
 
 
154
 
void codec2_encode(void *codec2_state, unsigned char * bits, short speech[])
155
 
{
156
 
    CODEC2 *c2;
157
 
    MODEL   model;
158
 
    int     voiced1, voiced2;
159
 
    int     lsp_indexes[LPC_ORD];
160
 
    int     energy_index;
161
 
    int     Wo_index;
162
 
    int     i;
163
 
    unsigned int nbit = 0;
164
 
 
165
 
    assert(codec2_state != NULL);
166
 
    c2 = (CODEC2*)codec2_state;
167
 
 
168
 
    /* first 10ms analysis frame - we just want voicing */
169
 
 
170
 
    analyse_one_frame(c2, &model, speech);
171
 
    voiced1 = model.voiced;
172
 
 
173
 
    /* second 10ms analysis frame */
174
 
 
175
 
    analyse_one_frame(c2, &model, &speech[N]);
176
 
    voiced2 = model.voiced;
177
 
 
178
 
    Wo_index = encode_Wo(model.Wo);
179
 
    encode_amplitudes(lsp_indexes,
180
 
                      &energy_index,
181
 
                      &model,
182
 
                       c2->Sn,
183
 
                       c2->w);
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));
188
 
    }
189
 
    pack(bits, &nbit, energy_index, E_BITS);
190
 
    pack(bits, &nbit, voiced1, 1);
191
 
    pack(bits, &nbit, voiced2, 1);
192
 
 
193
 
    assert(nbit == CODEC2_BITS_PER_FRAME);
194
 
}
195
 
 
196
 
/*---------------------------------------------------------------------------*\
197
 
 
198
 
  FUNCTION....: codec2_decode
199
 
  AUTHOR......: David Rowe
200
 
  DATE CREATED: 21/8/2010
201
 
 
202
 
  Decodes frames of 51 bits into 160 samples (20ms) of speech.
203
 
 
204
 
\*---------------------------------------------------------------------------*/
205
 
 
206
 
void codec2_decode(void *codec2_state, short speech[],
207
 
                   const unsigned char * bits)
208
 
{
209
 
    CODEC2 *c2;
210
 
    MODEL   model;
211
 
    int     voiced1, voiced2;
212
 
    int     lsp_indexes[LPC_ORD];
213
 
    float   lsps[LPC_ORD];
214
 
    int     energy_index;
215
 
    float   energy;
216
 
    int     Wo_index;
217
 
    float   ak[LPC_ORD+1];
218
 
    float   ak_interp[LPC_ORD+1];
219
 
    int     i;
220
 
    unsigned int nbit = 0;
221
 
    MODEL   model_interp;
222
 
 
223
 
    assert(codec2_state != NULL);
224
 
    c2 = (CODEC2*)codec2_state;
225
 
 
226
 
    /* unpack bit stream to integer codes */
227
 
 
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));
231
 
    }
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);
236
 
 
237
 
    /* decode integer codes to model parameters */
238
 
 
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,
243
 
                      ak,
244
 
                      lsp_indexes,
245
 
                      energy_index,
246
 
                      lsps,
247
 
                      &energy);
248
 
 
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]));
253
 
 
254
 
    /* interpolate middle frame's model parameters for adjacent frames */
255
 
 
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);
259
 
 
260
 
    /* synthesis two 10ms frames */
261
 
 
262
 
    synthesise_one_frame(c2, speech, &model_interp, ak_interp);
263
 
    synthesise_one_frame(c2, &speech[N], &model, ak);
264
 
 
265
 
    /* update memories (decode states) for next time */
266
 
 
267
 
    memcpy(&c2->prev_model, &model, sizeof(MODEL));
268
 
    memcpy(c2->prev_lsps, lsps, sizeof(lsps));
269
 
    c2->prev_energy = energy;
270
 
}
271
 
 
272
 
/*---------------------------------------------------------------------------*\
273
 
 
274
 
  FUNCTION....: synthesise_one_frame()
275
 
  AUTHOR......: David Rowe
276
 
  DATE CREATED: 23/8/2010
277
 
 
278
 
  Synthesise 80 speech samples (10ms) from model parameters.
279
 
 
280
 
\*---------------------------------------------------------------------------*/
281
 
 
282
 
void synthesise_one_frame(CODEC2 *c2, short speech[], MODEL *model, float ak[])
283
 
{
284
 
    int     i;
285
 
 
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);
289
 
 
290
 
    for(i=0; i<N; i++) {
291
 
        if (c2->Sn_[i] > 32767.0)
292
 
            speech[i] = 32767;
293
 
        else if (c2->Sn_[i] < -32767.0)
294
 
            speech[i] = -32767;
295
 
        else
296
 
            speech[i] = c2->Sn_[i];
297
 
    }
298
 
 
299
 
}
300
 
 
301
 
/*---------------------------------------------------------------------------*\
302
 
 
303
 
  FUNCTION....: analyse_one_frame()
304
 
  AUTHOR......: David Rowe
305
 
  DATE CREATED: 23/8/2010
306
 
 
307
 
  Extract sinusoidal model parameters from 80 speech samples (10ms of
308
 
  speech).
309
 
 
310
 
\*---------------------------------------------------------------------------*/
311
 
 
312
 
void analyse_one_frame(CODEC2 *c2, MODEL *model, short speech[])
313
 
{
314
 
    COMP    Sw[FFT_ENC];
315
 
    COMP    Sw_[FFT_ENC];
316
 
    COMP    Ew[FFT_ENC];
317
 
    float   pitch;
318
 
    int     i;
319
 
 
320
 
    /* Read input speech */
321
 
 
322
 
    for(i=0; i<M-N; i++)
323
 
      c2->Sn[i] = c2->Sn[i+N];
324
 
    for(i=0; i<N; i++)
325
 
      c2->Sn[i+M-N] = speech[i];
326
 
 
327
 
    dft_speech(Sw, c2->Sn, c2->w);
328
 
 
329
 
    /* Estimate pitch */
330
 
 
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;
334
 
 
335
 
    /* estimate model parameters */
336
 
 
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);
340
 
 
341
 
    c2->prev_Wo = model->Wo;
342
 
}