summaryrefslogtreecommitdiffstats
path: root/tools/rbspeex/rbspeexdec.c
blob: 90562f7309c4941eaae7373debeef223903b51f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 /**************************************************************************
  *             __________               __   ___.
  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
  *                     \/            \/     \/    \/            \/
  *
  * Copyright (C) 2007 Thom Johansen
  *
  * All files in this archive are subject to the GNU General Public License.
  * See the file COPYING in the source tree root for full license agreement.
  *
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  * KIND, either express or implied.
  *
  ***************************************************************************/
 
#include <speex/speex.h>
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
  
 #define USAGE_TEXT \
"Usage: rbspeexdec infile outfile\n"\
"rbspeexdec outputs mono 16 bit 16 kHz WAV files.\n"\
"WARNING: This tool will only decode files made with rbspeexenc!\n"

void put_ushort_le(unsigned short x, unsigned char *out)
{
    out[0] = x & 0xff;
    out[1] = x >> 8;
}

void put_uint_le(unsigned int x, unsigned char *out)
{
    out[0] = x & 0xff;
    out[1] = (x >> 8) & 0xff;
    out[2] = (x >> 16) & 0xff;
    out[3] = x >> 24;
}

int main(int argc, char **argv)
{
    FILE *fin, *fout;
    char *indata;
    short out[640]; /* max frame size (UWB) */
    unsigned char wavhdr[44];
    int numbytes;
    void *st; /* decoder state */
    SpeexBits bits;
    int i, tmp, lookahead, frame_size;
    unsigned int samples = 0;
    long insize;

    if (argc < 3) {
        printf(USAGE_TEXT);
        return 1;
    }

    /* Rockbox speex streams are always assumed to be WB */
    st = speex_decoder_init(&speex_wb_mode);
 
    /* Set the perceptual enhancement on (is default, but doesn't hurt) */
    tmp = 1;
    speex_decoder_ctl(st, SPEEX_SET_ENH, &tmp);
    speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
    speex_decoder_ctl(st, SPEEX_GET_FRAME_SIZE, &frame_size);
 
    if ((fin = fopen(argv[1], "rb")) == NULL) {
        printf("Error: could not open input file\n");
        return 1;
    }
    if ((fout = fopen(argv[2], "wb")) == NULL) {
        printf("Error: could not open output file\n");
        return 1;
    }
    /* slurp infile */
    fseek(fin, 0, SEEK_END);
    insize = ftell(fin);
    fseek(fin, 0, SEEK_SET);
    indata = malloc(insize);
    fread(indata, 1, insize, fin);
    fclose(fin);

    /* fill in wav header */
    strcpy(wavhdr, "RIFF");
    strcpy(wavhdr + 8, "WAVEfmt ");
    put_uint_le(16, wavhdr + 16);
    put_ushort_le(1, wavhdr + 20);      /* PCM data */
    put_ushort_le(1, wavhdr + 22);      /* mono */
    put_uint_le(16000, wavhdr + 24);    /* 16000 Hz */
    put_uint_le(16000*2, wavhdr + 28);  /* chan*sr*bbs/8 */
    put_ushort_le(2, wavhdr + 32);      /* chan*bps/8 */
    put_ushort_le(16, wavhdr + 34);     /* bits per sample */
    strcpy(wavhdr + 36, "data");
    fwrite(wavhdr, 1, 44, fout);        /* write header */
    /* make bit buffer use our own buffer */
    speex_bits_set_bit_buffer(&bits, indata, insize);
    while (speex_decode_int(st, &bits, out) == 0) {
        /* if no error, write decoded audio */
        fwrite(out + lookahead, sizeof(short), frame_size - lookahead, fout);
        samples += frame_size - lookahead;
        lookahead = 0; /* only skip samples at the start */
    }
    speex_decoder_destroy(st);
    /* now fill in the values in the wav header we didn't have at the start */
    fseek(fout, 4, SEEK_SET);
    put_uint_le(36 + samples*2, wavhdr); /* header size + data size */
    fwrite(wavhdr, 1, 4, fout);
    fseek(fout, 40, SEEK_SET);
    put_uint_le(samples*2, wavhdr);      /* data size */
    fwrite(wavhdr, 1, 4, fout);
    fclose(fout);
    free(indata);
    return 0;
}