testenc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <speex/speex.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <speex/speex_callbacks.h>
  8. #ifdef FIXED_DEBUG
  9. extern long long spx_mips;
  10. #endif
  11. #define FRAME_SIZE 160
  12. #include <math.h>
  13. int main(int argc, char **argv)
  14. {
  15. char *inFile, *outFile, *bitsFile;
  16. FILE *fin, *fout, *fbits=NULL;
  17. short in_short[FRAME_SIZE];
  18. short out_short[FRAME_SIZE];
  19. int snr_frames = 0;
  20. char cbits[200];
  21. int nbBits;
  22. int i;
  23. void *st;
  24. void *dec;
  25. SpeexBits bits;
  26. spx_int32_t tmp;
  27. int bitCount=0;
  28. spx_int32_t skip_group_delay;
  29. SpeexCallback callback;
  30. st = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));
  31. dec = speex_decoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));
  32. /* BEGIN: You probably don't need the following in a real application */
  33. callback.callback_id = SPEEX_INBAND_CHAR;
  34. callback.func = speex_std_char_handler;
  35. callback.data = stderr;
  36. speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
  37. callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
  38. callback.func = speex_std_mode_request_handler;
  39. callback.data = st;
  40. speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
  41. /* END of unnecessary stuff */
  42. tmp=1;
  43. speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
  44. tmp=0;
  45. speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
  46. tmp=8;
  47. speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
  48. tmp=1;
  49. speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
  50. /* Turn this off if you want to measure SNR (on by default) */
  51. tmp=1;
  52. speex_encoder_ctl(st, SPEEX_SET_HIGHPASS, &tmp);
  53. speex_decoder_ctl(dec, SPEEX_SET_HIGHPASS, &tmp);
  54. speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &skip_group_delay);
  55. speex_decoder_ctl(dec, SPEEX_GET_LOOKAHEAD, &tmp);
  56. skip_group_delay += tmp;
  57. if (argc != 4 && argc != 3)
  58. {
  59. fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc);
  60. exit(1);
  61. }
  62. inFile = argv[1];
  63. fin = fopen(inFile, "rb");
  64. outFile = argv[2];
  65. fout = fopen(outFile, "wb+");
  66. if (argc==4)
  67. {
  68. bitsFile = argv[3];
  69. fbits = fopen(bitsFile, "wb");
  70. }
  71. speex_bits_init(&bits);
  72. while (!feof(fin))
  73. {
  74. fread(in_short, sizeof(short), FRAME_SIZE, fin);
  75. if (feof(fin))
  76. break;
  77. speex_bits_reset(&bits);
  78. speex_encode_int(st, in_short, &bits);
  79. nbBits = speex_bits_write(&bits, cbits, 200);
  80. bitCount+=bits.nbBits;
  81. if (argc==4)
  82. fwrite(cbits, 1, nbBits, fbits);
  83. speex_bits_rewind(&bits);
  84. speex_decode_int(dec, &bits, out_short);
  85. speex_bits_reset(&bits);
  86. fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
  87. skip_group_delay = 0;
  88. }
  89. fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
  90. speex_encoder_destroy(st);
  91. speex_decoder_destroy(dec);
  92. speex_bits_destroy(&bits);
  93. #ifndef DISABLE_FLOAT_API
  94. {
  95. float sigpow,errpow,snr, seg_snr=0;
  96. sigpow = 0;
  97. errpow = 0;
  98. /* This code just computes SNR, so you don't need it either */
  99. rewind(fin);
  100. rewind(fout);
  101. while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin)
  102. &&
  103. FRAME_SIZE == fread(out_short, sizeof(short), FRAME_SIZE,fout) )
  104. {
  105. float s=0, e=0;
  106. for (i=0;i<FRAME_SIZE;++i) {
  107. s += (float)in_short[i] * in_short[i];
  108. e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
  109. }
  110. seg_snr += 10*log10((s+160)/(e+160));
  111. sigpow += s;
  112. errpow += e;
  113. snr_frames++;
  114. }
  115. snr = 10 * log10( sigpow / errpow );
  116. seg_snr /= snr_frames;
  117. fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
  118. #ifdef FIXED_DEBUG
  119. printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
  120. #endif
  121. }
  122. #endif
  123. fclose(fin);
  124. fclose(fout);
  125. return 0;
  126. }