g711_tests.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * g711_tests.c
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2006 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public 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, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \page g711_tests_page A-law and u-law conversion tests
  26. \section g711_tests_page_sec_1 What does it do?
  27. \section g711_tests_page_sec_2 How is it used?
  28. */
  29. #if defined(HAVE_CONFIG_H)
  30. #include "config.h"
  31. #endif
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <string.h>
  37. #include <sndfile.h>
  38. #include "spandsp.h"
  39. #include "spandsp-sim.h"
  40. #define BLOCK_LEN 160
  41. #define IN_FILE_NAME "../test-data/local/short_nb_voice.wav"
  42. #define ENCODED_FILE_NAME "g711.g711"
  43. #define OUT_FILE_NAME "post_g711.wav"
  44. int16_t amp[65536];
  45. uint8_t ulaw_data[65536];
  46. uint8_t alaw_data[65536];
  47. const uint8_t alaw_1khz_sine[] = {0x34, 0x21, 0x21, 0x34, 0xB4, 0xA1, 0xA1, 0xB4};
  48. const uint8_t ulaw_1khz_sine[] = {0x1E, 0x0B, 0x0B, 0x1E, 0x9E, 0x8B, 0x8B, 0x9E};
  49. static void compliance_tests(int log_audio)
  50. {
  51. SNDFILE *outhandle;
  52. power_meter_t power_meter;
  53. int outframes;
  54. int i;
  55. int block;
  56. int pre;
  57. int post;
  58. int post_post;
  59. int alaw_failures;
  60. int ulaw_failures;
  61. float worst_alaw;
  62. float worst_ulaw;
  63. float tmp;
  64. int len;
  65. g711_state_t *enc_state;
  66. g711_state_t *transcode;
  67. g711_state_t *dec_state;
  68. outhandle = NULL;
  69. if (log_audio)
  70. {
  71. if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL)
  72. {
  73. fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME);
  74. exit(2);
  75. }
  76. }
  77. printf("Conversion accuracy tests.\n");
  78. alaw_failures = 0;
  79. ulaw_failures = 0;
  80. worst_alaw = 0.0;
  81. worst_ulaw = 0.0;
  82. for (block = 0; block < 1; block++)
  83. {
  84. for (i = 0; i < 65536; i++)
  85. {
  86. pre = i - 32768;
  87. post = alaw_to_linear(linear_to_alaw(pre));
  88. if (abs(pre) > 140)
  89. {
  90. tmp = (float) abs(post - pre)/(float) abs(pre);
  91. if (tmp > 0.10)
  92. {
  93. printf("A-law: Excessive error at %d (%d)\n", pre, post);
  94. alaw_failures++;
  95. }
  96. if (tmp > worst_alaw)
  97. worst_alaw = tmp;
  98. }
  99. else
  100. {
  101. /* Small values need different handling for sensible measurement */
  102. if (abs(post - pre) > 15)
  103. {
  104. printf("A-law: Excessive error at %d (%d)\n", pre, post);
  105. alaw_failures++;
  106. }
  107. }
  108. amp[i] = post;
  109. }
  110. if (log_audio)
  111. {
  112. outframes = sf_writef_short(outhandle, amp, 65536);
  113. if (outframes != 65536)
  114. {
  115. fprintf(stderr, " Error writing audio file\n");
  116. exit(2);
  117. }
  118. }
  119. for (i = 0; i < 65536; i++)
  120. {
  121. pre = i - 32768;
  122. post = ulaw_to_linear(linear_to_ulaw(pre));
  123. if (abs(pre) > 40)
  124. {
  125. tmp = (float) abs(post - pre)/(float) abs(pre);
  126. if (tmp > 0.10)
  127. {
  128. printf("u-law: Excessive error at %d (%d)\n", pre, post);
  129. ulaw_failures++;
  130. }
  131. if (tmp > worst_ulaw)
  132. worst_ulaw = tmp;
  133. }
  134. else
  135. {
  136. /* Small values need different handling for sensible measurement */
  137. if (abs(post - pre) > 4)
  138. {
  139. printf("u-law: Excessive error at %d (%d)\n", pre, post);
  140. ulaw_failures++;
  141. }
  142. }
  143. amp[i] = post;
  144. }
  145. if (log_audio)
  146. {
  147. outframes = sf_writef_short(outhandle, amp, 65536);
  148. if (outframes != 65536)
  149. {
  150. fprintf(stderr, " Error writing audio file\n");
  151. exit(2);
  152. }
  153. }
  154. }
  155. printf("Worst A-law error (ignoring small values) %f%%\n", worst_alaw*100.0);
  156. printf("Worst u-law error (ignoring small values) %f%%\n", worst_ulaw*100.0);
  157. if (alaw_failures || ulaw_failures)
  158. {
  159. printf("%d A-law values with excessive error\n", alaw_failures);
  160. printf("%d u-law values with excessive error\n", ulaw_failures);
  161. printf("Tests failed\n");
  162. exit(2);
  163. }
  164. printf("Cyclic conversion repeatability tests.\n");
  165. /* Find what happens to every possible linear value after a round trip. */
  166. for (i = 0; i < 65536; i++)
  167. {
  168. pre = i - 32768;
  169. /* Make a round trip */
  170. post = alaw_to_linear(linear_to_alaw(pre));
  171. /* A second round trip should cause no further change */
  172. post_post = alaw_to_linear(linear_to_alaw(post));
  173. if (post_post != post)
  174. {
  175. printf("A-law second round trip mismatch - at %d, %d != %d\n", pre, post, post_post);
  176. printf("Tests failed\n");
  177. exit(2);
  178. }
  179. /* Make a round trip */
  180. post = ulaw_to_linear(linear_to_ulaw(pre));
  181. /* A second round trip should cause no further change */
  182. post_post = ulaw_to_linear(linear_to_ulaw(post));
  183. if (post_post != post)
  184. {
  185. printf("u-law round trip mismatch - at %d, %d != %d\n", pre, post, post_post);
  186. printf("Tests failed\n");
  187. exit(2);
  188. }
  189. }
  190. printf("Reference power level tests.\n");
  191. power_meter_init(&power_meter, 7);
  192. for (i = 0; i < 8000; i++)
  193. {
  194. amp[i] = ulaw_to_linear(ulaw_1khz_sine[i & 7]);
  195. power_meter_update(&power_meter, amp[i]);
  196. }
  197. printf("Reference u-law 1kHz tone is %fdBm0\n", power_meter_current_dbm0(&power_meter));
  198. if (log_audio)
  199. {
  200. outframes = sf_writef_short(outhandle, amp, 8000);
  201. if (outframes != 8000)
  202. {
  203. fprintf(stderr, " Error writing audio file\n");
  204. exit(2);
  205. }
  206. }
  207. if (0.1f < fabs(power_meter_current_dbm0(&power_meter)))
  208. {
  209. printf("Test failed.\n");
  210. exit(2);
  211. }
  212. for (i = 0; i < 8000; i++)
  213. {
  214. amp[i] = alaw_to_linear(alaw_1khz_sine[i & 7]);
  215. power_meter_update(&power_meter, amp[i]);
  216. }
  217. printf("Reference A-law 1kHz tone is %fdBm0\n", power_meter_current_dbm0(&power_meter));
  218. if (log_audio)
  219. {
  220. outframes = sf_writef_short(outhandle, amp, 8000);
  221. if (outframes != 8000)
  222. {
  223. fprintf(stderr, " Error writing audio file\n");
  224. exit(2);
  225. }
  226. }
  227. if (0.1f < fabs(power_meter_current_dbm0(&power_meter)))
  228. {
  229. printf("Test failed.\n");
  230. exit(2);
  231. }
  232. /* Check the transcoding functions. */
  233. printf("Testing transcoding A-law -> u-law -> A-law\n");
  234. for (i = 0; i < 256; i++)
  235. {
  236. if (alaw_to_ulaw(ulaw_to_alaw(i)) != i)
  237. {
  238. if (abs(alaw_to_ulaw(ulaw_to_alaw(i)) - i) > 1)
  239. {
  240. printf("u-law -> A-law -> u-law gave %d -> %d\n", i, alaw_to_ulaw(ulaw_to_alaw(i)));
  241. printf("Test failed\n");
  242. exit(2);
  243. }
  244. }
  245. }
  246. printf("Testing transcoding u-law -> A-law -> u-law\n");
  247. for (i = 0; i < 256; i++)
  248. {
  249. if (ulaw_to_alaw(alaw_to_ulaw(i)) != i)
  250. {
  251. if (abs(alaw_to_ulaw(ulaw_to_alaw(i)) - i) > 1)
  252. {
  253. printf("A-law -> u-law -> A-law gave %d -> %d\n", i, ulaw_to_alaw(alaw_to_ulaw(i)));
  254. printf("Test failed\n");
  255. exit(2);
  256. }
  257. }
  258. }
  259. enc_state = g711_init(NULL, G711_ALAW);
  260. transcode = g711_init(NULL, G711_ALAW);
  261. dec_state = g711_init(NULL, G711_ULAW);
  262. len = 65536;
  263. for (i = 0; i < len; i++)
  264. amp[i] = i - 32768;
  265. len = g711_encode(enc_state, alaw_data, amp, len);
  266. len = g711_transcode(transcode, ulaw_data, alaw_data, len);
  267. len = g711_decode(dec_state, amp, ulaw_data, len);
  268. if (len != 65536)
  269. {
  270. printf("Block coding gave the wrong length - %d instead of %d\n", len, 65536);
  271. printf("Test failed\n");
  272. exit(2);
  273. }
  274. for (i = 0; i < len; i++)
  275. {
  276. pre = i - 32768;
  277. post = amp[i];
  278. if (abs(pre) > 140)
  279. {
  280. tmp = (float) abs(post - pre)/(float) abs(pre);
  281. if (tmp > 0.10)
  282. {
  283. printf("Block: Excessive error at %d (%d)\n", pre, post);
  284. exit(2);
  285. }
  286. }
  287. else
  288. {
  289. /* Small values need different handling for sensible measurement */
  290. if (abs(post - pre) > 15)
  291. {
  292. printf("Block: Excessive error at %d (%d)\n", pre, post);
  293. exit(2);
  294. }
  295. }
  296. }
  297. g711_free(enc_state);
  298. g711_free(transcode);
  299. g711_free(dec_state);
  300. if (log_audio)
  301. {
  302. if (sf_close_telephony(outhandle))
  303. {
  304. fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
  305. exit(2);
  306. }
  307. }
  308. printf("Tests passed.\n");
  309. }
  310. /*- End of function --------------------------------------------------------*/
  311. int main(int argc, char *argv[])
  312. {
  313. SNDFILE *inhandle;
  314. SNDFILE *outhandle;
  315. int outframes;
  316. int opt;
  317. int samples;
  318. int len2;
  319. int len3;
  320. int basic_tests;
  321. int law;
  322. int encode;
  323. int decode;
  324. int file;
  325. const char *in_file;
  326. const char *out_file;
  327. g711_state_t *enc_state;
  328. g711_state_t *dec_state;
  329. int16_t indata[BLOCK_LEN];
  330. int16_t outdata[BLOCK_LEN];
  331. uint8_t g711data[BLOCK_LEN];
  332. basic_tests = true;
  333. law = G711_ALAW;
  334. encode = false;
  335. decode = false;
  336. in_file = NULL;
  337. out_file = NULL;
  338. while ((opt = getopt(argc, argv, "ad:e:l:u")) != -1)
  339. {
  340. switch (opt)
  341. {
  342. case 'a':
  343. law = G711_ALAW;
  344. basic_tests = false;
  345. break;
  346. case 'd':
  347. in_file = optarg;
  348. basic_tests = false;
  349. decode = true;
  350. break;
  351. case 'e':
  352. in_file = optarg;
  353. basic_tests = false;
  354. encode = true;
  355. break;
  356. case 'l':
  357. out_file = optarg;
  358. break;
  359. case 'u':
  360. law = G711_ULAW;
  361. basic_tests = false;
  362. break;
  363. default:
  364. //usage();
  365. exit(2);
  366. }
  367. }
  368. if (basic_tests)
  369. {
  370. compliance_tests(true);
  371. }
  372. else
  373. {
  374. if (!decode && !encode)
  375. {
  376. decode =
  377. encode = true;
  378. }
  379. if (in_file == NULL)
  380. {
  381. in_file = (encode) ? IN_FILE_NAME : ENCODED_FILE_NAME;
  382. }
  383. if (out_file == NULL)
  384. {
  385. out_file = (decode) ? OUT_FILE_NAME : ENCODED_FILE_NAME;
  386. }
  387. inhandle = NULL;
  388. outhandle = NULL;
  389. file = -1;
  390. enc_state = NULL;
  391. dec_state = NULL;
  392. if (encode)
  393. {
  394. if ((inhandle = sf_open_telephony_read(in_file, 1)) == NULL)
  395. {
  396. fprintf(stderr, " Cannot open audio file '%s'\n", in_file);
  397. exit(2);
  398. }
  399. enc_state = g711_init(NULL, law);
  400. }
  401. else
  402. {
  403. if ((file = open(in_file, O_RDONLY)) < 0)
  404. {
  405. fprintf(stderr, " Failed to open '%s'\n", in_file);
  406. exit(2);
  407. }
  408. }
  409. if (decode)
  410. {
  411. if ((outhandle = sf_open_telephony_write(out_file, 1)) == NULL)
  412. {
  413. fprintf(stderr, " Cannot create audio file '%s'\n", out_file);
  414. exit(2);
  415. }
  416. dec_state = g711_init(NULL, law);
  417. }
  418. else
  419. {
  420. if ((file = open(out_file, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
  421. {
  422. fprintf(stderr, " Failed to open '%s'\n", out_file);
  423. exit(2);
  424. }
  425. }
  426. for (;;)
  427. {
  428. if (encode)
  429. {
  430. samples = sf_readf_short(inhandle, indata, BLOCK_LEN);
  431. if (samples <= 0)
  432. break;
  433. len2 = g711_encode(enc_state, g711data, indata, samples);
  434. }
  435. else
  436. {
  437. len2 = read(file, g711data, BLOCK_LEN);
  438. if (len2 <= 0)
  439. break;
  440. }
  441. if (decode)
  442. {
  443. len3 = g711_decode(dec_state, outdata, g711data, len2);
  444. outframes = sf_writef_short(outhandle, outdata, len3);
  445. if (outframes != len3)
  446. {
  447. fprintf(stderr, " Error writing audio file\n");
  448. exit(2);
  449. }
  450. }
  451. else
  452. {
  453. len3 = write(file, g711data, len2);
  454. if (len3 <= 0)
  455. break;
  456. }
  457. }
  458. if (encode)
  459. {
  460. if (sf_close_telephony(inhandle))
  461. {
  462. fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME);
  463. exit(2);
  464. }
  465. }
  466. else
  467. {
  468. close(file);
  469. }
  470. if (decode)
  471. {
  472. if (sf_close_telephony(outhandle))
  473. {
  474. fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
  475. exit(2);
  476. }
  477. }
  478. else
  479. {
  480. close(file);
  481. }
  482. printf("'%s' translated to '%s' using %s.\n", in_file, out_file, (law == G711_ALAW) ? "A-law" : "u-law");
  483. }
  484. return 0;
  485. }
  486. /*- End of function --------------------------------------------------------*/
  487. /*- End of file ------------------------------------------------------------*/