| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pjmedia/wsola.h> |
| 20 |
#include <pj/log.h> |
| 21 |
#include <pj/pool.h> |
| 22 |
#include <pj/os.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <assert.h> |
| 25 |
|
| 26 |
#define CLOCK_RATE 16000 |
| 27 |
#define SAMPLES_PER_FRAME (10 * CLOCK_RATE / 1000) |
| 28 |
|
| 29 |
#define RESET() memset(buf1, 0, sizeof(buf1)), \ |
| 30 |
memset(buf2, 0, sizeof(buf2)), \ |
| 31 |
memset(frm1, 0, sizeof(frm1)), \ |
| 32 |
memset(frm2, 0, sizeof(frm2)) |
| 33 |
|
| 34 |
#if 0 |
| 35 |
void test_find_pitch(void) |
| 36 |
{ |
| 37 |
enum { ON = 111, FRM_PART_LEN=20 }; |
| 38 |
short buf2[SAMPLES_PER_FRAME*2], buf1[SAMPLES_PER_FRAME*2], |
| 39 |
frm2[SAMPLES_PER_FRAME], frm1[SAMPLES_PER_FRAME]; |
| 40 |
short *ref, *pos; |
| 41 |
|
| 42 |
|
| 43 |
RESET(); |
| 44 |
ref = buf1 + 10; |
| 45 |
*ref = ON; |
| 46 |
frm1[0] = ON; |
| 47 |
|
| 48 |
pos = pjmedia_wsola_find_pitch(frm1, SAMPLES_PER_FRAME, NULL, 0, |
| 49 |
buf1, SAMPLES_PER_FRAME*2, NULL, 0, PJ_TRUE); |
| 50 |
assert(pos == ref); |
| 51 |
|
| 52 |
|
| 53 |
RESET(); |
| 54 |
ref = buf1 + 17; |
| 55 |
*ref = ON; |
| 56 |
*(ref+FRM_PART_LEN) = ON; |
| 57 |
frm1[0] = ON; |
| 58 |
frm2[0] = ON; |
| 59 |
|
| 60 |
|
| 61 |
buf1[0] = ON; |
| 62 |
|
| 63 |
pos = pjmedia_wsola_find_pitch(frm1, FRM_PART_LEN, frm2, SAMPLES_PER_FRAME - FRM_PART_LEN, |
| 64 |
buf1, SAMPLES_PER_FRAME*2, NULL, 0, PJ_TRUE); |
| 65 |
assert(pos == ref); |
| 66 |
|
| 67 |
|
| 68 |
RESET(); |
| 69 |
ref = buf1 + 17; |
| 70 |
*ref = ON; |
| 71 |
buf2[17] = ON; |
| 72 |
frm1[0] = ON; |
| 73 |
frm1[FRM_PART_LEN] = ON; |
| 74 |
|
| 75 |
|
| 76 |
buf1[0] = ON; |
| 77 |
|
| 78 |
pos = pjmedia_wsola_find_pitch(frm1, SAMPLES_PER_FRAME, NULL, 0, |
| 79 |
buf1, FRM_PART_LEN, |
| 80 |
buf2, SAMPLES_PER_FRAME, |
| 81 |
PJ_TRUE); |
| 82 |
|
| 83 |
assert(pos == ref); |
| 84 |
} |
| 85 |
#endif |
| 86 |
|
| 87 |
int expand(pj_pool_t *pool, const char *filein, const char *fileout, |
| 88 |
int expansion_rate100, int lost_rate10, int lost_burst) |
| 89 |
{ |
| 90 |
enum { LOST_RATE = 10 }; |
| 91 |
FILE *in, *out; |
| 92 |
short frame[SAMPLES_PER_FRAME]; |
| 93 |
pjmedia_wsola *wsola; |
| 94 |
pj_timestamp elapsed, zero; |
| 95 |
unsigned samples; |
| 96 |
int last_lost = 0; |
| 97 |
|
| 98 |
|
| 99 |
assert(lost_rate10==0 || lost_burst > 0); |
| 100 |
|
| 101 |
in = fopen(filein, "rb"); |
| 102 |
if (!in) return 1; |
| 103 |
out = fopen(fileout, "wb"); |
| 104 |
if (!out) return 1; |
| 105 |
|
| 106 |
pjmedia_wsola_create(pool, CLOCK_RATE, SAMPLES_PER_FRAME, 1, 0, &wsola); |
| 107 |
|
| 108 |
samples = 0; |
| 109 |
elapsed.u64 = 0; |
| 110 |
|
| 111 |
while (fread(frame, SAMPLES_PER_FRAME*2, 1, in) == 1) { |
| 112 |
pj_timestamp t1, t2; |
| 113 |
|
| 114 |
if (lost_rate10 == 0) { |
| 115 |
|
| 116 |
|
| 117 |
pj_get_timestamp(&t1); |
| 118 |
pjmedia_wsola_save(wsola, frame, 0); |
| 119 |
pj_get_timestamp(&t2); |
| 120 |
|
| 121 |
pj_sub_timestamp(&t2, &t1); |
| 122 |
pj_add_timestamp(&elapsed, &t2); |
| 123 |
|
| 124 |
fwrite(frame, SAMPLES_PER_FRAME*2, 1, out); |
| 125 |
|
| 126 |
samples += SAMPLES_PER_FRAME; |
| 127 |
|
| 128 |
if ((rand() % 100) < expansion_rate100) { |
| 129 |
|
| 130 |
pj_get_timestamp(&t1); |
| 131 |
pjmedia_wsola_generate(wsola, frame); |
| 132 |
pj_get_timestamp(&t2); |
| 133 |
|
| 134 |
pj_sub_timestamp(&t2, &t1); |
| 135 |
pj_add_timestamp(&elapsed, &t2); |
| 136 |
|
| 137 |
samples += SAMPLES_PER_FRAME; |
| 138 |
|
| 139 |
fwrite(frame, SAMPLES_PER_FRAME*2, 1, out); |
| 140 |
} |
| 141 |
|
| 142 |
} else { |
| 143 |
|
| 144 |
|
| 145 |
if ((rand() % 10) < lost_rate10) { |
| 146 |
int burst; |
| 147 |
|
| 148 |
for (burst=0; burst<lost_burst; ++burst) { |
| 149 |
pj_get_timestamp(&t1); |
| 150 |
pjmedia_wsola_generate(wsola, frame); |
| 151 |
pj_get_timestamp(&t2); |
| 152 |
|
| 153 |
pj_sub_timestamp(&t2, &t1); |
| 154 |
pj_add_timestamp(&elapsed, &t2); |
| 155 |
|
| 156 |
samples += SAMPLES_PER_FRAME; |
| 157 |
|
| 158 |
fwrite(frame, SAMPLES_PER_FRAME*2, 1, out); |
| 159 |
} |
| 160 |
last_lost = 1; |
| 161 |
} else { |
| 162 |
pj_get_timestamp(&t1); |
| 163 |
pjmedia_wsola_save(wsola, frame, last_lost); |
| 164 |
pj_get_timestamp(&t2); |
| 165 |
|
| 166 |
pj_sub_timestamp(&t2, &t1); |
| 167 |
pj_add_timestamp(&elapsed, &t2); |
| 168 |
|
| 169 |
samples += SAMPLES_PER_FRAME; |
| 170 |
|
| 171 |
fwrite(frame, SAMPLES_PER_FRAME*2, 1, out); |
| 172 |
last_lost = 0; |
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
zero.u64 = 0; |
| 180 |
zero.u64 = pj_elapsed_usec(&zero, &elapsed); |
| 181 |
|
| 182 |
zero.u64 = samples * PJ_INT64(1000000) / zero.u64; |
| 183 |
assert(zero.u32.hi == 0); |
| 184 |
|
| 185 |
PJ_LOG(3,("test.c", "Processing: %f Msamples per second", |
| 186 |
zero.u32.lo/1000000.0)); |
| 187 |
PJ_LOG(3,("test.c", "CPU load for current settings: %f%%", |
| 188 |
CLOCK_RATE * 100.0 / zero.u32.lo)); |
| 189 |
|
| 190 |
pjmedia_wsola_destroy(wsola); |
| 191 |
fclose(in); |
| 192 |
fclose(out); |
| 193 |
|
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
static void save_file(const char *file, |
| 198 |
short frame[], unsigned count) |
| 199 |
{ |
| 200 |
FILE *f = fopen(file, "wb"); |
| 201 |
fwrite(frame, count, 2, f); |
| 202 |
fclose(f); |
| 203 |
} |
| 204 |
|
| 205 |
int compress(pj_pool_t *pool, |
| 206 |
const char *filein, const char *fileout, |
| 207 |
int rate10) |
| 208 |
{ |
| 209 |
enum { BUF_CNT = SAMPLES_PER_FRAME * 10 }; |
| 210 |
FILE *in, *out; |
| 211 |
pjmedia_wsola *wsola; |
| 212 |
short buf[BUF_CNT]; |
| 213 |
pj_timestamp elapsed, zero; |
| 214 |
unsigned samples = 0; |
| 215 |
|
| 216 |
in = fopen(filein, "rb"); |
| 217 |
if (!in) return 1; |
| 218 |
out = fopen(fileout, "wb"); |
| 219 |
if (!out) return 1; |
| 220 |
|
| 221 |
pjmedia_wsola_create(pool, CLOCK_RATE, SAMPLES_PER_FRAME, 1, 0, &wsola); |
| 222 |
|
| 223 |
elapsed.u64 = 0; |
| 224 |
|
| 225 |
for (;;) { |
| 226 |
unsigned size_del, count; |
| 227 |
pj_timestamp t1, t2; |
| 228 |
int i; |
| 229 |
|
| 230 |
if (fread(buf, sizeof(buf), 1, in) != 1) |
| 231 |
break; |
| 232 |
|
| 233 |
count = BUF_CNT; |
| 234 |
size_del = 0; |
| 235 |
pj_get_timestamp(&t1); |
| 236 |
|
| 237 |
for (i=0; i<rate10; ++i) { |
| 238 |
unsigned to_del = SAMPLES_PER_FRAME; |
| 239 |
#if 0 |
| 240 |
|
| 241 |
pjmedia_wsola_discard(wsola, buf, count, NULL, 0, &to_del); |
| 242 |
#elif 0 |
| 243 |
|
| 244 |
assert(count > SAMPLES_PER_FRAME); |
| 245 |
pjmedia_wsola_discard(wsola, buf, count-SAMPLES_PER_FRAME, |
| 246 |
buf+count-SAMPLES_PER_FRAME, SAMPLES_PER_FRAME, |
| 247 |
&to_del); |
| 248 |
#elif 0 |
| 249 |
|
| 250 |
assert(count > SAMPLES_PER_FRAME); |
| 251 |
pjmedia_wsola_discard(wsola, buf, SAMPLES_PER_FRAME, |
| 252 |
buf+SAMPLES_PER_FRAME, count-SAMPLES_PER_FRAME, |
| 253 |
&to_del); |
| 254 |
#elif 1 |
| 255 |
|
| 256 |
enum { TOT_LEN = 3 * SAMPLES_PER_FRAME }; |
| 257 |
unsigned buf1_len = (rand() % TOT_LEN); |
| 258 |
short *ptr = buf + count - TOT_LEN; |
| 259 |
assert(count > TOT_LEN); |
| 260 |
if (buf1_len==0) buf1_len=SAMPLES_PER_FRAME*2; |
| 261 |
pjmedia_wsola_discard(wsola, ptr, buf1_len, |
| 262 |
ptr+buf1_len, TOT_LEN-buf1_len, |
| 263 |
&to_del); |
| 264 |
#endif |
| 265 |
count -= to_del; |
| 266 |
size_del += to_del; |
| 267 |
} |
| 268 |
pj_get_timestamp(&t2); |
| 269 |
|
| 270 |
samples += BUF_CNT; |
| 271 |
|
| 272 |
pj_sub_timestamp(&t2, &t1); |
| 273 |
pj_add_timestamp(&elapsed, &t2); |
| 274 |
|
| 275 |
assert(size_del >= SAMPLES_PER_FRAME); |
| 276 |
|
| 277 |
fwrite(buf, count, 2, out); |
| 278 |
} |
| 279 |
|
| 280 |
pjmedia_wsola_destroy(wsola); |
| 281 |
fclose(in); |
| 282 |
fclose(out); |
| 283 |
|
| 284 |
zero.u64 = 0; |
| 285 |
zero.u64 = pj_elapsed_usec(&zero, &elapsed); |
| 286 |
|
| 287 |
zero.u64 = samples * PJ_INT64(1000000) / zero.u64; |
| 288 |
assert(zero.u32.hi == 0); |
| 289 |
|
| 290 |
PJ_LOG(3,("test.c", "Processing: %f Msamples per second", |
| 291 |
zero.u32.lo/1000000.0)); |
| 292 |
PJ_LOG(3,("test.c", "CPU load for current settings: %f%%", |
| 293 |
CLOCK_RATE * 100.0 / zero.u32.lo)); |
| 294 |
|
| 295 |
return 0; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
static void mem_test(pj_pool_t *pool) |
| 300 |
{ |
| 301 |
char unused[1024]; |
| 302 |
short *frame = pj_pool_alloc(pool, 240+4*160); |
| 303 |
pj_timestamp elapsed, zero, t1, t2; |
| 304 |
unsigned samples = 0; |
| 305 |
|
| 306 |
elapsed.u64 = 0; |
| 307 |
while (samples < 50000000) { |
| 308 |
|
| 309 |
pj_get_timestamp(&t1); |
| 310 |
pjmedia_move_samples(frame, frame+160, 240+2*160); |
| 311 |
pj_get_timestamp(&t2); |
| 312 |
pj_sub_timestamp(&t2, &t1); |
| 313 |
|
| 314 |
elapsed.u64 += t2.u64; |
| 315 |
|
| 316 |
memset(unused, 0, sizeof(unused)); |
| 317 |
samples += 160; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
zero.u64 = 0; |
| 324 |
zero.u64 = pj_elapsed_usec(&zero, &elapsed); |
| 325 |
|
| 326 |
zero.u64 = samples * PJ_INT64(1000000) / zero.u64; |
| 327 |
assert(zero.u32.hi == 0); |
| 328 |
|
| 329 |
PJ_LOG(3,("test.c", "Processing: %f Msamples per second", |
| 330 |
zero.u32.lo/1000000.0)); |
| 331 |
PJ_LOG(3,("test.c", "CPU load for current settings: %f%%", |
| 332 |
CLOCK_RATE * 100.0 / zero.u32.lo)); |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
int main() |
| 337 |
{ |
| 338 |
pj_caching_pool cp; |
| 339 |
pj_pool_t *pool; |
| 340 |
int i, rc; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
pj_init(); |
| 345 |
pj_caching_pool_init(&cp, NULL, 0); |
| 346 |
pool = pj_pool_create(&cp.factory, "", 1000, 1000, NULL); |
| 347 |
|
| 348 |
srand(2); |
| 349 |
|
| 350 |
rc = expand(pool, "galileo16.pcm", "temp1.pcm", 20, 0, 0); |
| 351 |
rc = compress(pool, "temp1.pcm", "output.pcm", 1); |
| 352 |
|
| 353 |
for (i=0; i<2; ++i) { |
| 354 |
rc = expand(pool, "output.pcm", "temp1.pcm", 20, 0, 0); |
| 355 |
rc = compress(pool, "temp1.pcm", "output.pcm", 1); |
| 356 |
} |
| 357 |
|
| 358 |
if (rc != 0) { |
| 359 |
puts("Error"); |
| 360 |
return 1; |
| 361 |
} |
| 362 |
|
| 363 |
#if 0 |
| 364 |
{ |
| 365 |
char s[10]; |
| 366 |
puts("Press ENTER to quit"); |
| 367 |
fgets(s, sizeof(s), stdin); |
| 368 |
} |
| 369 |
#endif |
| 370 |
|
| 371 |
return 0; |
| 372 |
} |