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 <netinet/in.h>
32 #include <arpa/inet.h>
36 #include "asterisk/lock.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/sched.h"
41 #include "asterisk/module.h"
42 #include "asterisk/endian.h"
44 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
46 /* Portions of the conversion code are by guido@sienanet.it */
48 #define WAV_BUF_SIZE 320
50 struct wav_desc { /* format-specific parameters */
59 #if __BYTE_ORDER == __LITTLE_ENDIAN
65 #if __BYTE_ORDER == __BIG_ENDIAN
67 (((((b) ) & 0xFF) << 24) | \
68 ((((b) >> 8) & 0xFF) << 16) | \
69 ((((b) >> 16) & 0xFF) << 8) | \
70 ((((b) >> 24) & 0xFF) ))
72 (((((b) ) & 0xFF) << 8) | \
73 ((((b) >> 8) & 0xFF) ))
74 #define ltohl(b) htoll(b)
75 #define ltohs(b) htols(b)
77 #error "Endianess not defined"
82 static int check_header(FILE *f)
84 int type, size, formtype;
86 short format, chans, bysam, bisam;
90 if (fread(&type, 1, 4, f) != 4) {
91 ast_log(LOG_WARNING, "Read failed (type)\n");
94 if (fread(&size, 1, 4, f) != 4) {
95 ast_log(LOG_WARNING, "Read failed (size)\n");
99 if (fread(&formtype, 1, 4, f) != 4) {
100 ast_log(LOG_WARNING, "Read failed (formtype)\n");
103 if (memcmp(&type, "RIFF", 4)) {
104 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
107 if (memcmp(&formtype, "WAVE", 4)) {
108 ast_log(LOG_WARNING, "Does not contain WAVE\n");
111 if (fread(&fmt, 1, 4, f) != 4) {
112 ast_log(LOG_WARNING, "Read failed (fmt)\n");
115 if (memcmp(&fmt, "fmt ", 4)) {
116 ast_log(LOG_WARNING, "Does not say fmt\n");
119 if (fread(&hsize, 1, 4, f) != 4) {
120 ast_log(LOG_WARNING, "Read failed (formtype)\n");
123 if (ltohl(hsize) < 16) {
124 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
127 if (fread(&format, 1, 2, f) != 2) {
128 ast_log(LOG_WARNING, "Read failed (format)\n");
131 if (ltohs(format) != 1) {
132 ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
135 if (fread(&chans, 1, 2, f) != 2) {
136 ast_log(LOG_WARNING, "Read failed (format)\n");
139 if (ltohs(chans) != 1) {
140 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
143 if (fread(&freq, 1, 4, f) != 4) {
144 ast_log(LOG_WARNING, "Read failed (freq)\n");
147 if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
148 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
151 /* Ignore the byte frequency */
152 if (fread(&bysec, 1, 4, f) != 4) {
153 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
156 /* Check bytes per sample */
157 if (fread(&bysam, 1, 2, f) != 2) {
158 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
161 if (ltohs(bysam) != 2) {
162 ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
165 if (fread(&bisam, 1, 2, f) != 2) {
166 ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
169 /* Skip any additional header */
170 if (fseek(f,ltohl(hsize)-16,SEEK_CUR) == -1 ) {
171 ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 );
174 /* Skip any facts and get the first data block */
179 /* Begin data chunk */
180 if (fread(&buf, 1, 4, f) != 4) {
181 ast_log(LOG_WARNING, "Read failed (data)\n");
184 /* Data has the actual length of data in it */
185 if (fread(&data, 1, 4, f) != 4) {
186 ast_log(LOG_WARNING, "Read failed (data)\n");
190 if(memcmp(buf, "data", 4) == 0 )
192 if(memcmp(buf, "fact", 4) != 0 ) {
193 ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
196 if (fseek(f,data,SEEK_CUR) == -1 ) {
197 ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
202 curpos = lseek(fd, 0, SEEK_CUR);
203 truelength = lseek(fd, 0, SEEK_END);
204 lseek(fd, curpos, SEEK_SET);
205 truelength -= curpos;
210 static int update_header(FILE *f)
213 int datalen,filelen,bytes;
216 fseek(f, 0, SEEK_END);
218 /* data starts 44 bytes in */
220 datalen = htoll(bytes);
221 /* chunk size is bytes of data plus 36 bytes of header */
222 filelen = htoll(36 + bytes);
225 ast_log(LOG_WARNING, "Unable to find our position\n");
228 if (fseek(f, 4, SEEK_SET)) {
229 ast_log(LOG_WARNING, "Unable to set our position\n");
232 if (fwrite(&filelen, 1, 4, f) != 4) {
233 ast_log(LOG_WARNING, "Unable to set write file size\n");
236 if (fseek(f, 40, SEEK_SET)) {
237 ast_log(LOG_WARNING, "Unable to set our position\n");
240 if (fwrite(&datalen, 1, 4, f) != 4) {
241 ast_log(LOG_WARNING, "Unable to set write datalen\n");
244 if (fseeko(f, cur, SEEK_SET)) {
245 ast_log(LOG_WARNING, "Unable to return to position\n");
251 static int write_header(FILE *f)
253 unsigned int hz=htoll(8000);
254 unsigned int bhz = htoll(16000);
255 unsigned int hs = htoll(16);
256 unsigned short fmt = htols(1);
257 unsigned short chans = htols(1);
258 unsigned short bysam = htols(2);
259 unsigned short bisam = htols(16);
260 unsigned int size = htoll(0);
261 /* Write a wav header, ignoring sizes which will be filled in later */
263 if (fwrite("RIFF", 1, 4, f) != 4) {
264 ast_log(LOG_WARNING, "Unable to write header\n");
267 if (fwrite(&size, 1, 4, f) != 4) {
268 ast_log(LOG_WARNING, "Unable to write header\n");
271 if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
272 ast_log(LOG_WARNING, "Unable to write header\n");
275 if (fwrite(&hs, 1, 4, f) != 4) {
276 ast_log(LOG_WARNING, "Unable to write header\n");
279 if (fwrite(&fmt, 1, 2, f) != 2) {
280 ast_log(LOG_WARNING, "Unable to write header\n");
283 if (fwrite(&chans, 1, 2, f) != 2) {
284 ast_log(LOG_WARNING, "Unable to write header\n");
287 if (fwrite(&hz, 1, 4, f) != 4) {
288 ast_log(LOG_WARNING, "Unable to write header\n");
291 if (fwrite(&bhz, 1, 4, f) != 4) {
292 ast_log(LOG_WARNING, "Unable to write header\n");
295 if (fwrite(&bysam, 1, 2, f) != 2) {
296 ast_log(LOG_WARNING, "Unable to write header\n");
299 if (fwrite(&bisam, 1, 2, f) != 2) {
300 ast_log(LOG_WARNING, "Unable to write header\n");
303 if (fwrite("data", 1, 4, f) != 4) {
304 ast_log(LOG_WARNING, "Unable to write header\n");
307 if (fwrite(&size, 1, 4, f) != 4) {
308 ast_log(LOG_WARNING, "Unable to write header\n");
314 static int wav_open(struct ast_filestream *s)
316 /* We don't have any header to read or anything really, but
317 if we did, it would go here. We also might want to check
318 and be sure it's a valid file. */
319 struct wav_desc *tmp = (struct wav_desc *)s->private;
320 if ((tmp->maxlen = check_header(s->f)) < 0)
325 static int wav_rewrite(struct ast_filestream *s, const char *comment)
327 /* We don't have any header to read or anything really, but
328 if we did, it would go here. We also might want to check
329 and be sure it's a valid file. */
331 if (write_header(s->f))
336 static void wav_close(struct ast_filestream *s)
339 struct wav_desc *fs = (struct wav_desc *)s->private;
340 /* Pad to even length */
342 fwrite(&zero, 1, 1, s->f);
345 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
348 int samples; /* actual samples read */
349 #if __BYTE_ORDER == __BIG_ENDIAN
353 int bytes = WAV_BUF_SIZE; /* in bytes */
355 /* Send a frame from the file to the appropriate channel */
356 struct wav_desc *fs = (struct wav_desc *)s->private;
359 if (fs->maxlen - here < bytes) /* truncate if necessary */
360 bytes = fs->maxlen - here;
363 /* ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
364 s->fr.frametype = AST_FRAME_VOICE;
365 s->fr.subclass = AST_FORMAT_SLINEAR;
367 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
369 if ( (res = fread(s->fr.data, 1, s->fr.datalen, s->f)) <= 0 ) {
371 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
375 s->fr.samples = samples = res / 2;
377 tmp = (short *)(s->fr.data);
378 #if __BYTE_ORDER == __BIG_ENDIAN
379 /* file format is little endian so we need to swap */
380 for( x = 0; x < samples; x++)
381 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
388 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
390 #if __BYTE_ORDER == __BIG_ENDIAN
392 short tmp[8000], *tmpi;
394 struct wav_desc *s = (struct wav_desc *)fs->private;
397 if (f->frametype != AST_FRAME_VOICE) {
398 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
401 if (f->subclass != AST_FORMAT_SLINEAR) {
402 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
408 #if __BYTE_ORDER == __BIG_ENDIAN
410 if (f->datalen > sizeof(tmp)) {
411 ast_log(LOG_WARNING, "Data length is too long\n");
415 for (x=0; x < f->datalen/2; x++)
416 tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
418 if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
421 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen ) {
423 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
427 s->bytes += f->datalen;
428 update_header(fs->f);
434 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
436 off_t min, max, cur, offset = 0, samples;
438 samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
439 min = 44; /* wav header is 44 bytes */
441 fseeko(fs->f, 0, SEEK_END);
443 if (whence == SEEK_SET)
444 offset = samples + min;
445 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
446 offset = samples + cur;
447 else if (whence == SEEK_END)
448 offset = max - samples;
449 if (whence != SEEK_FORCECUR) {
450 offset = (offset > max)?max:offset;
452 /* always protect the header space. */
453 offset = (offset < min)?min:offset;
454 return fseeko(fs->f, offset, SEEK_SET);
457 static int wav_trunc(struct ast_filestream *fs)
459 if (ftruncate(fileno(fs->f), ftello(fs->f)))
461 return update_header(fs->f);
464 static off_t wav_tell(struct ast_filestream *fs)
467 offset = ftello(fs->f);
468 /* subtract header size to get samples, then divide by 2 for 16 bit samples */
469 return (offset - 44)/2;
472 static const struct ast_format wav_f = {
475 .format = AST_FORMAT_SLINEAR,
477 .rewrite = wav_rewrite,
484 .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
485 .desc_size = sizeof(struct wav_desc),
488 static int load_module(void)
490 if (ast_format_register(&wav_f))
491 return AST_MODULE_LOAD_FAILURE;
492 return AST_MODULE_LOAD_SUCCESS;
495 static int unload_module(void)
497 return ast_format_unregister(wav_f.name);
500 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Microsoft WAV format (8000Hz Signed Linear)");