sampledec.c 1.81 KB
Newer Older
1
#include <speex/speex.h>
2 3
#include <stdio.h>

jm's avatar
jm committed
4
/*The frame size in hardcoded for this sample code but it doesn't have to be*/
5 6 7 8 9
#define FRAME_SIZE 160
int main(int argc, char **argv)
{
   char *outFile;
   FILE *fout;
jm's avatar
jm committed
10
   /*Holds the audio that will be written to file (16 bits per sample)*/
11
   short out[FRAME_SIZE];
jm's avatar
jm committed
12
   /*Speex handle samples as float, so we need an array of floats*/
13 14 15
   float output[FRAME_SIZE];
   char cbits[200];
   int nbBytes;
jm's avatar
jm committed
16
   /*Holds the state of the decoder*/
17
   void *state;
jm's avatar
jm committed
18
   /*Holds bits so they can be read and written to by the Speex routines*/
19 20 21
   SpeexBits bits;
   int i, tmp;

jm's avatar
jm committed
22
   /*Create a new decoder state in narrowband mode*/
23 24
   state = speex_decoder_init(&speex_nb_mode);

jm's avatar
jm committed
25
   /*Set the perceptual enhancement on*/
26 27 28 29 30 31
   tmp=1;
   speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);

   outFile = argv[1];
   fout = fopen(outFile, "w");

jm's avatar
jm committed
32
   /*Initialization of the structure that holds the bits*/
33 34 35
   speex_bits_init(&bits);
   while (1)
   {
Tristan Matthews's avatar
Tristan Matthews committed
36
      /*Read the size encoded by sampleenc, this part will likely be
jm's avatar
jm committed
37
        different in your application*/
38 39 40 41
      fread(&nbBytes, sizeof(int), 1, stdin);
      fprintf (stderr, "nbBytes: %d\n", nbBytes);
      if (feof(stdin))
         break;
Tristan Matthews's avatar
Tristan Matthews committed
42

jm's avatar
jm committed
43
      /*Read the "packet" encoded by sampleenc*/
44
      fread(cbits, 1, nbBytes, stdin);
jm's avatar
jm committed
45
      /*Copy the data into the bit-stream struct*/
46 47
      speex_bits_read_from(&bits, cbits, nbBytes);

jm's avatar
jm committed
48
      /*Decode the data*/
49 50
      speex_decode(state, &bits, output);

jm's avatar
jm committed
51
      /*Copy from float to short (16 bits) for output*/
52 53 54
      for (i=0;i<FRAME_SIZE;i++)
         out[i]=output[i];

jm's avatar
jm committed
55
      /*Write the decoded audio to file*/
56 57
      fwrite(out, sizeof(short), FRAME_SIZE, fout);
   }
Tristan Matthews's avatar
Tristan Matthews committed
58

jm's avatar
jm committed
59
   /*Destroy the decoder state*/
jm's avatar
jm committed
60
   speex_decoder_destroy(state);
jm's avatar
jm committed
61
   /*Destroy the bit-stream truct*/
62 63 64 65
   speex_bits_destroy(&bits);
   fclose(fout);
   return 0;
}