1 /**************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 Thom Johansen
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ***************************************************************************/
21 #include <speex/speex.h>
28 "Usage: rbspeexdec infile outfile\n"\
29 "rbspeexdec outputs mono 16 bit 16 kHz WAV files.\n"\
30 "WARNING: This tool will only decode files made with rbspeexenc!\n"
33 int main(int argc
, char **argv
)
37 short out
[640]; /* max frame size (UWB) */
38 unsigned char wavhdr
[44];
40 void *st
; /* decoder state */
42 int i
, tmp
, lookahead
, frame_size
;
43 unsigned int samples
= 0;
51 /* Rockbox speex streams are always assumed to be WB */
52 st
= speex_decoder_init(&speex_wb_mode
);
54 /* Set the perceptual enhancement on (is default, but doesn't hurt) */
56 speex_decoder_ctl(st
, SPEEX_SET_ENH
, &tmp
);
57 speex_decoder_ctl(st
, SPEEX_GET_LOOKAHEAD
, &lookahead
);
58 speex_decoder_ctl(st
, SPEEX_GET_FRAME_SIZE
, &frame_size
);
60 if ((fin
= fopen(argv
[1], "rb")) == NULL
) {
61 printf("Error: could not open input file\n");
64 if ((fout
= fopen(argv
[2], "wb")) == NULL
) {
65 printf("Error: could not open output file\n");
69 fseek(fin
, 0, SEEK_END
);
71 fseek(fin
, 0, SEEK_SET
);
72 indata
= malloc(insize
);
73 fread(indata
, 1, insize
, fin
);
76 /* fill in wav header */
77 strcpy(wavhdr
, "RIFF");
78 strcpy(wavhdr
+ 8, "WAVEfmt ");
79 put_uint_le(16, wavhdr
+ 16);
80 put_ushort_le(1, wavhdr
+ 20); /* PCM data */
81 put_ushort_le(1, wavhdr
+ 22); /* mono */
82 put_uint_le(16000, wavhdr
+ 24); /* 16000 Hz */
83 put_uint_le(16000*2, wavhdr
+ 28); /* chan*sr*bbs/8 */
84 put_ushort_le(2, wavhdr
+ 32); /* chan*bps/8 */
85 put_ushort_le(16, wavhdr
+ 34); /* bits per sample */
86 strcpy(wavhdr
+ 36, "data");
87 fwrite(wavhdr
, 1, 44, fout
); /* write header */
88 /* make bit buffer use our own buffer */
89 speex_bits_set_bit_buffer(&bits
, indata
, insize
);
90 while (speex_decode_int(st
, &bits
, out
) == 0) {
91 /* if no error, write decoded audio */
92 fwrite(out
+ lookahead
, sizeof(short), frame_size
- lookahead
, fout
);
93 samples
+= frame_size
- lookahead
;
94 lookahead
= 0; /* only skip samples at the start */
96 speex_decoder_destroy(st
);
97 /* now fill in the values in the wav header we didn't have at the start */
98 fseek(fout
, 4, SEEK_SET
);
99 put_uint_le(36 + samples
*2, wavhdr
); /* header size + data size */
100 fwrite(wavhdr
, 1, 4, fout
);
101 fseek(fout
, 40, SEEK_SET
);
102 put_uint_le(samples
*2, wavhdr
); /* data size */
103 fwrite(wavhdr
, 1, 4, fout
);