Prepare new maemo release
[maemo-rb.git] / tools / rbspeex / rbspeexenc.c
blobfae4ba1080794a43188bd8a33b9a3d4c4a3979bd
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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdbool.h>
26 #include "rbspeex.h"
28 #define USAGE_TEXT \
29 "Usage: rbspeexenc [options] infile outfile\n"\
30 "Options:\n"\
31 " -q x Quality, floating point number in the range [0-10], default 8.0\n"\
32 " -c x Complexity, increases quality for a given bitrate, but encodes\n"\
33 " slower, range [0-10], default 3\n"\
34 " -n Enable narrowband mode, will resample input to 8 kHz\n"\
35 " -v x Volume, amplitude multiplier, default 1.0\n\n"\
36 "rbspeexenc expects a mono 16 bit WAV file as input. Files will be resampled\n"\
37 "to either 16 kHz by default, or 8 kHz if narrowband mode is enabled.\n"\
38 "WARNING: This tool will create files that are only usable by Rockbox!\n"
41 int main(int argc, char **argv)
43 char *inname, *outname;
44 FILE *fin, *fout;
45 int i;
46 int complexity = 3;
47 float quality = 8.f, volume = 1.0f;
48 bool narrowband = false;
49 bool ret;
50 char errstr[512];
52 if (argc < 3) {
53 printf(USAGE_TEXT);
54 return 1;
57 i = 1;
58 while (i < argc - 2) {
59 if (strncmp(argv[i], "-q", 2) == 0)
60 quality = atof(argv[++i]);
61 else if (strncmp(argv[i], "-c", 2) == 0)
62 complexity = atoi(argv[++i]);
63 else if (strncmp(argv[i], "-v", 2) == 0)
64 volume = atof(argv[++i]);
65 else if (strncmp(argv[i], "-n", 2) == 0)
66 narrowband = true;
67 else {
68 printf("Error: unrecognized option '%s'\n", argv[i]);
69 return 1;
71 ++i;
73 inname = argv[argc - 2];
74 outname = argv[argc - 1];
76 if ((fin = fopen(inname, "rb")) == NULL) {
77 printf("Error: could not open input file\n");
78 return 1;
80 if ((fout = fopen(outname, "wb")) == NULL) {
81 printf("Error: could not open output file\n");
82 return 1;
85 ret = encode_file(fin, fout, quality, complexity, narrowband, volume,
86 errstr, sizeof(errstr));
87 fclose(fin);
88 fclose(fout);
89 if (!ret) {
90 /* Attempt to delete unfinished output */
91 printf("Error: %s\n", errstr);
92 remove(outname);
93 return 1;
95 return 0;