2
0

g722_tests.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * g722_tests.c - Test G.722 encode and decode.
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2005 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. /*! \file */
  26. /*! \page g722_tests_page G.722 tests
  27. \section g722_tests_page_sec_1 What does it do?
  28. This modules implements two sets of tests:
  29. - The tests defined in the G.722 specification, using the test data files supplied
  30. with the specification.
  31. - A generally audio quality test, consisting of compressing and decompressing a speeech
  32. file for audible comparison.
  33. The speech file should be recorded at 16 bits/sample, 16000 samples/second, and named
  34. "pre_g722.wav".
  35. The ITU tests use the codec in a special mode, in which the QMFs, which split and recombine the
  36. sub-bands, are disabled. This means they do not test 100% of the codec. This is the reason for
  37. including the additional listening test.
  38. \section g722_tests_page_sec_2 How is it used?
  39. To perform the tests in the G.722 specification you need to obtain the test data files from the
  40. specification. These are copyright material, and so cannot be distributed with this test software.
  41. The files, containing test vectors, which are supplied with the G.722 specification, should be
  42. copied to itutests/g722. The ITU tests can then be run by executing g722_tests without
  43. any parameters.
  44. To perform a general audio quality test, g722_tests should be run with a parameter specifying
  45. the required bit rate for compression. The valid parameters are "-48", "-56", and "-64".
  46. The file ../test-data/local/short_wb_voice.wav will be compressed to the specified bit rate, decompressed,
  47. and the resulting audio stored in post_g722.wav.
  48. */
  49. #if defined(HAVE_CONFIG_H)
  50. #include "config.h"
  51. #endif
  52. #include <stdlib.h>
  53. #include <stdio.h>
  54. #include <fcntl.h>
  55. #include <unistd.h>
  56. #include <memory.h>
  57. #include <ctype.h>
  58. #include <sndfile.h>
  59. #include "spandsp.h"
  60. #include "spandsp/private/g722.h"
  61. #define G722_SAMPLE_RATE 16000
  62. #define BLOCK_LEN 320
  63. #define MAX_TEST_VECTOR_LEN 40000
  64. #define TESTDATA_DIR "../test-data/itu/g722/"
  65. #define EIGHTK_IN_FILE_NAME "../test-data/local/short_nb_voice.wav"
  66. #define IN_FILE_NAME "../test-data/local/short_wb_voice.wav"
  67. #define ENCODED_FILE_NAME "g722.g722"
  68. #define OUT_FILE_NAME "post_g722.wav"
  69. #if 0
  70. static const char *itu_test_files[] =
  71. {
  72. TESTDATA_DIR "T1C1.XMT", /* 69973 bytes */
  73. TESTDATA_DIR "T1C2.XMT", /* 3605 bytes */
  74. TESTDATA_DIR "T1D3.COD", /* 69973 bytes */
  75. TESTDATA_DIR "T2R1.COD", /* 69973 bytes */
  76. TESTDATA_DIR "T2R2.COD", /* 3605 bytes */
  77. TESTDATA_DIR "T3L1.RC1", /* 69973 bytes */
  78. TESTDATA_DIR "T3L1.RC2", /* 69973 bytes */
  79. TESTDATA_DIR "T3L1.RC3", /* 69973 bytes */
  80. TESTDATA_DIR "T3H1.RC0", /* 69973 bytes */
  81. TESTDATA_DIR "T3L2.RC1", /* 3605 bytes */
  82. TESTDATA_DIR "T3L2.RC2", /* 3605 bytes */
  83. TESTDATA_DIR "T3L2.RC3", /* 3605 bytes */
  84. TESTDATA_DIR "T3H2.RC0", /* 3605 bytes */
  85. TESTDATA_DIR "T3L3.RC1", /* 69973 bytes */
  86. TESTDATA_DIR "T3L3.RC2", /* 69973 bytes */
  87. TESTDATA_DIR "T3L3.RC3", /* 69973 bytes */
  88. TESTDATA_DIR "T3H3.RC0" /* 69973 bytes */
  89. };
  90. #endif
  91. static const char *encode_test_files[] =
  92. {
  93. TESTDATA_DIR "T1C1.XMT",
  94. TESTDATA_DIR "T2R1.COD",
  95. TESTDATA_DIR "T1C2.XMT",
  96. TESTDATA_DIR "T2R2.COD",
  97. NULL
  98. };
  99. static const char *decode_test_files[] =
  100. {
  101. TESTDATA_DIR "T2R1.COD",
  102. TESTDATA_DIR "T3L1.RC1",
  103. TESTDATA_DIR "T3L1.RC2",
  104. TESTDATA_DIR "T3L1.RC3",
  105. TESTDATA_DIR "T3H1.RC0",
  106. TESTDATA_DIR "T2R2.COD",
  107. TESTDATA_DIR "T3L2.RC1",
  108. TESTDATA_DIR "T3L2.RC2",
  109. TESTDATA_DIR "T3L2.RC3",
  110. TESTDATA_DIR "T3H2.RC0",
  111. TESTDATA_DIR "T1D3.COD",
  112. TESTDATA_DIR "T3L3.RC1",
  113. TESTDATA_DIR "T3L3.RC2",
  114. TESTDATA_DIR "T3L3.RC3",
  115. TESTDATA_DIR "T3H3.RC0",
  116. NULL
  117. };
  118. int16_t itu_data[MAX_TEST_VECTOR_LEN];
  119. uint16_t itu_ref[MAX_TEST_VECTOR_LEN];
  120. uint16_t itu_ref_upper[MAX_TEST_VECTOR_LEN];
  121. uint8_t compressed[MAX_TEST_VECTOR_LEN];
  122. int16_t decompressed[MAX_TEST_VECTOR_LEN];
  123. static int hex_get(char *s)
  124. {
  125. int i;
  126. int value;
  127. int x;
  128. for (value = i = 0; i < 4; i++)
  129. {
  130. x = *s++ - 0x30;
  131. if (x > 9)
  132. x -= 0x07;
  133. if (x > 15)
  134. x -= 0x20;
  135. if (x < 0 || x > 15)
  136. return -1;
  137. value <<= 4;
  138. value |= x;
  139. }
  140. return value;
  141. }
  142. /*- End of function --------------------------------------------------------*/
  143. static int get_vector(FILE *file, uint16_t vec[])
  144. {
  145. char buf[132 + 1];
  146. char *s;
  147. int i;
  148. int value;
  149. while (fgets(buf, 133, file))
  150. {
  151. if (buf[0] == '/' && buf[1] == '*')
  152. continue;
  153. s = buf;
  154. i = 0;
  155. while ((value = hex_get(s)) >= 0)
  156. {
  157. vec[i++] = value;
  158. s += 4;
  159. }
  160. return i;
  161. }
  162. return 0;
  163. }
  164. /*- End of function --------------------------------------------------------*/
  165. static int get_test_vector(const char *file, uint16_t buf[], int max_len)
  166. {
  167. int octets;
  168. int i;
  169. FILE *infile;
  170. if ((infile = fopen(file, "r")) == NULL)
  171. {
  172. fprintf(stderr, " Failed to open '%s'\n", file);
  173. exit(2);
  174. }
  175. octets = 0;
  176. while ((i = get_vector(infile, buf + octets)) > 0)
  177. octets += i;
  178. fclose(infile);
  179. return octets;
  180. }
  181. /*- End of function --------------------------------------------------------*/
  182. static void itu_compliance_tests(void)
  183. {
  184. g722_encode_state_t *enc_state;
  185. g722_decode_state_t *dec_state;
  186. int i;
  187. int j;
  188. int k;
  189. int len_comp;
  190. int len_comp_lower;
  191. int len_comp_upper;
  192. int len_data;
  193. int len;
  194. int len2;
  195. int mode;
  196. int file;
  197. #if 1
  198. /* ITU G.722 encode tests, using configuration 1. The QMF is bypassed */
  199. for (file = 0; encode_test_files[file]; file += 2)
  200. {
  201. printf("Testing %s -> %s\n", encode_test_files[file], encode_test_files[file + 1]);
  202. /* Get the input data */
  203. len_data = get_test_vector(encode_test_files[file], (uint16_t *) itu_data, MAX_TEST_VECTOR_LEN);
  204. /* Get the reference output data */
  205. len_comp = get_test_vector(encode_test_files[file + 1], itu_ref, MAX_TEST_VECTOR_LEN);
  206. if (len_data != len_comp)
  207. {
  208. printf("Test data length mismatch\n");
  209. exit(2);
  210. }
  211. /* Process the input data */
  212. /* Skip the reset stuff at each end of the data */
  213. for (i = 0; i < len_data; i++)
  214. {
  215. if ((itu_data[i] & 1) == 0)
  216. break;
  217. }
  218. for (j = i; j < len_data; j++)
  219. {
  220. if ((itu_data[j] & 1))
  221. break;
  222. }
  223. len = j - i;
  224. enc_state = g722_encode_init(NULL, 64000, 0);
  225. enc_state->itu_test_mode = true;
  226. len2 = g722_encode(enc_state, compressed, itu_data + i, len);
  227. /* Check the result against the ITU's reference output data */
  228. j = 0;
  229. for (k = 0; k < len2; k++)
  230. {
  231. if ((compressed[k] & 0xFF) != ((itu_ref[k + i] >> 8) & 0xFF))
  232. {
  233. printf(">>> %6d %4x %4x\n", k, compressed[k] & 0xFF, itu_ref[k + i] & 0xFFFF);
  234. j++;
  235. }
  236. }
  237. printf("%d bad samples, out of %d/%d samples\n", j, len, len_data);
  238. if (j)
  239. {
  240. printf("Test failed\n");
  241. exit(2);
  242. }
  243. printf("Test passed\n");
  244. g722_encode_free(enc_state);
  245. }
  246. #endif
  247. #if 1
  248. /* ITU G.722 decode tests, using configuration 2. The QMF is bypassed */
  249. /* Run each of the tests for each of the modes - 48kbps, 56kbps and 64kbps. */
  250. for (mode = 1; mode <= 3; mode++)
  251. {
  252. for (file = 0; decode_test_files[file]; file += 5)
  253. {
  254. printf("Testing mode %d, %s -> %s + %s\n",
  255. mode,
  256. decode_test_files[file],
  257. decode_test_files[file + mode],
  258. decode_test_files[file + 4]);
  259. /* Get the input data */
  260. len_data = get_test_vector(decode_test_files[file], (uint16_t *) itu_data, MAX_TEST_VECTOR_LEN);
  261. /* Get the lower reference output data */
  262. len_comp_lower = get_test_vector(decode_test_files[file + mode], itu_ref, MAX_TEST_VECTOR_LEN);
  263. /* Get the upper reference output data */
  264. len_comp_upper = get_test_vector(decode_test_files[file + 4], itu_ref_upper, MAX_TEST_VECTOR_LEN);
  265. if (len_data != len_comp_lower || len_data != len_comp_upper)
  266. {
  267. printf("Test data length mismatch\n");
  268. exit(2);
  269. }
  270. /* Process the input data */
  271. /* Skip the reset stuff at each end of the data */
  272. for (i = 0; i < len_data; i++)
  273. {
  274. if ((itu_data[i] & 1) == 0)
  275. break;
  276. }
  277. for (j = i; j < len_data; j++)
  278. {
  279. if ((itu_data[j] & 1))
  280. break;
  281. }
  282. len = j - i;
  283. for (k = 0; k < len; k++)
  284. compressed[k] = itu_data[k + i] >> ((mode == 3) ? 10 : (mode == 2) ? 9 : 8);
  285. dec_state = g722_decode_init(NULL, (mode == 3) ? 48000 : (mode == 2) ? 56000 : 64000, 0);
  286. dec_state->itu_test_mode = true;
  287. len2 = g722_decode(dec_state, decompressed, compressed, len);
  288. /* Check the result against the ITU's reference output data */
  289. j = 0;
  290. for (k = 0; k < len2; k += 2)
  291. {
  292. if ((decompressed[k] & 0xFFFF) != (itu_ref[(k >> 1) + i] & 0xFFFF)
  293. ||
  294. (decompressed[k + 1] & 0xFFFF) != (itu_ref_upper[(k >> 1) + i] & 0xFFFF))
  295. {
  296. printf(">>> %6d %4x %4x %4x %4x\n", k >> 1, decompressed[k] & 0xFFFF, decompressed[k + 1] & 0xFFFF, itu_ref[(k >> 1) + i] & 0xFFFF, itu_ref_upper[(k >> 1) + i] & 0xFFFF);
  297. j++;
  298. }
  299. }
  300. printf("%d bad samples, out of %d/%d samples\n", j, len, len_data);
  301. if (j)
  302. {
  303. printf("Test failed\n");
  304. exit(2);
  305. }
  306. printf("Test passed\n");
  307. g722_decode_free(dec_state);
  308. }
  309. }
  310. #endif
  311. printf("Tests passed.\n");
  312. }
  313. /*- End of function --------------------------------------------------------*/
  314. static void signal_to_distortion_tests(void)
  315. {
  316. g722_encode_state_t *enc_state;
  317. g722_decode_state_t *dec_state;
  318. swept_tone_state_t *swept;
  319. power_meter_t *in_meter;
  320. power_meter_t *out_meter;
  321. int16_t original[1024];
  322. uint8_t compressed[1024];
  323. int16_t decompressed[1024];
  324. int len;
  325. int len2;
  326. int len3;
  327. int i;
  328. int32_t in_level;
  329. int32_t out_level;
  330. /* Test a back to back encoder/decoder pair to ensure we comply with Figure 11/G.722 to
  331. Figure 16/G.722, Figure A.1/G.722, and Figure A.2/G.722 */
  332. enc_state = g722_encode_init(NULL, 64000, 0);
  333. dec_state = g722_decode_init(NULL, 64000, 0);
  334. in_meter = power_meter_init(NULL, 7);
  335. out_meter = power_meter_init(NULL, 7);
  336. /* First some silence */
  337. len = 1024;
  338. memset(original, 0, len*sizeof(original[0]));
  339. for (i = 0; i < len; i++)
  340. in_level = power_meter_update(in_meter, original[i]);
  341. len2 = g722_encode(enc_state, compressed, original, len);
  342. len3 = g722_decode(dec_state, decompressed, compressed, len2);
  343. out_level = 0;
  344. for (i = 0; i < len3; i++)
  345. out_level = power_meter_update(out_meter, decompressed[i]);
  346. printf("Silence produces %d at the output\n", out_level);
  347. /* Now a swept tone test */
  348. swept = swept_tone_init(NULL, 25.0f, 3500.0f, -10.0f, 60*16000, false);
  349. do
  350. {
  351. len = swept_tone(swept, original, 1024);
  352. for (i = 0; i < len; i++)
  353. in_level = power_meter_update(in_meter, original[i]);
  354. len2 = g722_encode(enc_state, compressed, original, len);
  355. len3 = g722_decode(dec_state, decompressed, compressed, len2);
  356. for (i = 0; i < len3; i++)
  357. out_level = power_meter_update(out_meter, decompressed[i]);
  358. printf("%10d, %10d, %f\n", in_level, out_level, (float) out_level/in_level);
  359. }
  360. while (len > 0);
  361. swept_tone_free(swept);
  362. g722_encode_free(enc_state);
  363. g722_decode_free(dec_state);
  364. power_meter_free(in_meter);
  365. power_meter_free(out_meter);
  366. }
  367. /*- End of function --------------------------------------------------------*/
  368. int main(int argc, char *argv[])
  369. {
  370. g722_encode_state_t *enc_state;
  371. g722_decode_state_t *dec_state;
  372. int len2;
  373. int len3;
  374. int i;
  375. int file;
  376. SNDFILE *inhandle;
  377. SNDFILE *outhandle;
  378. SF_INFO info;
  379. int outframes;
  380. int samples;
  381. int opt;
  382. int itutests;
  383. int bit_rate;
  384. int eight_k_in;
  385. int eight_k_out;
  386. int encode;
  387. int decode;
  388. int tone_test;
  389. const char *in_file;
  390. const char *out_file;
  391. int16_t indata[BLOCK_LEN];
  392. int16_t outdata[BLOCK_LEN];
  393. uint8_t adpcmdata[BLOCK_LEN];
  394. float tone_level;
  395. uint32_t tone_phase;
  396. int32_t tone_phase_rate;
  397. bit_rate = 64000;
  398. eight_k_in = false;
  399. eight_k_out = false;
  400. itutests = true;
  401. encode = false;
  402. decode = false;
  403. tone_test = false;
  404. in_file = NULL;
  405. out_file = NULL;
  406. while ((opt = getopt(argc, argv, "b:d:e:i:l:o:t")) != -1)
  407. {
  408. switch (opt)
  409. {
  410. case 'b':
  411. bit_rate = atoi(optarg);
  412. if (bit_rate != 48000 && bit_rate != 56000 && bit_rate != 64000)
  413. {
  414. fprintf(stderr, "Invalid bit rate selected. Only 48000, 56000 and 64000 are valid.\n");
  415. exit(2);
  416. }
  417. itutests = false;
  418. break;
  419. case 'd':
  420. in_file = optarg;
  421. decode = true;
  422. itutests = false;
  423. break;
  424. case 'e':
  425. in_file = optarg;
  426. encode = true;
  427. itutests = false;
  428. break;
  429. case 'i':
  430. i = atoi(optarg);
  431. if (i != 8000 && i != 16000)
  432. {
  433. fprintf(stderr, "Invalid incoming sample rate. Only 8000 and 16000 are valid.\n");
  434. exit(2);
  435. }
  436. eight_k_in = (i == 8000);
  437. if (eight_k_in)
  438. in_file = EIGHTK_IN_FILE_NAME;
  439. break;
  440. case 'l':
  441. out_file = optarg;
  442. break;
  443. case 'o':
  444. i = atoi(optarg);
  445. if (i != 8000 && i != 16000)
  446. {
  447. fprintf(stderr, "Invalid outgoing sample rate. Only 8000 and 16000 are valid.\n");
  448. exit(2);
  449. }
  450. eight_k_out = (i == 8000);
  451. break;
  452. case 't':
  453. tone_test = true;
  454. itutests = false;
  455. break;
  456. default:
  457. //usage();
  458. exit(2);
  459. }
  460. }
  461. if (itutests)
  462. {
  463. itu_compliance_tests();
  464. signal_to_distortion_tests();
  465. }
  466. else
  467. {
  468. tone_level = dds_scaling_dbm0f(2.5f);
  469. tone_phase = 0;
  470. tone_phase_rate = dds_phase_ratef(1500.0f/2.0f);
  471. if (!decode && !encode)
  472. {
  473. decode =
  474. encode = true;
  475. }
  476. if (in_file == NULL)
  477. {
  478. if (encode)
  479. {
  480. if (eight_k_in)
  481. in_file = EIGHTK_IN_FILE_NAME;
  482. else
  483. in_file = IN_FILE_NAME;
  484. }
  485. else
  486. {
  487. in_file = ENCODED_FILE_NAME;
  488. }
  489. }
  490. if (out_file == NULL)
  491. {
  492. out_file = (decode) ? OUT_FILE_NAME : ENCODED_FILE_NAME;
  493. }
  494. inhandle = NULL;
  495. outhandle = NULL;
  496. file = -1;
  497. if (encode)
  498. {
  499. if (eight_k_in)
  500. {
  501. if ((inhandle = sf_open(in_file, SFM_READ, &info)) == NULL)
  502. {
  503. fprintf(stderr, " Cannot open audio file '%s'\n", in_file);
  504. exit(2);
  505. }
  506. if (info.samplerate != SAMPLE_RATE)
  507. {
  508. fprintf(stderr, " Unexpected sample rate %d in audio file '%s'\n", info.samplerate, in_file);
  509. exit(2);
  510. }
  511. if (info.channels != 1)
  512. {
  513. fprintf(stderr, " Unexpected number of channels in audio file '%s'\n", in_file);
  514. exit(2);
  515. }
  516. enc_state = g722_encode_init(NULL, bit_rate, G722_PACKED | G722_SAMPLE_RATE_8000);
  517. }
  518. else
  519. {
  520. if ((inhandle = sf_open(in_file, SFM_READ, &info)) == NULL)
  521. {
  522. fprintf(stderr, " Cannot open audio file '%s'\n", in_file);
  523. exit(2);
  524. }
  525. if (info.samplerate != G722_SAMPLE_RATE)
  526. {
  527. fprintf(stderr, " Unexpected sample rate %d in audio file '%s'\n", info.samplerate, in_file);
  528. exit(2);
  529. }
  530. if (info.channels != 1)
  531. {
  532. fprintf(stderr, " Unexpected number of channels in audio file '%s'\n", in_file);
  533. exit(2);
  534. }
  535. enc_state = g722_encode_init(NULL, bit_rate, G722_PACKED);
  536. }
  537. }
  538. else
  539. {
  540. if ((file = open(in_file, O_RDONLY)) < 0)
  541. {
  542. fprintf(stderr, " Failed to open '%s'\n", in_file);
  543. exit(2);
  544. }
  545. }
  546. if (decode)
  547. {
  548. memset(&info, 0, sizeof(info));
  549. info.frames = 0;
  550. info.samplerate = (eight_k_out) ? SAMPLE_RATE : G722_SAMPLE_RATE;
  551. info.channels = 1;
  552. info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
  553. info.sections = 1;
  554. info.seekable = 1;
  555. if ((outhandle = sf_open(out_file, SFM_WRITE, &info)) == NULL)
  556. {
  557. fprintf(stderr, " Cannot create audio file '%s'\n", out_file);
  558. exit(2);
  559. }
  560. if (eight_k_out)
  561. dec_state = g722_decode_init(NULL, bit_rate, G722_PACKED | G722_SAMPLE_RATE_8000);
  562. else
  563. dec_state = g722_decode_init(NULL, bit_rate, G722_PACKED);
  564. }
  565. else
  566. {
  567. if ((file = open(out_file, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
  568. {
  569. fprintf(stderr, " Failed to open '%s'\n", out_file);
  570. exit(2);
  571. }
  572. }
  573. for (;;)
  574. {
  575. if (encode)
  576. {
  577. samples = sf_readf_short(inhandle, indata, BLOCK_LEN);
  578. if (samples <= 0)
  579. break;
  580. if (tone_test)
  581. {
  582. for (i = 0; i < samples; i++)
  583. indata[i] = dds_modf(&tone_phase, tone_phase_rate, tone_level, 0);
  584. }
  585. len2 = g722_encode(enc_state, adpcmdata, indata, samples);
  586. }
  587. else
  588. {
  589. len2 = read(file, adpcmdata, BLOCK_LEN);
  590. if (len2 <= 0)
  591. break;
  592. }
  593. if (decode)
  594. {
  595. len3 = g722_decode(dec_state, outdata, adpcmdata, len2);
  596. outframes = sf_writef_short(outhandle, outdata, len3);
  597. if (outframes != len3)
  598. {
  599. fprintf(stderr, " Error writing audio file\n");
  600. exit(2);
  601. }
  602. }
  603. else
  604. {
  605. len3 = write(file, adpcmdata, len2);
  606. if (len3 <= 0)
  607. break;
  608. }
  609. }
  610. if (encode)
  611. {
  612. if (sf_close(inhandle))
  613. {
  614. fprintf(stderr, " Cannot close audio file '%s'\n", IN_FILE_NAME);
  615. exit(2);
  616. }
  617. g722_encode_free(enc_state);
  618. }
  619. else
  620. {
  621. close(file);
  622. }
  623. if (decode)
  624. {
  625. if (sf_close(outhandle))
  626. {
  627. fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
  628. exit(2);
  629. }
  630. g722_decode_free(dec_state);
  631. }
  632. else
  633. {
  634. close(file);
  635. }
  636. printf("'%s' translated to '%s' at %dbps.\n", in_file, out_file, bit_rate);
  637. }
  638. return 0;
  639. }
  640. /*- End of function --------------------------------------------------------*/
  641. /*- End of file ------------------------------------------------------------*/