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.

65 lines
1.8KB

  1. #include <speex/speex.h>
  2. #include <stdio.h>
  3. /*The frame size in hardcoded for this sample code but it doesn't have to be*/
  4. #define FRAME_SIZE 160
  5. int main(int argc, char **argv)
  6. {
  7. char *inFile;
  8. FILE *fin;
  9. short in[FRAME_SIZE];
  10. float input[FRAME_SIZE];
  11. char cbits[200];
  12. int nbBytes;
  13. /*Holds the state of the encoder*/
  14. void *state;
  15. /*Holds bits so they can be read and written to by the Speex routines*/
  16. SpeexBits bits;
  17. int i, tmp;
  18. /*Create a new encoder state in narrowband mode*/
  19. state = speex_encoder_init(&speex_nb_mode);
  20. /*Set the quality to 8 (15 kbps)*/
  21. tmp=8;
  22. speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
  23. inFile = argv[1];
  24. fin = fopen(inFile, "r");
  25. /*Initialization of the structure that holds the bits*/
  26. speex_bits_init(&bits);
  27. while (1)
  28. {
  29. /*Read a 16 bits/sample audio frame*/
  30. fread(in, sizeof(short), FRAME_SIZE, fin);
  31. if (feof(fin))
  32. break;
  33. /*Copy the 16 bits values to float so Speex can work on them*/
  34. for (i=0;i<FRAME_SIZE;i++)
  35. input[i]=in[i];
  36. /*Flush all the bits in the struct so we can encode a new frame*/
  37. speex_bits_reset(&bits);
  38. /*Encode the frame*/
  39. speex_encode(state, input, &bits);
  40. /*Copy the bits to an array of char that can be written*/
  41. nbBytes = speex_bits_write(&bits, cbits, 200);
  42. /*Write the size of the frame first. This is what sampledec expects but
  43. it's likely to be different in your own application*/
  44. fwrite(&nbBytes, sizeof(int), 1, stdout);
  45. /*Write the compressed data*/
  46. fwrite(cbits, 1, nbBytes, stdout);
  47. }
  48. /*Destroy the encoder state*/
  49. speex_encoder_destroy(state);
  50. /*Destroy the bit-packing struct*/
  51. speex_bits_destroy(&bits);
  52. fclose(fin);
  53. return 0;
  54. }