sampledec.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 *outFile;
  8. FILE *fout;
  9. /*Holds the audio that will be written to file (16 bits per sample)*/
  10. short out[FRAME_SIZE];
  11. /*Speex handle samples as float, so we need an array of floats*/
  12. float output[FRAME_SIZE];
  13. char cbits[200];
  14. int nbBytes;
  15. /*Holds the state of the decoder*/
  16. void *state;
  17. /*Holds bits so they can be read and written to by the Speex routines*/
  18. SpeexBits bits;
  19. int i, tmp;
  20. /*Create a new decoder state in narrowband mode*/
  21. state = speex_decoder_init(&speex_nb_mode);
  22. /*Set the perceptual enhancement on*/
  23. tmp=1;
  24. speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);
  25. outFile = argv[1];
  26. fout = fopen(outFile, "w");
  27. /*Initialization of the structure that holds the bits*/
  28. speex_bits_init(&bits);
  29. while (1)
  30. {
  31. /*Read the size encoded by sampleenc, this part will likely be
  32. different in your application*/
  33. fread(&nbBytes, sizeof(int), 1, stdin);
  34. fprintf (stderr, "nbBytes: %d\n", nbBytes);
  35. if (feof(stdin))
  36. break;
  37. /*Read the "packet" encoded by sampleenc*/
  38. fread(cbits, 1, nbBytes, stdin);
  39. /*Copy the data into the bit-stream struct*/
  40. speex_bits_read_from(&bits, cbits, nbBytes);
  41. /*Decode the data*/
  42. speex_decode(state, &bits, output);
  43. /*Copy from float to short (16 bits) for output*/
  44. for (i=0;i<FRAME_SIZE;i++)
  45. out[i]=output[i];
  46. /*Write the decoded audio to file*/
  47. fwrite(out, sizeof(short), FRAME_SIZE, fout);
  48. }
  49. /*Destroy the decoder state*/
  50. speex_decoder_destroy(state);
  51. /*Destroy the bit-stream truct*/
  52. speex_bits_destroy(&bits);
  53. fclose(fout);
  54. return 0;
  55. }