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$")
31 #include "asterisk/mod_format.h"
32 #include "asterisk/module.h"
33 #include "asterisk/endian.h"
35 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
37 /* Portions of the conversion code are by guido@sienanet.it */
39 #define WAV_BUF_SIZE 320
41 struct wav_desc { /* format-specific parameters */
51 #if __BYTE_ORDER == __LITTLE_ENDIAN
57 #if __BYTE_ORDER == __BIG_ENDIAN
59 (((((b) ) & 0xFF) << 24) | \
60 ((((b) >> 8) & 0xFF) << 16) | \
61 ((((b) >> 16) & 0xFF) << 8) | \
62 ((((b) >> 24) & 0xFF) ))
64 (((((b) ) & 0xFF) << 8) | \
65 ((((b) >> 8) & 0xFF) ))
66 #define ltohl(b) htoll(b)
67 #define ltohs(b) htols(b)
69 #error "Endianess not defined"
74 static int check_header_fmt(FILE *f, int hsize, int hz)
76 short format, chans, bysam, bisam;
80 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
83 if (fread(&format, 1, 2, f) != 2) {
84 ast_log(LOG_WARNING, "Read failed (format)\n");
87 if (ltohs(format) != 1) {
88 ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
91 if (fread(&chans, 1, 2, f) != 2) {
92 ast_log(LOG_WARNING, "Read failed (format)\n");
95 if (ltohs(chans) != 1) {
96 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
99 if (fread(&freq, 1, 4, f) != 4) {
100 ast_log(LOG_WARNING, "Read failed (freq)\n");
103 if (((ltohl(freq) != 8000) && (ltohl(freq) != 16000)) ||
104 ((ltohl(freq) == 8000) && (hz != 8000)) ||
105 ((ltohl(freq) == 16000) && (hz != 16000))) {
106 ast_log(LOG_WARNING, "Unexpected frequency mismatch %d (expecting %d)\n", ltohl(freq),hz);
109 /* Ignore the byte frequency */
110 if (fread(&bysec, 1, 4, f) != 4) {
111 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
114 /* Check bytes per sample */
115 if (fread(&bysam, 1, 2, f) != 2) {
116 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
119 if (ltohs(bysam) != 2) {
120 ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
123 if (fread(&bisam, 1, 2, f) != 2) {
124 ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
127 /* Skip any additional header */
128 if (fseek(f,ltohl(hsize)-16,SEEK_CUR) == -1 ) {
129 ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 );
135 static int check_header(FILE *f, int hz)
137 int type, size, formtype;
139 if (fread(&type, 1, 4, f) != 4) {
140 ast_log(LOG_WARNING, "Read failed (type)\n");
143 if (fread(&size, 1, 4, f) != 4) {
144 ast_log(LOG_WARNING, "Read failed (size)\n");
148 if (fread(&formtype, 1, 4, f) != 4) {
149 ast_log(LOG_WARNING, "Read failed (formtype)\n");
152 if (memcmp(&type, "RIFF", 4)) {
153 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
156 if (memcmp(&formtype, "WAVE", 4)) {
157 ast_log(LOG_WARNING, "Does not contain WAVE\n");
160 /* Skip any facts and get the first data block */
165 /* Begin data chunk */
166 if (fread(&buf, 1, 4, f) != 4) {
167 ast_log(LOG_WARNING, "Read failed (block header format)\n");
170 /* Data has the actual length of data in it */
171 if (fread(&data, 1, 4, f) != 4) {
172 ast_log(LOG_WARNING, "Read failed (block '%.4s' header length)\n", buf);
176 if (memcmp(&buf, "fmt ", 4) == 0) {
177 if (check_header_fmt(f, data, hz))
181 if(memcmp(buf, "data", 4) == 0 )
183 ast_log(LOG_DEBUG, "Skipping unknown block '%.4s'\n", buf);
184 if (fseek(f,data,SEEK_CUR) == -1 ) {
185 ast_log(LOG_WARNING, "Failed to skip '%.4s' block: %d\n", buf, data);
190 curpos = lseek(fd, 0, SEEK_CUR);
191 truelength = lseek(fd, 0, SEEK_END);
192 lseek(fd, curpos, SEEK_SET);
193 truelength -= curpos;
198 static int update_header(FILE *f)
201 int datalen,filelen,bytes;
204 fseek(f, 0, SEEK_END);
206 /* data starts 44 bytes in */
208 datalen = htoll(bytes);
209 /* chunk size is bytes of data plus 36 bytes of header */
210 filelen = htoll(36 + bytes);
213 ast_log(LOG_WARNING, "Unable to find our position\n");
216 if (fseek(f, 4, SEEK_SET)) {
217 ast_log(LOG_WARNING, "Unable to set our position\n");
220 if (fwrite(&filelen, 1, 4, f) != 4) {
221 ast_log(LOG_WARNING, "Unable to set write file size\n");
224 if (fseek(f, 40, SEEK_SET)) {
225 ast_log(LOG_WARNING, "Unable to set our position\n");
228 if (fwrite(&datalen, 1, 4, f) != 4) {
229 ast_log(LOG_WARNING, "Unable to set write datalen\n");
232 if (fseeko(f, cur, SEEK_SET)) {
233 ast_log(LOG_WARNING, "Unable to return to position\n");
239 static int write_header(FILE *f, int writehz)
243 unsigned int hs = htoll(16);
244 unsigned short fmt = htols(1);
245 unsigned short chans = htols(1);
246 unsigned short bysam = htols(2);
247 unsigned short bisam = htols(16);
248 unsigned int size = htoll(0);
250 if (writehz == 16000) {
257 /* Write a wav header, ignoring sizes which will be filled in later */
259 if (fwrite("RIFF", 1, 4, f) != 4) {
260 ast_log(LOG_WARNING, "Unable to write header\n");
263 if (fwrite(&size, 1, 4, f) != 4) {
264 ast_log(LOG_WARNING, "Unable to write header\n");
267 if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
268 ast_log(LOG_WARNING, "Unable to write header\n");
271 if (fwrite(&hs, 1, 4, f) != 4) {
272 ast_log(LOG_WARNING, "Unable to write header\n");
275 if (fwrite(&fmt, 1, 2, f) != 2) {
276 ast_log(LOG_WARNING, "Unable to write header\n");
279 if (fwrite(&chans, 1, 2, f) != 2) {
280 ast_log(LOG_WARNING, "Unable to write header\n");
283 if (fwrite(&hz, 1, 4, f) != 4) {
284 ast_log(LOG_WARNING, "Unable to write header\n");
287 if (fwrite(&bhz, 1, 4, f) != 4) {
288 ast_log(LOG_WARNING, "Unable to write header\n");
291 if (fwrite(&bysam, 1, 2, f) != 2) {
292 ast_log(LOG_WARNING, "Unable to write header\n");
295 if (fwrite(&bisam, 1, 2, f) != 2) {
296 ast_log(LOG_WARNING, "Unable to write header\n");
299 if (fwrite("data", 1, 4, f) != 4) {
300 ast_log(LOG_WARNING, "Unable to write header\n");
303 if (fwrite(&size, 1, 4, f) != 4) {
304 ast_log(LOG_WARNING, "Unable to write header\n");
310 static int wav_open(struct ast_filestream *s)
312 /* We don't have any header to read or anything really, but
313 if we did, it would go here. We also might want to check
314 and be sure it's a valid file. */
315 struct wav_desc *tmp = (struct wav_desc *)s->_private;
316 if ((tmp->maxlen = check_header(s->f, (s->fmt->format.id == AST_FORMAT_SLINEAR16 ? 16000 : 8000))) < 0)
321 static int wav_rewrite(struct ast_filestream *s, const char *comment)
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. */
327 struct wav_desc *tmp = (struct wav_desc *)s->_private;
328 tmp->hz = (s->fmt->format.id == AST_FORMAT_SLINEAR16 ? 16000 : 8000);
329 if (write_header(s->f,tmp->hz))
334 static void wav_close(struct ast_filestream *s)
337 struct wav_desc *fs = (struct wav_desc *)s->_private;
343 /* Pad to even length */
344 if (fs->bytes & 0x1) {
345 if (!fwrite(&zero, 1, 1, s->f)) {
346 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
351 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
354 int samples; /* actual samples read */
355 #if __BYTE_ORDER == __BIG_ENDIAN
361 /* Send a frame from the file to the appropriate channel */
362 struct wav_desc *fs = (struct wav_desc *)s->_private;
364 bytes = (fs->hz == 16000 ? (WAV_BUF_SIZE * 2) : WAV_BUF_SIZE);
367 if (fs->maxlen - here < bytes) /* truncate if necessary */
368 bytes = fs->maxlen - here;
371 /* ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
372 s->fr.frametype = AST_FRAME_VOICE;
373 ast_format_set(&s->fr.subclass.format, (fs->hz == 16000 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR), 0);
375 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
377 if ( (res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) <= 0 ) {
379 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
383 s->fr.samples = samples = res / 2;
385 tmp = (short *)(s->fr.data.ptr);
386 #if __BYTE_ORDER == __BIG_ENDIAN
387 /* file format is little endian so we need to swap */
388 for( x = 0; x < samples; x++)
389 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
396 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
398 #if __BYTE_ORDER == __BIG_ENDIAN
400 short tmp[16000], *tmpi;
402 struct wav_desc *s = (struct wav_desc *)fs->_private;
405 if (f->frametype != AST_FRAME_VOICE) {
406 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
409 if ((f->subclass.format.id != AST_FORMAT_SLINEAR) && (f->subclass.format.id != AST_FORMAT_SLINEAR16)) {
410 ast_log(LOG_WARNING, "Asked to write non-SLINEAR%s frame (%s)!\n", s->hz == 16000 ? "16" : "", ast_getformatname(&f->subclass.format));
413 if (ast_format_cmp(&f->subclass.format, &fs->fmt->format) == AST_FORMAT_CMP_NOT_EQUAL) {
414 ast_log(LOG_WARNING, "Can't change SLINEAR frequency during write\n");
420 #if __BYTE_ORDER == __BIG_ENDIAN
422 if (f->datalen > sizeof(tmp)) {
423 ast_log(LOG_WARNING, "Data length is too long\n");
427 for (x=0; x < f->datalen/2; x++)
428 tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
430 if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
433 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen ) {
435 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
439 s->bytes += f->datalen;
445 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
447 off_t min, max, cur, offset = 0, samples;
449 samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
450 min = 44; /* wav header is 44 bytes */
452 fseeko(fs->f, 0, SEEK_END);
454 if (whence == SEEK_SET)
455 offset = samples + min;
456 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
457 offset = samples + cur;
458 else if (whence == SEEK_END)
459 offset = max - samples;
460 if (whence != SEEK_FORCECUR) {
461 offset = (offset > max)?max:offset;
463 /* always protect the header space. */
464 offset = (offset < min)?min:offset;
465 return fseeko(fs->f, offset, SEEK_SET);
468 static int wav_trunc(struct ast_filestream *fs)
470 if (ftruncate(fileno(fs->f), ftello(fs->f)))
472 return update_header(fs->f);
475 static off_t wav_tell(struct ast_filestream *fs)
478 offset = ftello(fs->f);
479 /* subtract header size to get samples, then divide by 2 for 16 bit samples */
480 return (offset - 44)/2;
483 static struct ast_format_def wav16_f = {
487 .rewrite = wav_rewrite,
494 .buf_size = (WAV_BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
495 .desc_size = sizeof(struct wav_desc),
498 static struct ast_format_def wav_f = {
502 .rewrite = wav_rewrite,
509 .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
510 .desc_size = sizeof(struct wav_desc),
513 static int load_module(void)
515 ast_format_set(&wav_f.format, AST_FORMAT_SLINEAR, 0);
516 ast_format_set(&wav16_f.format, AST_FORMAT_SLINEAR16, 0);
517 if (ast_format_def_register(&wav_f)
518 || ast_format_def_register(&wav16_f))
519 return AST_MODULE_LOAD_FAILURE;
520 return AST_MODULE_LOAD_SUCCESS;
523 static int unload_module(void)
525 return ast_format_def_unregister(wav_f.name)
526 || ast_format_def_unregister(wav16_f.name);
529 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV/WAV16 format (8kHz/16kHz Signed Linear)",
531 .unload = unload_module,
532 .load_pri = AST_MODPRI_APP_DEPEND