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