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 *inFile;
953a5a1b3Sopenharmony_ci   FILE *fin;
1053a5a1b3Sopenharmony_ci   short in[FRAME_SIZE];
1153a5a1b3Sopenharmony_ci   float input[FRAME_SIZE];
1253a5a1b3Sopenharmony_ci   char cbits[200];
1353a5a1b3Sopenharmony_ci   int nbBytes;
1453a5a1b3Sopenharmony_ci   /*Holds the state of the encoder*/
1553a5a1b3Sopenharmony_ci   void *state;
1653a5a1b3Sopenharmony_ci   /*Holds bits so they can be read and written to by the Speex routines*/
1753a5a1b3Sopenharmony_ci   SpeexBits bits;
1853a5a1b3Sopenharmony_ci   int i, tmp;
1953a5a1b3Sopenharmony_ci
2053a5a1b3Sopenharmony_ci   /*Create a new encoder state in narrowband mode*/
2153a5a1b3Sopenharmony_ci   state = speex_encoder_init(&speex_nb_mode);
2253a5a1b3Sopenharmony_ci
2353a5a1b3Sopenharmony_ci   /*Set the quality to 8 (15 kbps)*/
2453a5a1b3Sopenharmony_ci   tmp=8;
2553a5a1b3Sopenharmony_ci   speex_encoder_ctl(state, SPEEX_SET_QUALITY, &tmp);
2653a5a1b3Sopenharmony_ci
2753a5a1b3Sopenharmony_ci   inFile = argv[1];
2853a5a1b3Sopenharmony_ci   fin = fopen(inFile, "r");
2953a5a1b3Sopenharmony_ci
3053a5a1b3Sopenharmony_ci   /*Initialization of the structure that holds the bits*/
3153a5a1b3Sopenharmony_ci   speex_bits_init(&bits);
3253a5a1b3Sopenharmony_ci   while (1)
3353a5a1b3Sopenharmony_ci   {
3453a5a1b3Sopenharmony_ci      /*Read a 16 bits/sample audio frame*/
3553a5a1b3Sopenharmony_ci      fread(in, sizeof(short), FRAME_SIZE, fin);
3653a5a1b3Sopenharmony_ci      if (feof(fin))
3753a5a1b3Sopenharmony_ci         break;
3853a5a1b3Sopenharmony_ci      /*Copy the 16 bits values to float so Speex can work on them*/
3953a5a1b3Sopenharmony_ci      for (i=0;i<FRAME_SIZE;i++)
4053a5a1b3Sopenharmony_ci         input[i]=in[i];
4153a5a1b3Sopenharmony_ci
4253a5a1b3Sopenharmony_ci      /*Flush all the bits in the struct so we can encode a new frame*/
4353a5a1b3Sopenharmony_ci      speex_bits_reset(&bits);
4453a5a1b3Sopenharmony_ci
4553a5a1b3Sopenharmony_ci      /*Encode the frame*/
4653a5a1b3Sopenharmony_ci      speex_encode(state, input, &bits);
4753a5a1b3Sopenharmony_ci      /*Copy the bits to an array of char that can be written*/
4853a5a1b3Sopenharmony_ci      nbBytes = speex_bits_write(&bits, cbits, 200);
4953a5a1b3Sopenharmony_ci
5053a5a1b3Sopenharmony_ci      /*Write the size of the frame first. This is what sampledec expects but
5153a5a1b3Sopenharmony_ci       it's likely to be different in your own application*/
5253a5a1b3Sopenharmony_ci      fwrite(&nbBytes, sizeof(int), 1, stdout);
5353a5a1b3Sopenharmony_ci      /*Write the compressed data*/
5453a5a1b3Sopenharmony_ci      fwrite(cbits, 1, nbBytes, stdout);
5553a5a1b3Sopenharmony_ci
5653a5a1b3Sopenharmony_ci   }
5753a5a1b3Sopenharmony_ci
5853a5a1b3Sopenharmony_ci   /*Destroy the encoder state*/
5953a5a1b3Sopenharmony_ci   speex_encoder_destroy(state);
6053a5a1b3Sopenharmony_ci   /*Destroy the bit-packing struct*/
6153a5a1b3Sopenharmony_ci   speex_bits_destroy(&bits);
6253a5a1b3Sopenharmony_ci   fclose(fin);
6353a5a1b3Sopenharmony_ci   return 0;
6453a5a1b3Sopenharmony_ci}
65