diff options
Diffstat (limited to 'tools/rbspeex')
-rw-r--r-- | tools/rbspeex/Makefile | 8 | ||||
-rw-r--r-- | tools/rbspeex/rbspeexdec.c | 118 | ||||
-rw-r--r-- | tools/rbspeex/rbspeexenc.c | 11 |
3 files changed, 133 insertions, 4 deletions
diff --git a/tools/rbspeex/Makefile b/tools/rbspeex/Makefile index 2e8a692c3d..f6e70def96 100644 --- a/tools/rbspeex/Makefile +++ b/tools/rbspeex/Makefile | |||
@@ -26,14 +26,14 @@ endif | |||
26 | # This sets up 'SRC' based on the files mentioned in SOURCES | 26 | # This sets up 'SRC' based on the files mentioned in SOURCES |
27 | SRC := $(shell cat $(SPEEXSRC)/SOURCES | $(CC) $(CFLAGS) -E -P - | grep -v "^\#") | 27 | SRC := $(shell cat $(SPEEXSRC)/SOURCES | $(CC) $(CFLAGS) -E -P - | grep -v "^\#") |
28 | 28 | ||
29 | SOURCES = $(SRC:%.c=$(SPEEXSRC)/%.c) rbspeexenc.c | 29 | SOURCES = $(SRC:%.c=$(SPEEXSRC)/%.c) rbspeexenc.c rbspeexdec.c |
30 | OBJS := $(SRC:%.c=%.o) | 30 | OBJS := $(SRC:%.c=%.o) |
31 | DEPFILE = dep-speex | 31 | DEPFILE = dep-speex |
32 | DIRS = | 32 | DIRS = |
33 | 33 | ||
34 | .PHONY : all | 34 | .PHONY : all |
35 | 35 | ||
36 | all: ../rbspeexenc | 36 | all: ../rbspeexenc ../rbspeexdec |
37 | 37 | ||
38 | $(DEPFILE): $(SOURCES) | 38 | $(DEPFILE): $(SOURCES) |
39 | $(SILENT)rm -f $(DEPFILE) | 39 | $(SILENT)rm -f $(DEPFILE) |
@@ -57,6 +57,10 @@ libspeex.a: $(OBJS) $(DEPFILE) | |||
57 | @echo Linking ../rbspeexenc | 57 | @echo Linking ../rbspeexenc |
58 | $(SILENT)$(CC) $(CFLAGS) -o ../rbspeexenc rbspeexenc.o libspeex.a -lm | 58 | $(SILENT)$(CC) $(CFLAGS) -o ../rbspeexenc rbspeexenc.o libspeex.a -lm |
59 | 59 | ||
60 | ../rbspeexdec: $(OBJS) libspeex.a rbspeexdec.o | ||
61 | @echo Linking ../rbspeexdec | ||
62 | $(SILENT)$(CC) $(CFLAGS) -o ../rbspeexdec rbspeexdec.o libspeex.a -lm | ||
63 | |||
60 | %.o: | 64 | %.o: |
61 | @echo CC $< | 65 | @echo CC $< |
62 | $(SILENT)$(CC) $(CFLAGS) -c $< -o $@ | 66 | $(SILENT)$(CC) $(CFLAGS) -c $< -o $@ |
diff --git a/tools/rbspeex/rbspeexdec.c b/tools/rbspeex/rbspeexdec.c new file mode 100644 index 0000000000..90562f7309 --- /dev/null +++ b/tools/rbspeex/rbspeexdec.c | |||
@@ -0,0 +1,118 @@ | |||
1 | /************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2007 Thom Johansen | ||
10 | * | ||
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. | ||
13 | * | ||
14 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
15 | * KIND, either express or implied. | ||
16 | * | ||
17 | ***************************************************************************/ | ||
18 | |||
19 | #include <speex/speex.h> | ||
20 | #include <stdio.h> | ||
21 | #include <stdlib.h> | ||
22 | #include "string.h" | ||
23 | |||
24 | #define USAGE_TEXT \ | ||
25 | "Usage: rbspeexdec infile outfile\n"\ | ||
26 | "rbspeexdec outputs mono 16 bit 16 kHz WAV files.\n"\ | ||
27 | "WARNING: This tool will only decode files made with rbspeexenc!\n" | ||
28 | |||
29 | void put_ushort_le(unsigned short x, unsigned char *out) | ||
30 | { | ||
31 | out[0] = x & 0xff; | ||
32 | out[1] = x >> 8; | ||
33 | } | ||
34 | |||
35 | void put_uint_le(unsigned int x, unsigned char *out) | ||
36 | { | ||
37 | out[0] = x & 0xff; | ||
38 | out[1] = (x >> 8) & 0xff; | ||
39 | out[2] = (x >> 16) & 0xff; | ||
40 | out[3] = x >> 24; | ||
41 | } | ||
42 | |||
43 | int main(int argc, char **argv) | ||
44 | { | ||
45 | FILE *fin, *fout; | ||
46 | char *indata; | ||
47 | short out[640]; /* max frame size (UWB) */ | ||
48 | unsigned char wavhdr[44]; | ||
49 | int numbytes; | ||
50 | void *st; /* decoder state */ | ||
51 | SpeexBits bits; | ||
52 | int i, tmp, lookahead, frame_size; | ||
53 | unsigned int samples = 0; | ||
54 | long insize; | ||
55 | |||
56 | if (argc < 3) { | ||
57 | printf(USAGE_TEXT); | ||
58 | return 1; | ||
59 | } | ||
60 | |||
61 | /* Rockbox speex streams are always assumed to be WB */ | ||
62 | st = speex_decoder_init(&speex_wb_mode); | ||
63 | |||
64 | /* Set the perceptual enhancement on (is default, but doesn't hurt) */ | ||
65 | tmp = 1; | ||
66 | speex_decoder_ctl(st, SPEEX_SET_ENH, &tmp); | ||
67 | speex_decoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead); | ||
68 | speex_decoder_ctl(st, SPEEX_GET_FRAME_SIZE, &frame_size); | ||
69 | |||
70 | if ((fin = fopen(argv[1], "rb")) == NULL) { | ||
71 | printf("Error: could not open input file\n"); | ||
72 | return 1; | ||
73 | } | ||
74 | if ((fout = fopen(argv[2], "wb")) == NULL) { | ||
75 | printf("Error: could not open output file\n"); | ||
76 | return 1; | ||
77 | } | ||
78 | /* slurp infile */ | ||
79 | fseek(fin, 0, SEEK_END); | ||
80 | insize = ftell(fin); | ||
81 | fseek(fin, 0, SEEK_SET); | ||
82 | indata = malloc(insize); | ||
83 | fread(indata, 1, insize, fin); | ||
84 | fclose(fin); | ||
85 | |||
86 | /* fill in wav header */ | ||
87 | strcpy(wavhdr, "RIFF"); | ||
88 | strcpy(wavhdr + 8, "WAVEfmt "); | ||
89 | put_uint_le(16, wavhdr + 16); | ||
90 | put_ushort_le(1, wavhdr + 20); /* PCM data */ | ||
91 | put_ushort_le(1, wavhdr + 22); /* mono */ | ||
92 | put_uint_le(16000, wavhdr + 24); /* 16000 Hz */ | ||
93 | put_uint_le(16000*2, wavhdr + 28); /* chan*sr*bbs/8 */ | ||
94 | put_ushort_le(2, wavhdr + 32); /* chan*bps/8 */ | ||
95 | put_ushort_le(16, wavhdr + 34); /* bits per sample */ | ||
96 | strcpy(wavhdr + 36, "data"); | ||
97 | fwrite(wavhdr, 1, 44, fout); /* write header */ | ||
98 | /* make bit buffer use our own buffer */ | ||
99 | speex_bits_set_bit_buffer(&bits, indata, insize); | ||
100 | while (speex_decode_int(st, &bits, out) == 0) { | ||
101 | /* if no error, write decoded audio */ | ||
102 | fwrite(out + lookahead, sizeof(short), frame_size - lookahead, fout); | ||
103 | samples += frame_size - lookahead; | ||
104 | lookahead = 0; /* only skip samples at the start */ | ||
105 | } | ||
106 | speex_decoder_destroy(st); | ||
107 | /* now fill in the values in the wav header we didn't have at the start */ | ||
108 | fseek(fout, 4, SEEK_SET); | ||
109 | put_uint_le(36 + samples*2, wavhdr); /* header size + data size */ | ||
110 | fwrite(wavhdr, 1, 4, fout); | ||
111 | fseek(fout, 40, SEEK_SET); | ||
112 | put_uint_le(samples*2, wavhdr); /* data size */ | ||
113 | fwrite(wavhdr, 1, 4, fout); | ||
114 | fclose(fout); | ||
115 | free(indata); | ||
116 | return 0; | ||
117 | } | ||
118 | |||
diff --git a/tools/rbspeex/rbspeexenc.c b/tools/rbspeex/rbspeexenc.c index 7869602f44..d3c3f7712c 100644 --- a/tools/rbspeex/rbspeexenc.c +++ b/tools/rbspeex/rbspeexenc.c | |||
@@ -30,7 +30,7 @@ | |||
30 | " -c x Complexity, increases quality for a given bitrate, but encodes\n"\ | 30 | " -c x Complexity, increases quality for a given bitrate, but encodes\n"\ |
31 | " slower, range [0-10], default 3\n"\ | 31 | " slower, range [0-10], default 3\n"\ |
32 | " -n Enable narrowband mode, will resample input to 8 kHz\n\n"\ | 32 | " -n Enable narrowband mode, will resample input to 8 kHz\n\n"\ |
33 | " -v x Volume, amplitude multiplier, default 1.0.\n"\ | 33 | " -v x Volume, amplitude multiplier, default 1.0\n"\ |
34 | "rbspeexenc expects a mono 16 bit WAV file as input. Files will be resampled\n"\ | 34 | "rbspeexenc expects a mono 16 bit WAV file as input. Files will be resampled\n"\ |
35 | "to either 16 kHz by default, or 8 kHz if narrowband mode is enabled.\n"\ | 35 | "to either 16 kHz by default, or 8 kHz if narrowband mode is enabled.\n"\ |
36 | "WARNING: This tool will create files that are only usable by Rockbox!\n" | 36 | "WARNING: This tool will create files that are only usable by Rockbox!\n" |
@@ -146,6 +146,10 @@ int main(int argc, char **argv) | |||
146 | volume = atof(argv[++i]); | 146 | volume = atof(argv[++i]); |
147 | else if (strncmp(argv[i], "-n", 2) == 0) | 147 | else if (strncmp(argv[i], "-n", 2) == 0) |
148 | narrowband = true; | 148 | narrowband = true; |
149 | else { | ||
150 | printf("Error: unrecognized option '%s'\n", argv[i]); | ||
151 | return 1; | ||
152 | } | ||
149 | ++i; | 153 | ++i; |
150 | } | 154 | } |
151 | 155 | ||
@@ -210,7 +214,10 @@ int main(int argc, char **argv) | |||
210 | 214 | ||
211 | speex_bits_init(&bits); | 215 | speex_bits_init(&bits); |
212 | inpos = in; | 216 | inpos = in; |
213 | fout = fopen(argv[argc - 1], "wb"); | 217 | if ((fout = fopen(argv[argc - 1], "wb")) == NULL) { |
218 | printf("Error: could not open output file\n"); | ||
219 | return 1; | ||
220 | } | ||
214 | 221 | ||
215 | while (numsamples > 0) { | 222 | while (numsamples > 0) { |
216 | int samples = frame_size; | 223 | int samples = frame_size; |