testenc.c 3.74 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 8
#include <stdio.h>
#include <stdlib.h>

9
#ifdef FIXED_DEBUG
jm's avatar
jm committed
10 11 12
extern long long spx_mips;
#endif

13
#define FRAME_SIZE 160
14
#include <math.h>
15
int main(int argc, char **argv)
16
{
17
   char *inFile, *outFile, *bitsFile;
jmvalin's avatar
jmvalin committed
18
   FILE *fin, *fout, *fbits=NULL;
jm's avatar
jm committed
19 20 21
   short in_short[FRAME_SIZE];
   short out_short[FRAME_SIZE];
   int snr_frames = 0;
22 23
   char cbits[200];
   int nbBits;
24
   int i;
25 26
   void *st;
   void *dec;
27
   SpeexBits bits;
28
   spx_int32_t tmp;
jmvalin's avatar
jmvalin committed
29
   int bitCount=0;
jm's avatar
jm committed
30
   spx_int32_t skip_group_delay;
31
   SpeexCallback callback;
32

33 34
   st = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));
   dec = speex_decoder_init(speex_lib_get_mode(SPEEX_MODEID_NB));
35

36
   /* BEGIN: You probably don't need the following in a real application */
37 38 39 40 41 42 43 44 45
   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);
46
   /* END of unnecessary stuff */
47

jm's avatar
jm committed
48
   tmp=1;
49
   speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
jmvalin's avatar
jmvalin committed
50 51
   tmp=0;
   speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
52
   tmp=8;
jmvalin's avatar
jmvalin committed
53
   speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
54
   tmp=1;
jmvalin's avatar
jmvalin committed
55
   speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
56

57 58 59 60 61
   /* Turn this off if you want to measure SNR (on by default) */
   tmp=1;
   speex_encoder_ctl(st, SPEEX_SET_HIGHPASS, &tmp);
   speex_decoder_ctl(dec, SPEEX_SET_HIGHPASS, &tmp);

62 63 64
   speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &skip_group_delay);
   speex_decoder_ctl(dec, SPEEX_GET_LOOKAHEAD, &tmp);
   skip_group_delay += tmp;
jmvalin's avatar
...  
jmvalin committed
65

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

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

92 93
      if (argc==4)
         fwrite(cbits, 1, nbBits, fbits);
94
      speex_bits_rewind(&bits);
jm's avatar
jm committed
95

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

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

107 108 109 110 111 112
#ifndef DISABLE_FLOAT_API
   {
   float sigpow,errpow,snr, seg_snr=0;
   sigpow = 0;
   errpow = 0;

113
   /* This code just computes SNR, so you don't need it either */
jm's avatar
jm committed
114 115 116
   rewind(fin);
   rewind(fout);

Tristan Matthews's avatar
Tristan Matthews committed
117
   while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin)
jm's avatar
jm committed
118 119 120 121 122
           &&
           FRAME_SIZE ==  fread(out_short, sizeof(short), FRAME_SIZE,fout) )
   {
	float s=0, e=0;
        for (i=0;i<FRAME_SIZE;++i) {
123 124
            s += (float)in_short[i] * in_short[i];
            e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
jm's avatar
jm committed
125
        }
126
	seg_snr += 10*log10((s+160)/(e+160));
jm's avatar
jm committed
127 128 129 130 131 132 133
	sigpow += s;
	errpow += e;
	snr_frames++;
   }
   snr = 10 * log10( sigpow / errpow );
   seg_snr /= snr_frames;
   fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
jm's avatar
jm committed
134

135
#ifdef FIXED_DEBUG
jm's avatar
jm committed
136 137
   printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
#endif
138 139 140 141 142 143
   }
#endif

   fclose(fin);
   fclose(fout);

144
   return 0;
145
}