1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Thom Johansen 2007-11-28 13:42:44 +00:00
parent c1b718403a
commit df0df311dc
3 changed files with 133 additions and 4 deletions

View file

@ -26,14 +26,14 @@ endif
# This sets up 'SRC' based on the files mentioned in SOURCES # This sets up 'SRC' based on the files mentioned in SOURCES
SRC := $(shell cat $(SPEEXSRC)/SOURCES | $(CC) $(CFLAGS) -E -P - | grep -v "^\#") 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) OBJS := $(SRC:%.c=%.o)
DEPFILE = dep-speex DEPFILE = dep-speex
DIRS = DIRS =
.PHONY : all .PHONY : all
all: ../rbspeexenc all: ../rbspeexenc ../rbspeexdec
$(DEPFILE): $(SOURCES) $(DEPFILE): $(SOURCES)
$(SILENT)rm -f $(DEPFILE) $(SILENT)rm -f $(DEPFILE)
@ -57,6 +57,10 @@ libspeex.a: $(OBJS) $(DEPFILE)
@echo Linking ../rbspeexenc @echo Linking ../rbspeexenc
$(SILENT)$(CC) $(CFLAGS) -o ../rbspeexenc rbspeexenc.o libspeex.a -lm $(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: %.o:
@echo CC $< @echo CC $<
$(SILENT)$(CC) $(CFLAGS) -c $< -o $@ $(SILENT)$(CC) $(CFLAGS) -c $< -o $@

118
tools/rbspeex/rbspeexdec.c Normal file
View file

@ -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 <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;
}

View file

@ -30,7 +30,7 @@
" -c x Complexity, increases quality for a given bitrate, but encodes\n"\ " -c x Complexity, increases quality for a given bitrate, but encodes\n"\
" slower, range [0-10], default 3\n"\ " slower, range [0-10], default 3\n"\
" -n Enable narrowband mode, will resample input to 8 kHz\n\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"\ "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"\ "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" "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]); volume = atof(argv[++i]);
else if (strncmp(argv[i], "-n", 2) == 0) else if (strncmp(argv[i], "-n", 2) == 0)
narrowband = true; narrowband = true;
else {
printf("Error: unrecognized option '%s'\n", argv[i]);
return 1;
}
++i; ++i;
} }
@ -210,7 +214,10 @@ int main(int argc, char **argv)
speex_bits_init(&bits); speex_bits_init(&bits);
inpos = in; 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) { while (numsamples > 0) {
int samples = frame_size; int samples = frame_size;