5 #include <speex/speex.h>
8 #include <speex/speex_callbacks.h>
11 extern long long spx_mips;
14 #define FRAME_SIZE 640
16 int main(int argc, char **argv)
18 char *inFile, *outFile, *bitsFile;
19 FILE *fin, *fout, *fbits=NULL;
20 short in_short[FRAME_SIZE];
21 short out_short[FRAME_SIZE];
22 float in_float[FRAME_SIZE];
23 float sigpow,errpow,snr, seg_snr=0;
33 spx_int32_t skip_group_delay;
34 SpeexCallback callback;
39 st = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_UWB));
40 dec = speex_decoder_init(speex_lib_get_mode(SPEEX_MODEID_UWB));
42 callback.callback_id = SPEEX_INBAND_CHAR;
43 callback.func = speex_std_char_handler;
44 callback.data = stderr;
45 speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
47 callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
48 callback.func = speex_std_mode_request_handler;
50 speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
53 speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
55 speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
57 speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
59 speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
61 speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &skip_group_delay);
62 speex_decoder_ctl(dec, SPEEX_GET_LOOKAHEAD, &tmp);
63 skip_group_delay += tmp;
66 if (argc != 4 && argc != 3)
68 fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc);
72 fin = fopen(inFile, "rb");
74 fout = fopen(outFile, "wb+");
78 fbits = fopen(bitsFile, "wb");
80 speex_bits_init(&bits);
83 fread(in_short, sizeof(short), FRAME_SIZE, fin);
86 for (i=0;i<FRAME_SIZE;i++)
87 in_float[i]=in_short[i];
88 speex_bits_reset(&bits);
90 speex_encode_int(st, in_short, &bits);
91 nbBits = speex_bits_write(&bits, cbits, 200);
92 bitCount+=bits.nbBits;
95 fwrite(cbits, 1, nbBits, fbits);
96 speex_bits_rewind(&bits);
98 speex_decode_int(dec, &bits, out_short);
99 speex_bits_reset(&bits);
101 fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
102 skip_group_delay = 0;
104 fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
105 speex_encoder_destroy(st);
106 speex_decoder_destroy(dec);
111 while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin)
113 FRAME_SIZE == fread(out_short, sizeof(short), FRAME_SIZE,fout) )
116 for (i=0;i<FRAME_SIZE;++i) {
117 s += (float)in_short[i] * in_short[i];
118 e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
120 seg_snr += 10*log10((s+1)/(e+1));
128 snr = 10 * log10( sigpow / errpow );
129 seg_snr /= snr_frames;
130 fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
133 printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));