2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Work with WAV in the proprietary Microsoft format.
22 * Microsoft WAV format (8000hz Signed Linear)
23 * \arg File name extension: wav (lower case)
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
40 #include "asterisk/lock.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/sched.h"
45 #include "asterisk/module.h"
46 #include "asterisk/endian.h"
48 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
50 /* Portions of the conversion code are by guido@sienanet.it */
52 #define WAV_BUF_SIZE 320
54 struct wav_desc { /* format-specific parameters */
64 #define GAIN 2 /* 2^GAIN is the multiple to increase the volume by */
66 #if __BYTE_ORDER == __LITTLE_ENDIAN
72 #if __BYTE_ORDER == __BIG_ENDIAN
74 (((((b) ) & 0xFF) << 24) | \
75 ((((b) >> 8) & 0xFF) << 16) | \
76 ((((b) >> 16) & 0xFF) << 8) | \
77 ((((b) >> 24) & 0xFF) ))
79 (((((b) ) & 0xFF) << 8) | \
80 ((((b) >> 8) & 0xFF) ))
81 #define ltohl(b) htoll(b)
82 #define ltohs(b) htols(b)
84 #error "Endianess not defined"
89 static int check_header(FILE *f)
91 int type, size, formtype;
93 short format, chans, bysam, bisam;
97 if (fread(&type, 1, 4, f) != 4) {
98 ast_log(LOG_WARNING, "Read failed (type)\n");
101 if (fread(&size, 1, 4, f) != 4) {
102 ast_log(LOG_WARNING, "Read failed (size)\n");
106 if (fread(&formtype, 1, 4, f) != 4) {
107 ast_log(LOG_WARNING, "Read failed (formtype)\n");
110 if (memcmp(&type, "RIFF", 4)) {
111 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
114 if (memcmp(&formtype, "WAVE", 4)) {
115 ast_log(LOG_WARNING, "Does not contain WAVE\n");
118 if (fread(&fmt, 1, 4, f) != 4) {
119 ast_log(LOG_WARNING, "Read failed (fmt)\n");
122 if (memcmp(&fmt, "fmt ", 4)) {
123 ast_log(LOG_WARNING, "Does not say fmt\n");
126 if (fread(&hsize, 1, 4, f) != 4) {
127 ast_log(LOG_WARNING, "Read failed (formtype)\n");
130 if (ltohl(hsize) < 16) {
131 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
134 if (fread(&format, 1, 2, f) != 2) {
135 ast_log(LOG_WARNING, "Read failed (format)\n");
138 if (ltohs(format) != 1) {
139 ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
142 if (fread(&chans, 1, 2, f) != 2) {
143 ast_log(LOG_WARNING, "Read failed (format)\n");
146 if (ltohs(chans) != 1) {
147 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
150 if (fread(&freq, 1, 4, f) != 4) {
151 ast_log(LOG_WARNING, "Read failed (freq)\n");
154 if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
155 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
158 /* Ignore the byte frequency */
159 if (fread(&bysec, 1, 4, f) != 4) {
160 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
163 /* Check bytes per sample */
164 if (fread(&bysam, 1, 2, f) != 2) {
165 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
168 if (ltohs(bysam) != 2) {
169 ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
172 if (fread(&bisam, 1, 2, f) != 2) {
173 ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
176 /* Skip any additional header */
177 if (fseek(f,ltohl(hsize)-16,SEEK_CUR) == -1 ) {
178 ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 );
181 /* Skip any facts and get the first data block */
186 /* Begin data chunk */
187 if (fread(&buf, 1, 4, f) != 4) {
188 ast_log(LOG_WARNING, "Read failed (data)\n");
191 /* Data has the actual length of data in it */
192 if (fread(&data, 1, 4, f) != 4) {
193 ast_log(LOG_WARNING, "Read failed (data)\n");
197 if(memcmp(buf, "data", 4) == 0 )
199 if(memcmp(buf, "fact", 4) != 0 ) {
200 ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
203 if (fseek(f,data,SEEK_CUR) == -1 ) {
204 ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
209 curpos = lseek(fd, 0, SEEK_CUR);
210 truelength = lseek(fd, 0, SEEK_END);
211 lseek(fd, curpos, SEEK_SET);
212 truelength -= curpos;
217 static int update_header(FILE *f)
220 int datalen,filelen,bytes;
223 fseek(f, 0, SEEK_END);
225 /* data starts 44 bytes in */
227 datalen = htoll(bytes);
228 /* chunk size is bytes of data plus 36 bytes of header */
229 filelen = htoll(36 + bytes);
232 ast_log(LOG_WARNING, "Unable to find our position\n");
235 if (fseek(f, 4, SEEK_SET)) {
236 ast_log(LOG_WARNING, "Unable to set our position\n");
239 if (fwrite(&filelen, 1, 4, f) != 4) {
240 ast_log(LOG_WARNING, "Unable to set write file size\n");
243 if (fseek(f, 40, SEEK_SET)) {
244 ast_log(LOG_WARNING, "Unable to set our position\n");
247 if (fwrite(&datalen, 1, 4, f) != 4) {
248 ast_log(LOG_WARNING, "Unable to set write datalen\n");
251 if (fseeko(f, cur, SEEK_SET)) {
252 ast_log(LOG_WARNING, "Unable to return to position\n");
258 static int write_header(FILE *f)
260 unsigned int hz=htoll(8000);
261 unsigned int bhz = htoll(16000);
262 unsigned int hs = htoll(16);
263 unsigned short fmt = htols(1);
264 unsigned short chans = htols(1);
265 unsigned short bysam = htols(2);
266 unsigned short bisam = htols(16);
267 unsigned int size = htoll(0);
268 /* Write a wav header, ignoring sizes which will be filled in later */
270 if (fwrite("RIFF", 1, 4, f) != 4) {
271 ast_log(LOG_WARNING, "Unable to write header\n");
274 if (fwrite(&size, 1, 4, f) != 4) {
275 ast_log(LOG_WARNING, "Unable to write header\n");
278 if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
279 ast_log(LOG_WARNING, "Unable to write header\n");
282 if (fwrite(&hs, 1, 4, f) != 4) {
283 ast_log(LOG_WARNING, "Unable to write header\n");
286 if (fwrite(&fmt, 1, 2, f) != 2) {
287 ast_log(LOG_WARNING, "Unable to write header\n");
290 if (fwrite(&chans, 1, 2, f) != 2) {
291 ast_log(LOG_WARNING, "Unable to write header\n");
294 if (fwrite(&hz, 1, 4, f) != 4) {
295 ast_log(LOG_WARNING, "Unable to write header\n");
298 if (fwrite(&bhz, 1, 4, f) != 4) {
299 ast_log(LOG_WARNING, "Unable to write header\n");
302 if (fwrite(&bysam, 1, 2, f) != 2) {
303 ast_log(LOG_WARNING, "Unable to write header\n");
306 if (fwrite(&bisam, 1, 2, f) != 2) {
307 ast_log(LOG_WARNING, "Unable to write header\n");
310 if (fwrite("data", 1, 4, f) != 4) {
311 ast_log(LOG_WARNING, "Unable to write header\n");
314 if (fwrite(&size, 1, 4, f) != 4) {
315 ast_log(LOG_WARNING, "Unable to write header\n");
321 static int wav_open(struct ast_filestream *s)
323 /* We don't have any header to read or anything really, but
324 if we did, it would go here. We also might want to check
325 and be sure it's a valid file. */
326 struct wav_desc *tmp = (struct wav_desc *)s->private;
327 if ((tmp->maxlen = check_header(s->f)) < 0)
332 static int wav_rewrite(struct ast_filestream *s, const char *comment)
334 /* We don't have any header to read or anything really, but
335 if we did, it would go here. We also might want to check
336 and be sure it's a valid file. */
338 if (write_header(s->f))
343 static void wav_close(struct ast_filestream *s)
346 struct wav_desc *fs = (struct wav_desc *)s->private;
347 /* Pad to even length */
349 fwrite(&zero, 1, 1, s->f);
352 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
355 int samples; /* actual samples read */
358 int bytes = WAV_BUF_SIZE; /* in bytes */
360 /* Send a frame from the file to the appropriate channel */
361 struct wav_desc *fs = (struct wav_desc *)s->private;
364 if (fs->maxlen - here < bytes) /* truncate if necessary */
365 bytes = fs->maxlen - here;
368 /* ast_log(LOG_DEBUG, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
369 s->fr.frametype = AST_FRAME_VOICE;
370 s->fr.subclass = AST_FORMAT_SLINEAR;
372 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
374 if ( (res = fread(s->fr.data, 1, s->fr.datalen, s->f)) <= 0 ) {
376 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
380 s->fr.samples = samples = res / 2;
382 tmp = (short *)(s->fr.data);
383 #if __BYTE_ORDER == __BIG_ENDIAN
384 /* file format is little endian so we need to swap */
385 for( x = 0; x < samples; x++)
386 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
390 for (x=0; x < samples; x++) {
391 if (tmp[x] & ((1 << GAIN) - 1)) {
392 /* If it has data down low, then it's not something we've artificially increased gain
393 on, so we don't need to gain adjust it */
399 for (x=0; x < samples; x++)
400 tmp[x] = tmp[x] >> GAIN;
408 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
411 short tmp[8000], *tmpi;
413 struct wav_desc *s = (struct wav_desc *)fs->private;
416 if (f->frametype != AST_FRAME_VOICE) {
417 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
420 if (f->subclass != AST_FORMAT_SLINEAR) {
421 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
424 if (f->datalen > sizeof(tmp)) {
425 ast_log(LOG_WARNING, "Data length is too long\n");
432 printf("Data Length: %d\n", f->datalen);
436 /* Volume adjust here to accomodate */
437 for (x=0;x<f->datalen/2;x++) {
438 tmpf = ((float)tmpi[x]) * ((float)(1 << GAIN));
444 tmp[x] &= ~((1 << GAIN) - 1);
446 #if __BYTE_ORDER == __BIG_ENDIAN
447 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
451 if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
452 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
456 s->bytes += f->datalen;
457 update_header(fs->f);
463 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
465 off_t min, max, cur, offset = 0, samples;
467 samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
468 min = 44; /* wav header is 44 bytes */
470 fseeko(fs->f, 0, SEEK_END);
472 if (whence == SEEK_SET)
473 offset = samples + min;
474 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
475 offset = samples + cur;
476 else if (whence == SEEK_END)
477 offset = max - samples;
478 if (whence != SEEK_FORCECUR) {
479 offset = (offset > max)?max:offset;
481 /* always protect the header space. */
482 offset = (offset < min)?min:offset;
483 return fseeko(fs->f, offset, SEEK_SET);
486 static int wav_trunc(struct ast_filestream *fs)
488 if (ftruncate(fileno(fs->f), ftello(fs->f)))
490 return update_header(fs->f);
493 static off_t wav_tell(struct ast_filestream *fs)
496 offset = ftello(fs->f);
497 /* subtract header size to get samples, then divide by 2 for 16 bit samples */
498 return (offset - 44)/2;
501 static const struct ast_format wav_f = {
504 .format = AST_FORMAT_SLINEAR,
506 .rewrite = wav_rewrite,
513 .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
514 .desc_size = sizeof(struct wav_desc),
515 .module = &mod_data, /* XXX */
518 static int load_module(void *mod)
520 return ast_format_register(&wav_f);
523 static int unload_module(void *mod)
525 return ast_format_unregister(wav_f.name);
528 static const char *description(void)
530 return "Microsoft WAV format (8000hz Signed Linear)";
533 static const char *key(void)
535 return ASTERISK_GPL_KEY;