Revert "skin tags: fix the id3 track/disc numbers in conditionals"
[maemo-rb.git] / tools / rbspeex / rbspeexdec.c
blob14ee97169777cf0bc4c8f5b98edc0f3b082c0fcf
1 /**************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
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>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "string.h"
25 #include "rbspeex.h"
27 #define USAGE_TEXT \
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)
35 FILE *fin, *fout;
36 char *indata;
37 short out[640]; /* max frame size (UWB) */
38 unsigned char wavhdr[44];
39 int numbytes;
40 void *st; /* decoder state */
41 SpeexBits bits;
42 int i, tmp, lookahead, frame_size;
43 unsigned int samples = 0;
44 long insize;
46 if (argc < 3) {
47 printf(USAGE_TEXT);
48 return 1;
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) */
55 tmp = 1;
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");
62 return 1;
64 if ((fout = fopen(argv[2], "wb")) == NULL) {
65 printf("Error: could not open output file\n");
66 return 1;
68 /* slurp infile */
69 fseek(fin, 0, SEEK_END);
70 insize = ftell(fin);
71 fseek(fin, 0, SEEK_SET);
72 indata = malloc(insize);
73 fread(indata, 1, insize, fin);
74 fclose(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 #if defined(__BIG_ENDIAN__)
93 /* byteswap samples from host (big) endianess to file (little) before
94 * writing. */
95 unsigned int a = frame_size - lookahead;
96 while(a--) {
97 out[lookahead + a] = ((unsigned short)out[lookahead+a]<<8)&0xff00
98 | ((unsigned short)out[lookahead+a]>>8)&0x00ff;
100 #endif
101 fwrite(out + lookahead, sizeof(short), frame_size - lookahead, fout);
102 samples += frame_size - lookahead;
103 lookahead = 0; /* only skip samples at the start */
105 speex_decoder_destroy(st);
106 /* now fill in the values in the wav header we didn't have at the start */
107 fseek(fout, 4, SEEK_SET);
108 put_uint_le(36 + samples*2, wavhdr); /* header size + data size */
109 fwrite(wavhdr, 1, 4, fout);
110 fseek(fout, 40, SEEK_SET);
111 put_uint_le(samples*2, wavhdr); /* data size */
112 fwrite(wavhdr, 1, 4, fout);
113 fclose(fout);
114 free(indata);
115 return 0;