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 <stdbool.h>
33 #include <string.h>
34 #include <math.h>
35 
36 #include "defines.h"
37 #include "codec2_fft.h"
38 #include "sine.h"
39 #include "nlp.h"
40 #include "dump.h"
41 #include "lpc.h"
42 #include "quantise.h"
43 #include "phase.h"
44 #include "interp.h"
45 #include "postfilter.h"
46 #include "codec2.h"
47 #include "lsp.h"
48 #include "newamp2.h"
49 #include "codec2_internal.h"
50 #include "machdep.h"
51 #include "bpf.h"
52 #include "bpfb.h"
53 #include "c2wideband.h"
54 
55 #include "debug_alloc.h"
56 
57 /*---------------------------------------------------------------------------* \
58 
59                              FUNCTION HEADERS
60 
61 \*---------------------------------------------------------------------------*/
62 
63 void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[]);
64 void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model,
65 			  COMP Aw[], float gain);
66 void codec2_encode_3200(struct CODEC2 *c2, unsigned char * bits, short speech[]);
67 void codec2_decode_3200(struct CODEC2 *c2, short speech[], const unsigned char * bits);
68 void codec2_encode_2400(struct CODEC2 *c2, unsigned char * bits, short speech[]);
69 void codec2_decode_2400(struct CODEC2 *c2, short speech[], const unsigned char * bits);
70 void codec2_encode_1600(struct CODEC2 *c2, unsigned char * bits, short speech[]);
71 void codec2_decode_1600(struct CODEC2 *c2, short speech[], const unsigned char * bits);
72 void codec2_encode_1400(struct CODEC2 *c2, unsigned char * bits, short speech[]);
73 void codec2_decode_1400(struct CODEC2 *c2, short speech[], const unsigned char * bits);
74 void codec2_encode_1300(struct CODEC2 *c2, unsigned char * bits, short speech[]);
75 void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits, float ber_est);
76 void codec2_encode_1200(struct CODEC2 *c2, unsigned char * bits, short speech[]);
77 void codec2_decode_1200(struct CODEC2 *c2, short speech[], const unsigned char * bits);
78 void codec2_encode_700c(struct CODEC2 *c2, unsigned char * bits, short speech[]);
79 void codec2_decode_700c(struct CODEC2 *c2, short speech[], const unsigned char * bits);
80 void codec2_encode_450(struct CODEC2 *c2, unsigned char * bits, short speech[]);
81 void codec2_decode_450(struct CODEC2 *c2, short speech[], const unsigned char * bits);
82 void codec2_decode_450pwb(struct CODEC2 *c2, short speech[], const unsigned char * bits);
83 static void ear_protection(float in_out[], int n);
84 
85 
86 
87 /*---------------------------------------------------------------------------*\
88 
89                                 FUNCTIONS
90 
91 \*---------------------------------------------------------------------------*/
92 
93 /*---------------------------------------------------------------------------*\
94 
95   FUNCTION....: codec2_create
96   AUTHOR......: David Rowe
97   DATE CREATED: 21/8/2010
98 
99   Create and initialise an instance of the codec.  Returns a pointer
100   to the codec states or NULL on failure.  One set of states is
101   sufficient for a full duuplex codec (i.e. an encoder and decoder).
102   You don't need separate states for encoders and decoders.  See
103   c2enc.c and c2dec.c for examples.
104 
105 \*---------------------------------------------------------------------------*/
106 
107 
108 //Don't create CODEC2_MODE_450PWB for Encoding as it has undefined behavior !
codec2_create(int mode)109 struct CODEC2 * codec2_create(int mode)
110 {
111     struct CODEC2 *c2;
112     int            i,l;
113 
114     // ALL POSSIBLE MODES MUST BE CHECKED HERE!
115     // we test if the desired mode is enabled at compile time
116     // and return NULL if not
117 
118     if (false == ( CODEC2_MODE_ACTIVE(CODEC2_MODE_3200, mode)
119 		   || CODEC2_MODE_ACTIVE(CODEC2_MODE_2400, mode)
120 		   || CODEC2_MODE_ACTIVE(CODEC2_MODE_1600, mode)
121 		   || CODEC2_MODE_ACTIVE(CODEC2_MODE_1400, mode)
122 		   || CODEC2_MODE_ACTIVE(CODEC2_MODE_1300, mode)
123 		   || CODEC2_MODE_ACTIVE(CODEC2_MODE_1200, mode)
124 		   || CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, mode)
125 		   || CODEC2_MODE_ACTIVE(CODEC2_MODE_450, mode)
126 		   || CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, mode)
127 		) )
128     {
129         return NULL;
130     }
131 
132     c2 = (struct CODEC2*)MALLOC(sizeof(struct CODEC2));
133     if (c2 == NULL)
134 	return NULL;
135 
136     c2->mode = mode;
137 
138     /* store constants in a few places for convenience */
139 
140     if (CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, mode) == 0) {
141         c2->c2const = c2const_create(8000, N_S);
142     }else{
143         c2->c2const = c2const_create(16000, N_S);
144     }
145     c2->Fs = c2->c2const.Fs;
146     int n_samp = c2->n_samp = c2->c2const.n_samp;
147     int m_pitch = c2->m_pitch = c2->c2const.m_pitch;
148 
149     c2->Pn = (float*)MALLOC(2*n_samp*sizeof(float));
150     if (c2->Pn == NULL) {
151 	return NULL;
152     }
153     c2->Sn_ = (float*)MALLOC(2*n_samp*sizeof(float));
154     if (c2->Sn_ == NULL) {
155         FREE(c2->Pn);
156 	return NULL;
157     }
158     c2->w = (float*)MALLOC(m_pitch*sizeof(float));
159     if (c2->w == NULL) {
160         FREE(c2->Pn);
161         FREE(c2->Sn_);
162 	return NULL;
163     }
164     c2->Sn = (float*)MALLOC(m_pitch*sizeof(float));
165     if (c2->Sn == NULL) {
166         FREE(c2->Pn);
167         FREE(c2->Sn_);
168         FREE(c2->w);
169 	return NULL;
170     }
171 
172     for(i=0; i<m_pitch; i++)
173 	c2->Sn[i] = 1.0;
174     c2->hpf_states[0] = c2->hpf_states[1] = 0.0;
175     for(i=0; i<2*n_samp; i++)
176 	c2->Sn_[i] = 0;
177     c2->fft_fwd_cfg = codec2_fft_alloc(FFT_ENC, 0, NULL, NULL);
178     c2->fftr_fwd_cfg = codec2_fftr_alloc(FFT_ENC, 0, NULL, NULL);
179     make_analysis_window(&c2->c2const, c2->fft_fwd_cfg, c2->w,c2->W);
180     make_synthesis_window(&c2->c2const, c2->Pn);
181     c2->fftr_inv_cfg = codec2_fftr_alloc(FFT_DEC, 1, NULL, NULL);
182     c2->prev_f0_enc = 1/P_MAX_S;
183     c2->bg_est = 0.0;
184     c2->ex_phase = 0.0;
185 
186     for(l=1; l<=MAX_AMP; l++)
187 	c2->prev_model_dec.A[l] = 0.0;
188     c2->prev_model_dec.Wo = TWO_PI/c2->c2const.p_max;
189     c2->prev_model_dec.L = PI/c2->prev_model_dec.Wo;
190     c2->prev_model_dec.voiced = 0;
191 
192     for(i=0; i<LPC_ORD; i++) {
193       c2->prev_lsps_dec[i] = i*PI/(LPC_ORD+1);
194     }
195     c2->prev_e_dec = 1;
196 
197     c2->nlp = nlp_create(&c2->c2const);
198     if (c2->nlp == NULL) {
199 	return NULL;
200     }
201 
202     c2->lpc_pf = 1; c2->bass_boost = 1; c2->beta = LPCPF_BETA; c2->gamma = LPCPF_GAMMA;
203 
204     c2->xq_enc[0] = c2->xq_enc[1] = 0.0;
205     c2->xq_dec[0] = c2->xq_dec[1] = 0.0;
206 
207     c2->smoothing = 0;
208     c2->se = 0.0; c2->nse = 0;
209     c2->user_rate_K_vec_no_mean_ = NULL;
210     c2->post_filter_en = 1;
211 
212     c2->bpf_buf = (float*)MALLOC(sizeof(float)*(BPF_N+4*c2->n_samp));
213     assert(c2->bpf_buf != NULL);
214     for(i=0; i<BPF_N+4*c2->n_samp; i++)
215         c2->bpf_buf[i] = 0.0;
216 
217     c2->softdec = NULL;
218     c2->gray = 1;
219 
220     /* newamp1 initialisation */
221 
222     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, c2->mode)) {
223         mel_sample_freqs_kHz(c2->rate_K_sample_freqs_kHz, NEWAMP1_K, ftomel(200.0), ftomel(3700.0) );
224         int k;
225         for(k=0; k<NEWAMP1_K; k++) {
226             c2->prev_rate_K_vec_[k] = 0.0;
227             c2->eq[k] = 0.0;
228         }
229         c2->eq_en = 0;
230         c2->Wo_left = 0.0;
231         c2->voicing_left = 0;;
232         c2->phase_fft_fwd_cfg = codec2_fft_alloc(NEWAMP1_PHASE_NFFT, 0, NULL, NULL);
233         c2->phase_fft_inv_cfg = codec2_fft_alloc(NEWAMP1_PHASE_NFFT, 1, NULL, NULL);
234     }
235 
236     /* newamp2 initialisation */
237 
238     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450, c2->mode)) {
239         n2_mel_sample_freqs_kHz(c2->n2_rate_K_sample_freqs_kHz, NEWAMP2_K);
240         int k;
241         for(k=0; k<NEWAMP2_K; k++) {
242             c2->n2_prev_rate_K_vec_[k] = 0.0;
243         }
244         c2->Wo_left = 0.0;
245         c2->voicing_left = 0;;
246         c2->phase_fft_fwd_cfg = codec2_fft_alloc(NEWAMP2_PHASE_NFFT, 0, NULL, NULL);
247         c2->phase_fft_inv_cfg = codec2_fft_alloc(NEWAMP2_PHASE_NFFT, 1, NULL, NULL);
248     }
249     /* newamp2 PWB initialisation */
250 
251     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, c2->mode)) {
252         n2_mel_sample_freqs_kHz(c2->n2_pwb_rate_K_sample_freqs_kHz, NEWAMP2_16K_K);
253         int k;
254         for(k=0; k<NEWAMP2_16K_K; k++) {
255             c2->n2_pwb_prev_rate_K_vec_[k] = 0.0;
256         }
257         c2->Wo_left = 0.0;
258         c2->voicing_left = 0;;
259         c2->phase_fft_fwd_cfg = codec2_fft_alloc(NEWAMP2_PHASE_NFFT, 0, NULL, NULL);
260         c2->phase_fft_inv_cfg = codec2_fft_alloc(NEWAMP2_PHASE_NFFT, 1, NULL, NULL);
261     }
262 
263     c2->fmlfeat = NULL; c2->fmlmodel = NULL;
264 
265     // make sure that one of the two decode function pointers is empty
266     // for the encode function pointer this is not required since we always set it
267     // to a meaningful value
268 
269     c2->decode = NULL;
270     c2->decode_ber = NULL;
271 
272     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_3200, c2->mode))
273     {
274 	c2->encode = codec2_encode_3200;
275 	c2->decode = codec2_decode_3200;
276     }
277 
278     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_2400, c2->mode))
279     {
280 	c2->encode = codec2_encode_2400;
281 	c2->decode = codec2_decode_2400;
282     }
283 
284     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1600, c2->mode))
285     {
286 	c2->encode = codec2_encode_1600;
287 	c2->decode = codec2_decode_1600;
288     }
289 
290     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1400, c2->mode))
291     {
292 	c2->encode = codec2_encode_1400;
293 	c2->decode = codec2_decode_1400;
294     }
295 
296     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1300, c2->mode))
297     {
298 	c2->encode = codec2_encode_1300;
299 	c2->decode_ber = codec2_decode_1300;
300     }
301 
302     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1200, c2->mode))
303     {
304 	c2->encode = codec2_encode_1200;
305 	c2->decode = codec2_decode_1200;
306     }
307 
308     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, c2->mode))
309     {
310 	c2->encode = codec2_encode_700c;
311 	c2->decode = codec2_decode_700c;
312     }
313 
314     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450, c2->mode))
315     {
316 	c2->encode = codec2_encode_450;
317 	c2->decode = codec2_decode_450;
318     }
319 
320     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, c2->mode))
321     {
322     	//Encode PWB doesnt make sense
323 	c2->encode = codec2_encode_450;
324 	c2->decode = codec2_decode_450pwb;
325     }
326 
327 
328     return c2;
329 }
330 
331 /*---------------------------------------------------------------------------*\
332 
333   FUNCTION....: codec2_destroy
334   AUTHOR......: David Rowe
335   DATE CREATED: 21/8/2010
336 
337   Destroy an instance of the codec.
338 
339 \*---------------------------------------------------------------------------*/
340 
codec2_destroy(struct CODEC2 * c2)341 void codec2_destroy(struct CODEC2 *c2)
342 {
343     assert(c2 != NULL);
344     FREE(c2->bpf_buf);
345     nlp_destroy(c2->nlp);
346     codec2_fft_free(c2->fft_fwd_cfg);
347     codec2_fftr_free(c2->fftr_fwd_cfg);
348     codec2_fftr_free(c2->fftr_inv_cfg);
349     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, c2->mode)) {
350         codec2_fft_free(c2->phase_fft_fwd_cfg);
351         codec2_fft_free(c2->phase_fft_inv_cfg);
352     }
353     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450, c2->mode)) {
354         codec2_fft_free(c2->phase_fft_fwd_cfg);
355         codec2_fft_free(c2->phase_fft_inv_cfg);
356     }
357     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, c2->mode)) {
358         codec2_fft_free(c2->phase_fft_fwd_cfg);
359         codec2_fft_free(c2->phase_fft_inv_cfg);
360     }
361     FREE(c2->Pn);
362     FREE(c2->Sn);
363     FREE(c2->w);
364     FREE(c2->Sn_);
365     FREE(c2);
366 }
367 
368 /*---------------------------------------------------------------------------*\
369 
370   FUNCTION....: codec2_bits_per_frame
371   AUTHOR......: David Rowe
372   DATE CREATED: Nov 14 2011
373 
374   Returns the number of bits per frame.
375 
376 \*---------------------------------------------------------------------------*/
377 
codec2_bits_per_frame(struct CODEC2 * c2)378 int codec2_bits_per_frame(struct CODEC2 *c2) {
379     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_3200, c2->mode))
380 	return 64;
381     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_2400, c2->mode))
382 	return 48;
383     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1600, c2->mode))
384 	return 64;
385     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1400, c2->mode))
386 	return 56;
387     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1300, c2->mode))
388 	return 52;
389     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1200, c2->mode))
390 	return 48;
391     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, c2->mode))
392 	return 28;
393     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450, c2->mode))
394 	return 18;
395     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, c2->mode))
396 	return 18;
397 
398     return 0; /* shouldn't get here */
399 }
400 
401 
402 /*---------------------------------------------------------------------------*\
403 
404   FUNCTION....: codec2_bytes_per_frame
405   DATE CREATED: April 2021
406 
407   Returns the number of bytes per frame.  Useful for allocated storage for
408   codec2_encode()/codec2_decode().  Note the number of bits may not be a
409   multiple of 8, thefore some bits in the last byte may be unused.
410 
411 \*---------------------------------------------------------------------------*/
412 
codec2_bytes_per_frame(struct CODEC2 * c2)413 int codec2_bytes_per_frame(struct CODEC2 *c2) {
414 	return (codec2_bits_per_frame(c2)+7)/8;
415 }
416 
417 
418 /*---------------------------------------------------------------------------*\
419 
420   FUNCTION....: codec2_samples_per_frame
421   AUTHOR......: David Rowe
422   DATE CREATED: Nov 14 2011
423 
424   Returns the number of speech samples per frame.
425 
426 \*---------------------------------------------------------------------------*/
427 
codec2_samples_per_frame(struct CODEC2 * c2)428 int codec2_samples_per_frame(struct CODEC2 *c2) {
429     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_3200, c2->mode))
430 	return 160;
431     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_2400, c2->mode))
432 	return 160;
433     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1600, c2->mode))
434 	return 320;
435     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1400, c2->mode))
436 	return 320;
437     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1300, c2->mode))
438 	return 320;
439     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1200, c2->mode))
440 	return 320;
441     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, c2->mode))
442 	return 320;
443     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450, c2->mode))
444 	return 320;
445     if  ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, c2->mode))
446 	return 640;
447     return 0; /* shouldnt get here */
448 }
449 
450 
451 /*---------------------------------------------------------------------------*\
452 
453   FUNCTION....: codec2_encode
454   AUTHOR......: David Rowe
455   DATE CREATED: Nov 14 2011
456 
457   Take an input buffer of speech samples, and compress them to a packed buffer
458   of bytes.
459 
460 \*---------------------------------------------------------------------------*/
461 
codec2_encode(struct CODEC2 * c2,unsigned char * bytes,short speech[])462 void codec2_encode(struct CODEC2 *c2, unsigned char *bytes, short speech[])
463 {
464     assert(c2 != NULL);
465     assert(c2->encode != NULL);
466 
467     c2->encode(c2, bytes, speech);
468 
469 }
470 
471 /*---------------------------------------------------------------------------*\
472 
473   FUNCTION....: codec2_decode
474   AUTHOR......: David Rowe
475   DATE CREATED: Nov 14 2011
476 
477   Take an input packed buffer of bytes, and decode them to a buffer of speech
478   samples.
479 
480 \*---------------------------------------------------------------------------*/
481 
codec2_decode(struct CODEC2 * c2,short speech[],const unsigned char * bytes)482 void codec2_decode(struct CODEC2 *c2, short speech[], const unsigned char *bytes)
483 {
484     codec2_decode_ber(c2, speech, bytes, 0.0);
485 }
486 
codec2_decode_ber(struct CODEC2 * c2,short speech[],const unsigned char * bits,float ber_est)487 void codec2_decode_ber(struct CODEC2 *c2, short speech[], const unsigned char *bits, float ber_est)
488 {
489     assert(c2 != NULL);
490     assert(c2->decode != NULL || c2->decode_ber != NULL);
491 
492     if (c2->decode != NULL)
493     {
494 	c2->decode(c2, speech, bits);
495     }
496     else
497     {
498 	c2->decode_ber(c2, speech, bits, ber_est);
499     }
500 }
501 
502 
503 /*---------------------------------------------------------------------------*\
504 
505   FUNCTION....: codec2_encode_3200
506   AUTHOR......: David Rowe
507   DATE CREATED: 13 Sep 2012
508 
509   Encodes 160 speech samples (20ms of speech) into 64 bits.
510 
511   The codec2 algorithm actually operates internally on 10ms (80
512   sample) frames, so we run the encoding algorithm twice.  On the
513   first frame we just send the voicing bits.  On the second frame we
514   send all model parameters.  Compared to 2400 we use a larger number
515   of bits for the LSPs and non-VQ pitch and energy.
516 
517   The bit allocation is:
518 
519     Parameter                      bits/frame
520     --------------------------------------
521     Harmonic magnitudes (LSPs)     50
522     Pitch (Wo)                      7
523     Energy                          5
524     Voicing (10ms update)           2
525     TOTAL                          64
526 
527 \*---------------------------------------------------------------------------*/
528 
codec2_encode_3200(struct CODEC2 * c2,unsigned char * bits,short speech[])529 void codec2_encode_3200(struct CODEC2 *c2, unsigned char * bits, short speech[])
530 {
531     MODEL   model;
532     float   ak[LPC_ORD+1];
533     float   lsps[LPC_ORD];
534     float   e;
535     int     Wo_index, e_index;
536     int     lspd_indexes[LPC_ORD];
537     int     i;
538     unsigned int nbit = 0;
539 
540     assert(c2 != NULL);
541 
542     memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8));
543 
544     /* first 10ms analysis frame - we just want voicing */
545 
546     analyse_one_frame(c2, &model, speech);
547     pack(bits, &nbit, model.voiced, 1);
548 
549     /* second 10ms analysis frame */
550 
551     analyse_one_frame(c2, &model, &speech[c2->n_samp]);
552     pack(bits, &nbit, model.voiced, 1);
553     Wo_index = encode_Wo(&c2->c2const, model.Wo, WO_BITS);
554     pack(bits, &nbit, Wo_index, WO_BITS);
555 
556     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
557     e_index = encode_energy(e, E_BITS);
558     pack(bits, &nbit, e_index, E_BITS);
559 
560     encode_lspds_scalar(lspd_indexes, lsps, LPC_ORD);
561     for(i=0; i<LSPD_SCALAR_INDEXES; i++) {
562 	pack(bits, &nbit, lspd_indexes[i], lspd_bits(i));
563     }
564     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
565 }
566 
567 
568 /*---------------------------------------------------------------------------*\
569 
570   FUNCTION....: codec2_decode_3200
571   AUTHOR......: David Rowe
572   DATE CREATED: 13 Sep 2012
573 
574   Decodes a frame of 64 bits into 160 samples (20ms) of speech.
575 
576 \*---------------------------------------------------------------------------*/
577 
codec2_decode_3200(struct CODEC2 * c2,short speech[],const unsigned char * bits)578 void codec2_decode_3200(struct CODEC2 *c2, short speech[], const unsigned char * bits)
579 {
580     MODEL   model[2];
581     int     lspd_indexes[LPC_ORD];
582     float   lsps[2][LPC_ORD];
583     int     Wo_index, e_index;
584     float   e[2];
585     float   snr;
586     float   ak[2][LPC_ORD+1];
587     int     i,j;
588     unsigned int nbit = 0;
589     COMP    Aw[FFT_ENC];
590 
591     assert(c2 != NULL);
592 
593     /* only need to zero these out due to (unused) snr calculation */
594 
595     for(i=0; i<2; i++)
596 	for(j=1; j<=MAX_AMP; j++)
597 	    model[i].A[j] = 0.0;
598 
599     /* unpack bits from channel ------------------------------------*/
600 
601     /* this will partially fill the model params for the 2 x 10ms
602        frames */
603 
604     model[0].voiced = unpack(bits, &nbit, 1);
605     model[1].voiced = unpack(bits, &nbit, 1);
606 
607     Wo_index = unpack(bits, &nbit, WO_BITS);
608     model[1].Wo = decode_Wo(&c2->c2const, Wo_index, WO_BITS);
609     model[1].L  = PI/model[1].Wo;
610 
611     e_index = unpack(bits, &nbit, E_BITS);
612     e[1] = decode_energy(e_index, E_BITS);
613 
614     for(i=0; i<LSPD_SCALAR_INDEXES; i++) {
615 	lspd_indexes[i] = unpack(bits, &nbit, lspd_bits(i));
616     }
617     decode_lspds_scalar(&lsps[1][0], lspd_indexes, LPC_ORD);
618 
619     /* interpolate ------------------------------------------------*/
620 
621     /* Wo and energy are sampled every 20ms, so we interpolate just 1
622        10ms frame between 20ms samples */
623 
624     interp_Wo(&model[0], &c2->prev_model_dec, &model[1], c2->c2const.Wo_min);
625     e[0] = interp_energy(c2->prev_e_dec, e[1]);
626 
627     /* LSPs are sampled every 20ms so we interpolate the frame in
628        between, then recover spectral amplitudes */
629 
630     interpolate_lsp_ver2(&lsps[0][0], c2->prev_lsps_dec, &lsps[1][0], 0.5, LPC_ORD);
631 
632     for(i=0; i<2; i++) {
633 	lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD);
634 	aks_to_M2(c2->fftr_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0,
635                   c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma, Aw);
636 	apply_lpc_correction(&model[i]);
637 	synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], Aw, 1.0);
638     }
639 
640     /* update memories for next frame ----------------------------*/
641 
642     c2->prev_model_dec = model[1];
643     c2->prev_e_dec = e[1];
644     for(i=0; i<LPC_ORD; i++)
645 	c2->prev_lsps_dec[i] = lsps[1][i];
646 }
647 
648 
649 /*---------------------------------------------------------------------------*\
650 
651   FUNCTION....: codec2_encode_2400
652   AUTHOR......: David Rowe
653   DATE CREATED: 21/8/2010
654 
655   Encodes 160 speech samples (20ms of speech) into 48 bits.
656 
657   The codec2 algorithm actually operates internally on 10ms (80
658   sample) frames, so we run the encoding algorithm twice.  On the
659   first frame we just send the voicing bit.  On the second frame we
660   send all model parameters.
661 
662   The bit allocation is:
663 
664     Parameter                      bits/frame
665     --------------------------------------
666     Harmonic magnitudes (LSPs)     36
667     Joint VQ of Energy and Wo       8
668     Voicing (10ms update)           2
669     Spare                           2
670     TOTAL                          48
671 
672 \*---------------------------------------------------------------------------*/
673 
codec2_encode_2400(struct CODEC2 * c2,unsigned char * bits,short speech[])674 void codec2_encode_2400(struct CODEC2 *c2, unsigned char * bits, short speech[])
675 {
676     MODEL   model;
677     float   ak[LPC_ORD+1];
678     float   lsps[LPC_ORD];
679     float   e;
680     int     WoE_index;
681     int     lsp_indexes[LPC_ORD];
682     int     i;
683     int     spare = 0;
684     unsigned int nbit = 0;
685 
686     assert(c2 != NULL);
687 
688     memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8));
689 
690     /* first 10ms analysis frame - we just want voicing */
691 
692     analyse_one_frame(c2, &model, speech);
693     pack(bits, &nbit, model.voiced, 1);
694 
695     /* second 10ms analysis frame */
696 
697     analyse_one_frame(c2, &model, &speech[c2->n_samp]);
698     pack(bits, &nbit, model.voiced, 1);
699 
700     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
701     WoE_index = encode_WoE(&model, e, c2->xq_enc);
702     pack(bits, &nbit, WoE_index, WO_E_BITS);
703 
704     encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
705     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
706 	pack(bits, &nbit, lsp_indexes[i], lsp_bits(i));
707     }
708     pack(bits, &nbit, spare, 2);
709 
710     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
711 }
712 
713 
714 /*---------------------------------------------------------------------------*\
715 
716   FUNCTION....: codec2_decode_2400
717   AUTHOR......: David Rowe
718   DATE CREATED: 21/8/2010
719 
720   Decodes frames of 48 bits into 160 samples (20ms) of speech.
721 
722 \*---------------------------------------------------------------------------*/
723 
codec2_decode_2400(struct CODEC2 * c2,short speech[],const unsigned char * bits)724 void codec2_decode_2400(struct CODEC2 *c2, short speech[], const unsigned char * bits)
725 {
726     MODEL   model[2];
727     int     lsp_indexes[LPC_ORD];
728     float   lsps[2][LPC_ORD];
729     int     WoE_index;
730     float   e[2];
731     float   snr;
732     float   ak[2][LPC_ORD+1];
733     int     i,j;
734     unsigned int nbit = 0;
735     COMP    Aw[FFT_ENC];
736 
737     assert(c2 != NULL);
738 
739     /* only need to zero these out due to (unused) snr calculation */
740 
741     for(i=0; i<2; i++)
742 	for(j=1; j<=MAX_AMP; j++)
743 	    model[i].A[j] = 0.0;
744 
745     /* unpack bits from channel ------------------------------------*/
746 
747     /* this will partially fill the model params for the 2 x 10ms
748        frames */
749 
750     model[0].voiced = unpack(bits, &nbit, 1);
751 
752     model[1].voiced = unpack(bits, &nbit, 1);
753     WoE_index = unpack(bits, &nbit, WO_E_BITS);
754     decode_WoE(&c2->c2const, &model[1], &e[1], c2->xq_dec, WoE_index);
755 
756     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
757 	lsp_indexes[i] = unpack(bits, &nbit, lsp_bits(i));
758     }
759     decode_lsps_scalar(&lsps[1][0], lsp_indexes, LPC_ORD);
760     check_lsp_order(&lsps[1][0], LPC_ORD);
761     bw_expand_lsps(&lsps[1][0], LPC_ORD, 50.0, 100.0);
762 
763     /* interpolate ------------------------------------------------*/
764 
765     /* Wo and energy are sampled every 20ms, so we interpolate just 1
766        10ms frame between 20ms samples */
767 
768     interp_Wo(&model[0], &c2->prev_model_dec, &model[1], c2->c2const.Wo_min);
769     e[0] = interp_energy(c2->prev_e_dec, e[1]);
770 
771     /* LSPs are sampled every 20ms so we interpolate the frame in
772        between, then recover spectral amplitudes */
773 
774     interpolate_lsp_ver2(&lsps[0][0], c2->prev_lsps_dec, &lsps[1][0], 0.5, LPC_ORD);
775     for(i=0; i<2; i++) {
776 	lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD);
777 	aks_to_M2(c2->fftr_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0,
778                   c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma, Aw);
779 	apply_lpc_correction(&model[i]);
780 	synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], Aw, 1.0);
781 
782 	/* dump parameters for deep learning experiments */
783 
784 	if (c2->fmlfeat != NULL) {
785 	    /* 10 LSPs - energy - Wo - voicing flag - 10 LPCs */
786 	    fwrite(&lsps[i][0], LPC_ORD, sizeof(float), c2->fmlfeat);
787 	    fwrite(&e[i], 1, sizeof(float), c2->fmlfeat);
788 	    fwrite(&model[i].Wo, 1, sizeof(float), c2->fmlfeat);
789 	    float voiced_float = model[i].voiced;
790 	    fwrite(&voiced_float, 1, sizeof(float), c2->fmlfeat);
791 	    fwrite(&ak[i][1], LPC_ORD, sizeof(float), c2->fmlfeat);
792 	}
793     }
794 
795     /* update memories for next frame ----------------------------*/
796 
797     c2->prev_model_dec = model[1];
798     c2->prev_e_dec = e[1];
799     for(i=0; i<LPC_ORD; i++)
800 	c2->prev_lsps_dec[i] = lsps[1][i];
801 }
802 
803 
804 /*---------------------------------------------------------------------------*\
805 
806   FUNCTION....: codec2_encode_1600
807   AUTHOR......: David Rowe
808   DATE CREATED: Feb 28 2013
809 
810   Encodes 320 speech samples (40ms of speech) into 64 bits.
811 
812   The codec2 algorithm actually operates internally on 10ms (80
813   sample) frames, so we run the encoding algorithm 4 times:
814 
815   frame 0: voicing bit
816   frame 1: voicing bit, Wo and E
817   frame 2: voicing bit
818   frame 3: voicing bit, Wo and E, scalar LSPs
819 
820   The bit allocation is:
821 
822     Parameter                      frame 2  frame 4   Total
823     -------------------------------------------------------
824     Harmonic magnitudes (LSPs)      0       36        36
825     Pitch (Wo)                      7        7        14
826     Energy                          5        5        10
827     Voicing (10ms update)           2        2         4
828     TOTAL                          14       50        64
829 
830 \*---------------------------------------------------------------------------*/
831 
codec2_encode_1600(struct CODEC2 * c2,unsigned char * bits,short speech[])832 void codec2_encode_1600(struct CODEC2 *c2, unsigned char * bits, short speech[])
833 {
834     MODEL   model;
835     float   lsps[LPC_ORD];
836     float   ak[LPC_ORD+1];
837     float   e;
838     int     lsp_indexes[LPC_ORD];
839     int     Wo_index, e_index;
840     int     i;
841     unsigned int nbit = 0;
842 
843     assert(c2 != NULL);
844 
845     memset(bits, '\0',  ((codec2_bits_per_frame(c2) + 7) / 8));
846 
847     /* frame 1: - voicing ---------------------------------------------*/
848 
849     analyse_one_frame(c2, &model, speech);
850     pack(bits, &nbit, model.voiced, 1);
851 
852     /* frame 2: - voicing, scalar Wo & E -------------------------------*/
853 
854     analyse_one_frame(c2, &model, &speech[c2->n_samp]);
855     pack(bits, &nbit, model.voiced, 1);
856 
857     Wo_index = encode_Wo(&c2->c2const, model.Wo, WO_BITS);
858     pack(bits, &nbit, Wo_index, WO_BITS);
859 
860     /* need to run this just to get LPC energy */
861     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
862     e_index = encode_energy(e, E_BITS);
863     pack(bits, &nbit, e_index, E_BITS);
864 
865     /* frame 3: - voicing ---------------------------------------------*/
866 
867     analyse_one_frame(c2, &model, &speech[2*c2->n_samp]);
868     pack(bits, &nbit, model.voiced, 1);
869 
870     /* frame 4: - voicing, scalar Wo & E, scalar LSPs ------------------*/
871 
872     analyse_one_frame(c2, &model, &speech[3*c2->n_samp]);
873     pack(bits, &nbit, model.voiced, 1);
874 
875     Wo_index = encode_Wo(&c2->c2const, model.Wo, WO_BITS);
876     pack(bits, &nbit, Wo_index, WO_BITS);
877 
878     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
879     e_index = encode_energy(e, E_BITS);
880     pack(bits, &nbit, e_index, E_BITS);
881 
882     encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
883     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
884 	pack(bits, &nbit, lsp_indexes[i], lsp_bits(i));
885     }
886 
887     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
888 }
889 
890 
891 /*---------------------------------------------------------------------------*\
892 
893   FUNCTION....: codec2_decode_1600
894   AUTHOR......: David Rowe
895   DATE CREATED: 11 May 2012
896 
897   Decodes frames of 64 bits into 320 samples (40ms) of speech.
898 
899 \*---------------------------------------------------------------------------*/
900 
codec2_decode_1600(struct CODEC2 * c2,short speech[],const unsigned char * bits)901 void codec2_decode_1600(struct CODEC2 *c2, short speech[], const unsigned char * bits)
902 {
903     MODEL   model[4];
904     int     lsp_indexes[LPC_ORD];
905     float   lsps[4][LPC_ORD];
906     int     Wo_index, e_index;
907     float   e[4];
908     float   snr;
909     float   ak[4][LPC_ORD+1];
910     int     i,j;
911     unsigned int nbit = 0;
912     float   weight;
913     COMP    Aw[FFT_ENC];
914 
915     assert(c2 != NULL);
916 
917     /* only need to zero these out due to (unused) snr calculation */
918 
919     for(i=0; i<4; i++)
920 	for(j=1; j<=MAX_AMP; j++)
921 	    model[i].A[j] = 0.0;
922 
923     /* unpack bits from channel ------------------------------------*/
924 
925     /* this will partially fill the model params for the 4 x 10ms
926        frames */
927 
928     model[0].voiced = unpack(bits, &nbit, 1);
929 
930     model[1].voiced = unpack(bits, &nbit, 1);
931     Wo_index = unpack(bits, &nbit, WO_BITS);
932     model[1].Wo = decode_Wo(&c2->c2const, Wo_index, WO_BITS);
933     model[1].L  = PI/model[1].Wo;
934 
935     e_index = unpack(bits, &nbit, E_BITS);
936     e[1] = decode_energy(e_index, E_BITS);
937 
938     model[2].voiced = unpack(bits, &nbit, 1);
939 
940     model[3].voiced = unpack(bits, &nbit, 1);
941     Wo_index = unpack(bits, &nbit, WO_BITS);
942     model[3].Wo = decode_Wo(&c2->c2const, Wo_index, WO_BITS);
943     model[3].L  = PI/model[3].Wo;
944 
945     e_index = unpack(bits, &nbit, E_BITS);
946     e[3] = decode_energy(e_index, E_BITS);
947 
948     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
949 	lsp_indexes[i] = unpack(bits, &nbit, lsp_bits(i));
950     }
951     decode_lsps_scalar(&lsps[3][0], lsp_indexes, LPC_ORD);
952     check_lsp_order(&lsps[3][0], LPC_ORD);
953     bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0);
954 
955     /* interpolate ------------------------------------------------*/
956 
957     /* Wo and energy are sampled every 20ms, so we interpolate just 1
958        10ms frame between 20ms samples */
959 
960     interp_Wo(&model[0], &c2->prev_model_dec, &model[1], c2->c2const.Wo_min);
961     e[0] = interp_energy(c2->prev_e_dec, e[1]);
962     interp_Wo(&model[2], &model[1], &model[3], c2->c2const.Wo_min);
963     e[2] = interp_energy(e[1], e[3]);
964 
965     /* LSPs are sampled every 40ms so we interpolate the 3 frames in
966        between, then recover spectral amplitudes */
967 
968     for(i=0, weight=0.25; i<3; i++, weight += 0.25) {
969 	interpolate_lsp_ver2(&lsps[i][0], c2->prev_lsps_dec, &lsps[3][0], weight, LPC_ORD);
970     }
971     for(i=0; i<4; i++) {
972 	lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD);
973 	aks_to_M2(c2->fftr_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0,
974                   c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma, Aw);
975 	apply_lpc_correction(&model[i]);
976 	synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], Aw, 1.0);
977     }
978 
979     /* update memories for next frame ----------------------------*/
980 
981     c2->prev_model_dec = model[3];
982     c2->prev_e_dec = e[3];
983     for(i=0; i<LPC_ORD; i++)
984 	c2->prev_lsps_dec[i] = lsps[3][i];
985 
986 }
987 
988 /*---------------------------------------------------------------------------*\
989 
990   FUNCTION....: codec2_encode_1400
991   AUTHOR......: David Rowe
992   DATE CREATED: May 11 2012
993 
994   Encodes 320 speech samples (40ms of speech) into 56 bits.
995 
996   The codec2 algorithm actually operates internally on 10ms (80
997   sample) frames, so we run the encoding algorithm 4 times:
998 
999   frame 0: voicing bit
1000   frame 1: voicing bit, joint VQ of Wo and E
1001   frame 2: voicing bit
1002   frame 3: voicing bit, joint VQ of Wo and E, scalar LSPs
1003 
1004   The bit allocation is:
1005 
1006     Parameter                      frame 2  frame 4   Total
1007     -------------------------------------------------------
1008     Harmonic magnitudes (LSPs)      0       36        36
1009     Energy+Wo                       8        8        16
1010     Voicing (10ms update)           2        2         4
1011     TOTAL                          10       46        56
1012 
1013 \*---------------------------------------------------------------------------*/
1014 
codec2_encode_1400(struct CODEC2 * c2,unsigned char * bits,short speech[])1015 void codec2_encode_1400(struct CODEC2 *c2, unsigned char * bits, short speech[])
1016 {
1017     MODEL   model;
1018     float   lsps[LPC_ORD];
1019     float   ak[LPC_ORD+1];
1020     float   e;
1021     int     lsp_indexes[LPC_ORD];
1022     int     WoE_index;
1023     int     i;
1024     unsigned int nbit = 0;
1025 
1026     assert(c2 != NULL);
1027 
1028     memset(bits, '\0',  ((codec2_bits_per_frame(c2) + 7) / 8));
1029 
1030     /* frame 1: - voicing ---------------------------------------------*/
1031 
1032     analyse_one_frame(c2, &model, speech);
1033     pack(bits, &nbit, model.voiced, 1);
1034 
1035     /* frame 2: - voicing, joint Wo & E -------------------------------*/
1036 
1037     analyse_one_frame(c2, &model, &speech[c2->n_samp]);
1038     pack(bits, &nbit, model.voiced, 1);
1039 
1040     /* need to run this just to get LPC energy */
1041     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
1042 
1043     WoE_index = encode_WoE(&model, e, c2->xq_enc);
1044     pack(bits, &nbit, WoE_index, WO_E_BITS);
1045 
1046     /* frame 3: - voicing ---------------------------------------------*/
1047 
1048     analyse_one_frame(c2, &model, &speech[2*c2->n_samp]);
1049     pack(bits, &nbit, model.voiced, 1);
1050 
1051     /* frame 4: - voicing, joint Wo & E, scalar LSPs ------------------*/
1052 
1053     analyse_one_frame(c2, &model, &speech[3*c2->n_samp]);
1054     pack(bits, &nbit, model.voiced, 1);
1055 
1056     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
1057     WoE_index = encode_WoE(&model, e, c2->xq_enc);
1058     pack(bits, &nbit, WoE_index, WO_E_BITS);
1059 
1060     encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
1061     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
1062 	pack(bits, &nbit, lsp_indexes[i], lsp_bits(i));
1063     }
1064 
1065     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
1066 }
1067 
1068 
1069 /*---------------------------------------------------------------------------*\
1070 
1071   FUNCTION....: codec2_decode_1400
1072   AUTHOR......: David Rowe
1073   DATE CREATED: 11 May 2012
1074 
1075   Decodes frames of 56 bits into 320 samples (40ms) of speech.
1076 
1077 \*---------------------------------------------------------------------------*/
1078 
codec2_decode_1400(struct CODEC2 * c2,short speech[],const unsigned char * bits)1079 void codec2_decode_1400(struct CODEC2 *c2, short speech[], const unsigned char * bits)
1080 {
1081     MODEL   model[4];
1082     int     lsp_indexes[LPC_ORD];
1083     float   lsps[4][LPC_ORD];
1084     int     WoE_index;
1085     float   e[4];
1086     float   snr;
1087     float   ak[4][LPC_ORD+1];
1088     int     i,j;
1089     unsigned int nbit = 0;
1090     float   weight;
1091     COMP    Aw[FFT_ENC];
1092 
1093     assert(c2 != NULL);
1094 
1095     /* only need to zero these out due to (unused) snr calculation */
1096 
1097     for(i=0; i<4; i++)
1098 	for(j=1; j<=MAX_AMP; j++)
1099 	    model[i].A[j] = 0.0;
1100 
1101     /* unpack bits from channel ------------------------------------*/
1102 
1103     /* this will partially fill the model params for the 4 x 10ms
1104        frames */
1105 
1106     model[0].voiced = unpack(bits, &nbit, 1);
1107 
1108     model[1].voiced = unpack(bits, &nbit, 1);
1109     WoE_index = unpack(bits, &nbit, WO_E_BITS);
1110     decode_WoE(&c2->c2const, &model[1], &e[1], c2->xq_dec, WoE_index);
1111 
1112     model[2].voiced = unpack(bits, &nbit, 1);
1113 
1114     model[3].voiced = unpack(bits, &nbit, 1);
1115     WoE_index = unpack(bits, &nbit, WO_E_BITS);
1116     decode_WoE(&c2->c2const, &model[3], &e[3], c2->xq_dec, WoE_index);
1117 
1118     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
1119 	lsp_indexes[i] = unpack(bits, &nbit, lsp_bits(i));
1120     }
1121     decode_lsps_scalar(&lsps[3][0], lsp_indexes, LPC_ORD);
1122     check_lsp_order(&lsps[3][0], LPC_ORD);
1123     bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0);
1124 
1125     /* interpolate ------------------------------------------------*/
1126 
1127     /* Wo and energy are sampled every 20ms, so we interpolate just 1
1128        10ms frame between 20ms samples */
1129 
1130     interp_Wo(&model[0], &c2->prev_model_dec, &model[1], c2->c2const.Wo_min);
1131     e[0] = interp_energy(c2->prev_e_dec, e[1]);
1132     interp_Wo(&model[2], &model[1], &model[3], c2->c2const.Wo_min);
1133     e[2] = interp_energy(e[1], e[3]);
1134 
1135     /* LSPs are sampled every 40ms so we interpolate the 3 frames in
1136        between, then recover spectral amplitudes */
1137 
1138     for(i=0, weight=0.25; i<3; i++, weight += 0.25) {
1139 	interpolate_lsp_ver2(&lsps[i][0], c2->prev_lsps_dec, &lsps[3][0], weight, LPC_ORD);
1140     }
1141     for(i=0; i<4; i++) {
1142 	lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD);
1143 	aks_to_M2(c2->fftr_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0,
1144                   c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma, Aw);
1145 	apply_lpc_correction(&model[i]);
1146 	synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], Aw, 1.0);
1147     }
1148 
1149     /* update memories for next frame ----------------------------*/
1150 
1151     c2->prev_model_dec = model[3];
1152     c2->prev_e_dec = e[3];
1153     for(i=0; i<LPC_ORD; i++)
1154 	c2->prev_lsps_dec[i] = lsps[3][i];
1155 
1156 }
1157 
1158 /*---------------------------------------------------------------------------*\
1159 
1160   FUNCTION....: codec2_encode_1300
1161   AUTHOR......: David Rowe
1162   DATE CREATED: March 14 2013
1163 
1164   Encodes 320 speech samples (40ms of speech) into 52 bits.
1165 
1166   The codec2 algorithm actually operates internally on 10ms (80
1167   sample) frames, so we run the encoding algorithm 4 times:
1168 
1169   frame 0: voicing bit
1170   frame 1: voicing bit,
1171   frame 2: voicing bit
1172   frame 3: voicing bit, Wo and E, scalar LSPs
1173 
1174   The bit allocation is:
1175 
1176     Parameter                      frame 2  frame 4   Total
1177     -------------------------------------------------------
1178     Harmonic magnitudes (LSPs)      0       36        36
1179     Pitch (Wo)                      0        7         7
1180     Energy                          0        5         5
1181     Voicing (10ms update)           2        2         4
1182     TOTAL                           2       50        52
1183 
1184 \*---------------------------------------------------------------------------*/
1185 
codec2_encode_1300(struct CODEC2 * c2,unsigned char * bits,short speech[])1186 void codec2_encode_1300(struct CODEC2 *c2, unsigned char * bits, short speech[])
1187 {
1188     MODEL   model;
1189     float   lsps[LPC_ORD];
1190     float   ak[LPC_ORD+1];
1191     float   e;
1192     int     lsp_indexes[LPC_ORD];
1193     int     Wo_index, e_index;
1194     int     i;
1195     unsigned int nbit = 0;
1196 
1197     assert(c2 != NULL);
1198 
1199     memset(bits, '\0',  ((codec2_bits_per_frame(c2) + 7) / 8));
1200 
1201     /* frame 1: - voicing ---------------------------------------------*/
1202 
1203     analyse_one_frame(c2, &model, speech);
1204     pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray);
1205 
1206     /* frame 2: - voicing ---------------------------------------------*/
1207 
1208     analyse_one_frame(c2, &model, &speech[c2->n_samp]);
1209     pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray);
1210 
1211     /* frame 3: - voicing ---------------------------------------------*/
1212 
1213     analyse_one_frame(c2, &model, &speech[2*c2->n_samp]);
1214     pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray);
1215 
1216     /* frame 4: - voicing, scalar Wo & E, scalar LSPs ------------------*/
1217 
1218     analyse_one_frame(c2, &model, &speech[3*c2->n_samp]);
1219     pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray);
1220 
1221     Wo_index = encode_Wo(&c2->c2const, model.Wo, WO_BITS);
1222     pack_natural_or_gray(bits, &nbit, Wo_index, WO_BITS, c2->gray);
1223 
1224     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
1225     e_index = encode_energy(e, E_BITS);
1226     pack_natural_or_gray(bits, &nbit, e_index, E_BITS, c2->gray);
1227 
1228     encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
1229     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
1230 	pack_natural_or_gray(bits, &nbit, lsp_indexes[i], lsp_bits(i), c2->gray);
1231     }
1232 
1233     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
1234 }
1235 
1236 
1237 /*---------------------------------------------------------------------------*\
1238 
1239   FUNCTION....: codec2_decode_1300
1240   AUTHOR......: David Rowe
1241   DATE CREATED: 11 May 2012
1242 
1243   Decodes frames of 52 bits into 320 samples (40ms) of speech.
1244 
1245 \*---------------------------------------------------------------------------*/
1246 
codec2_decode_1300(struct CODEC2 * c2,short speech[],const unsigned char * bits,float ber_est)1247 void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits, float ber_est)
1248 {
1249     MODEL   model[4];
1250     int     lsp_indexes[LPC_ORD];
1251     float   lsps[4][LPC_ORD];
1252     int     Wo_index, e_index;
1253     float   e[4];
1254     float   snr;
1255     float   ak[4][LPC_ORD+1];
1256     int     i,j;
1257     unsigned int nbit = 0;
1258     float   weight;
1259     COMP    Aw[FFT_ENC];
1260 
1261     assert(c2 != NULL);
1262 
1263     /* only need to zero these out due to (unused) snr calculation */
1264 
1265     for(i=0; i<4; i++)
1266 	for(j=1; j<=MAX_AMP; j++)
1267 	    model[i].A[j] = 0.0;
1268 
1269     /* unpack bits from channel ------------------------------------*/
1270 
1271     /* this will partially fill the model params for the 4 x 10ms
1272        frames */
1273 
1274     model[0].voiced = unpack_natural_or_gray(bits, &nbit, 1, c2->gray);
1275     model[1].voiced = unpack_natural_or_gray(bits, &nbit, 1, c2->gray);
1276     model[2].voiced = unpack_natural_or_gray(bits, &nbit, 1, c2->gray);
1277     model[3].voiced = unpack_natural_or_gray(bits, &nbit, 1, c2->gray);
1278 
1279     Wo_index = unpack_natural_or_gray(bits, &nbit, WO_BITS, c2->gray);
1280     model[3].Wo = decode_Wo(&c2->c2const, Wo_index, WO_BITS);
1281     model[3].L  = PI/model[3].Wo;
1282 
1283     e_index = unpack_natural_or_gray(bits, &nbit, E_BITS, c2->gray);
1284     e[3] = decode_energy(e_index, E_BITS);
1285 
1286     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
1287 	lsp_indexes[i] = unpack_natural_or_gray(bits, &nbit, lsp_bits(i), c2->gray);
1288     }
1289     decode_lsps_scalar(&lsps[3][0], lsp_indexes, LPC_ORD);
1290     check_lsp_order(&lsps[3][0], LPC_ORD);
1291     bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0);
1292 
1293     if (ber_est > 0.15) {
1294         model[0].voiced =  model[1].voiced = model[2].voiced = model[3].voiced = 0;
1295         e[3] = decode_energy(10, E_BITS);
1296         bw_expand_lsps(&lsps[3][0], LPC_ORD, 200.0, 200.0);
1297         //fprintf(stderr, "soft mute\n");
1298     }
1299 
1300     /* interpolate ------------------------------------------------*/
1301 
1302     /* Wo, energy, and LSPs are sampled every 40ms so we interpolate
1303        the 3 frames in between */
1304 
1305     for(i=0, weight=0.25; i<3; i++, weight += 0.25) {
1306 	interpolate_lsp_ver2(&lsps[i][0], c2->prev_lsps_dec, &lsps[3][0], weight, LPC_ORD);
1307         interp_Wo2(&model[i], &c2->prev_model_dec, &model[3], weight, c2->c2const.Wo_min);
1308         e[i] = interp_energy2(c2->prev_e_dec, e[3],weight);
1309     }
1310 
1311     /* then recover spectral amplitudes */
1312 
1313     for(i=0; i<4; i++) {
1314 	lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD);
1315 	aks_to_M2(c2->fftr_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0,
1316                   c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma, Aw);
1317 	apply_lpc_correction(&model[i]);
1318 	synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], Aw, 1.0);
1319 
1320 	/* dump parameters for deep learning experiments */
1321 
1322 	if (c2->fmlfeat != NULL) {
1323 	    /* 10 LSPs - energy - Wo - voicing flag - 10 LPCs */
1324 	    fwrite(&lsps[i][0], LPC_ORD, sizeof(float), c2->fmlfeat);
1325 	    fwrite(&e[i], 1, sizeof(float), c2->fmlfeat);
1326 	    fwrite(&model[i].Wo, 1, sizeof(float), c2->fmlfeat);
1327 	    float voiced_float = model[i].voiced;
1328 	    fwrite(&voiced_float, 1, sizeof(float), c2->fmlfeat);
1329 	    fwrite(&ak[i][1], LPC_ORD, sizeof(float), c2->fmlfeat);
1330 	}
1331     }
1332 
1333     #ifdef DUMP
1334     dump_lsp_(&lsps[3][0]);
1335     dump_ak_(&ak[3][0], LPC_ORD);
1336     #endif
1337 
1338     /* update memories for next frame ----------------------------*/
1339 
1340     c2->prev_model_dec = model[3];
1341     c2->prev_e_dec = e[3];
1342     for(i=0; i<LPC_ORD; i++)
1343 	c2->prev_lsps_dec[i] = lsps[3][i];
1344 
1345 }
1346 
1347 
1348 /*---------------------------------------------------------------------------*\
1349 
1350   FUNCTION....: codec2_encode_1200
1351   AUTHOR......: David Rowe
1352   DATE CREATED: Nov 14 2011
1353 
1354   Encodes 320 speech samples (40ms of speech) into 48 bits.
1355 
1356   The codec2 algorithm actually operates internally on 10ms (80
1357   sample) frames, so we run the encoding algorithm four times:
1358 
1359   frame 0: voicing bit
1360   frame 1: voicing bit, joint VQ of Wo and E
1361   frame 2: voicing bit
1362   frame 3: voicing bit, joint VQ of Wo and E, VQ LSPs
1363 
1364   The bit allocation is:
1365 
1366     Parameter                      frame 2  frame 4   Total
1367     -------------------------------------------------------
1368     Harmonic magnitudes (LSPs)      0       27        27
1369     Energy+Wo                       8        8        16
1370     Voicing (10ms update)           2        2         4
1371     Spare                           0        1         1
1372     TOTAL                          10       38        48
1373 
1374 \*---------------------------------------------------------------------------*/
1375 
codec2_encode_1200(struct CODEC2 * c2,unsigned char * bits,short speech[])1376 void codec2_encode_1200(struct CODEC2 *c2, unsigned char * bits, short speech[])
1377 {
1378     MODEL   model;
1379     float   lsps[LPC_ORD];
1380     float   lsps_[LPC_ORD];
1381     float   ak[LPC_ORD+1];
1382     float   e;
1383     int     lsp_indexes[LPC_ORD];
1384     int     WoE_index;
1385     int     i;
1386     int     spare = 0;
1387     unsigned int nbit = 0;
1388 
1389     assert(c2 != NULL);
1390 
1391     memset(bits, '\0',  ((codec2_bits_per_frame(c2) + 7) / 8));
1392 
1393     /* frame 1: - voicing ---------------------------------------------*/
1394 
1395     analyse_one_frame(c2, &model, speech);
1396     pack(bits, &nbit, model.voiced, 1);
1397 
1398     /* frame 2: - voicing, joint Wo & E -------------------------------*/
1399 
1400     analyse_one_frame(c2, &model, &speech[c2->n_samp]);
1401     pack(bits, &nbit, model.voiced, 1);
1402 
1403     /* need to run this just to get LPC energy */
1404     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
1405 
1406     WoE_index = encode_WoE(&model, e, c2->xq_enc);
1407     pack(bits, &nbit, WoE_index, WO_E_BITS);
1408 
1409     /* frame 3: - voicing ---------------------------------------------*/
1410 
1411     analyse_one_frame(c2, &model, &speech[2*c2->n_samp]);
1412     pack(bits, &nbit, model.voiced, 1);
1413 
1414     /* frame 4: - voicing, joint Wo & E, scalar LSPs ------------------*/
1415 
1416     analyse_one_frame(c2, &model, &speech[3*c2->n_samp]);
1417     pack(bits, &nbit, model.voiced, 1);
1418 
1419     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, c2->m_pitch, LPC_ORD);
1420     WoE_index = encode_WoE(&model, e, c2->xq_enc);
1421     pack(bits, &nbit, WoE_index, WO_E_BITS);
1422 
1423     encode_lsps_vq(lsp_indexes, lsps, lsps_, LPC_ORD);
1424     for(i=0; i<LSP_PRED_VQ_INDEXES; i++) {
1425 	pack(bits, &nbit, lsp_indexes[i], lsp_pred_vq_bits(i));
1426     }
1427     pack(bits, &nbit, spare, 1);
1428 
1429     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
1430 }
1431 
1432 
1433 /*---------------------------------------------------------------------------*\
1434 
1435   FUNCTION....: codec2_decode_1200
1436   AUTHOR......: David Rowe
1437   DATE CREATED: 14 Feb 2012
1438 
1439   Decodes frames of 48 bits into 320 samples (40ms) of speech.
1440 
1441 \*---------------------------------------------------------------------------*/
1442 
codec2_decode_1200(struct CODEC2 * c2,short speech[],const unsigned char * bits)1443 void codec2_decode_1200(struct CODEC2 *c2, short speech[], const unsigned char * bits)
1444 {
1445     MODEL   model[4];
1446     int     lsp_indexes[LPC_ORD];
1447     float   lsps[4][LPC_ORD];
1448     int     WoE_index;
1449     float   e[4];
1450     float   snr;
1451     float   ak[4][LPC_ORD+1];
1452     int     i,j;
1453     unsigned int nbit = 0;
1454     float   weight;
1455     COMP    Aw[FFT_ENC];
1456 
1457     assert(c2 != NULL);
1458 
1459     /* only need to zero these out due to (unused) snr calculation */
1460 
1461     for(i=0; i<4; i++)
1462 	for(j=1; j<=MAX_AMP; j++)
1463 	    model[i].A[j] = 0.0;
1464 
1465     /* unpack bits from channel ------------------------------------*/
1466 
1467     /* this will partially fill the model params for the 4 x 10ms
1468        frames */
1469 
1470     model[0].voiced = unpack(bits, &nbit, 1);
1471 
1472     model[1].voiced = unpack(bits, &nbit, 1);
1473     WoE_index = unpack(bits, &nbit, WO_E_BITS);
1474     decode_WoE(&c2->c2const, &model[1], &e[1], c2->xq_dec, WoE_index);
1475 
1476     model[2].voiced = unpack(bits, &nbit, 1);
1477 
1478     model[3].voiced = unpack(bits, &nbit, 1);
1479     WoE_index = unpack(bits, &nbit, WO_E_BITS);
1480     decode_WoE(&c2->c2const, &model[3], &e[3], c2->xq_dec, WoE_index);
1481 
1482     for(i=0; i<LSP_PRED_VQ_INDEXES; i++) {
1483 	lsp_indexes[i] = unpack(bits, &nbit, lsp_pred_vq_bits(i));
1484     }
1485     decode_lsps_vq(lsp_indexes, &lsps[3][0], LPC_ORD , 0);
1486     check_lsp_order(&lsps[3][0], LPC_ORD);
1487     bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0);
1488 
1489     /* interpolate ------------------------------------------------*/
1490 
1491     /* Wo and energy are sampled every 20ms, so we interpolate just 1
1492        10ms frame between 20ms samples */
1493 
1494     interp_Wo(&model[0], &c2->prev_model_dec, &model[1], c2->c2const.Wo_min);
1495     e[0] = interp_energy(c2->prev_e_dec, e[1]);
1496     interp_Wo(&model[2], &model[1], &model[3], c2->c2const.Wo_min);
1497     e[2] = interp_energy(e[1], e[3]);
1498 
1499     /* LSPs are sampled every 40ms so we interpolate the 3 frames in
1500        between, then recover spectral amplitudes */
1501 
1502     for(i=0, weight=0.25; i<3; i++, weight += 0.25) {
1503 	interpolate_lsp_ver2(&lsps[i][0], c2->prev_lsps_dec, &lsps[3][0], weight, LPC_ORD);
1504     }
1505     for(i=0; i<4; i++) {
1506 	lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD);
1507 	aks_to_M2(c2->fftr_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0,
1508                   c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma, Aw);
1509 	apply_lpc_correction(&model[i]);
1510 	synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], Aw, 1.0);
1511     }
1512 
1513     /* update memories for next frame ----------------------------*/
1514 
1515     c2->prev_model_dec = model[3];
1516     c2->prev_e_dec = e[3];
1517     for(i=0; i<LPC_ORD; i++)
1518 	c2->prev_lsps_dec[i] = lsps[3][i];
1519 }
1520 
1521 
1522 /*---------------------------------------------------------------------------*\
1523 
1524   FUNCTION....: codec2_encode_700c
1525   AUTHOR......: David Rowe
1526   DATE CREATED: Jan 2017
1527 
1528   Version c of 700 bit/s codec that uses newamp1 fixed rate VQ of amplitudes.
1529 
1530   Encodes 320 speech samples (40ms of speech) into 28 bits.
1531 
1532   The codec2 algorithm actually operates internally on 10ms (80
1533   sample) frames, so we run the encoding algorithm four times:
1534 
1535   frame 0: nothing
1536   frame 1: nothing
1537   frame 2: nothing
1538   frame 3: 18 bit 2 stage VQ (9 bits/stage), 4 bits energy,
1539            6 bit scalar Wo/voicing. No spare bits.
1540 
1541   Voicing is encoded using the 0 index of the Wo quantiser.
1542 
1543   The bit allocation is:
1544 
1545     Parameter                      frames 1-3   frame 4   Total
1546     -----------------------------------------------------------
1547     Harmonic magnitudes (rate k VQ)     0         18        18
1548     Energy                              0          4         4
1549     log Wo/voicing                      0          6         6
1550     TOTAL                               0         28        28
1551 
1552 \*---------------------------------------------------------------------------*/
1553 
codec2_encode_700c(struct CODEC2 * c2,unsigned char * bits,short speech[])1554 void codec2_encode_700c(struct CODEC2 *c2, unsigned char * bits, short speech[])
1555 {
1556     MODEL        model;
1557     int          indexes[4], i, M=4;
1558     unsigned int nbit = 0;
1559 
1560     assert(c2 != NULL);
1561 
1562     memset(bits, '\0',  ((codec2_bits_per_frame(c2) + 7) / 8));
1563 
1564     for(i=0; i<M; i++) {
1565         analyse_one_frame(c2, &model, &speech[i*c2->n_samp]);
1566     }
1567 
1568     int K = 20;
1569     float rate_K_vec[K], mean;
1570     float rate_K_vec_no_mean[K], rate_K_vec_no_mean_[K];
1571 
1572     newamp1_model_to_indexes(&c2->c2const,
1573                              indexes,
1574                              &model,
1575                              rate_K_vec,
1576                              c2->rate_K_sample_freqs_kHz,
1577                              K,
1578                              &mean,
1579                              rate_K_vec_no_mean,
1580                              rate_K_vec_no_mean_, &c2->se, c2->eq, c2->eq_en);
1581     c2->nse += K;
1582 
1583 #ifndef CORTEX_M4
1584     /* dump features for deep learning experiments */
1585     if (c2->fmlfeat != NULL) {
1586         fwrite(&mean, 1, sizeof(float), c2->fmlfeat);
1587         fwrite(rate_K_vec_no_mean, K, sizeof(float), c2->fmlfeat);
1588         fwrite(rate_K_vec_no_mean_, K, sizeof(float), c2->fmlfeat);
1589 	MODEL model_; memcpy(&model_, &model, sizeof(model));
1590 	float rate_K_vec_[K];
1591 	for(int k=0; k<K; k++)
1592 	    rate_K_vec_[k] = rate_K_vec_no_mean_[k] + mean;
1593 	resample_rate_L(&c2->c2const, &model_, rate_K_vec_, c2->rate_K_sample_freqs_kHz, K);
1594         fwrite(&model_.A, MAX_AMP, sizeof(float), c2->fmlfeat);
1595     }
1596     if (c2->fmlmodel != NULL)
1597 	fwrite(&model,sizeof(MODEL),1,c2->fmlmodel);
1598 #endif
1599 
1600     pack_natural_or_gray(bits, &nbit, indexes[0], 9, 0);
1601     pack_natural_or_gray(bits, &nbit, indexes[1], 9, 0);
1602     pack_natural_or_gray(bits, &nbit, indexes[2], 4, 0);
1603     pack_natural_or_gray(bits, &nbit, indexes[3], 6, 0);
1604 
1605     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
1606 }
1607 
1608 
1609 /*---------------------------------------------------------------------------*\
1610 
1611   FUNCTION....: codec2_decode_700c
1612   AUTHOR......: David Rowe
1613   DATE CREATED: August 2015
1614 
1615   Decodes frames of 28 bits into 320 samples (40ms) of speech.
1616 
1617 \*---------------------------------------------------------------------------*/
1618 
codec2_decode_700c(struct CODEC2 * c2,short speech[],const unsigned char * bits)1619 void codec2_decode_700c(struct CODEC2 *c2, short speech[], const unsigned char * bits)
1620 {
1621     MODEL   model[4];
1622     int     indexes[4];
1623     int     i;
1624     unsigned int nbit = 0;
1625 
1626     assert(c2 != NULL);
1627 
1628     /* unpack bits from channel ------------------------------------*/
1629 
1630     indexes[0] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1631     indexes[1] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1632     indexes[2] = unpack_natural_or_gray(bits, &nbit, 4, 0);
1633     indexes[3] = unpack_natural_or_gray(bits, &nbit, 6, 0);
1634 
1635     int M = 4;
1636     COMP  HH[M][MAX_AMP+1];
1637     float interpolated_surface_[M][NEWAMP1_K];
1638 
1639     newamp1_indexes_to_model(&c2->c2const,
1640                              model,
1641                              (COMP*)HH,
1642                              (float*)interpolated_surface_,
1643                              c2->prev_rate_K_vec_,
1644                              &c2->Wo_left,
1645                              &c2->voicing_left,
1646                              c2->rate_K_sample_freqs_kHz,
1647                              NEWAMP1_K,
1648                              c2->phase_fft_fwd_cfg,
1649                              c2->phase_fft_inv_cfg,
1650                              indexes,
1651                              c2->user_rate_K_vec_no_mean_,
1652                              c2->post_filter_en);
1653 
1654 
1655    for(i=0; i<M; i++) {
1656        if (c2->fmlfeat != NULL) {
1657 	   /* We use standard nb_features=55 feature records for compatability with train_lpcnet.py */
1658 	   float features[55] = {0};
1659 	   /* just using 18/20 for compatability with LPCNet, coarse scaling for NN imput */
1660 	   for(int j=0; j<18; j++)
1661 	       features[j] = (interpolated_surface_[i][j]-30)/40;
1662 	   int pitch_index = 21 + 2.0*M_PI/model[i].Wo;
1663 	   features[36] = 0.02*(pitch_index-100);
1664 	   features[37] = model[i].voiced;
1665 	   fwrite(features, 55, sizeof(float), c2->fmlfeat);
1666        }
1667 
1668        /* 700C is a little quieter so lets apply some experimentally derived audio gain */
1669        synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], &HH[i][0], 1.5);
1670    }
1671 }
1672 
1673 /*---------------------------------------------------------------------------*\
1674 
1675   FUNCTION....: codec2_energy_700c
1676   AUTHOR......: Jeroen Vreeken
1677   DATE CREATED: Jan 2017
1678 
1679   Decodes energy value from encoded bits.
1680 
1681 \*---------------------------------------------------------------------------*/
1682 
codec2_energy_700c(struct CODEC2 * c2,const unsigned char * bits)1683 float codec2_energy_700c(struct CODEC2 *c2, const unsigned char * bits)
1684 {
1685     int     indexes[4];
1686     unsigned int nbit = 0;
1687 
1688     assert(c2 != NULL);
1689 
1690     /* unpack bits from channel ------------------------------------*/
1691 
1692     indexes[0] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1693     indexes[1] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1694     indexes[2] = unpack_natural_or_gray(bits, &nbit, 4, 0);
1695     indexes[3] = unpack_natural_or_gray(bits, &nbit, 6, 0);
1696 
1697     float mean = newamp1_energy_cb[0].cb[indexes[2]];
1698     mean -= 10;
1699     if (indexes[3] == 0)
1700     	mean -= 10;
1701 
1702     return POW10F(mean/10.0);
1703 }
1704 
codec2_energy_450(struct CODEC2 * c2,const unsigned char * bits)1705 float codec2_energy_450(struct CODEC2 *c2, const unsigned char * bits)
1706 {
1707     int     indexes[4];
1708     unsigned int nbit = 0;
1709 
1710     assert(c2 != NULL);
1711 
1712     /* unpack bits from channel ------------------------------------*/
1713 
1714     indexes[0] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1715     //indexes[1] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1716     indexes[2] = unpack_natural_or_gray(bits, &nbit, 3, 0);
1717     indexes[3] = unpack_natural_or_gray(bits, &nbit, 6, 0);
1718 
1719     float mean = newamp2_energy_cb[0].cb[indexes[2]];
1720     mean -= 10;
1721     if (indexes[3] == 0)
1722     	mean -= 10;
1723 
1724     return POW10F(mean/10.0);
1725 }
1726 
1727 /*---------------------------------------------------------------------------*\
1728 
1729   FUNCTION....: codec2_get_energy()
1730   AUTHOR......: Jeroen Vreeken
1731   DATE CREATED: 08/03/2016
1732 
1733   Extract energy value from an encoded frame.
1734 
1735 \*---------------------------------------------------------------------------*/
1736 
codec2_get_energy(struct CODEC2 * c2,const unsigned char * bits)1737 float codec2_get_energy(struct CODEC2 *c2, const unsigned char *bits)
1738 {
1739     assert(c2 != NULL);
1740     assert(
1741 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_3200, c2->mode)) ||
1742 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_2400, c2->mode)) ||
1743 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1600, c2->mode)) ||
1744 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1400, c2->mode)) ||
1745 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1300, c2->mode)) ||
1746 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1200, c2->mode)) ||
1747 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, c2->mode)) ||
1748 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450, c2->mode)) ||
1749 	   ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, c2->mode))
1750 	   );
1751     MODEL model;
1752     float xq_dec[2] = {};
1753     int e_index, WoE_index;
1754     float e = 0.0f;
1755     unsigned int nbit;
1756 
1757     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_3200, c2->mode)) {
1758         nbit = 1 + 1 + WO_BITS;
1759 	e_index = unpack(bits, &nbit, E_BITS);
1760         e = decode_energy(e_index, E_BITS);
1761     }
1762     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_2400, c2->mode)) {
1763         nbit = 1 + 1;
1764         WoE_index = unpack(bits, &nbit, WO_E_BITS);
1765         decode_WoE(&c2->c2const, &model, &e, xq_dec, WoE_index);
1766     }
1767     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1600, c2->mode)) {
1768         nbit = 1 + 1 + WO_BITS;
1769         e_index = unpack(bits, &nbit, E_BITS);
1770         e = decode_energy(e_index, E_BITS);
1771     }
1772     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1400, c2->mode)) {
1773         nbit = 1 + 1;
1774         WoE_index = unpack(bits, &nbit, WO_E_BITS);
1775         decode_WoE(&c2->c2const, &model, &e, xq_dec, WoE_index);
1776     }
1777     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1300, c2->mode)) {
1778         nbit = 1 + 1 + 1 + 1 + WO_BITS;
1779         e_index = unpack_natural_or_gray(bits, &nbit, E_BITS, c2->gray);
1780         e = decode_energy(e_index, E_BITS);
1781     }
1782     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_1200, c2->mode)) {
1783         nbit = 1 + 1;
1784         WoE_index = unpack(bits, &nbit, WO_E_BITS);
1785         decode_WoE(&c2->c2const, &model, &e, xq_dec, WoE_index);
1786     }
1787     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, c2->mode)) {
1788         e = codec2_energy_700c(c2, bits);
1789     }
1790     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_450, c2->mode) ||  CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, c2->mode)) {
1791         e = codec2_energy_450(c2, bits);
1792     }
1793 
1794     return e;
1795 }
1796 
1797 
1798 /*---------------------------------------------------------------------------*\
1799 
1800   FUNCTION....: codec2_encode_450
1801   AUTHOR......: Thomas Kurin and Stefan Erhardt
1802   INSTITUTE...:	Institute for Electronics Engineering, University of Erlangen-Nuremberg
1803   DATE CREATED: July 2018
1804 
1805   450 bit/s codec that uses newamp2 fixed rate VQ of amplitudes.
1806 
1807   Encodes 320 speech samples (40ms of speech) into 28 bits.
1808 
1809   The codec2 algorithm actually operates internally on 10ms (80
1810   sample) frames, so we run the encoding algorithm four times:
1811 
1812   frame 0: nothing
1813   frame 1: nothing
1814   frame 2: nothing
1815   frame 3: 9 bit 1 stage VQ, 3 bits energy,
1816            6 bit scalar Wo/voicing/plosive. No spare bits.
1817 
1818   If a plosive is detected the frame at the energy-step is encoded.
1819 
1820   Voicing is encoded using the 000000 index of the Wo quantiser.
1821   Plosive is encoded using the 111111 index of the Wo quantiser.
1822 
1823   The bit allocation is:
1824 
1825     Parameter                      frames 1-3   frame 4   Total
1826     -----------------------------------------------------------
1827     Harmonic magnitudes (rate k VQ)     0          9         9
1828     Energy                              0          3         3
1829     log Wo/voicing/plosive              0          6         6
1830     TOTAL                               0         18        18
1831 
1832 
1833 \*---------------------------------------------------------------------------*/
1834 
codec2_encode_450(struct CODEC2 * c2,unsigned char * bits,short speech[])1835 void codec2_encode_450(struct CODEC2 *c2, unsigned char * bits, short speech[])
1836 {
1837 	MODEL        model;
1838     int          indexes[4], i,h, M=4;
1839     unsigned int nbit = 0;
1840     int plosiv = 0;
1841     float energydelta[M];
1842 	int spectralCounter;
1843 
1844     assert(c2 != NULL);
1845 
1846     memset(bits, '\0',  ((codec2_bits_per_frame(c2) + 7) / 8));
1847     for(i=0; i<M; i++){
1848         analyse_one_frame(c2, &model, &speech[i*c2->n_samp]);
1849         energydelta[i] = 0;
1850         spectralCounter = 0;
1851         for(h = 0;h<(model.L);h++){
1852 			//only detect above 300 Hz
1853 			if(h*model.Wo*(c2->c2const.Fs/2000.0)/M_PI > 0.3){
1854 				energydelta[i] = (double)energydelta[i] + (double)20.0*log10(model.A[10]+1E-16);
1855 				spectralCounter = spectralCounter+1;
1856 			}
1857 
1858 			}
1859 		energydelta[i] = energydelta[i] / spectralCounter ;
1860     }
1861     //Constants for plosive Detection tdB = threshold; minPwr = from below this level plosives have to rise
1862     float tdB = 15; //not fixed can be changed
1863     float minPwr = 15; //not fixed can be changed
1864 		if((c2->energy_prev)<minPwr && energydelta[0]>((c2->energy_prev)+tdB)){
1865 
1866 			plosiv = 1;
1867 		}
1868 		if(energydelta[0]<minPwr && energydelta[1]>(energydelta[0]+tdB)){
1869 
1870 			plosiv = 2;
1871 		}
1872 		if(energydelta[1]<minPwr &&energydelta[2]>(energydelta[1]+tdB)){
1873 
1874 			plosiv = 3;
1875 		}
1876 		if(energydelta[2]<minPwr &&energydelta[3]>(energydelta[2]+tdB)){
1877 
1878 			plosiv = 4;
1879 		}
1880 	if(plosiv != 0 && plosiv != 4){
1881 		analyse_one_frame(c2, &model, &speech[(plosiv-1)*c2->n_samp]);
1882 		}
1883 
1884     c2->energy_prev = energydelta[3];
1885 
1886 
1887     int K = 29;
1888     float rate_K_vec[K], mean;
1889     float rate_K_vec_no_mean[K], rate_K_vec_no_mean_[K];
1890     if(plosiv > 0){
1891 		plosiv = 1;
1892 	}
1893     newamp2_model_to_indexes(&c2->c2const,
1894                              indexes,
1895                              &model,
1896                              rate_K_vec,
1897                              c2->n2_rate_K_sample_freqs_kHz,
1898                              K,
1899                              &mean,
1900                              rate_K_vec_no_mean,
1901                              rate_K_vec_no_mean_,
1902                              plosiv);
1903 
1904 
1905 	pack_natural_or_gray(bits, &nbit, indexes[0], 9, 0);
1906     //pack_natural_or_gray(bits, &nbit, indexes[1], 9, 0);
1907     pack_natural_or_gray(bits, &nbit, indexes[2], 3, 0);
1908     pack_natural_or_gray(bits, &nbit, indexes[3], 6, 0);
1909 
1910     assert(nbit == (unsigned)codec2_bits_per_frame(c2));
1911 }
1912 
1913 
1914 /*---------------------------------------------------------------------------*\
1915 
1916   FUNCTION....: codec2_decode_450
1917   AUTHOR......: Thomas Kurin and Stefan Erhardt
1918   INSTITUTE...:	Institute for Electronics Engineering, University of Erlangen-Nuremberg
1919   DATE CREATED: July 2018
1920 
1921 \*---------------------------------------------------------------------------*/
1922 
codec2_decode_450(struct CODEC2 * c2,short speech[],const unsigned char * bits)1923 void codec2_decode_450(struct CODEC2 *c2, short speech[], const unsigned char * bits)
1924 {
1925     MODEL   model[4];
1926     int     indexes[4];
1927     int     i;
1928     unsigned int nbit = 0;
1929 
1930     assert(c2 != NULL);
1931 
1932     /* unpack bits from channel ------------------------------------*/
1933 
1934     indexes[0] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1935     //indexes[1] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1936     indexes[2] = unpack_natural_or_gray(bits, &nbit, 3, 0);
1937     indexes[3] = unpack_natural_or_gray(bits, &nbit, 6, 0);
1938 
1939     int M = 4;
1940     COMP  HH[M][MAX_AMP+1];
1941     float interpolated_surface_[M][NEWAMP2_K];
1942     int pwbFlag = 0;
1943 
1944     newamp2_indexes_to_model(&c2->c2const,
1945                              model,
1946                              (COMP*)HH,
1947                              (float*)interpolated_surface_,
1948                              c2->n2_prev_rate_K_vec_,
1949                              &c2->Wo_left,
1950                              &c2->voicing_left,
1951                              c2->n2_rate_K_sample_freqs_kHz,
1952                              NEWAMP2_K,
1953                              c2->phase_fft_fwd_cfg,
1954                              c2->phase_fft_inv_cfg,
1955                              indexes,
1956                              1.5,
1957                              pwbFlag);
1958 
1959 
1960    for(i=0; i<M; i++) {
1961        synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], &HH[i][0], 1.5);
1962    }
1963 }
1964 
1965 /*---------------------------------------------------------------------------*\
1966 
1967   FUNCTION....: codec2_decode_450pwb
1968   AUTHOR......: Thomas Kurin and Stefan Erhardt
1969   INSTITUTE...:	Institute for Electronics Engineering, University of Erlangen-Nuremberg
1970   DATE CREATED: July 2018
1971 
1972   Decodes the 450 codec data in pseudo wideband at 16kHz samplerate.
1973 
1974 \*---------------------------------------------------------------------------*/
1975 
codec2_decode_450pwb(struct CODEC2 * c2,short speech[],const unsigned char * bits)1976 void codec2_decode_450pwb(struct CODEC2 *c2, short speech[], const unsigned char * bits)
1977 {
1978     MODEL   model[4];
1979     int     indexes[4];
1980     int     i;
1981     unsigned int nbit = 0;
1982 
1983     assert(c2 != NULL);
1984 
1985     /* unpack bits from channel ------------------------------------*/
1986 
1987     indexes[0] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1988     //indexes[1] = unpack_natural_or_gray(bits, &nbit, 9, 0);
1989     indexes[2] = unpack_natural_or_gray(bits, &nbit, 3, 0);
1990     indexes[3] = unpack_natural_or_gray(bits, &nbit, 6, 0);
1991 
1992     int M = 4;
1993     COMP  HH[M][MAX_AMP+1];
1994     float interpolated_surface_[M][NEWAMP2_16K_K];
1995     int pwbFlag = 1;
1996 
1997     newamp2_indexes_to_model(&c2->c2const,
1998                              model,
1999                              (COMP*)HH,
2000                              (float*)interpolated_surface_,
2001                              c2->n2_pwb_prev_rate_K_vec_,
2002                              &c2->Wo_left,
2003                              &c2->voicing_left,
2004                              c2->n2_pwb_rate_K_sample_freqs_kHz,
2005                              NEWAMP2_16K_K,
2006                              c2->phase_fft_fwd_cfg,
2007                              c2->phase_fft_inv_cfg,
2008                              indexes,
2009                              1.5,
2010                              pwbFlag);
2011 
2012 
2013    for(i=0; i<M; i++) {
2014        synthesise_one_frame(c2, &speech[c2->n_samp*i], &model[i], &HH[i][0], 1.5);
2015    }
2016 }
2017 
2018 
2019 /*---------------------------------------------------------------------------* \
2020 
2021   FUNCTION....: synthesise_one_frame()
2022   AUTHOR......: David Rowe
2023   DATE CREATED: 23/8/2010
2024 
2025   Synthesise 80 speech samples (10ms) from model parameters.
2026 
2027 \*---------------------------------------------------------------------------*/
2028 
synthesise_one_frame(struct CODEC2 * c2,short speech[],MODEL * model,COMP Aw[],float gain)2029 void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model, COMP Aw[], float gain)
2030 {
2031     int     i;
2032 
2033     if ( CODEC2_MODE_ACTIVE(CODEC2_MODE_700C, c2->mode) || CODEC2_MODE_ACTIVE(CODEC2_MODE_450, c2->mode) || CODEC2_MODE_ACTIVE(CODEC2_MODE_450PWB, c2->mode)  ) {
2034         /* newamp1/2, we've already worked out rate L phase */
2035         COMP *H = Aw;
2036         phase_synth_zero_order(c2->n_samp, model, &c2->ex_phase, H);
2037     } else {
2038         /* LPC based phase synthesis */
2039         COMP H[MAX_AMP+1];
2040         sample_phase(model, H, Aw);
2041         phase_synth_zero_order(c2->n_samp, model, &c2->ex_phase, H);
2042     }
2043 
2044     postfilter(model, &c2->bg_est);
2045     synthesise(c2->n_samp, c2->fftr_inv_cfg, c2->Sn_, model, c2->Pn, 1);
2046 
2047     for(i=0; i<c2->n_samp; i++) {
2048         c2->Sn_[i] *= gain;
2049     }
2050 
2051     ear_protection(c2->Sn_, c2->n_samp);
2052 
2053     for(i=0; i<c2->n_samp; i++) {
2054 	if (c2->Sn_[i] > 32767.0)
2055 	    speech[i] = 32767;
2056 	else if (c2->Sn_[i] < -32767.0)
2057 	    speech[i] = -32767;
2058 	else
2059 	    speech[i] = c2->Sn_[i];
2060     }
2061 
2062 }
2063 
2064 
2065 /*---------------------------------------------------------------------------* \
2066 
2067   FUNCTION....: analyse_one_frame()
2068   AUTHOR......: David Rowe
2069   DATE CREATED: 23/8/2010
2070 
2071   Extract sinusoidal model parameters from 80 speech samples (10ms of
2072   speech).
2073 
2074 \*---------------------------------------------------------------------------*/
2075 
analyse_one_frame(struct CODEC2 * c2,MODEL * model,short speech[])2076 void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[])
2077 {
2078     COMP    Sw[FFT_ENC];
2079     float   pitch;
2080     int     i;
2081     int     n_samp = c2->n_samp;
2082     int     m_pitch = c2->m_pitch;
2083 
2084     /* Read input speech */
2085 
2086     for(i=0; i<m_pitch-n_samp; i++)
2087       c2->Sn[i] = c2->Sn[i+n_samp];
2088     for(i=0; i<n_samp; i++)
2089       c2->Sn[i+m_pitch-n_samp] = speech[i];
2090 
2091     dft_speech(&c2->c2const, c2->fft_fwd_cfg, Sw, c2->Sn, c2->w);
2092 
2093     /* Estimate pitch */
2094     nlp(c2->nlp, c2->Sn, n_samp, &pitch, Sw, c2->W, &c2->prev_f0_enc);
2095     model->Wo = TWO_PI/pitch;
2096     model->L = PI/model->Wo;
2097 
2098     /* estimate model parameters */
2099     two_stage_pitch_refinement(&c2->c2const, model, Sw);
2100 
2101     /* estimate phases when doing ML experiments */
2102     if (c2->fmlfeat != NULL)
2103 	estimate_amplitudes(model, Sw, c2->W, 1);
2104     else
2105 	estimate_amplitudes(model, Sw, c2->W, 0);
2106     est_voicing_mbe(&c2->c2const, model, Sw, c2->W);
2107     #ifdef DUMP
2108     dump_model(model);
2109     #endif
2110 }
2111 
2112 
2113 /*---------------------------------------------------------------------------* \
2114 
2115   FUNCTION....: ear_protection()
2116   AUTHOR......: David Rowe
2117   DATE CREATED: Nov 7 2012
2118 
2119   Limits output level to protect ears when there are bit errors or the input
2120   is overdriven.  This doesn't correct or mask bit errors, just reduces the
2121   worst of their damage.
2122 
2123 \*---------------------------------------------------------------------------*/
2124 
ear_protection(float in_out[],int n)2125 static void ear_protection(float in_out[], int n) {
2126     float max_sample, over, gain;
2127     int   i;
2128 
2129     /* find maximum sample in frame */
2130 
2131     max_sample = 0.0;
2132     for(i=0; i<n; i++)
2133         if (in_out[i] > max_sample)
2134             max_sample = in_out[i];
2135 
2136     /* determine how far above set point */
2137 
2138     over = max_sample/30000.0;
2139 
2140     /* If we are x dB over set point we reduce level by 2x dB, this
2141        attenuates major excursions in amplitude (likely to be caused
2142        by bit errors) more than smaller ones */
2143 
2144     if (over > 1.0) {
2145         gain = 1.0/(over*over);
2146         for(i=0; i<n; i++)
2147             in_out[i] *= gain;
2148     }
2149 }
2150 
2151 
codec2_set_lpc_post_filter(struct CODEC2 * c2,int enable,int bass_boost,float beta,float gamma)2152 void codec2_set_lpc_post_filter(struct CODEC2 *c2, int enable, int bass_boost, float beta, float gamma)
2153 {
2154     assert((beta >= 0.0) && (beta <= 1.0));
2155     assert((gamma >= 0.0) && (gamma <= 1.0));
2156     c2->lpc_pf = enable;
2157     c2->bass_boost = bass_boost;
2158     c2->beta = beta;
2159     c2->gamma = gamma;
2160 }
2161 
2162 
2163 /*
2164    Allows optional stealing of one of the voicing bits for use as a
2165    spare bit, only 1300 & 1400 & 1600 bit/s supported for now.
2166    Experimental method of sending voice/data frames for FreeDV.
2167 */
2168 
codec2_get_spare_bit_index(struct CODEC2 * c2)2169 int codec2_get_spare_bit_index(struct CODEC2 *c2)
2170 {
2171     assert(c2 != NULL);
2172 
2173     switch(c2->mode) {
2174     case CODEC2_MODE_1300:
2175         return 2; // bit 2 (3th bit) is v2 (third voicing bit)
2176         break;
2177     case CODEC2_MODE_1400:
2178         return 10; // bit 10 (11th bit) is v2 (third voicing bit)
2179         break;
2180     case CODEC2_MODE_1600:
2181         return 15; // bit 15 (16th bit) is v2 (third voicing bit)
2182         break;
2183     }
2184 
2185     return -1;
2186 }
2187 
2188 /*
2189    Reconstructs the spare voicing bit.  Note works on unpacked bits
2190    for convenience.
2191 */
2192 
codec2_rebuild_spare_bit(struct CODEC2 * c2,char unpacked_bits[])2193 int codec2_rebuild_spare_bit(struct CODEC2 *c2, char unpacked_bits[])
2194 {
2195     int v1,v3;
2196 
2197     assert(c2 != NULL);
2198 
2199     v1 = unpacked_bits[1];
2200 
2201     switch(c2->mode) {
2202     case CODEC2_MODE_1300:
2203 
2204         v3 = unpacked_bits[1+1+1];
2205 
2206         /* if either adjacent frame is voiced, make this one voiced */
2207 
2208         unpacked_bits[2] = (v1 || v3);
2209 
2210         return 0;
2211 
2212         break;
2213 
2214     case CODEC2_MODE_1400:
2215 
2216         v3 = unpacked_bits[1+1+8+1];
2217 
2218         /* if either adjacent frame is voiced, make this one voiced */
2219 
2220         unpacked_bits[10] = (v1 || v3);
2221 
2222         return 0;
2223 
2224         break;
2225 
2226     case CODEC2_MODE_1600:
2227         v3 = unpacked_bits[1+1+8+5+1];
2228 
2229         /* if either adjacent frame is voiced, make this one voiced */
2230 
2231         unpacked_bits[15] = (v1 || v3);
2232 
2233         return 0;
2234 
2235         break;
2236     }
2237 
2238     return -1;
2239 }
2240 
codec2_set_natural_or_gray(struct CODEC2 * c2,int gray)2241 void codec2_set_natural_or_gray(struct CODEC2 *c2, int gray)
2242 {
2243     assert(c2 != NULL);
2244     c2->gray = gray;
2245 }
2246 
codec2_set_softdec(struct CODEC2 * c2,float * softdec)2247 void codec2_set_softdec(struct CODEC2 *c2, float *softdec)
2248 {
2249     assert(c2 != NULL);
2250     c2->softdec = softdec;
2251 }
2252 
codec2_open_mlfeat(struct CODEC2 * codec2_state,char * feat_fn,char * model_fn)2253 void codec2_open_mlfeat(struct CODEC2 *codec2_state, char *feat_fn, char *model_fn) {
2254     if ((codec2_state->fmlfeat = fopen(feat_fn, "wb")) == NULL) {
2255 	fprintf(stderr, "error opening machine learning feature file: %s\n", feat_fn);
2256 	exit(1);
2257     }
2258     if (model_fn) {
2259 	if ((codec2_state->fmlmodel = fopen(model_fn, "wb")) == NULL) {
2260 	fprintf(stderr, "error opening machine learning Codec 2 model file: %s\n", feat_fn);
2261 	exit(1);
2262 	}
2263     }
2264 }
2265 
2266 #ifndef __EMBEDDED__
codec2_load_codebook(struct CODEC2 * codec2_state,int num,char * filename)2267 void codec2_load_codebook(struct CODEC2 *codec2_state, int num, char *filename) {
2268     FILE *f;
2269 
2270     if ((f = fopen(filename, "rb")) == NULL) {
2271 	fprintf(stderr, "error opening codebook file: %s\n", filename);
2272 	exit(1);
2273     }
2274     //fprintf(stderr, "reading newamp1vq_cb[%d] k=%d m=%d\n", num, newamp1vq_cb[num].k, newamp1vq_cb[num].m);
2275     float tmp[newamp1vq_cb[num].k*newamp1vq_cb[num].m];
2276     int nread = fread(tmp, sizeof(float), newamp1vq_cb[num].k*newamp1vq_cb[num].m, f);
2277     float *p = (float*)newamp1vq_cb[num].cb;
2278     for(int i=0; i<newamp1vq_cb[num].k*newamp1vq_cb[num].m; i++)
2279        p[i] = tmp[i];
2280     // fprintf(stderr, "nread = %d %f %f\n", nread, newamp1vq_cb[num].cb[0], newamp1vq_cb[num].cb[1]);
2281     assert(nread == newamp1vq_cb[num].k*newamp1vq_cb[num].m);
2282     fclose(f);
2283 }
2284 #endif
2285 
codec2_get_var(struct CODEC2 * codec2_state)2286 float codec2_get_var(struct CODEC2 *codec2_state) {
2287     if (codec2_state->nse)
2288         return codec2_state->se/codec2_state->nse;
2289     else
2290         return 0;
2291 }
2292 
codec2_enable_user_ratek(struct CODEC2 * codec2_state,int * K)2293 float *codec2_enable_user_ratek(struct CODEC2 *codec2_state, int *K) {
2294     codec2_state->user_rate_K_vec_no_mean_ = (float*)malloc(sizeof(float)*NEWAMP1_K);
2295     *K = NEWAMP1_K;
2296     return codec2_state->user_rate_K_vec_no_mean_;
2297 }
2298 
codec2_700c_post_filter(struct CODEC2 * codec2_state,int en)2299 void codec2_700c_post_filter(struct CODEC2 *codec2_state, int en) {
2300     codec2_state->post_filter_en = en;
2301 }
2302 
codec2_700c_eq(struct CODEC2 * codec2_state,int en)2303 void codec2_700c_eq(struct CODEC2 *codec2_state, int en) {
2304     codec2_state->eq_en = en;
2305     codec2_state->se = 0.0; codec2_state->nse = 0;
2306 }
2307