Merge branch 'master' of https://scm.cri.ensmp.fr/git/Faustine
[Faustine.git] / interpretor / lib / src / libsndfile-1.0.25 / src / G72x / g72x_test.c
1 /*
2 ** Copyright (C) 1999-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <math.h>
24
25 #include "g72x.h"
26 #include "g72x_priv.h"
27
28 #ifndef M_PI
29 #define M_PI 3.14159265358979323846264338
30 #endif
31
32 #define BUFFER_SIZE (1<<14) /* Should be (1<<14) */
33 #define SAMPLE_RATE 11025
34
35
36 static void g721_test (void) ;
37 static void g723_test (double margin) ;
38
39 static void gen_signal_double (double *data, double scale, int datalen) ;
40 static int error_function (double data, double orig, double margin) ;
41
42 static int oct_save_short (short *a, short *b, int len) ;
43
44 int
45 main (int argc, char *argv [])
46 { int bDoAll = 0 ;
47 int nTests = 0 ;
48
49 if (argc != 2)
50 { printf ("Usage : %s <test>\n", argv [0]) ;
51 printf (" Where <test> is one of the following:\n") ;
52 printf (" g721 - test G721 encoder and decoder\n") ;
53 printf (" g723 - test G721 encoder and decoder\n") ;
54 printf (" all - perform all tests\n") ;
55 exit (1) ;
56 } ;
57
58 bDoAll=!strcmp (argv [1], "all");
59
60 if (bDoAll || ! strcmp (argv [1], "g721"))
61 { g721_test () ;
62 nTests++ ;
63 } ;
64
65 if (bDoAll || ! strcmp (argv [1], "g723"))
66 { g723_test (0.53) ;
67 nTests++ ;
68 } ;
69
70 if (nTests == 0)
71 { printf ("Mono : ************************************\n") ;
72 printf ("Mono : * No '%s' test defined.\n", argv [1]) ;
73 printf ("Mono : ************************************\n") ;
74 return 1 ;
75 } ;
76
77 return 0 ;
78 } /* main */
79
80 static void
81 g721_test (void)
82 {
83 return ;
84 } /* g721_test */
85
86 static void
87 g723_test (double margin)
88 { static double orig_buffer [BUFFER_SIZE] ;
89 static short orig [BUFFER_SIZE] ;
90 static short data [BUFFER_SIZE] ;
91
92 G72x_STATE encoder_state, decoder_state ;
93
94 long k ;
95 int code, position, max_err ;
96
97 private_init_state (&encoder_state) ;
98 encoder_state.encoder = g723_24_encoder ;
99 encoder_state.codec_bits = 3 ;
100
101 private_init_state (&decoder_state) ;
102 decoder_state.decoder = g723_24_decoder ;
103 decoder_state.codec_bits = 3 ;
104
105 memset (data, 0, BUFFER_SIZE * sizeof (short)) ;
106 memset (orig, 0, BUFFER_SIZE * sizeof (short)) ;
107
108 printf (" g723_test : ") ;
109 fflush (stdout) ;
110
111 gen_signal_double (orig_buffer, 32000.0, BUFFER_SIZE) ;
112 for (k = 0 ; k < BUFFER_SIZE ; k++)
113 orig [k] = (short) orig_buffer [k] ;
114
115 /* Write and read data here. */
116 position = 0 ;
117 max_err = 0 ;
118 for (k = 0 ; k < BUFFER_SIZE ; k++)
119 { code = encoder_state.encoder (orig [k], &encoder_state) ;
120 data [k] = decoder_state.decoder (code, &decoder_state) ;
121 if (abs (orig [k] - data [k]) > max_err)
122 { position = k ;
123 max_err = abs (orig [k] - data [k]) ;
124 } ;
125 } ;
126
127 printf ("\n\nMax error of %d at postion %d.\n", max_err, position) ;
128
129 for (k = 0 ; k < BUFFER_SIZE ; k++)
130 { if (error_function (data [k], orig [k], margin))
131 { printf ("Line %d: Incorrect sample A (#%ld : %d should be %d).\n", __LINE__, k, data [k], orig [k]) ;
132 oct_save_short (orig, data, BUFFER_SIZE) ;
133 exit (1) ;
134 } ;
135 } ;
136
137
138 printf ("ok\n") ;
139
140 return ;
141 } /* g723_test */
142
143
144 #define SIGNAL_MAXVAL 30000.0
145 #define DECAY_COUNT 1000
146
147 static void
148 gen_signal_double (double *gendata, double scale, int gendatalen)
149 { int k, ramplen ;
150 double amp = 0.0 ;
151
152 ramplen = DECAY_COUNT ;
153
154 for (k = 0 ; k < gendatalen ; k++)
155 { if (k <= ramplen)
156 amp = scale * k / ((double) ramplen) ;
157 else if (k > gendatalen - ramplen)
158 amp = scale * (gendatalen - k) / ((double) ramplen) ;
159
160 gendata [k] = amp * (0.4 * sin (33.3 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))
161 + 0.3 * cos (201.1 * 2.0 * M_PI * ((double) (k+1)) / ((double) SAMPLE_RATE))) ;
162 } ;
163
164 return ;
165 } /* gen_signal_double */
166
167 static int
168 error_function (double data, double orig, double margin)
169 { double error ;
170
171 if (fabs (orig) <= 500.0)
172 error = fabs (fabs (data) - fabs(orig)) / 2000.0 ;
173 else if (fabs (orig) <= 1000.0)
174 error = fabs (data - orig) / 3000.0 ;
175 else
176 error = fabs (data - orig) / fabs (orig) ;
177
178 if (error > margin)
179 { printf ("\n\n*******************\nError : %f\n", error) ;
180 return 1 ;
181 } ;
182 return 0 ;
183 } /* error_function */
184
185 static int
186 oct_save_short (short *a, short *b, int len)
187 { FILE *file ;
188 int k ;
189
190 if (! (file = fopen ("error.dat", "w")))
191 return 1 ;
192
193 fprintf (file, "# Not created by Octave\n") ;
194
195 fprintf (file, "# name: a\n") ;
196 fprintf (file, "# type: matrix\n") ;
197 fprintf (file, "# rows: %d\n", len) ;
198 fprintf (file, "# columns: 1\n") ;
199
200 for (k = 0 ; k < len ; k++)
201 fprintf (file, "% d\n", a [k]) ;
202
203 fprintf (file, "# name: b\n") ;
204 fprintf (file, "# type: matrix\n") ;
205 fprintf (file, "# rows: %d\n", len) ;
206 fprintf (file, "# columns: 1\n") ;
207
208 for (k = 0 ; k < len ; k++)
209 fprintf (file, "% d\n", b [k]) ;
210
211 fclose (file) ;
212 return 0 ;
213 } /* oct_save_short */
214