1 /**************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 Thom Johansen
11 * All files in this archive are subject to the GNU General Public License.
12 * See the file COPYING in the source tree root for full license agreement.
14 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 * KIND, either express or implied.
17 ***************************************************************************/
19 #include <speex/speex.h>
26 "Usage: rbspeexdec infile outfile\n"\
27 "rbspeexdec outputs mono 16 bit 16 kHz WAV files.\n"\
28 "WARNING: This tool will only decode files made with rbspeexenc!\n"
31 int main(int argc
, char **argv
)
35 short out
[640]; /* max frame size (UWB) */
36 unsigned char wavhdr
[44];
38 void *st
; /* decoder state */
40 int i
, tmp
, lookahead
, frame_size
;
41 unsigned int samples
= 0;
49 /* Rockbox speex streams are always assumed to be WB */
50 st
= speex_decoder_init(&speex_wb_mode
);
52 /* Set the perceptual enhancement on (is default, but doesn't hurt) */
54 speex_decoder_ctl(st
, SPEEX_SET_ENH
, &tmp
);
55 speex_decoder_ctl(st
, SPEEX_GET_LOOKAHEAD
, &lookahead
);
56 speex_decoder_ctl(st
, SPEEX_GET_FRAME_SIZE
, &frame_size
);
58 if ((fin
= fopen(argv
[1], "rb")) == NULL
) {
59 printf("Error: could not open input file\n");
62 if ((fout
= fopen(argv
[2], "wb")) == NULL
) {
63 printf("Error: could not open output file\n");
67 fseek(fin
, 0, SEEK_END
);
69 fseek(fin
, 0, SEEK_SET
);
70 indata
= malloc(insize
);
71 fread(indata
, 1, insize
, fin
);
74 /* fill in wav header */
75 strcpy(wavhdr
, "RIFF");
76 strcpy(wavhdr
+ 8, "WAVEfmt ");
77 put_uint_le(16, wavhdr
+ 16);
78 put_ushort_le(1, wavhdr
+ 20); /* PCM data */
79 put_ushort_le(1, wavhdr
+ 22); /* mono */
80 put_uint_le(16000, wavhdr
+ 24); /* 16000 Hz */
81 put_uint_le(16000*2, wavhdr
+ 28); /* chan*sr*bbs/8 */
82 put_ushort_le(2, wavhdr
+ 32); /* chan*bps/8 */
83 put_ushort_le(16, wavhdr
+ 34); /* bits per sample */
84 strcpy(wavhdr
+ 36, "data");
85 fwrite(wavhdr
, 1, 44, fout
); /* write header */
86 /* make bit buffer use our own buffer */
87 speex_bits_set_bit_buffer(&bits
, indata
, insize
);
88 while (speex_decode_int(st
, &bits
, out
) == 0) {
89 /* if no error, write decoded audio */
90 fwrite(out
+ lookahead
, sizeof(short), frame_size
- lookahead
, fout
);
91 samples
+= frame_size
- lookahead
;
92 lookahead
= 0; /* only skip samples at the start */
94 speex_decoder_destroy(st
);
95 /* now fill in the values in the wav header we didn't have at the start */
96 fseek(fout
, 4, SEEK_SET
);
97 put_uint_le(36 + samples
*2, wavhdr
); /* header size + data size */
98 fwrite(wavhdr
, 1, 4, fout
);
99 fseek(fout
, 40, SEEK_SET
);
100 put_uint_le(samples
*2, wavhdr
); /* data size */
101 fwrite(wavhdr
, 1, 4, fout
);