153a5a1b3Sopenharmony_ci#include <speex/speex.h>
253a5a1b3Sopenharmony_ci#include <stdio.h>
353a5a1b3Sopenharmony_ci
453a5a1b3Sopenharmony_ci/*The frame size in hardcoded for this sample code but it doesn't have to be*/
553a5a1b3Sopenharmony_ci#define FRAME_SIZE 160
653a5a1b3Sopenharmony_ciint main(int argc, char **argv)
753a5a1b3Sopenharmony_ci{
853a5a1b3Sopenharmony_ci   char *outFile;
953a5a1b3Sopenharmony_ci   FILE *fout;
1053a5a1b3Sopenharmony_ci   /*Holds the audio that will be written to file (16 bits per sample)*/
1153a5a1b3Sopenharmony_ci   short out[FRAME_SIZE];
1253a5a1b3Sopenharmony_ci   /*Speex handle samples as float, so we need an array of floats*/
1353a5a1b3Sopenharmony_ci   float output[FRAME_SIZE];
1453a5a1b3Sopenharmony_ci   char cbits[200];
1553a5a1b3Sopenharmony_ci   int nbBytes;
1653a5a1b3Sopenharmony_ci   /*Holds the state of the decoder*/
1753a5a1b3Sopenharmony_ci   void *state;
1853a5a1b3Sopenharmony_ci   /*Holds bits so they can be read and written to by the Speex routines*/
1953a5a1b3Sopenharmony_ci   SpeexBits bits;
2053a5a1b3Sopenharmony_ci   int i, tmp;
2153a5a1b3Sopenharmony_ci
2253a5a1b3Sopenharmony_ci   /*Create a new decoder state in narrowband mode*/
2353a5a1b3Sopenharmony_ci   state = speex_decoder_init(&speex_nb_mode);
2453a5a1b3Sopenharmony_ci
2553a5a1b3Sopenharmony_ci   /*Set the perceptual enhancement on*/
2653a5a1b3Sopenharmony_ci   tmp=1;
2753a5a1b3Sopenharmony_ci   speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);
2853a5a1b3Sopenharmony_ci
2953a5a1b3Sopenharmony_ci   outFile = argv[1];
3053a5a1b3Sopenharmony_ci   fout = fopen(outFile, "w");
3153a5a1b3Sopenharmony_ci
3253a5a1b3Sopenharmony_ci   /*Initialization of the structure that holds the bits*/
3353a5a1b3Sopenharmony_ci   speex_bits_init(&bits);
3453a5a1b3Sopenharmony_ci   while (1)
3553a5a1b3Sopenharmony_ci   {
3653a5a1b3Sopenharmony_ci      /*Read the size encoded by sampleenc, this part will likely be
3753a5a1b3Sopenharmony_ci        different in your application*/
3853a5a1b3Sopenharmony_ci      fread(&nbBytes, sizeof(int), 1, stdin);
3953a5a1b3Sopenharmony_ci      fprintf (stderr, "nbBytes: %d\n", nbBytes);
4053a5a1b3Sopenharmony_ci      if (feof(stdin))
4153a5a1b3Sopenharmony_ci         break;
4253a5a1b3Sopenharmony_ci
4353a5a1b3Sopenharmony_ci      /*Read the "packet" encoded by sampleenc*/
4453a5a1b3Sopenharmony_ci      fread(cbits, 1, nbBytes, stdin);
4553a5a1b3Sopenharmony_ci      /*Copy the data into the bit-stream struct*/
4653a5a1b3Sopenharmony_ci      speex_bits_read_from(&bits, cbits, nbBytes);
4753a5a1b3Sopenharmony_ci
4853a5a1b3Sopenharmony_ci      /*Decode the data*/
4953a5a1b3Sopenharmony_ci      speex_decode(state, &bits, output);
5053a5a1b3Sopenharmony_ci
5153a5a1b3Sopenharmony_ci      /*Copy from float to short (16 bits) for output*/
5253a5a1b3Sopenharmony_ci      for (i=0;i<FRAME_SIZE;i++)
5353a5a1b3Sopenharmony_ci         out[i]=output[i];
5453a5a1b3Sopenharmony_ci
5553a5a1b3Sopenharmony_ci      /*Write the decoded audio to file*/
5653a5a1b3Sopenharmony_ci      fwrite(out, sizeof(short), FRAME_SIZE, fout);
5753a5a1b3Sopenharmony_ci   }
5853a5a1b3Sopenharmony_ci
5953a5a1b3Sopenharmony_ci   /*Destroy the decoder state*/
6053a5a1b3Sopenharmony_ci   speex_decoder_destroy(state);
6153a5a1b3Sopenharmony_ci   /*Destroy the bit-stream truct*/
6253a5a1b3Sopenharmony_ci   speex_bits_destroy(&bits);
6353a5a1b3Sopenharmony_ci   fclose(fout);
6453a5a1b3Sopenharmony_ci   return 0;
6553a5a1b3Sopenharmony_ci}
66