lpc10_tests.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * lpc10_tests.c - Test the LPC10 low bit rate speech codec.
  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. /*! \file */
  26. /*! \page lpc10_tests_page LPC10 codec tests
  27. \section lpc10_tests_page_sec_1 What does it do?
  28. \section lpc10_tests_page_sec_2 How is it used?
  29. To perform a general audio quality test, lpc10 should be run. The file ../test-data/local/dam9.wav
  30. will be compressed to LPC10 data, decompressed, and the resulting audio stored in post_lpc10.wav.
  31. */
  32. #if defined(HAVE_CONFIG_H)
  33. #include "config.h"
  34. #endif
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <assert.h>
  41. #include <ctype.h>
  42. #include <sndfile.h>
  43. #include "spandsp.h"
  44. #include "spandsp-sim.h"
  45. #define BLOCK_LEN 180
  46. #define BLOCKS_PER_READ 5
  47. #define IN_FILE_NAME "../test-data/local/dam9.wav"
  48. #define REF_FILE_NAME "../test-data/local/dam9_lpc55.wav"
  49. #define COMPRESS_FILE_NAME "lpc10_out.lpc10"
  50. #define DECOMPRESS_FILE_NAME "lpc10_in.lpc10"
  51. #define OUT_FILE_NAME "post_lpc10.wav"
  52. int main(int argc, char *argv[])
  53. {
  54. SNDFILE *inhandle;
  55. SNDFILE *refhandle;
  56. SNDFILE *outhandle;
  57. int frames;
  58. double pre_energy;
  59. double post_energy;
  60. double ref_energy;
  61. double diff_energy;
  62. int16_t pre_amp[BLOCKS_PER_READ*BLOCK_LEN];
  63. int16_t post_amp[BLOCKS_PER_READ*BLOCK_LEN];
  64. int16_t ref_amp[BLOCKS_PER_READ*BLOCK_LEN];
  65. int16_t log_amp[BLOCKS_PER_READ*BLOCK_LEN*3];
  66. uint8_t lpc10_data[BLOCKS_PER_READ*7];
  67. double xx;
  68. lpc10_encode_state_t *lpc10_enc_state;
  69. lpc10_decode_state_t *lpc10_dec_state;
  70. int i;
  71. int block_no;
  72. bool log_error;
  73. bool compress;
  74. bool decompress;
  75. const char *in_file_name;
  76. int compress_file;
  77. int decompress_file;
  78. int len;
  79. int opt;
  80. int enc_len;
  81. int dec_len;
  82. compress = false;
  83. decompress = false;
  84. log_error = true;
  85. in_file_name = IN_FILE_NAME;
  86. while ((opt = getopt(argc, argv, "cdi:l")) != -1)
  87. {
  88. switch (opt)
  89. {
  90. case 'c':
  91. compress = true;
  92. break;
  93. case 'd':
  94. decompress = true;
  95. break;
  96. case 'i':
  97. in_file_name = optarg;
  98. break;
  99. case 'l':
  100. log_error = false;
  101. break;
  102. default:
  103. //usage();
  104. exit(2);
  105. }
  106. }
  107. compress_file = -1;
  108. decompress_file = -1;
  109. inhandle = NULL;
  110. refhandle = NULL;
  111. outhandle = NULL;
  112. if (!decompress)
  113. {
  114. if ((inhandle = sf_open_telephony_read(in_file_name, 1)) == NULL)
  115. {
  116. fprintf(stderr, " Cannot open audio file '%s'\n", in_file_name);
  117. exit(2);
  118. }
  119. if ((refhandle = sf_open_telephony_read(REF_FILE_NAME, 1)) == NULL)
  120. {
  121. fprintf(stderr, " Cannot open audio file '%s'\n", REF_FILE_NAME);
  122. exit(2);
  123. }
  124. }
  125. else
  126. {
  127. if ((decompress_file = open(DECOMPRESS_FILE_NAME, O_RDONLY)) < 0)
  128. {
  129. fprintf(stderr, " Cannot open decompressed data file '%s'\n", DECOMPRESS_FILE_NAME);
  130. exit(2);
  131. }
  132. }
  133. if ((outhandle = sf_open_telephony_write(OUT_FILE_NAME, 1)) == NULL)
  134. {
  135. fprintf(stderr, " Cannot create audio file '%s'\n", OUT_FILE_NAME);
  136. exit(2);
  137. }
  138. if ((lpc10_enc_state = lpc10_encode_init(NULL, true)) == NULL)
  139. {
  140. fprintf(stderr, " Cannot create encoder\n");
  141. exit(2);
  142. }
  143. if ((lpc10_dec_state = lpc10_decode_init(NULL, true)) == NULL)
  144. {
  145. fprintf(stderr, " Cannot create decoder\n");
  146. exit(2);
  147. }
  148. if (compress)
  149. {
  150. if ((compress_file = open(COMPRESS_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
  151. {
  152. fprintf(stderr, " Cannot create compressed data file '%s'\n", COMPRESS_FILE_NAME);
  153. exit(2);
  154. }
  155. }
  156. pre_energy = 0.0;
  157. post_energy = 0.0;
  158. ref_energy = 0.0;
  159. diff_energy = 0.0;
  160. if (decompress)
  161. {
  162. while ((len = read(decompress_file, lpc10_data, BLOCKS_PER_READ*7)) > 0)
  163. {
  164. lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, len/7);
  165. sf_writef_short(outhandle, post_amp, BLOCK_LEN*len/7);
  166. }
  167. }
  168. else
  169. {
  170. block_no = 0;
  171. while ((frames = sf_readf_short(inhandle, pre_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN
  172. &&
  173. (frames = sf_readf_short(refhandle, ref_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN)
  174. {
  175. enc_len = lpc10_encode(lpc10_enc_state, lpc10_data, pre_amp, BLOCKS_PER_READ*BLOCK_LEN);
  176. if (compress)
  177. write(compress_file, lpc10_data, enc_len);
  178. dec_len = lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, enc_len);
  179. for (i = 0; i < dec_len; i++)
  180. {
  181. pre_energy += (double) pre_amp[i]*(double) pre_amp[i];
  182. post_energy += (double) post_amp[i]*(double) post_amp[i];
  183. ref_energy += (double) ref_amp[i]*(double) ref_amp[i];
  184. /* The reference file has some odd clipping, so eliminate this from the
  185. energy measurement. */
  186. if (ref_amp[i] == 32767 || ref_amp[i] == -32768)
  187. xx = 0.0;
  188. else
  189. xx = post_amp[i] - ref_amp[i];
  190. diff_energy += (double) xx*(double) xx;
  191. log_amp[i] = xx;
  192. }
  193. block_no++;
  194. if (log_error)
  195. sf_writef_short(outhandle, log_amp, dec_len);
  196. else
  197. sf_writef_short(outhandle, post_amp, dec_len);
  198. }
  199. if (sf_close_telephony(inhandle))
  200. {
  201. fprintf(stderr, " Cannot close audio file '%s'\n", in_file_name);
  202. exit(2);
  203. }
  204. if (sf_close_telephony(refhandle))
  205. {
  206. fprintf(stderr, " Cannot close audio file '%s'\n", REF_FILE_NAME);
  207. exit(2);
  208. }
  209. }
  210. if (sf_close_telephony(outhandle))
  211. {
  212. fprintf(stderr, " Cannot close audio file '%s'\n", OUT_FILE_NAME);
  213. exit(2);
  214. }
  215. if (compress)
  216. close(compress_file);
  217. if (decompress)
  218. close(decompress_file);
  219. lpc10_encode_free(lpc10_enc_state);
  220. lpc10_decode_free(lpc10_dec_state);
  221. if (!decompress)
  222. {
  223. printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
  224. printf("Difference energy is %f%% of the total.\n", 100.0*diff_energy/ref_energy);
  225. if (fabs(1.0 - post_energy/pre_energy) > 0.05
  226. ||
  227. fabs(diff_energy/post_energy) > 0.03)
  228. {
  229. printf("Tests failed.\n");
  230. exit(2);
  231. }
  232. printf("Tests passed.\n");
  233. }
  234. return 0;
  235. }
  236. /*- End of function --------------------------------------------------------*/
  237. /*- End of file ------------------------------------------------------------*/