=Norig) twidx-=Norig;
228 | C_MUL(t,scratch[q] , twiddles[twidx] );
229 | C_ADDTO( Fout[ k ] ,t);
230 | }
231 | k += m;
232 | }
233 | }
234 | KISS_FFT_TMP_FREE(scratch);
235 | }
236 |
237 | static
238 | void kf_work(
239 | kiss_fft_cpx * Fout,
240 | const kiss_fft_cpx * f,
241 | const size_t fstride,
242 | int in_stride,
243 | int * factors,
244 | const kiss_fft_cfg st
245 | )
246 | {
247 | kiss_fft_cpx * Fout_beg=Fout;
248 | const int p=*factors++; /* the radix */
249 | const int m=*factors++; /* stage's fft length/p */
250 | const kiss_fft_cpx * Fout_end = Fout + p*m;
251 |
252 | #ifdef _OPENMP
253 | // use openmp extensions at the
254 | // top-level (not recursive)
255 | if (fstride==1 && p<=5)
256 | {
257 | int k;
258 |
259 | // execute the p different work units in different threads
260 | # pragma omp parallel for
261 | for (k=0;k floor_sqrt)
324 | p = n; /* no more factors, skip to end */
325 | }
326 | n /= p;
327 | *facbuf++ = p;
328 | *facbuf++ = n;
329 | } while (n > 1);
330 | }
331 |
332 | /*
333 | *
334 | * User-callable function to allocate all necessary storage space for the fft.
335 | *
336 | * The return value is a contiguous block of memory, allocated with malloc. As such,
337 | * It can be freed with free(), rather than a kiss_fft-specific function.
338 | * */
339 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem )
340 | {
341 | kiss_fft_cfg st=NULL;
342 | size_t memneeded = sizeof(struct kiss_fft_state)
343 | + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/
344 |
345 | if ( lenmem==NULL ) {
346 | st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded );
347 | }else{
348 | if (mem != NULL && *lenmem >= memneeded)
349 | st = (kiss_fft_cfg)mem;
350 | *lenmem = memneeded;
351 | }
352 | if (st) {
353 | int i;
354 | st->nfft=nfft;
355 | st->inverse = inverse_fft;
356 |
357 | for (i=0;iinverse)
361 | phase *= -1;
362 | kf_cexp(st->twiddles+i, phase );
363 | }
364 |
365 | kf_factor(nfft,st->factors);
366 | }
367 | return st;
368 | }
369 |
370 |
371 | void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride)
372 | {
373 | if (fin == fout) {
374 | //NOTE: this is not really an in-place FFT algorithm.
375 | //It just performs an out-of-place FFT into a temp buffer
376 | kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft);
377 | kf_work(tmpbuf,fin,1,in_stride, st->factors,st);
378 | memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft);
379 | KISS_FFT_TMP_FREE(tmpbuf);
380 | }else{
381 | kf_work( fout, fin, 1,in_stride, st->factors,st );
382 | }
383 | }
384 |
385 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
386 | {
387 | kiss_fft_stride(cfg,fin,fout,1);
388 | }
389 |
390 |
391 | void kiss_fft_cleanup(void)
392 | {
393 | // nothing needed any more
394 | }
395 |
396 | int kiss_fft_next_fast_size(int n)
397 | {
398 | while(1) {
399 | int m=n;
400 | while ( (m%2) == 0 ) m/=2;
401 | while ( (m%3) == 0 ) m/=3;
402 | while ( (m%5) == 0 ) m/=5;
403 | if (m<=1)
404 | break; /* n is completely factorable by twos, threes, and fives */
405 | n++;
406 | }
407 | return n;
408 | }
409 |
--------------------------------------------------------------------------------
/jni/codec2/kiss_fft.h:
--------------------------------------------------------------------------------
1 | #ifndef KISS_FFT_H
2 | #define KISS_FFT_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | #ifdef __cplusplus
10 | extern "C" {
11 | #endif
12 |
13 | /*
14 | ATTENTION!
15 | If you would like a :
16 | -- a utility that will handle the caching of fft objects
17 | -- real-only (no imaginary time component ) FFT
18 | -- a multi-dimensional FFT
19 | -- a command-line utility to perform ffts
20 | -- a command-line utility to perform fast-convolution filtering
21 |
22 | Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c
23 | in the tools/ directory.
24 | */
25 |
26 | #ifdef USE_SIMD
27 | # include
28 | # define kiss_fft_scalar __m128
29 | #define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16)
30 | #define KISS_FFT_FREE _mm_free
31 | #else
32 | #define KISS_FFT_MALLOC malloc
33 | #define KISS_FFT_FREE free
34 | #endif
35 |
36 |
37 | #ifdef FIXED_POINT
38 | #include
39 | # if (FIXED_POINT == 32)
40 | # define kiss_fft_scalar int32_t
41 | # else
42 | # define kiss_fft_scalar int16_t
43 | # endif
44 | #else
45 | # ifndef kiss_fft_scalar
46 | /* default is float */
47 | # define kiss_fft_scalar float
48 | # endif
49 | #endif
50 |
51 | typedef struct {
52 | kiss_fft_scalar r;
53 | kiss_fft_scalar i;
54 | }kiss_fft_cpx;
55 |
56 | typedef struct kiss_fft_state* kiss_fft_cfg;
57 |
58 | /*
59 | * kiss_fft_alloc
60 | *
61 | * Initialize a FFT (or IFFT) algorithm's cfg/state buffer.
62 | *
63 | * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL);
64 | *
65 | * The return value from fft_alloc is a cfg buffer used internally
66 | * by the fft routine or NULL.
67 | *
68 | * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc.
69 | * The returned value should be free()d when done to avoid memory leaks.
70 | *
71 | * The state can be placed in a user supplied buffer 'mem':
72 | * If lenmem is not NULL and mem is not NULL and *lenmem is large enough,
73 | * then the function places the cfg in mem and the size used in *lenmem
74 | * and returns mem.
75 | *
76 | * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough),
77 | * then the function returns NULL and places the minimum cfg
78 | * buffer size in *lenmem.
79 | * */
80 |
81 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem);
82 |
83 | /*
84 | * kiss_fft(cfg,in_out_buf)
85 | *
86 | * Perform an FFT on a complex input buffer.
87 | * for a forward FFT,
88 | * fin should be f[0] , f[1] , ... ,f[nfft-1]
89 | * fout will be F[0] , F[1] , ... ,F[nfft-1]
90 | * Note that each element is complex and can be accessed like
91 | f[k].r and f[k].i
92 | * */
93 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
94 |
95 | /*
96 | A more generic version of the above function. It reads its input from every Nth sample.
97 | * */
98 | void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride);
99 |
100 | /* If kiss_fft_alloc allocated a buffer, it is one contiguous
101 | buffer and can be simply free()d when no longer needed*/
102 | #define kiss_fft_free free
103 |
104 | /*
105 | Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up
106 | your compiler output to call this before you exit.
107 | */
108 | void kiss_fft_cleanup(void);
109 |
110 |
111 | /*
112 | * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5)
113 | */
114 | int kiss_fft_next_fast_size(int n);
115 |
116 | /* for real ffts, we need an even size */
117 | #define kiss_fftr_next_fast_size_real(n) \
118 | (kiss_fft_next_fast_size( ((n)+1)>>1)<<1)
119 |
120 | #ifdef __cplusplus
121 | }
122 | #endif
123 |
124 | #endif
125 |
--------------------------------------------------------------------------------
/jni/codec2/lpc.c:
--------------------------------------------------------------------------------
1 | /*---------------------------------------------------------------------------*\
2 |
3 | FILE........: lpc.c
4 | AUTHOR......: David Rowe
5 | DATE CREATED: 30 Sep 1990 (!)
6 |
7 | Linear Prediction functions written in C.
8 |
9 | \*---------------------------------------------------------------------------*/
10 |
11 | /*
12 | Copyright (C) 2009-2012 David Rowe
13 |
14 | All rights reserved.
15 |
16 | This program is free software; you can redistribute it and/or modify
17 | it under the terms of the GNU Lesser General Public License version 2.1, as
18 | published by the Free Software Foundation. This program is
19 | distributed in the hope that it will be useful, but WITHOUT ANY
20 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
22 | License for more details.
23 |
24 | You should have received a copy of the GNU Lesser General Public License
25 | along with this program; if not, see .
26 | */
27 |
28 | #define LPC_MAX_N 512 /* maximum no. of samples in frame */
29 | #define PI 3.141592654 /* mathematical constant */
30 |
31 | #define ALPHA 1.0
32 | #define BETA 0.94
33 |
34 | #include
35 | #include
36 | #include "defines.h"
37 | #include "lpc.h"
38 |
39 | /*---------------------------------------------------------------------------*\
40 |
41 | pre_emp()
42 |
43 | Pre-emphasise (high pass filter with zero close to 0 Hz) a frame of
44 | speech samples. Helps reduce dynamic range of LPC spectrum, giving
45 | greater weight and hensea better match to low energy formants.
46 |
47 | Should be balanced by de-emphasis of the output speech.
48 |
49 | \*---------------------------------------------------------------------------*/
50 |
51 | void pre_emp(
52 | float Sn_pre[], /* output frame of speech samples */
53 | float Sn[], /* input frame of speech samples */
54 | float *mem, /* Sn[-1]single sample memory */
55 | int Nsam /* number of speech samples to use */
56 | )
57 | {
58 | int i;
59 |
60 | for(i=0; i 1.0)
173 | k[i] = 0.0;
174 |
175 | a[i][i] = k[i];
176 |
177 | for(j=1; j<=i-1; j++)
178 | a[i][j] = a[i-1][j] + k[i]*a[i-1][i-j]; /* Equation 38c, Makhoul */
179 |
180 | E[i] = (1-k[i]*k[i])*E[i-1]; /* Equation 38d, Makhoul */
181 | }
182 |
183 | for(i=1; i<=order; i++)
184 | lpcs[i] = a[order][i];
185 | lpcs[0] = 1.0;
186 | }
187 |
188 | /*---------------------------------------------------------------------------*\
189 |
190 | inverse_filter()
191 |
192 | Inverse Filter, A(z). Produces an array of residual samples from an array
193 | of input samples and linear prediction coefficients.
194 |
195 | The filter memory is stored in the first order samples of the input array.
196 |
197 | \*---------------------------------------------------------------------------*/
198 |
199 | void inverse_filter(
200 | float Sn[], /* Nsam input samples */
201 | float a[], /* LPCs for this frame of samples */
202 | int Nsam, /* number of samples */
203 | float res[], /* Nsam residual samples */
204 | int order /* order of LPC */
205 | )
206 | {
207 | int i,j; /* loop variables */
208 |
209 | for(i=0; i.
26 | */
27 |
28 | #ifndef __LPC__
29 | #define __LPC__
30 |
31 | #define LPC_MAX_ORDER 20
32 |
33 | void pre_emp(float Sn_pre[], float Sn[], float *mem, int Nsam);
34 | void de_emp(float Sn_se[], float Sn[], float *mem, int Nsam);
35 | void hanning_window(float Sn[], float Wn[], int Nsam);
36 | void autocorrelate(float Sn[], float Rn[], int Nsam, int order);
37 | void levinson_durbin(float R[], float lpcs[], int order);
38 | void inverse_filter(float Sn[], float a[], int Nsam, float res[], int order);
39 | void synthesis_filter(float res[], float a[], int Nsam, int order, float Sn_[]);
40 | void find_aks(float Sn[], float a[], int Nsam, int order, float *E);
41 | void weight(float ak[], float gamma, int order, float akw[]);
42 |
43 | #endif
44 |
--------------------------------------------------------------------------------
/jni/codec2/lsp.c:
--------------------------------------------------------------------------------
1 | /*---------------------------------------------------------------------------*\
2 |
3 | FILE........: lsp.c
4 | AUTHOR......: David Rowe
5 | DATE CREATED: 24/2/93
6 |
7 |
8 | This file contains functions for LPC to LSP conversion and LSP to
9 | LPC conversion. Note that the LSP coefficients are not in radians
10 | format but in the x domain of the unit circle.
11 |
12 | \*---------------------------------------------------------------------------*/
13 |
14 | /*
15 | Copyright (C) 2009 David Rowe
16 |
17 | All rights reserved.
18 |
19 | This program is free software; you can redistribute it and/or modify
20 | it under the terms of the GNU Lesser General Public License version 2.1, as
21 | published by the Free Software Foundation. This program is
22 | distributed in the hope that it will be useful, but WITHOUT ANY
23 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
24 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
25 | License for more details.
26 |
27 | You should have received a copy of the GNU Lesser General Public License
28 | along with this program; if not, see .
29 | */
30 |
31 | #include "defines.h"
32 | #include "lsp.h"
33 | #include
34 | #include
35 | #include
36 |
37 | /* Only 10 gets used, so far. */
38 | #define LSP_MAX_ORDER 20
39 |
40 | /*---------------------------------------------------------------------------*\
41 |
42 | Introduction to Line Spectrum Pairs (LSPs)
43 | ------------------------------------------
44 |
45 | LSPs are used to encode the LPC filter coefficients {ak} for
46 | transmission over the channel. LSPs have several properties (like
47 | less sensitivity to quantisation noise) that make them superior to
48 | direct quantisation of {ak}.
49 |
50 | A(z) is a polynomial of order lpcrdr with {ak} as the coefficients.
51 |
52 | A(z) is transformed to P(z) and Q(z) (using a substitution and some
53 | algebra), to obtain something like:
54 |
55 | A(z) = 0.5[P(z)(z+z^-1) + Q(z)(z-z^-1)] (1)
56 |
57 | As you can imagine A(z) has complex zeros all over the z-plane. P(z)
58 | and Q(z) have the very neat property of only having zeros _on_ the
59 | unit circle. So to find them we take a test point z=exp(jw) and
60 | evaluate P (exp(jw)) and Q(exp(jw)) using a grid of points between 0
61 | and pi.
62 |
63 | The zeros (roots) of P(z) also happen to alternate, which is why we
64 | swap coefficients as we find roots. So the process of finding the
65 | LSP frequencies is basically finding the roots of 5th order
66 | polynomials.
67 |
68 | The root so P(z) and Q(z) occur in symmetrical pairs at +/-w, hence
69 | the name Line Spectrum Pairs (LSPs).
70 |
71 | To convert back to ak we just evaluate (1), "clocking" an impulse
72 | thru it lpcrdr times gives us the impulse response of A(z) which is
73 | {ak}.
74 |
75 | \*---------------------------------------------------------------------------*/
76 |
77 | /*---------------------------------------------------------------------------*\
78 |
79 | FUNCTION....: cheb_poly_eva()
80 | AUTHOR......: David Rowe
81 | DATE CREATED: 24/2/93
82 |
83 | This function evalutes a series of chebyshev polynomials
84 |
85 | FIXME: performing memory allocation at run time is very inefficient,
86 | replace with stack variables of MAX_P size.
87 |
88 | \*---------------------------------------------------------------------------*/
89 |
90 |
91 | static float
92 | cheb_poly_eva(float *coef,float x,int m)
93 | /* float coef[] coefficients of the polynomial to be evaluated */
94 | /* float x the point where polynomial is to be evaluated */
95 | /* int m order of the polynomial */
96 | {
97 | int i;
98 | float *t,*u,*v,sum;
99 | float T[(LSP_MAX_ORDER / 2) + 1];
100 |
101 | /* Initialise pointers */
102 |
103 | t = T; /* T[i-2] */
104 | *t++ = 1.0;
105 | u = t--; /* T[i-1] */
106 | *u++ = x;
107 | v = u--; /* T[i] */
108 |
109 | /* Evaluate chebyshev series formulation using iterative approach */
110 |
111 | for(i=2;i<=m/2;i++)
112 | *v++ = (2*x)*(*u++) - *t++; /* T[i] = 2*x*T[i-1] - T[i-2] */
113 |
114 | sum=0.0; /* initialise sum to zero */
115 | t = T; /* reset pointer */
116 |
117 | /* Evaluate polynomial and return value also free memory space */
118 |
119 | for(i=0;i<=m/2;i++)
120 | sum+=coef[(m/2)-i]**t++;
121 |
122 | return sum;
123 | }
124 |
125 |
126 | /*---------------------------------------------------------------------------*\
127 |
128 | FUNCTION....: lpc_to_lsp()
129 | AUTHOR......: David Rowe
130 | DATE CREATED: 24/2/93
131 |
132 | This function converts LPC coefficients to LSP coefficients.
133 |
134 | \*---------------------------------------------------------------------------*/
135 |
136 | int lpc_to_lsp (float *a, int lpcrdr, float *freq, int nb, float delta)
137 | /* float *a lpc coefficients */
138 | /* int lpcrdr order of LPC coefficients (10) */
139 | /* float *freq LSP frequencies in radians */
140 | /* int nb number of sub-intervals (4) */
141 | /* float delta grid spacing interval (0.02) */
142 | {
143 | float psuml,psumr,psumm,temp_xr,xl,xr,xm = 0;
144 | float temp_psumr;
145 | int i,j,m,flag,k;
146 | float *px; /* ptrs of respective P'(z) & Q'(z) */
147 | float *qx;
148 | float *p;
149 | float *q;
150 | float *pt; /* ptr used for cheb_poly_eval()
151 | whether P' or Q' */
152 | int roots=0; /* number of roots found */
153 | float Q[LSP_MAX_ORDER + 1];
154 | float P[LSP_MAX_ORDER + 1];
155 |
156 | flag = 1;
157 | m = lpcrdr/2; /* order of P'(z) & Q'(z) polynimials */
158 |
159 | /* Allocate memory space for polynomials */
160 |
161 | /* determine P'(z)'s and Q'(z)'s coefficients where
162 | P'(z) = P(z)/(1 + z^(-1)) and Q'(z) = Q(z)/(1-z^(-1)) */
163 |
164 | px = P; /* initilaise ptrs */
165 | qx = Q;
166 | p = px;
167 | q = qx;
168 | *px++ = 1.0;
169 | *qx++ = 1.0;
170 | for(i=1;i<=m;i++){
171 | *px++ = a[i]+a[lpcrdr+1-i]-*p++;
172 | *qx++ = a[i]-a[lpcrdr+1-i]+*q++;
173 | }
174 | px = P;
175 | qx = Q;
176 | for(i=0;i= -1.0)){
201 | xr = xl - delta ; /* interval spacing */
202 | psumr = cheb_poly_eva(pt,xr,lpcrdr);/* poly(xl-delta_x) */
203 | temp_psumr = psumr;
204 | temp_xr = xr;
205 |
206 | /* if no sign change increment xr and re-evaluate
207 | poly(xr). Repeat til sign change. if a sign change has
208 | occurred the interval is bisected and then checked again
209 | for a sign change which determines in which interval the
210 | zero lies in. If there is no sign change between poly(xm)
211 | and poly(xl) set interval between xm and xr else set
212 | interval between xl and xr and repeat till root is located
213 | within the specified limits */
214 |
215 | if((psumr*psuml)<0.0){
216 | roots++;
217 |
218 | psumm=psuml;
219 | for(k=0;k<=nb;k++){
220 | xm = (xl+xr)/2; /* bisect the interval */
221 | psumm=cheb_poly_eva(pt,xm,lpcrdr);
222 | if(psumm*psuml>0.){
223 | psuml=psumm;
224 | xl=xm;
225 | }
226 | else{
227 | psumr=psumm;
228 | xr=xm;
229 | }
230 | }
231 |
232 | /* once zero is found, reset initial interval to xr */
233 | freq[j] = (xm);
234 | xl = xm;
235 | flag = 0; /* reset flag for next search */
236 | }
237 | else{
238 | psuml=temp_psumr;
239 | xl=temp_xr;
240 | }
241 | }
242 | }
243 |
244 | /* convert from x domain to radians */
245 |
246 | for(i=0; i.
29 | */
30 |
31 | #ifndef __LSP__
32 | #define __LSP__
33 |
34 | int lpc_to_lsp (float *a, int lpcrdr, float *freq, int nb, float delta);
35 | void lsp_to_lpc(float *freq, float *ak, int lpcrdr);
36 |
37 | #endif
38 |
--------------------------------------------------------------------------------
/jni/codec2/nlp.h:
--------------------------------------------------------------------------------
1 | /*---------------------------------------------------------------------------*\
2 |
3 | FILE........: nlp.c
4 | AUTHOR......: David Rowe
5 | DATE CREATED: 23/3/93
6 |
7 | Non Linear Pitch (NLP) estimation functions.
8 |
9 | \*---------------------------------------------------------------------------*/
10 |
11 | /*
12 | Copyright (C) 2009 David Rowe
13 |
14 | All rights reserved.
15 |
16 | This program is free software; you can redistribute it and/or modify
17 | it under the terms of the GNU Lesser General Public License version 2.1, as
18 | published by the Free Software Foundation. This program is
19 | distributed in the hope that it will be useful, but WITHOUT ANY
20 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
22 | License for more details.
23 |
24 | You should have received a copy of the GNU Lesser General Public License
25 | along with this program; if not, see .
26 | */
27 |
28 | #ifndef __NLP__
29 | #define __NLP__
30 |
31 | #include "comp.h"
32 |
33 | void *nlp_create();
34 | void nlp_destroy(void *nlp_state);
35 | float nlp(void *nlp_state, float Sn[], int n, int m, int pmin, int pmax,
36 | float *pitch, COMP Sw[], COMP W[], float *prev_Wo);
37 |
38 | #endif
39 |
--------------------------------------------------------------------------------
/jni/codec2/os.h:
--------------------------------------------------------------------------------
1 | /* Generate using fir1(47,1/6) in Octave */
2 |
3 | const float fdmdv_os_filter[]= {
4 | -3.55606818e-04,
5 | -8.98615286e-04,
6 | -1.40119781e-03,
7 | -1.71713852e-03,
8 | -1.56471179e-03,
9 | -6.28128960e-04,
10 | 1.24522223e-03,
11 | 3.83138676e-03,
12 | 6.41309478e-03,
13 | 7.85893186e-03,
14 | 6.93514929e-03,
15 | 2.79361991e-03,
16 | -4.51051400e-03,
17 | -1.36671853e-02,
18 | -2.21034939e-02,
19 | -2.64084653e-02,
20 | -2.31425052e-02,
21 | -9.84218694e-03,
22 | 1.40648474e-02,
23 | 4.67316298e-02,
24 | 8.39615986e-02,
25 | 1.19925275e-01,
26 | 1.48381174e-01,
27 | 1.64097819e-01,
28 | 1.64097819e-01,
29 | 1.48381174e-01,
30 | 1.19925275e-01,
31 | 8.39615986e-02,
32 | 4.67316298e-02,
33 | 1.40648474e-02,
34 | -9.84218694e-03,
35 | -2.31425052e-02,
36 | -2.64084653e-02,
37 | -2.21034939e-02,
38 | -1.36671853e-02,
39 | -4.51051400e-03,
40 | 2.79361991e-03,
41 | 6.93514929e-03,
42 | 7.85893186e-03,
43 | 6.41309478e-03,
44 | 3.83138676e-03,
45 | 1.24522223e-03,
46 | -6.28128960e-04,
47 | -1.56471179e-03,
48 | -1.71713852e-03,
49 | -1.40119781e-03,
50 | -8.98615286e-04,
51 | -3.55606818e-04
52 | };
53 |
54 |
--------------------------------------------------------------------------------
/jni/codec2/pack.c:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (C) 2010 Perens LLC
3 |
4 | All rights reserved.
5 |
6 | This program is free software; you can redistribute it and/or modify
7 | it under the terms of the GNU Lesser General Public License version 2.1, as
8 | published by the Free Software Foundation. This program is
9 | distributed in the hope that it will be useful, but WITHOUT ANY
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
12 | License for more details.
13 |
14 | You should have received a copy of the GNU Lesser General Public License
15 | along with this program; if not, see .
16 | */
17 |
18 | #include "defines.h"
19 | #include "quantise.h"
20 | #include
21 |
22 | /* Compile-time constants */
23 | /* Size of unsigned char in bits. Assumes 8 bits-per-char. */
24 | static const unsigned int WordSize = 8;
25 |
26 | /* Mask to pick the bit component out of bitIndex. */
27 | static const unsigned int IndexMask = 0x7;
28 |
29 | /* Used to pick the word component out of bitIndex. */
30 | static const unsigned int ShiftRight = 3;
31 |
32 | /** Pack a bit field into a bit string, encoding the field in Gray code.
33 | *
34 | * The output is an array of unsigned char data. The fields are efficiently
35 | * packed into the bit string. The Gray coding is a naive attempt to reduce
36 | * the effect of single-bit errors, we expect to do a better job as the
37 | * codec develops.
38 | *
39 | * This code would be simpler if it just set one bit at a time in the string,
40 | * but would hit the same cache line more often. I'm not sure the complexity
41 | * gains us anything here.
42 | *
43 | * Although field is currently of int type rather than unsigned for
44 | * compatibility with the rest of the code, indices are always expected to
45 | * be >= 0.
46 | */
47 | void
48 | pack(
49 | unsigned char * bitArray, /* The output bit string. */
50 | unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/
51 | int field, /* The bit field to be packed. */
52 | unsigned int fieldWidth/* Width of the field in BITS, not bytes. */
53 | )
54 | {
55 | /* Convert the field to Gray code */
56 | field = (field >> 1) ^ field;
57 |
58 | do {
59 | unsigned int bI = *bitIndex;
60 | unsigned int bitsLeft = WordSize - (bI & IndexMask);
61 | unsigned int sliceWidth =
62 | bitsLeft < fieldWidth ? bitsLeft : fieldWidth;
63 | unsigned int wordIndex = bI >> ShiftRight;
64 |
65 | bitArray[wordIndex] |=
66 | ((unsigned char)((field >> (fieldWidth - sliceWidth))
67 | << (bitsLeft - sliceWidth)));
68 |
69 | *bitIndex = bI + sliceWidth;
70 | fieldWidth -= sliceWidth;
71 | } while ( fieldWidth != 0 );
72 | }
73 |
74 | /** Unpack a field from a bit string, converting from Gray code to binary.
75 | *
76 | */
77 | int
78 | unpack(
79 | const unsigned char * bitArray, /* The input bit string. */
80 | unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/
81 | unsigned int fieldWidth/* Width of the field in BITS, not bytes. */
82 | )
83 | {
84 | unsigned int field = 0;
85 | unsigned int t;
86 |
87 | do {
88 | unsigned int bI = *bitIndex;
89 | unsigned int bitsLeft = WordSize - (bI & IndexMask);
90 | unsigned int sliceWidth =
91 | bitsLeft < fieldWidth ? bitsLeft : fieldWidth;
92 |
93 | field |= (((bitArray[bI >> ShiftRight] >> (bitsLeft - sliceWidth)) & ((1 << sliceWidth) - 1)) << (fieldWidth - sliceWidth));
94 |
95 | *bitIndex = bI + sliceWidth;
96 | fieldWidth -= sliceWidth;
97 | } while ( fieldWidth != 0 );
98 |
99 | /* Convert from Gray code to binary. Works for maximum 8-bit fields. */
100 | t = field ^ (field >> 8);
101 | t ^= (t >> 4);
102 | t ^= (t >> 2);
103 | t ^= (t >> 1);
104 | return t;
105 | }
106 |
--------------------------------------------------------------------------------
/jni/codec2/phase.c:
--------------------------------------------------------------------------------
1 | /*---------------------------------------------------------------------------*\
2 |
3 | FILE........: phase.c
4 | AUTHOR......: David Rowe
5 | DATE CREATED: 1/2/09
6 |
7 | Functions for modelling and synthesising phase.
8 |
9 | \*---------------------------------------------------------------------------*/
10 |
11 | /*
12 | Copyright (C) 2009 David Rowe
13 |
14 | All rights reserved.
15 |
16 | This program is free software; you can redistribute it and/or modify
17 | it under the terms of the GNU Lesser General Public License version 2.1, as
18 | published by the Free Software Foundation. This program is
19 | distributed in the hope that it will be useful, but WITHOUT ANY
20 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
22 | License for more details.
23 |
24 | You should have received a copy of the GNU Lesser General Public License
25 | along with this program; if not,see .
26 | */
27 |
28 | #include "defines.h"
29 | #include "phase.h"
30 | #include "kiss_fft.h"
31 | #include "comp.h"
32 | #include "glottal.c"
33 |
34 | #include
35 | #include
36 | #include
37 | #include
38 | #include
39 |
40 | #define GLOTTAL_FFT_SIZE 512
41 |
42 | /*---------------------------------------------------------------------------*\
43 |
44 | aks_to_H()
45 |
46 | Samples the complex LPC synthesis filter spectrum at the harmonic
47 | frequencies.
48 |
49 | \*---------------------------------------------------------------------------*/
50 |
51 | void aks_to_H(
52 | kiss_fft_cfg fft_fwd_cfg,
53 | MODEL *model, /* model parameters */
54 | float aks[], /* LPC's */
55 | float G, /* energy term */
56 | COMP H[], /* complex LPC spectral samples */
57 | int order
58 | )
59 | {
60 | COMP pw[FFT_ENC]; /* power spectrum (input) */
61 | COMP Pw[FFT_ENC]; /* power spectrum (output) */
62 | int i,m; /* loop variables */
63 | int am,bm; /* limits of current band */
64 | float r; /* no. rads/bin */
65 | float Em; /* energy in band */
66 | float Am; /* spectral amplitude sample */
67 | int b; /* centre bin of harmonic */
68 | float phi_; /* phase of LPC spectra */
69 |
70 | r = TWO_PI/(FFT_ENC);
71 |
72 | /* Determine DFT of A(exp(jw)) ------------------------------------------*/
73 |
74 | init_comp_array(pw, FFT_ENC);
75 |
76 | for(i=0; i<=order; i++)
77 | pw[i].real = aks[i];
78 |
79 | kiss_fft(fft_fwd_cfg, (kiss_fft_cpx *)pw, (kiss_fft_cpx *)Pw);
80 |
81 | /* Sample magnitude and phase at harmonics */
82 |
83 | for(m=1; m<=model->L; m++) {
84 | am = floorf((m - 0.5)*model->Wo/r + 0.5);
85 | bm = floorf((m + 0.5)*model->Wo/r + 0.5);
86 | b = floorf(m*model->Wo/r + 0.5);
87 |
88 | Em = 0.0;
89 | for(i=am; iWo)*N/2;
218 | */
219 |
220 | ex_phase[0] += (model->Wo)*N;
221 | ex_phase[0] -= TWO_PI*floorf(ex_phase[0]/TWO_PI + 0.5);
222 | r = TWO_PI/GLOTTAL_FFT_SIZE;
223 |
224 | for(m=1; m<=model->L; m++) {
225 |
226 | /* generate excitation */
227 |
228 | if (model->voiced) {
229 | //float rnd;
230 |
231 | b = floorf(m*model->Wo/r + 0.5);
232 | if (b > ((GLOTTAL_FFT_SIZE/2)-1)) {
233 | b = (GLOTTAL_FFT_SIZE/2)-1;
234 | }
235 |
236 | /* I think adding a little jitter helps improve low pitch
237 | males like hts1a. This moves the onset of each harmonic
238 | over +/- 0.25 of a sample.
239 | */
240 | //jitter = 0.25*(1.0 - 2.0*rand()/RAND_MAX);
241 | jitter = 0;
242 |
243 | //rnd = (PI/8)*(1.0 - 2.0*rand()/RAND_MAX);
244 | Ex[m].real = cosf(ex_phase[0]*m/* - jitter*model->Wo*m + glottal[b]*/);
245 | Ex[m].imag = sinf(ex_phase[0]*m/* - jitter*model->Wo*m + glottal[b]*/);
246 | }
247 | else {
248 |
249 | /* When a few samples were tested I found that LPC filter
250 | phase is not needed in the unvoiced case, but no harm in
251 | keeping it.
252 | */
253 | float phi = TWO_PI*(float)rand()/RAND_MAX;
254 | Ex[m].real = cosf(phi);
255 | Ex[m].imag = sinf(phi);
256 | }
257 |
258 | /* filter using LPC filter */
259 |
260 | A_[m].real = H[m].real*Ex[m].real - H[m].imag*Ex[m].imag;
261 | A_[m].imag = H[m].imag*Ex[m].real + H[m].real*Ex[m].imag;
262 |
263 | /* modify sinusoidal phase */
264 |
265 | new_phi = atan2f(A_[m].imag, A_[m].real+1E-12);
266 | model->phi[m] = new_phi;
267 | }
268 |
269 | }
270 |
271 |
--------------------------------------------------------------------------------
/jni/codec2/phase.h:
--------------------------------------------------------------------------------
1 | /*---------------------------------------------------------------------------*\
2 |
3 | FILE........: phase.h
4 | AUTHOR......: David Rowe
5 | DATE CREATED: 1/2/09
6 |
7 | Functions for modelling phase.
8 |
9 | \*---------------------------------------------------------------------------*/
10 |
11 | /*
12 | Copyright (C) 2009 David Rowe
13 |
14 | All rights reserved.
15 |
16 | This program is free software; you can redistribute it and/or modify
17 | it under the terms of the GNU Lesser General Public License version 2.1, as
18 | published by the Free Software Foundation. This program is
19 | distributed in the hope that it will be useful, but WITHOUT ANY
20 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
22 | License for more details.
23 |
24 | You should have received a copy of the GNU Lesser General Public License
25 | along with this program; if not, see .
26 | */
27 |
28 | #ifndef __PHASE__
29 | #define __PHASE__
30 |
31 | #include "kiss_fft.h"
32 |
33 | void phase_synth_zero_order(kiss_fft_cfg fft_dec_cfg,
34 | MODEL *model,
35 | float aks[],
36 | float *ex_phase,
37 | int order);
38 |
39 | #endif
40 |
--------------------------------------------------------------------------------
/jni/codec2/pilot_coeff.h:
--------------------------------------------------------------------------------
1 | /* Generated by pilot_coeff_file() Octave function */
2 |
3 | const float pilot_coeff[]={
4 | 0.00204705,
5 | 0.00276339,
6 | 0.00432595,
7 | 0.00697042,
8 | 0.0108452,
9 | 0.0159865,
10 | 0.0223035,
11 | 0.029577,
12 | 0.0374709,
13 | 0.045557,
14 | 0.0533491,
15 | 0.0603458,
16 | 0.0660751,
17 | 0.070138,
18 | 0.0722452,
19 | 0.0722452,
20 | 0.070138,
21 | 0.0660751,
22 | 0.0603458,
23 | 0.0533491,
24 | 0.045557,
25 | 0.0374709,
26 | 0.029577,
27 | 0.0223035,
28 | 0.0159865,
29 | 0.0108452,
30 | 0.00697042,
31 | 0.00432595,
32 | 0.00276339,
33 | 0.00204705
34 | };
35 |
--------------------------------------------------------------------------------
/jni/codec2/postfilter.c:
--------------------------------------------------------------------------------
1 | /*---------------------------------------------------------------------------*\
2 |
3 | FILE........: postfilter.c
4 | AUTHOR......: David Rowe
5 | DATE CREATED: 13/09/09
6 |
7 | Postfilter to improve sound quality for speech with high levels of
8 | background noise. Unlike mixed-excitation models requires no bits
9 | to be transmitted to handle background noise.
10 |
11 | \*---------------------------------------------------------------------------*/
12 |
13 | /*
14 | Copyright (C) 2009 David Rowe
15 |
16 | All rights reserved.
17 |
18 | This program is free software; you can redistribute it and/or modify
19 | it under the terms of the GNU Lesser General Public License version 2.1, as
20 | published by the Free Software Foundation. This program is
21 | distributed in the hope that it will be useful, but WITHOUT ANY
22 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 | License for more details.
25 |
26 | You should have received a copy of the GNU Lesser General Public License
27 | along with this program; if not, see .
28 | */
29 |
30 | #include
31 | #include
32 | #include
33 | #include
34 |
35 | #include "defines.h"
36 | #include "comp.h"
37 | #include "dump.h"
38 | #include "postfilter.h"
39 |
40 | /*---------------------------------------------------------------------------*\
41 |
42 | DEFINES
43 |
44 | \*---------------------------------------------------------------------------*/
45 |
46 | #define BG_THRESH 40.0 /* only consider low levels signals for bg_est */
47 | #define BG_BETA 0.1 /* averaging filter constant */
48 | #define BG_MARGIN 6.0 /* harmonics this far above BG noise are
49 | randomised. Helped make bg noise less
50 | spikey (impulsive) for mmt1, but speech was
51 | perhaps a little rougher.
52 | */
53 |
54 | /*---------------------------------------------------------------------------*\
55 |
56 | postfilter()
57 |
58 | The post filter is designed to help with speech corrupted by
59 | background noise. The zero phase model tends to make speech with
60 | background noise sound "clicky". With high levels of background
61 | noise the low level inter-formant parts of the spectrum will contain
62 | noise rather than speech harmonics, so modelling them as voiced
63 | (i.e. a continuous, non-random phase track) is inaccurate.
64 |
65 | Some codecs (like MBE) have a mixed voicing model that breaks the
66 | spectrum into voiced and unvoiced regions. Several bits/frame
67 | (5-12) are required to transmit the frequency selective voicing
68 | information. Mixed excitation also requires accurate voicing
69 | estimation (parameter estimators always break occasionally under
70 | exceptional conditions).
71 |
72 | In our case we use a post filter approach which requires no
73 | additional bits to be transmitted. The decoder measures the average
74 | level of the background noise during unvoiced frames. If a harmonic
75 | is less than this level it is made unvoiced by randomising it's
76 | phases.
77 |
78 | This idea is rather experimental. Some potential problems that may
79 | happen:
80 |
81 | 1/ If someone says "aaaaaaaahhhhhhhhh" will background estimator track
82 | up to speech level? This would be a bad thing.
83 |
84 | 2/ If background noise suddenly dissapears from the source speech does
85 | estimate drop quickly? What is noise suddenly re-appears?
86 |
87 | 3/ Background noise with a non-flat sepctrum. Current algorithm just
88 | comsiders scpetrum as a whole, but this could be broken up into
89 | bands, each with their own estimator.
90 |
91 | 4/ Males and females with the same level of background noise. Check
92 | performance the same. Changing Wo affects width of each band, may
93 | affect bg energy estimates.
94 |
95 | 5/ Not sure what happens during long periods of voiced speech
96 | e.g. "sshhhhhhh"
97 |
98 | \*---------------------------------------------------------------------------*/
99 |
100 | void postfilter(
101 | MODEL *model,
102 | float *bg_est
103 | )
104 | {
105 | int m, uv;
106 | float e;
107 |
108 | /* determine average energy across spectrum */
109 |
110 | e = 0.0;
111 | for(m=1; m<=model->L; m++)
112 | e += model->A[m]*model->A[m];
113 |
114 | assert(e > 0.0);
115 | e = 10.0*log10f(e/model->L);
116 |
117 | /* If beneath threhold, update bg estimate. The idea
118 | of the threshold is to prevent updating during high level
119 | speech. */
120 |
121 | if ((e < BG_THRESH) && !model->voiced)
122 | *bg_est = *bg_est*(1.0 - BG_BETA) + e*BG_BETA;
123 |
124 | /* now mess with phases during voiced frames to make any harmonics
125 | less then our background estimate unvoiced.
126 | */
127 |
128 | uv = 0;
129 | if (model->voiced)
130 | for(m=1; m<=model->L; m++)
131 | if (20.0*log10f(model->A[m]) < (*bg_est + BG_MARGIN)) {
132 | model->phi[m] = TWO_PI*(float)rand()/RAND_MAX;
133 | uv++;
134 | }
135 |
136 | #ifdef DUMP
137 | dump_bg(e, *bg_est, 100.0*uv/model->L);
138 | #endif
139 |
140 | }
141 |
--------------------------------------------------------------------------------
/jni/codec2/postfilter.h:
--------------------------------------------------------------------------------
1 | /*---------------------------------------------------------------------------*\
2 |
3 | FILE........: postfilter.h
4 | AUTHOR......: David Rowe
5 | DATE CREATED: 13/09/09
6 |
7 | Postfilter header file.
8 |
9 | \*---------------------------------------------------------------------------*/
10 |
11 | /*
12 | Copyright (C) 2009 David Rowe
13 |
14 | All rights reserved.
15 |
16 | This program is free software; you can redistribute it and/or modify
17 | it under the terms of the GNU Lesser General Public License version 2.1, as
18 | published by the Free Software Foundation. This program is
19 | distributed in the hope that it will be useful, but WITHOUT ANY
20 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
22 | License for more details.
23 |
24 | You should have received a copy of the GNU Lesser General Public License
25 | along with this program; if not, see .
26 | */
27 |
28 | #ifndef __POSTFILTER__
29 | #define __POSTFILTER__
30 |
31 | void postfilter(MODEL *model, float *bg_est);
32 |
33 | #endif
34 |
--------------------------------------------------------------------------------
/jni/codec2/quantise.h:
--------------------------------------------------------------------------------
1 | /*---------------------------------------------------------------------------*\
2 |
3 | FILE........: quantise.h
4 | AUTHOR......: David Rowe
5 | DATE CREATED: 31/5/92
6 |
7 | Quantisation functions for the sinusoidal coder.
8 |
9 | \*---------------------------------------------------------------------------*/
10 |
11 | /*
12 | All rights reserved.
13 |
14 | This program is free software; you can redistribute it and/or modify
15 | it under the terms of the GNU Lesser General Public License version 2.1, as
16 | published by the Free Software Foundation. This program is
17 | distributed in the hope that it will be useful, but WITHOUT ANY
18 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
20 | License for more details.
21 |
22 | You should have received a copy of the GNU Lesser General Public License
23 | along with this program; if not, see .
24 | */
25 |
26 | #ifndef __QUANTISE__
27 | #define __QUANTISE__
28 |
29 | #include "kiss_fft.h"
30 |
31 | #define WO_BITS 7
32 | #define WO_LEVELS (1<.
26 | */
27 |
28 | #ifndef __SINE__
29 | #define __SINE__
30 |
31 | #include "defines.h"
32 | #include "comp.h"
33 | #include "kiss_fft.h"
34 |
35 | void make_analysis_window(kiss_fft_cfg fft_fwd_cfg, float w[], COMP W[]);
36 | float hpf(float x, float states[]);
37 | void dft_speech(kiss_fft_cfg fft_fwd_cfg, COMP Sw[], float Sn[], float w[]);
38 | void two_stage_pitch_refinement(MODEL *model, COMP Sw[]);
39 | void estimate_amplitudes(MODEL *model, COMP Sw[], COMP W[]);
40 | float est_voicing_mbe(MODEL *model, COMP Sw[], COMP W[], COMP Sw_[],COMP Ew[],
41 | float prev_Wo);
42 | void make_synthesis_window(float Pn[]);
43 | void synthesise(kiss_fft_cfg fft_inv_cfg, float Sn_[], MODEL *model, float Pn[], int shift);
44 |
45 | #endif
46 |
--------------------------------------------------------------------------------
/jni/codec2/test_bits.h:
--------------------------------------------------------------------------------
1 | /* Generated by test_bits_file() Octave function */
2 |
3 | const int test_bits[]={
4 | 0,
5 | 1,
6 | 1,
7 | 0,
8 | 0,
9 | 0,
10 | 1,
11 | 1,
12 | 0,
13 | 0,
14 | 1,
15 | 0,
16 | 1,
17 | 0,
18 | 0,
19 | 1,
20 | 0,
21 | 1,
22 | 1,
23 | 0,
24 | 0,
25 | 1,
26 | 1,
27 | 0,
28 | 0,
29 | 0,
30 | 0,
31 | 0,
32 | 0,
33 | 0,
34 | 0,
35 | 0,
36 | 0,
37 | 0,
38 | 0,
39 | 0,
40 | 1,
41 | 1,
42 | 1,
43 | 0,
44 | 1,
45 | 1,
46 | 0,
47 | 0,
48 | 1,
49 | 1,
50 | 1,
51 | 0,
52 | 1,
53 | 1,
54 | 0,
55 | 1,
56 | 1,
57 | 1,
58 | 1,
59 | 1,
60 | 0,
61 | 0,
62 | 1,
63 | 0,
64 | 0,
65 | 1,
66 | 1,
67 | 1,
68 | 0,
69 | 0,
70 | 1,
71 | 1,
72 | 1,
73 | 0,
74 | 0,
75 | 0,
76 | 0,
77 | 1,
78 | 1,
79 | 1,
80 | 0,
81 | 0,
82 | 1,
83 | 1,
84 | 1,
85 | 1,
86 | 1,
87 | 0,
88 | 1,
89 | 1,
90 | 1,
91 | 0,
92 | 0,
93 | 1,
94 | 1,
95 | 0,
96 | 1,
97 | 1,
98 | 1,
99 | 1,
100 | 1,
101 | 1,
102 | 1,
103 | 0,
104 | 0,
105 | 1,
106 | 1,
107 | 0,
108 | 1,
109 | 0,
110 | 0,
111 | 0,
112 | 1,
113 | 1,
114 | 1,
115 | 0
116 | };
117 |
--------------------------------------------------------------------------------
/jni/codec2/varicode.c:
--------------------------------------------------------------------------------
1 | //==========================================================================
2 | // Name: varicode.h
3 | // Purpose: Varicode encoded and decode functions
4 | // Created: Nov 24, 2012
5 | // Authors: David Rowe
6 | //
7 | // To test:
8 | // $ gcc varicode.c -o varicode -DVARICODE_UNITTEST -Wall
9 | // $ ./varicode
10 | //
11 | // License:
12 | //
13 | // This program is free software; you can redistribute it and/or modify
14 | // it under the terms of the GNU General Public License version 2.1,
15 | // as published by the Free Software Foundation. This program is
16 | // distributed in the hope that it will be useful, but WITHOUT ANY
17 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
19 | // License for more details.
20 | //
21 | // You should have received a copy of the GNU General Public License
22 | // along with this program; if not, see .
23 | //
24 | //==========================================================================
25 |
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include "varicode.h"
31 | #include "varicode_table.h"
32 |
33 |
34 | /*
35 | output is an unpacked array of bits of maximum size max_out. Note
36 | unpacked arrays are a more suitable form for modulator input.
37 | */
38 |
39 | int varicode_encode(short varicode_out[], char ascii_in[], int max_out, int n_in) {
40 | int n_out, index, n_zeros, v_len;
41 | unsigned short byte1, byte2, packed;
42 |
43 | n_out = 0;
44 |
45 | while(n_in && (n_out < max_out)) {
46 |
47 | assert((unsigned int)(*ascii_in) < 128);
48 |
49 | index = 2*(unsigned int)(*ascii_in);
50 | byte1 = varicode_table[index];
51 | byte2 = varicode_table[index+1];
52 | packed = (byte1 << 8) + byte2;
53 | //printf("n_in: %d ascii_in: %c index: %d packed 0x%x\n", n_in, *ascii_in, index, packed);
54 | ascii_in++;
55 |
56 | n_zeros = 0;
57 | v_len = 0;
58 | while ((n_zeros < 2) && (n_out < max_out) && (v_len <= VARICODE_MAX_BITS)) {
59 | if (packed & 0x8000) {
60 | *varicode_out = 1;
61 | n_zeros = 0;
62 | }
63 | else {
64 | *varicode_out = 0;
65 | n_zeros++;
66 | }
67 | //printf("packed: 0x%x *varicode_out: %d n_zeros: %d v_len: %d\n", packed, *varicode_out, n_zeros,v_len );
68 | packed <<= 1;
69 | varicode_out++;
70 |
71 | n_out++;
72 | v_len++;
73 | }
74 | assert(v_len <= VARICODE_MAX_BITS);
75 |
76 | n_in--;
77 |
78 | }
79 |
80 | return n_out;
81 | }
82 |
83 | void varicode_decode_init(struct VARICODE_DEC *dec_states)
84 | {
85 | dec_states->state = 0;
86 | dec_states->n_zeros = 0;
87 | dec_states->v_len = 0;
88 | dec_states->packed = 0;
89 | }
90 |
91 | static int decode_one_bit(struct VARICODE_DEC *s, char *single_ascii, short varicode_in)
92 | {
93 | int found, i;
94 | unsigned short byte1, byte2;
95 |
96 | //printf("decode_one_bit : state: %d varicode_in: %d packed: 0x%x n_zeros: %d\n",
97 | // s->state, varicode_in, s->packed, s->n_zeros);
98 |
99 | if (s->state == 0) {
100 | if (!varicode_in)
101 | return 0;
102 | else
103 | s->state = 1;
104 | }
105 |
106 | if (s->state == 1) {
107 | if (varicode_in) {
108 | s->packed |= (0x8000 >> s->v_len);
109 | s->n_zeros = 0;
110 | }
111 | else {
112 | s->n_zeros++;
113 | }
114 | s->v_len++;
115 |
116 | found = 0;
117 |
118 | /* end of character code */
119 |
120 | if (s->n_zeros == 2) {
121 | if (s->v_len) {
122 | byte1 = s->packed >> 8;
123 | byte2 = s->packed & 0xff;
124 |
125 | /* run thru table but note with bit errors means we might
126 | not actually find a match */
127 |
128 | for(i=0; i<128; i++) {
129 | if ((byte1 == varicode_table[2*i]) && (byte2 == varicode_table[2*i+1])) {
130 | found = 1;
131 | *single_ascii = i;
132 | }
133 | }
134 | }
135 | varicode_decode_init(s);
136 | }
137 |
138 | /* code can run too long if we have a bit error */
139 |
140 | if (s->v_len > VARICODE_MAX_BITS)
141 | varicode_decode_init(s);
142 | }
143 |
144 | return found;
145 | }
146 |
147 | int varicode_decode(struct VARICODE_DEC *dec_states, char ascii_out[], short varicode_in[], int max_out, int n_in) {
148 | int output, n_out;
149 | char single_ascii;
150 |
151 | n_out = 0;
152 |
153 | //printf("varicode_decode: n_in: %d\n", n_in);
154 |
155 | while(n_in && (n_out < max_out)) {
156 | output = decode_one_bit(dec_states, &single_ascii, *varicode_in);
157 | varicode_in++;
158 | n_in--;
159 | if (output) {
160 | *ascii_out++ = single_ascii;
161 | n_out++;
162 | }
163 | }
164 |
165 | return n_out;
166 | }
167 |
168 |
169 | #ifdef VARICODE_UNITTEST
170 | int main(void) {
171 | char *ascii_in;
172 | short *varicode;
173 | int i, n_varicode_bits_out, n_ascii_chars_out, length, half;
174 | char *ascii_out;
175 | struct VARICODE_DEC dec_states;
176 |
177 | length = sizeof(varicode_table)/2;
178 |
179 | ascii_in = (char*)malloc(length);
180 | varicode = (short*)malloc(VARICODE_MAX_BITS*sizeof(short)*length);
181 | ascii_out = (char*)malloc(length);
182 |
183 | // 1. test all Varicode codes -------------------------------------------------------------
184 |
185 | for(i=0; i.
19 | //
20 | //==========================================================================
21 |
22 | #ifndef __VARICODE__
23 | #define __VARICODE__
24 |
25 | #ifdef __cplusplus
26 | extern "C" {
27 |
28 | #endif
29 |
30 | #define VARICODE_MAX_BITS (10+2) /* max varicode bits for each ascii character */
31 | /* 10 bits for code plus 2 0 bits for inter-character space */
32 |
33 | struct VARICODE_DEC {
34 | int state;
35 | int n_zeros;
36 | int v_len;
37 | unsigned short packed;
38 | };
39 |
40 | int varicode_encode(short varicode_out[], char ascii_in[], int max_out, int n_in);
41 | void varicode_decode_init(struct VARICODE_DEC *dec_states);
42 | int varicode_decode(struct VARICODE_DEC *dec_states, char ascii_out[], short varicode_in[], int max_out, int n_in);
43 |
44 | #ifdef __cplusplus
45 | }
46 | #endif
47 |
48 | #endif
49 |
--------------------------------------------------------------------------------
/jni/codec2/varicode_table.h:
--------------------------------------------------------------------------------
1 | //==========================================================================
2 | // Name: varicode_table.h
3 | // Purpose: Varicode look up table
4 | // Created: Nov 24, 2012
5 | // Authors: Clint Turner, KA7OEI, Peter Martinez, G3PLX
6 | //
7 | // License:
8 | //
9 | // This program is free software; you can redistribute it and/or modify
10 | // it under the terms of the GNU General Public License version 2.1,
11 | // as published by the Free Software Foundation. This program is
12 | // distributed in the hope that it will be useful, but WITHOUT ANY
13 | // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 | // License for more details.
16 | //
17 | // You should have received a copy of the GNU General Public License
18 | // along with this program; if not, see .
19 | //
20 | //==========================================================================
21 |
22 | /* The following table defines the PKS31 varicode. There are 128 entries,
23 | corresponding to ASCII characters 0-127 with two bytes for each entry. The bits
24 | for the varicode are to be shifted out MSB-first for both bytes, with the first byte
25 | in the table being the first one to be sent.
26 |
27 | More than one zero in sequence signifies the end of the character (i.e.
28 | two zeroes are the intercharacter sequence, so at least two zeroes should always be
29 | sent before the next character is sent.
30 |
31 | This file is constructed with information from the article "PSK31 Fundamentals"
32 | by Peter Martinez, G3PLX by Clint Turner, KA7OEI
33 | */
34 | unsigned char const varicode_table[256] = {
35 | 0b10101010,
36 | 0b11000000, // 0 NUL
37 | 0b10110110,
38 | 0b11000000, // 1 SOH
39 | 0b10111011,
40 | 0b01000000, // 2 STX
41 | 0b11011101,
42 | 0b11000000, // 3 ETX
43 | 0b10111010,
44 | 0b11000000, // 4 EOT
45 | 0b11010111,
46 | 0b11000000, // 5 ENQ
47 | 0b10111011,
48 | 0b11000000, // 6 ACK
49 | 0b10111111,
50 | 0b01000000, // 7 BEL
51 | 0b10111111,
52 | 0b11000000, // 8 BS
53 | 0b11101111,
54 | 0b00000000, // 9 HT
55 | 0b11101000,
56 | 0b00000000, // 10 LF
57 | 0b11011011,
58 | 0b11000000, // 11 VT
59 | 0b10110111,
60 | 0b01000000, // 12 FF
61 | 0b11111000,
62 | 0b00000000, // 13 CR
63 | 0b11011101,
64 | 0b01000000, // 14 SO
65 | 0b11101010,
66 | 0b11000000, // 15 SI
67 | 0b10111101,
68 | 0b11000000, // 16 DLE
69 | 0b10111101,
70 | 0b01000000, // 17 DC1
71 | 0b11101011,
72 | 0b01000000, // 18 DC2
73 | 0b11101011,
74 | 0b11000000, // 19 DC3
75 | 0b11010110,
76 | 0b11000000, // 20 DC4
77 | 0b11011010,
78 | 0b11000000, // 21 NAK
79 | 0b11011011,
80 | 0b01000000, // 22 SYN
81 | 0b11010101,
82 | 0b11000000, // 23 ETB
83 | 0b11011110,
84 | 0b11000000, // 24 CAN
85 | 0b11011111,
86 | 0b01000000, // 25 EM
87 | 0b11101101,
88 | 0b11000000, // 26 SUB
89 | 0b11010101,
90 | 0b01000000, // 27 ESC
91 | 0b11010111,
92 | 0b01000000, // 28 FS
93 | 0b11101110,
94 | 0b11000000, // 29 GS
95 | 0b10111110,
96 | 0b11000000, // 30 RS
97 | 0b11011111,
98 | 0b11000000, // 31 US
99 | 0b10000000,
100 | 0b00000000, // 32 SP
101 | 0b11111111,
102 | 0b10000000, // 33 !
103 | 0b10101111,
104 | 0b10000000, // 34 "
105 | 0b11111010,
106 | 0b10000000, // 35 #
107 | 0b11101101,
108 | 0b10000000, // 36 $
109 | 0b10110101,
110 | 0b01000000, // 37 %
111 | 0b10101110,
112 | 0b11000000, // 38 &
113 | 0b10111111,
114 | 0b10000000, // 39 '
115 | 0b11111011,
116 | 0b00000000, // 40 (
117 | 0b11110111,
118 | 0b00000000, // 41 )
119 | 0b10110111,
120 | 0b10000000, // 42 *
121 | 0b11101111,
122 | 0b10000000, // 43 +
123 | 0b11101010,
124 | 0b00000000, // 44 ,
125 | 0b11010100,
126 | 0b00000000, // 45 -
127 | 0b10101110,
128 | 0b00000000, // 46 .
129 | 0b11010111,
130 | 0b10000000, // 47 /
131 | 0b10110111,
132 | 0b00000000, // 48 0
133 | 0b10111101,
134 | 0b00000000, // 49 1
135 | 0b11101101,
136 | 0b00000000, // 50 2
137 | 0b11111111,
138 | 0b00000000, // 51 3
139 | 0b10111011,
140 | 0b10000000, // 52 4
141 | 0b10101101,
142 | 0b10000000, // 53 5
143 | 0b10110101,
144 | 0b10000000, // 54 6
145 | 0b11010110,
146 | 0b10000000, // 55 7
147 | 0b11010101,
148 | 0b10000000, // 56 8
149 | 0b11011011,
150 | 0b10000000, // 57 9
151 | 0b11110101,
152 | 0b00000000, // 58 :
153 | 0b11011110,
154 | 0b10000000, // 59 ;
155 | 0b11110110,
156 | 0b10000000, // 60 <
157 | 0b10101010,
158 | 0b00000000, // 61 =
159 | 0b11101011,
160 | 0b10000000, // 62 >
161 | 0b10101011,
162 | 0b11000000, // 63 ?
163 | 0b10101111,
164 | 0b01000000, // 64 @
165 | 0b11111010,
166 | 0b00000000, // 65 A
167 | 0b11101011,
168 | 0b00000000, // 66 B
169 | 0b10101101,
170 | 0b00000000, // 67 C
171 | 0b10110101,
172 | 0b00000000, // 68 D
173 | 0b11101110,
174 | 0b00000000, // 69 E
175 | 0b11011011,
176 | 0b00000000, // 70 F
177 | 0b11111101,
178 | 0b00000000, // 71 G
179 | 0b10101010,
180 | 0b10000000, // 72 H
181 | 0b11111110,
182 | 0b00000000, // 73 I
183 | 0b11111110,
184 | 0b10000000, // 74 J
185 | 0b10111110,
186 | 0b10000000, // 75 K
187 | 0b11010111,
188 | 0b00000000, // 76 L
189 | 0b10111011,
190 | 0b00000000, // 77 M
191 | 0b11011101,
192 | 0b00000000, // 78 N
193 | 0b10101011,
194 | 0b00000000, // 79 O
195 | 0b11010101,
196 | 0b00000000, // 80 P
197 | 0b11101110,
198 | 0b10000000, // 81 Q
199 | 0b10101111,
200 | 0b00000000, // 82 R
201 | 0b11011110,
202 | 0b00000000, // 83 S
203 | 0b11011010,
204 | 0b00000000, // 84 T
205 | 0b10101011,
206 | 0b10000000, // 85 U
207 | 0b11011010,
208 | 0b10000000, // 86 V
209 | 0b10101110,
210 | 0b10000000, // 87 W
211 | 0b10111010,
212 | 0b10000000, // 88 X
213 | 0b10111101,
214 | 0b10000000, // 89 Y
215 | 0b10101011,
216 | 0b01000000, // 90 Z
217 | 0b11111011,
218 | 0b10000000, // 91 [
219 | 0b11110111,
220 | 0b10000000, // 92 "\"
221 | 0b11111101,
222 | 0b10000000, // 93 ]
223 | 0b10101111,
224 | 0b11000000, // 94 ^
225 | 0b10110110,
226 | 0b10000000, // 95 _ (underline)
227 | 0b10110111,
228 | 0b11000000, // 96 `
229 | 0b10110000,
230 | 0b00000000, // 97 a
231 | 0b10111110,
232 | 0b00000000, // 98 b
233 | 0b10111100,
234 | 0b00000000, // 99 c
235 | 0b10110100,
236 | 0b00000000, // 100 d
237 | 0b11000000,
238 | 0b00000000, // 101 e
239 | 0b11110100,
240 | 0b00000000, // 102 f
241 | 0b10110110,
242 | 0b00000000, // 103 g
243 | 0b10101100,
244 | 0b00000000, // 104 h
245 | 0b11010000,
246 | 0b00000000, // 105 i
247 | 0b11110101,
248 | 0b10000000, // 106 j
249 | 0b10111111,
250 | 0b00000000, // 107 k
251 | 0b11011000,
252 | 0b00000000, // 108 l
253 | 0b11101100,
254 | 0b00000000, // 109 m
255 | 0b11110000,
256 | 0b00000000, // 110 n
257 | 0b11100000,
258 | 0b00000000, // 111 o
259 | 0b11111100,
260 | 0b00000000, // 112 p
261 | 0b11011111,
262 | 0b10000000, // 113 q
263 | 0b10101000,
264 | 0b00000000, // 114 r
265 | 0b10111000,
266 | 0b00000000, // 115 s
267 | 0b10100000,
268 | 0b00000000, // 116 t
269 | 0b11011100,
270 | 0b00000000, // 117 u
271 | 0b11110110,
272 | 0b00000000, // 118 v
273 | 0b11010110,
274 | 0b00000000, // 119 w
275 | 0b11011111,
276 | 0b00000000, // 120 x
277 | 0b10111010,
278 | 0b00000000, // 121 y
279 | 0b11101010,
280 | 0b10000000, // 122 z
281 | 0b10101101,
282 | 0b11000000, // 123 {
283 | 0b11011101,
284 | 0b10000000, // 124 |
285 | 0b10101101,
286 | 0b01000000, // 125 }
287 | 0b10110101,
288 | 0b11000000, // 126 ~
289 | 0b11101101,
290 | 0b01000000, // 127 (del)
291 | };
292 |
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 | android.library=true
16 |
--------------------------------------------------------------------------------
/src/com/obteq/android/codec2/Codec2.java:
--------------------------------------------------------------------------------
1 | package com.obteq.android.codec2;
2 |
3 | public class Codec2 {
4 | static {
5 | System.loadLibrary("codec2");
6 | }
7 |
8 | public final static int CODEC2_MODE_3200 = 0;
9 | public final static int CODEC2_MODE_2400 = 1;
10 | public final static int CODEC2_MODE_1400 = 2;
11 | public final static int CODEC2_MODE_1200 = 3;
12 |
13 | public native static long create(int mode);
14 |
15 | public native static int getSamplesPerFrame(long con);
16 |
17 | public native static int getBitsSize(long con);
18 |
19 | public native static int destroy(long con);
20 |
21 | public native static long encode(long con, short[] buf, char[] bits);
22 |
23 | public native static long decode(long con, short[] buf, char[] bits);
24 | }
25 |
--------------------------------------------------------------------------------