1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "speex.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include "speex_callbacks.h"
9
10 #ifdef FIXED_DEBUG
11 extern long long spx_mips;
12 #endif
13
14 #define FRAME_SIZE 320
15 #include <math.h>
main(int argc,char ** argv)16 int main(int argc, char **argv)
17 {
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;
24 int snr_frames = 0;
25 char cbits[200];
26 int nbBits;
27 int i;
28 void *st;
29 void *dec;
30 SpeexBits bits;
31 int tmp;
32 int bitCount=0;
33 int skip_group_delay;
34 SpeexCallback callback;
35
36 sigpow = 0;
37 errpow = 0;
38
39 st = speex_encoder_init(&speex_wb_mode);
40 dec = speex_decoder_init(&speex_wb_mode);
41
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);
46
47 callback.callback_id = SPEEX_INBAND_MODE_REQUEST;
48 callback.func = speex_std_mode_request_handler;
49 callback.data = st;
50 speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback);
51
52 tmp=0;
53 speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp);
54 tmp=0;
55 speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
56 tmp=8;
57 speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp);
58 tmp=2;
59 speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp);
60 tmp=3;
61 speex_encoder_ctl(st, SPEEX_SET_HIGH_MODE, &tmp);
62 tmp=6;
63 speex_encoder_ctl(st, SPEEX_SET_LOW_MODE, &tmp);
64
65
66 speex_mode_query(&speex_wb_mode, SPEEX_MODE_FRAME_SIZE, &tmp);
67 fprintf (stderr, "frame size: %d\n", tmp);
68 skip_group_delay = 223;
69
70 if (argc != 4 && argc != 3)
71 {
72 fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc);
73 exit(1);
74 }
75 inFile = argv[1];
76 fin = fopen(inFile, "r");
77 outFile = argv[2];
78 fout = fopen(outFile, "w+");
79 if (argc==4)
80 {
81 bitsFile = argv[3];
82 fbits = fopen(bitsFile, "w");
83 }
84 speex_bits_init(&bits);
85 while (!feof(fin))
86 {
87 fread(in_short, sizeof(short), FRAME_SIZE, fin);
88 if (feof(fin))
89 break;
90 for (i=0;i<FRAME_SIZE;i++)
91 in_float[i]=in_short[i];
92 speex_bits_reset(&bits);
93
94 speex_encode_int(st, in_short, &bits);
95 nbBits = speex_bits_write(&bits, cbits, 200);
96 bitCount+=bits.nbBits;
97
98 if (argc==4)
99 fwrite(cbits, 1, nbBits, fbits);
100 speex_bits_rewind(&bits);
101
102 speex_decode_int(dec, &bits, out_short);
103 speex_bits_reset(&bits);
104
105 fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout);
106 skip_group_delay = 0;
107 }
108 fprintf (stderr, "Total encoded size: %d bits\n", bitCount);
109 speex_encoder_destroy(st);
110 speex_decoder_destroy(dec);
111
112 rewind(fin);
113 rewind(fout);
114
115 while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin)
116 &&
117 FRAME_SIZE == fread(out_short, sizeof(short), FRAME_SIZE,fout) )
118 {
119 float s=0, e=0;
120 for (i=0;i<FRAME_SIZE;++i) {
121 s += (float)in_short[i] * in_short[i];
122 e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]);
123 }
124 seg_snr += 10*log10((s+160)/(e+160));
125 sigpow += s;
126 errpow += e;
127 snr_frames++;
128 }
129 fclose(fin);
130 fclose(fout);
131
132 snr = 10 * log10( sigpow / errpow );
133 seg_snr /= snr_frames;
134 fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr);
135
136 #ifdef FIXED_DEBUG
137 printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames));
138 #endif
139
140 return 1;
141 }
142