| 1 |
#ifdef HAVE_CONFIG_H |
| 2 |
#include "config.h" |
| 3 |
#endif |
| 4 |
|
| 5 |
#include "speex/speex_callbacks.h" |
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
|
| 9 |
#ifdef FIXED_DEBUG |
| 10 |
extern long long spx_mips; |
| 11 |
#endif |
| 12 |
|
| 13 |
#define FRAME_SIZE 160 |
| 14 |
#include <math.h> |
| 15 |
int main(int argc, char **argv) |
| 16 |
{ |
| 17 |
char *inFile, *outFile, *bitsFile; |
| 18 |
FILE *fin, *fout, *fbits=NULL; |
| 19 |
short in_short[FRAME_SIZE]; |
| 20 |
short out_short[FRAME_SIZE]; |
| 21 |
int snr_frames = 0; |
| 22 |
char cbits[200]; |
| 23 |
int nbBits; |
| 24 |
int i; |
| 25 |
void *st; |
| 26 |
void *dec; |
| 27 |
SpeexBits bits; |
| 28 |
spx_int32_t tmp; |
| 29 |
int bitCount=0; |
| 30 |
spx_int32_t skip_group_delay; |
| 31 |
SpeexCallback callback; |
| 32 |
|
| 33 |
st = speex_encoder_init(speex_lib_get_mode(SPEEX_MODEID_NB)); |
| 34 |
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 |
callback.callback_id = SPEEX_INBAND_CHAR; |
| 38 |
callback.func = speex_std_char_handler; |
| 39 |
callback.data = stderr; |
| 40 |
speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback); |
| 41 |
|
| 42 |
callback.callback_id = SPEEX_INBAND_MODE_REQUEST; |
| 43 |
callback.func = speex_std_mode_request_handler; |
| 44 |
callback.data = st; |
| 45 |
speex_decoder_ctl(dec, SPEEX_SET_HANDLER, &callback); |
| 46 |
/* END of unnecessary stuff */ |
| 47 |
|
| 48 |
tmp=1; |
| 49 |
speex_decoder_ctl(dec, SPEEX_SET_ENH, &tmp); |
| 50 |
tmp=0; |
| 51 |
speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp); |
| 52 |
tmp=8; |
| 53 |
speex_encoder_ctl(st, SPEEX_SET_QUALITY, &tmp); |
| 54 |
tmp=1; |
| 55 |
speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &tmp); |
| 56 |
|
| 57 |
/* Turn this off if you want to measure SNR (on by default) */ |
| 58 |
tmp=1; |
| 59 |
speex_encoder_ctl(st, SPEEX_SET_HIGHPASS, &tmp); |
| 60 |
speex_decoder_ctl(dec, SPEEX_SET_HIGHPASS, &tmp); |
| 61 |
|
| 62 |
speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &skip_group_delay); |
| 63 |
speex_decoder_ctl(dec, SPEEX_GET_LOOKAHEAD, &tmp); |
| 64 |
skip_group_delay += tmp; |
| 65 |
|
| 66 |
if (argc != 4 && argc != 3) |
| 67 |
{ |
| 68 |
fprintf (stderr, "Usage: encode [in file] [out file] [bits file]\nargc = %d", argc); |
| 69 |
exit(1); |
| 70 |
} |
| 71 |
inFile = argv[1]; |
| 72 |
fin = fopen(inFile, "rb"); |
| 73 |
outFile = argv[2]; |
| 74 |
fout = fopen(outFile, "wb+"); |
| 75 |
if (argc==4) |
| 76 |
{ |
| 77 |
bitsFile = argv[3]; |
| 78 |
fbits = fopen(bitsFile, "wb"); |
| 79 |
} |
| 80 |
speex_bits_init(&bits); |
| 81 |
while (!feof(fin)) |
| 82 |
{ |
| 83 |
fread(in_short, sizeof(short), FRAME_SIZE, fin); |
| 84 |
if (feof(fin)) |
| 85 |
break; |
| 86 |
speex_bits_reset(&bits); |
| 87 |
|
| 88 |
speex_encode_int(st, in_short, &bits); |
| 89 |
nbBits = speex_bits_write(&bits, cbits, 200); |
| 90 |
bitCount+=bits.nbBits; |
| 91 |
|
| 92 |
if (argc==4) |
| 93 |
fwrite(cbits, 1, nbBits, fbits); |
| 94 |
speex_bits_rewind(&bits); |
| 95 |
|
| 96 |
speex_decode_int(dec, &bits, out_short); |
| 97 |
speex_bits_reset(&bits); |
| 98 |
|
| 99 |
fwrite(&out_short[skip_group_delay], sizeof(short), FRAME_SIZE-skip_group_delay, fout); |
| 100 |
skip_group_delay = 0; |
| 101 |
} |
| 102 |
fprintf (stderr, "Total encoded size: %d bits\n", bitCount); |
| 103 |
speex_encoder_destroy(st); |
| 104 |
speex_decoder_destroy(dec); |
| 105 |
speex_bits_destroy(&bits); |
| 106 |
|
| 107 |
#ifndef DISABLE_FLOAT_API |
| 108 |
{ |
| 109 |
float sigpow,errpow,snr, seg_snr=0; |
| 110 |
sigpow = 0; |
| 111 |
errpow = 0; |
| 112 |
|
| 113 |
/* This code just computes SNR, so you don't need it either */ |
| 114 |
rewind(fin); |
| 115 |
rewind(fout); |
| 116 |
|
| 117 |
while ( FRAME_SIZE == fread(in_short, sizeof(short), FRAME_SIZE, fin) |
| 118 |
&& |
| 119 |
FRAME_SIZE == fread(out_short, sizeof(short), FRAME_SIZE,fout) ) |
| 120 |
{ |
| 121 |
float s=0, e=0; |
| 122 |
for (i=0;i<FRAME_SIZE;++i) { |
| 123 |
s += (float)in_short[i] * in_short[i]; |
| 124 |
e += ((float)in_short[i]-out_short[i]) * ((float)in_short[i]-out_short[i]); |
| 125 |
} |
| 126 |
seg_snr += 10*log10((s+160)/(e+160)); |
| 127 |
sigpow += s; |
| 128 |
errpow += e; |
| 129 |
snr_frames++; |
| 130 |
} |
| 131 |
snr = 10 * log10( sigpow / errpow ); |
| 132 |
seg_snr /= snr_frames; |
| 133 |
fprintf(stderr,"SNR = %f\nsegmental SNR = %f\n",snr, seg_snr); |
| 134 |
|
| 135 |
#ifdef FIXED_DEBUG |
| 136 |
printf ("Total: %f MIPS\n", (float)(1e-6*50*spx_mips/snr_frames)); |
| 137 |
#endif |
| 138 |
} |
| 139 |
#endif |
| 140 |
|
| 141 |
fclose(fin); |
| 142 |
fclose(fout); |
| 143 |
|
| 144 |
return 0; |
| 145 |
} |