Fix red. Why did it go unnoticed through the build system?
[maemo-rb.git] / tools / rbspeex / rbspeex.c
blobb72ff381d950fca9d7308208a708304d10000db5
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 <speex/speex.h>
22 #include <speex/speex_resampler.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdbool.h>
28 #include "rbspeex.h"
30 /* Read an unaligned 32-bit little endian long from buffer. */
31 unsigned int get_long_le(unsigned char *p)
33 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
36 void put_ushort_le(unsigned short x, unsigned char *out)
38 out[0] = x & 0xff;
39 out[1] = x >> 8;
42 void put_uint_le(unsigned int x, unsigned char *out)
44 out[0] = x & 0xff;
45 out[1] = (x >> 8) & 0xff;
46 out[2] = (x >> 16) & 0xff;
47 out[3] = x >> 24;
52 bool get_wave_metadata(FILE *fd, int *numchan, int *bps, int *sr, int *numsamples)
54 unsigned char buf[1024];
55 unsigned long totalsamples = 0;
56 unsigned long channels = 0;
57 unsigned long bitspersample = 0;
58 unsigned long numbytes = 0;
59 size_t read_bytes;
60 int i;
62 if ((read_bytes = fread(buf, 1, 12, fd)) < 12)
63 return false;
65 if ((memcmp(buf, "RIFF",4) != 0) || (memcmp(&buf[8], "WAVE", 4) != 0))
66 return false;
68 /* iterate over WAVE chunks until 'data' chunk */
69 while (1) {
70 /* get chunk header */
71 if ((read_bytes = fread(buf, 1, 8, fd)) < 8)
72 return false;
74 /* chunkSize */
75 i = get_long_le(&buf[4]);
77 if (memcmp(buf, "fmt ", 4) == 0) {
78 /* get rest of chunk */
79 if ((read_bytes = fread(buf, 1, 16, fd)) < 16)
80 return false;
82 i -= 16;
84 channels = *numchan = buf[2] | (buf[3] << 8);
85 *sr = get_long_le(&buf[4]);
86 /* wBitsPerSample */
87 bitspersample = *bps = buf[14] | (buf[15] << 8);
88 } else if (memcmp(buf, "data", 4) == 0) {
89 numbytes = i;
90 break;
91 } else if (memcmp(buf, "fact", 4) == 0) {
92 /* dwSampleLength */
93 if (i >= 4) {
94 /* get rest of chunk */
95 if ((read_bytes = fread(buf, 1, 4, fd)) < 4)
96 return false;
98 i -= 4;
99 totalsamples = get_long_le(buf);
103 /* seek to next chunk (even chunk sizes must be padded) */
104 if (i & 0x01)
105 i++;
107 if (fseek(fd, i, SEEK_CUR) < 0)
108 return false;
111 if ((numbytes == 0) || (channels == 0))
112 return false;
114 if (totalsamples == 0) {
115 /* for PCM only */
116 totalsamples = numbytes/((((bitspersample - 1) / 8) + 1)*channels);
118 *numsamples = totalsamples;
119 return true;
122 /* We'll eat an entire WAV file here, and encode it with Speex, packing the
123 * bits as tightly as we can. Output is completely raw, with absolutely
124 * nothing to identify the contents. Files are left open, so remember to close
125 * them.
127 bool encode_file(FILE *fin, FILE *fout, float quality, int complexity,
128 bool narrowband, float volume, char *errstr, size_t errlen)
130 spx_int16_t *in = NULL, *inpos;
131 spx_int16_t enc_buf[640]; /* Max frame size */
132 char cbits[200];
133 void *st = NULL;
134 SpeexResamplerState *resampler = NULL;
135 SpeexBits bits;
136 int i, tmp, target_sr, numchan, bps, sr, numsamples, frame_size, lookahead;
137 int nbytes;
138 bool ret = true;
139 int a;
141 if (!get_wave_metadata(fin, &numchan, &bps, &sr, &numsamples)) {
142 snprintf(errstr, errlen, "invalid WAV file");
143 return false;
145 if (numchan != 1) {
146 snprintf(errstr, errlen, "input file must be mono");
147 return false;
149 if (bps != 16) {
150 snprintf(errstr, errlen, "samples must be 16 bit");
151 return false;
154 /* Allocate an encoder of specified type, defaults to wideband */
155 st = speex_encoder_init(narrowband ? &speex_nb_mode : &speex_wb_mode);
156 if (narrowband)
157 target_sr = 8000;
158 else
159 target_sr = 16000;
160 speex_bits_init(&bits);
162 /* VBR */
163 tmp = 1;
164 speex_encoder_ctl(st, SPEEX_SET_VBR, &tmp);
165 /* Quality, 0-10 */
166 speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &quality);
167 /* Complexity, 0-10 */
168 speex_encoder_ctl(st, SPEEX_SET_COMPLEXITY, &complexity);
169 speex_encoder_ctl(st, SPEEX_GET_FRAME_SIZE, &frame_size);
170 speex_encoder_ctl(st, SPEEX_GET_LOOKAHEAD, &lookahead);
172 /* Read input samples into a buffer */
173 in = calloc(numsamples + lookahead, sizeof(spx_int16_t));
174 if (in == NULL) {
175 snprintf(errstr, errlen, "could not allocate clip memory");
176 ret = false;
177 goto finish;
179 if (fread(in, 2, numsamples, fin) != numsamples) {
180 snprintf(errstr, errlen, "could not read input file data");
181 ret = false;
182 goto finish;
184 #if defined(__BIG_ENDIAN__)
185 /* byteswap read bytes to host endianess. */
186 a = numsamples;
187 while(a--) {
188 *(in + a) = ((unsigned short)(*(in + a)) >> 8) & 0x00ff
189 | ((unsigned short)(*(in + a)) << 8) & 0xff00;
191 #endif
193 if (volume != 1.0f) {
194 for (i = 0; i < numsamples; ++i)
195 in[i] *= volume;
198 if (sr != target_sr) {
199 resampler = speex_resampler_init(1, sr, target_sr, 10, NULL);
200 speex_resampler_skip_zeros(resampler);
203 /* There will be 'lookahead' samples of zero at the end of the array, to
204 * make sure the Speex encoder is allowed to spit out all its data at clip
205 * end */
206 numsamples += lookahead;
208 inpos = in;
209 while (numsamples > 0) {
210 int samples = frame_size;
212 /* Check if we need to resample */
213 if (sr != target_sr) {
214 spx_uint32_t in_len = numsamples, out_len = frame_size;
215 double resample_factor = (double)sr/(double)target_sr;
216 /* Calculate how many input samples are needed for one full frame
217 * out, and add some, just in case. */
218 spx_uint32_t samples_in = frame_size*resample_factor + 50;
220 /* Limit this or resampler will try to allocate it all on stack */
221 if (in_len > samples_in)
222 in_len = samples_in;
223 speex_resampler_process_int(resampler, 0, inpos, &in_len,
224 enc_buf, &out_len);
225 inpos += in_len;
226 samples = out_len;
227 numsamples -= in_len;
228 } else {
229 if (samples > numsamples)
230 samples = numsamples;
231 memcpy(enc_buf, inpos, samples*2);
232 inpos += frame_size;
233 numsamples -= frame_size;
235 /* Pad out with zeros if we didn't fill all input */
236 memset(enc_buf + samples, 0, (frame_size - samples)*2);
238 if (speex_encode_int(st, enc_buf, &bits) < 0) {
239 snprintf(errstr, errlen, "encoder error");
240 ret = false;
241 goto finish;
244 /* Copy the bits to an array of char that can be written */
245 nbytes = speex_bits_write_whole_bytes(&bits, cbits, 200);
247 /* Write the compressed data */
248 if (fwrite(cbits, 1, nbytes, fout) != nbytes) {
249 snprintf(errstr, errlen, "could not write output data");
250 ret = false;
251 goto finish;
254 /* Squeeze out the last bits */
255 nbytes = speex_bits_write(&bits, cbits, 200);
256 if (fwrite(cbits, 1, nbytes, fout) != nbytes) {
257 snprintf(errstr, errlen, "could not write output data");
258 ret = false;
261 finish:
262 if (st != NULL)
263 speex_encoder_destroy(st);
264 speex_bits_destroy(&bits);
265 if (resampler != NULL)
266 speex_resampler_destroy(resampler);
267 if (in != NULL)
268 free(in);
269 return ret;