Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.4 KiB

  1. #include "speex.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "speex_callbacks.h"
  5. #define FRAME_SIZE 160
  6. #include <math.h>
  7. int main(int argc, char **argv)
  8. {
  9. char *inFile, *outFile, *bitsFile;
  10. FILE *fin, *fout, *fbits=NULL;
  11. short in[FRAME_SIZE];
  12. float input[FRAME_SIZE], bak[FRAME_SIZE], bak2[FRAME_SIZE];
  13. char cbits[200];
  14. int nbBits;
  15. int i;
  16. void *st;
  17. void *dec;
  18. SpeexBits bits;
  19. int tmp;
  20. int bitCount=0;
  21. SpeexCallback callback;
  22. for (i=0;i<FRAME_SIZE;i++)
  23. bak2[i]=0;
  24. st = speex_encoder_init(&speex_nb_mode);
  25. dec = speex_decoder_init(&speex_nb_mode);
  26. callback.callback_id = SPEEX_INBAND_CHAR;
  27. callback.func = speex_std_char_handler;
  28. callback.data = stderr;
  29. speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
  30. callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
  31. callback.func = speex_std_mode_request_handler;
  32. callback.data = st;
  33. speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
  34. tmp=0;
  35. speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
  36. tmp=0;
  37. speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
  38. tmp=8;
  39. speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
  40. tmp=1;
  41. speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
  42. speex_mode_query(&speex_nb_mode, SPEEX_MODE_FRAME_SIZE, &tmp);
  43. fprintf (stderr, "frame size: %d\n", tmp);
  44. if (argc != 4 && argc != 3)
  45. {
  46. fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc);
  47. exit(1);
  48. }
  49. inFile = argv[1];
  50. fin = fopen(inFile, "r");
  51. outFile = argv[2];
  52. fout = fopen(outFile, "w");
  53. if (argc==4)
  54. {
  55. bitsFile = argv[3];
  56. fbits = fopen(bitsFile, "w");
  57. }
  58. speex_bits_init(&bits);
  59. while (!feof(fin))
  60. {
  61. fread(in, sizeof(short), FRAME_SIZE, fin);
  62. if (feof(fin))
  63. break;
  64. for (i=0;i<FRAME_SIZE;i++)
  65. bak[i]=input[i]=in[i];
  66. speex_bits_reset(&bits);
  67. /*
  68. speex_bits_pack(&bits, 14, 5);
  69. speex_bits_pack(&bits, SPEEX_INBAND_CHAR, 4);
  70. speex_bits_pack(&bits, 'A', 8);
  71. speex_bits_pack(&bits, 14, 5);
  72. speex_bits_pack(&bits, SPEEX_INBAND_MODE_REQUEST, 4);
  73. speex_bits_pack(&bits, 7, 4);
  74. speex_bits_pack(&bits, 15, 5);
  75. speex_bits_pack(&bits, 2, 4);
  76. speex_bits_pack(&bits, 0, 16);
  77. */
  78. speex_encode(st, input, &bits);
  79. nbBits = speex_bits_write(&bits, cbits, 200);
  80. bitCount+=bits.nbBits;
  81. printf ("Encoding frame in %d bits\n", nbBits*8);
  82. if (argc==4)
  83. fwrite(cbits, 1, nbBits, fbits);
  84. {
  85. float enoise=0, esig=0, snr;
  86. for (i=0;i<FRAME_SIZE;i++)
  87. {
  88. enoise+=(bak2[i]-input[i])*(bak2[i]-input[i]);
  89. esig += bak2[i]*bak2[i];
  90. }
  91. snr = 10*log10((esig+1)/(enoise+1));
  92. printf ("real SNR = %f\n", snr);
  93. }
  94. speex_bits_rewind(&bits);
  95. speex_decode(dec, &bits, input);
  96. /* Save the bits here */
  97. for (i=0;i<FRAME_SIZE;i++)
  98. {
  99. if (input[i]>32000)
  100. input[i]=32000;
  101. else if (input[i]<-32000)
  102. input[i]=-32000;
  103. }
  104. speex_bits_reset(&bits);
  105. for (i=0;i<FRAME_SIZE;i++)
  106. in[i]=(short)input[i];
  107. for (i=0;i<FRAME_SIZE;i++)
  108. bak2[i]=bak[i];
  109. fwrite(in, sizeof(short), FRAME_SIZE, fout);
  110. }
  111. fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
  112. speex_encoder_destroy(st);
  113. speex_decoder_destroy(dec);
  114. return 1;
  115. }