1
/* $Id: wsola_test.c 3550 2011-05-05 05:33:27Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjmedia/wsola.h>
27
#define CLOCK_RATE 16000
28
#define SAMPLES_PER_FRAME (10 * CLOCK_RATE / 1000)
30
#define RESET() memset(buf1, 0, sizeof(buf1)), \
31
memset(buf2, 0, sizeof(buf2)), \
32
memset(frm1, 0, sizeof(frm1)), \
33
memset(frm2, 0, sizeof(frm2))
36
void test_find_pitch(void)
38
enum { ON = 111, FRM_PART_LEN=20 };
39
short buf2[SAMPLES_PER_FRAME*2], buf1[SAMPLES_PER_FRAME*2],
40
frm2[SAMPLES_PER_FRAME], frm1[SAMPLES_PER_FRAME];
43
/* Case 1. all contiguous */
49
pos = pjmedia_wsola_find_pitch(frm1, SAMPLES_PER_FRAME, NULL, 0,
50
buf1, SAMPLES_PER_FRAME*2, NULL, 0, PJ_TRUE);
53
/* Case 2: contiguous buffer, non-contiguous frame */
57
*(ref+FRM_PART_LEN) = ON;
64
pos = pjmedia_wsola_find_pitch(frm1, FRM_PART_LEN, frm2, SAMPLES_PER_FRAME - FRM_PART_LEN,
65
buf1, SAMPLES_PER_FRAME*2, NULL, 0, PJ_TRUE);
68
/* Case 3: non-contiguous buffer, contiguous frame, found in buf1 */
74
frm1[FRM_PART_LEN] = ON;
79
pos = pjmedia_wsola_find_pitch(frm1, SAMPLES_PER_FRAME, NULL, 0,
81
buf2, SAMPLES_PER_FRAME,
88
int expand(pj_pool_t *pool, const char *filein, const char *fileout,
89
int expansion_rate100, int lost_rate10, int lost_burst)
91
enum { LOST_RATE = 10 };
93
short frame[SAMPLES_PER_FRAME];
95
pj_timestamp elapsed, zero;
99
/* Lost burst must be > 0 */
100
assert(lost_rate10==0 || lost_burst > 0);
102
in = fopen(filein, "rb");
104
out = fopen(fileout, "wb");
107
pjmedia_wsola_create(pool, CLOCK_RATE, SAMPLES_PER_FRAME, 1, 0, &wsola);
112
while (fread(frame, SAMPLES_PER_FRAME*2, 1, in) == 1) {
115
if (lost_rate10 == 0) {
118
pj_get_timestamp(&t1);
119
pjmedia_wsola_save(wsola, frame, 0);
120
pj_get_timestamp(&t2);
122
pj_sub_timestamp(&t2, &t1);
123
pj_add_timestamp(&elapsed, &t2);
125
fwrite(frame, SAMPLES_PER_FRAME*2, 1, out);
127
samples += SAMPLES_PER_FRAME;
129
if ((rand() % 100) < expansion_rate100) {
131
pj_get_timestamp(&t1);
132
pjmedia_wsola_generate(wsola, frame);
133
pj_get_timestamp(&t2);
135
pj_sub_timestamp(&t2, &t1);
136
pj_add_timestamp(&elapsed, &t2);
138
samples += SAMPLES_PER_FRAME;
140
fwrite(frame, SAMPLES_PER_FRAME*2, 1, out);
146
if ((rand() % 10) < lost_rate10) {
149
for (burst=0; burst<lost_burst; ++burst) {
150
pj_get_timestamp(&t1);
151
pjmedia_wsola_generate(wsola, frame);
152
pj_get_timestamp(&t2);
154
pj_sub_timestamp(&t2, &t1);
155
pj_add_timestamp(&elapsed, &t2);
157
samples += SAMPLES_PER_FRAME;
159
fwrite(frame, SAMPLES_PER_FRAME*2, 1, out);
163
pj_get_timestamp(&t1);
164
pjmedia_wsola_save(wsola, frame, last_lost);
165
pj_get_timestamp(&t2);
167
pj_sub_timestamp(&t2, &t1);
168
pj_add_timestamp(&elapsed, &t2);
170
samples += SAMPLES_PER_FRAME;
172
fwrite(frame, SAMPLES_PER_FRAME*2, 1, out);
181
zero.u64 = pj_elapsed_usec(&zero, &elapsed);
183
zero.u64 = samples * PJ_INT64(1000000) / zero.u64;
184
assert(zero.u32.hi == 0);
186
PJ_LOG(3,("test.c", "Processing: %f Msamples per second",
187
zero.u32.lo/1000000.0));
188
PJ_LOG(3,("test.c", "CPU load for current settings: %f%%",
189
CLOCK_RATE * 100.0 / zero.u32.lo));
191
pjmedia_wsola_destroy(wsola);
198
static void save_file(const char *file,
199
short frame[], unsigned count)
201
FILE *f = fopen(file, "wb");
202
fwrite(frame, count, 2, f);
206
int compress(pj_pool_t *pool,
207
const char *filein, const char *fileout,
210
enum { BUF_CNT = SAMPLES_PER_FRAME * 10 };
212
pjmedia_wsola *wsola;
214
pj_timestamp elapsed, zero;
215
unsigned samples = 0;
217
in = fopen(filein, "rb");
219
out = fopen(fileout, "wb");
222
pjmedia_wsola_create(pool, CLOCK_RATE, SAMPLES_PER_FRAME, 1, 0, &wsola);
227
unsigned size_del, count;
231
if (fread(buf, sizeof(buf), 1, in) != 1)
236
pj_get_timestamp(&t1);
238
for (i=0; i<rate10; ++i) {
239
unsigned to_del = SAMPLES_PER_FRAME;
241
/* Method 1: buf1 contiguous */
242
pjmedia_wsola_discard(wsola, buf, count, NULL, 0, &to_del);
244
/* Method 2: split, majority in buf1 */
245
assert(count > SAMPLES_PER_FRAME);
246
pjmedia_wsola_discard(wsola, buf, count-SAMPLES_PER_FRAME,
247
buf+count-SAMPLES_PER_FRAME, SAMPLES_PER_FRAME,
250
/* Method 3: split, majority in buf2 */
251
assert(count > SAMPLES_PER_FRAME);
252
pjmedia_wsola_discard(wsola, buf, SAMPLES_PER_FRAME,
253
buf+SAMPLES_PER_FRAME, count-SAMPLES_PER_FRAME,
256
/* Method 4: split, each with small length */
257
enum { TOT_LEN = 3 * SAMPLES_PER_FRAME };
258
unsigned buf1_len = (rand() % TOT_LEN);
259
short *ptr = buf + count - TOT_LEN;
260
assert(count > TOT_LEN);
261
if (buf1_len==0) buf1_len=SAMPLES_PER_FRAME*2;
262
pjmedia_wsola_discard(wsola, ptr, buf1_len,
263
ptr+buf1_len, TOT_LEN-buf1_len,
269
pj_get_timestamp(&t2);
273
pj_sub_timestamp(&t2, &t1);
274
pj_add_timestamp(&elapsed, &t2);
276
assert(size_del >= SAMPLES_PER_FRAME);
278
fwrite(buf, count, 2, out);
281
pjmedia_wsola_destroy(wsola);
286
zero.u64 = pj_elapsed_usec(&zero, &elapsed);
288
zero.u64 = samples * PJ_INT64(1000000) / zero.u64;
289
assert(zero.u32.hi == 0);
291
PJ_LOG(3,("test.c", "Processing: %f Msamples per second",
292
zero.u32.lo/1000000.0));
293
PJ_LOG(3,("test.c", "CPU load for current settings: %f%%",
294
CLOCK_RATE * 100.0 / zero.u32.lo));
300
static void mem_test(pj_pool_t *pool)
303
short *frame = pj_pool_alloc(pool, 240+4*160);
304
pj_timestamp elapsed, zero, t1, t2;
305
unsigned samples = 0;
308
while (samples < 50000000) {
310
pj_get_timestamp(&t1);
311
pjmedia_move_samples(frame, frame+160, 240+2*160);
312
pj_get_timestamp(&t2);
313
pj_sub_timestamp(&t2, &t1);
315
elapsed.u64 += t2.u64;
317
memset(unused, 0, sizeof(unused));
325
zero.u64 = pj_elapsed_usec(&zero, &elapsed);
327
zero.u64 = samples * PJ_INT64(1000000) / zero.u64;
328
assert(zero.u32.hi == 0);
330
PJ_LOG(3,("test.c", "Processing: %f Msamples per second",
331
zero.u32.lo/1000000.0));
332
PJ_LOG(3,("test.c", "CPU load for current settings: %f%%",
333
CLOCK_RATE * 100.0 / zero.u32.lo));
346
pj_caching_pool_init(&cp, NULL, 0);
347
pool = pj_pool_create(&cp.factory, "", 1000, 1000, NULL);
351
rc = expand(pool, "galileo16.pcm", "temp1.pcm", 20, 0, 0);
352
rc = compress(pool, "temp1.pcm", "output.pcm", 1);
354
for (i=0; i<2; ++i) {
355
rc = expand(pool, "output.pcm", "temp1.pcm", 20, 0, 0);
356
rc = compress(pool, "temp1.pcm", "output.pcm", 1);
367
puts("Press ENTER to quit");
368
fgets(s, sizeof(s), stdin);