testenc.c 3.7 KB

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