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