From df0df311dc2545d8332b088ad281b1dfeaf0f3c8 Mon Sep 17 00:00:00 2001 From: Thom Johansen Date: Wed, 28 Nov 2007 13:42:44 +0000 Subject: Add rbspeexdec, decoder for the Rockbox voice clips. Also nitpick a bit on rbspeexenc while I'm at it. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@15842 a1c6a512-1295-4272-9138-f99709370657 --- tools/rbspeex/Makefile | 8 ++- tools/rbspeex/rbspeexdec.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ tools/rbspeex/rbspeexenc.c | 11 ++++- 3 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 tools/rbspeex/rbspeexdec.c 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 # This sets up 'SRC' based on the files mentioned in SOURCES SRC := $(shell cat $(SPEEXSRC)/SOURCES | $(CC) $(CFLAGS) -E -P - | grep -v "^\#") -SOURCES = $(SRC:%.c=$(SPEEXSRC)/%.c) rbspeexenc.c +SOURCES = $(SRC:%.c=$(SPEEXSRC)/%.c) rbspeexenc.c rbspeexdec.c OBJS := $(SRC:%.c=%.o) DEPFILE = dep-speex DIRS = .PHONY : all -all: ../rbspeexenc +all: ../rbspeexenc ../rbspeexdec $(DEPFILE): $(SOURCES) $(SILENT)rm -f $(DEPFILE) @@ -57,6 +57,10 @@ libspeex.a: $(OBJS) $(DEPFILE) @echo Linking ../rbspeexenc $(SILENT)$(CC) $(CFLAGS) -o ../rbspeexenc rbspeexenc.o libspeex.a -lm +../rbspeexdec: $(OBJS) libspeex.a rbspeexdec.o + @echo Linking ../rbspeexdec + $(SILENT)$(CC) $(CFLAGS) -o ../rbspeexdec rbspeexdec.o libspeex.a -lm + %.o: @echo CC $< $(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 @@ + /************************************************************************** + * __________ __ ___. + * 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 +#include +#include +#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; +} + 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 @@ " -c x Complexity, increases quality for a given bitrate, but encodes\n"\ " slower, range [0-10], default 3\n"\ " -n Enable narrowband mode, will resample input to 8 kHz\n\n"\ -" -v x Volume, amplitude multiplier, default 1.0.\n"\ +" -v x Volume, amplitude multiplier, default 1.0\n"\ "rbspeexenc expects a mono 16 bit WAV file as input. Files will be resampled\n"\ "to either 16 kHz by default, or 8 kHz if narrowband mode is enabled.\n"\ "WARNING: This tool will create files that are only usable by Rockbox!\n" @@ -146,6 +146,10 @@ int main(int argc, char **argv) volume = atof(argv[++i]); else if (strncmp(argv[i], "-n", 2) == 0) narrowband = true; + else { + printf("Error: unrecognized option '%s'\n", argv[i]); + return 1; + } ++i; } @@ -210,7 +214,10 @@ int main(int argc, char **argv) speex_bits_init(&bits); inpos = in; - fout = fopen(argv[argc - 1], "wb"); + if ((fout = fopen(argv[argc - 1], "wb")) == NULL) { + printf("Error: could not open output file\n"); + return 1; + } while (numsamples > 0) { int samples = frame_size; -- cgit v1.2.3