testenc_uwb.c 3.41 KB
Newer Older
1 2 3 4
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

Ron's avatar
Ron committed
5
#include "speex/speex_callbacks.h"
6 7
#include <stdio.h>
#include <stdlib.h>
jm's avatar
jm committed
8

9
#ifdef FIXED_DEBUG
jm's avatar
jm committed
10 11
extern long long spx_mips;
#endif
12 13 14 15 16 17 18

#define FRAME_SIZE 640
#include <math.h>
int main(int argc, char **argv)
{
   char *inFile, *outFile, *bitsFile;
   FILE *fin, *fout, *fbits=NULL;
jm's avatar
jm committed
19 20 21 22 23
   short in_short[FRAME_SIZE];
   short out_short[FRAME_SIZE];
   float in_float[FRAME_SIZE];
   float sigpow,errpow,snr, seg_snr=0;
   int snr_frames = 0;
24 25 26 27 28 29
   char cbits[200];
   int nbBits;
   int i;
   void *st;
   void *dec;
   SpeexBits bits;
30
   spx_int32_t tmp;
31
   int bitCount=0;
jm's avatar
jm committed
32
   spx_int32_t skip_group_delay;
jm's avatar
jm committed
33 34 35 36
   SpeexCallback callback;

   sigpow = 0;
   errpow = 0;
37

38 39
   st = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_UWB));
   dec = speex_decoder_init(speex_lib_get_mode(SPEEX_MODEID_UWB));
jm's avatar
jm committed
40 41 42 43 44 45 46 47 48 49 50

   callback.callback_id = SPEEX_INBAND_CHAR;
   callback.func = speex_std_char_handler;
   callback.data = stderr;
   speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);

   callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
   callback.func = speex_std_mode_request_handler;
   callback.data = st;
   speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);

51
   tmp=0;
jm's avatar
jm committed
52
   speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
53 54
   tmp=0;
   speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
jm's avatar
jm committed
55
   tmp=7;
56
   speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
jm's avatar
jm committed
57
   tmp=1;
58
   speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
jm's avatar
jm committed
59

60 61 62 63
   speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &skip_group_delay);
   speex_decoder_ctl(dec, SPEEX_GET_LOOKAHEAD, &tmp);
   skip_group_delay += tmp;

jm's avatar
jm committed
64

65
   if (argc != 4 && argc != 3)
jm's avatar
jm committed
66
   {
67 68 69 70
      fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc);
      exit(1);
   }
   inFile = argv[1];
71
   fin = fopen(inFile, "rb");
72
   outFile = argv[2];
73
   fout = fopen(outFile, "wb+");
74 75 76
   if (argc==4)
   {
      bitsFile = argv[3];
77
      fbits = fopen(bitsFile, "wb");
78 79 80 81
   }
   speex_bits_init(&bits);
   while (!feof(fin))
   {
jm's avatar
jm committed
82
      fread(in_short, sizeof(short), FRAME_SIZE, fin);
83 84 85
      if (feof(fin))
         break;
      for (i=0;i<FRAME_SIZE;i++)
jm's avatar
jm committed
86
         in_float[i]=in_short[i];
87
      speex_bits_reset(&bits);
jm's avatar
jm committed
88

89
      speex_encode_int(st, in_short, &bits);
90 91
      nbBits = speex_bits_write(&bits, cbits, 200);
      bitCount+=bits.nbBits;
jm's avatar
jm committed
92

93 94 95 96
      if (argc==4)
         fwrite(cbits, 1, nbBits, fbits);
      speex_bits_rewind(&bits);

97
      speex_decode_int(dec, &bits, out_short);
98
      speex_bits_reset(&bits);
jm's avatar
jm committed
99 100 101

      fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
      skip_group_delay = 0;
102 103 104 105
   }
   fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
   speex_encoder_destroy(st);
   speex_decoder_destroy(dec);
jm's avatar
jm committed
106 107 108 109

   rewind(fin);
   rewind(fout);

Tristan Matthews's avatar
Tristan Matthews committed
110
   while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin)
jm's avatar
jm committed
111 112 113 114 115 116 117 118 119 120 121 122 123
           &&
           FRAME_SIZE ==  fread(out_short, sizeof(short), FRAME_SIZE,fout) )
   {
	float s=0, e=0;
        for (i=0;i<FRAME_SIZE;++i) {
            s += (float)in_short[i] * in_short[i];
            e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
        }
	seg_snr += 10*log10((s+1)/(e+1));
	sigpow += s;
	errpow += e;
	snr_frames++;
   }
124 125
   fclose(fin);
   fclose(fout);
jm's avatar
jm committed
126 127 128 129 130

   snr = 10 * log10( sigpow / errpow );
   seg_snr /= snr_frames;
   fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);

131
#ifdef FIXED_DEBUG
jm's avatar
jm committed
132 133
   printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
#endif
Tristan Matthews's avatar
Tristan Matthews committed
134

135 136
   return 1;
}