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 */
63 #if __BYTE_ORDER == __LITTLE_ENDIAN
69 #if __BYTE_ORDER == __BIG_ENDIAN
71 (((((b) ) & 0xFF) << 24) | \
72 ((((b) >> 8) & 0xFF) << 16) | \
73 ((((b) >> 16) & 0xFF) << 8) | \
74 ((((b) >> 24) & 0xFF) ))
76 (((((b) ) & 0xFF) << 8) | \
77 ((((b) >> 8) & 0xFF) ))
78 #define ltohl(b) htoll(b)
79 #define ltohs(b) htols(b)
81 #error "Endianess not defined"
86 static int check_header(FILE *f)
88 int type, size, formtype;
90 short format, chans, bysam, bisam;
94 if (fread(&type, 1, 4, f) != 4) {
95 ast_log(LOG_WARNING, "Read failed (type)\n");
98 if (fread(&size, 1, 4, f) != 4) {
99 ast_log(LOG_WARNING, "Read failed (size)\n");
103 if (fread(&formtype, 1, 4, f) != 4) {
104 ast_log(LOG_WARNING, "Read failed (formtype)\n");
107 if (memcmp(&type, "RIFF", 4)) {
108 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
111 if (memcmp(&formtype, "WAVE", 4)) {
112 ast_log(LOG_WARNING, "Does not contain WAVE\n");
115 if (fread(&fmt, 1, 4, f) != 4) {
116 ast_log(LOG_WARNING, "Read failed (fmt)\n");
119 if (memcmp(&fmt, "fmt ", 4)) {
120 ast_log(LOG_WARNING, "Does not say fmt\n");
123 if (fread(&hsize, 1, 4, f) != 4) {
124 ast_log(LOG_WARNING, "Read failed (formtype)\n");
127 if (ltohl(hsize) < 16) {
128 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
131 if (fread(&format, 1, 2, f) != 2) {
132 ast_log(LOG_WARNING, "Read failed (format)\n");
135 if (ltohs(format) != 1) {
136 ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
139 if (fread(&chans, 1, 2, f) != 2) {
140 ast_log(LOG_WARNING, "Read failed (format)\n");
143 if (ltohs(chans) != 1) {
144 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
147 if (fread(&freq, 1, 4, f) != 4) {
148 ast_log(LOG_WARNING, "Read failed (freq)\n");
151 if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
152 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
155 /* Ignore the byte frequency */
156 if (fread(&bysec, 1, 4, f) != 4) {
157 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
160 /* Check bytes per sample */
161 if (fread(&bysam, 1, 2, f) != 2) {
162 ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
165 if (ltohs(bysam) != 2) {
166 ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
169 if (fread(&bisam, 1, 2, f) != 2) {
170 ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
173 /* Skip any additional header */
174 if (fseek(f,ltohl(hsize)-16,SEEK_CUR) == -1 ) {
175 ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 );
178 /* Skip any facts and get the first data block */
183 /* Begin data chunk */
184 if (fread(&buf, 1, 4, f) != 4) {
185 ast_log(LOG_WARNING, "Read failed (data)\n");
188 /* Data has the actual length of data in it */
189 if (fread(&data, 1, 4, f) != 4) {
190 ast_log(LOG_WARNING, "Read failed (data)\n");
194 if(memcmp(buf, "data", 4) == 0 )
196 if(memcmp(buf, "fact", 4) != 0 ) {
197 ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
200 if (fseek(f,data,SEEK_CUR) == -1 ) {
201 ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
206 curpos = lseek(fd, 0, SEEK_CUR);
207 truelength = lseek(fd, 0, SEEK_END);
208 lseek(fd, curpos, SEEK_SET);
209 truelength -= curpos;
214 static int update_header(FILE *f)
217 int datalen,filelen,bytes;
220 fseek(f, 0, SEEK_END);
222 /* data starts 44 bytes in */
224 datalen = htoll(bytes);
225 /* chunk size is bytes of data plus 36 bytes of header */
226 filelen = htoll(36 + bytes);
229 ast_log(LOG_WARNING, "Unable to find our position\n");
232 if (fseek(f, 4, SEEK_SET)) {
233 ast_log(LOG_WARNING, "Unable to set our position\n");
236 if (fwrite(&filelen, 1, 4, f) != 4) {
237 ast_log(LOG_WARNING, "Unable to set write file size\n");
240 if (fseek(f, 40, SEEK_SET)) {
241 ast_log(LOG_WARNING, "Unable to set our position\n");
244 if (fwrite(&datalen, 1, 4, f) != 4) {
245 ast_log(LOG_WARNING, "Unable to set write datalen\n");
248 if (fseeko(f, cur, SEEK_SET)) {
249 ast_log(LOG_WARNING, "Unable to return to position\n");
255 static int write_header(FILE *f)
257 unsigned int hz=htoll(8000);
258 unsigned int bhz = htoll(16000);
259 unsigned int hs = htoll(16);
260 unsigned short fmt = htols(1);
261 unsigned short chans = htols(1);
262 unsigned short bysam = htols(2);
263 unsigned short bisam = htols(16);
264 unsigned int size = htoll(0);
265 /* Write a wav header, ignoring sizes which will be filled in later */
267 if (fwrite("RIFF", 1, 4, f) != 4) {
268 ast_log(LOG_WARNING, "Unable to write header\n");
271 if (fwrite(&size, 1, 4, f) != 4) {
272 ast_log(LOG_WARNING, "Unable to write header\n");
275 if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
276 ast_log(LOG_WARNING, "Unable to write header\n");
279 if (fwrite(&hs, 1, 4, f) != 4) {
280 ast_log(LOG_WARNING, "Unable to write header\n");
283 if (fwrite(&fmt, 1, 2, f) != 2) {
284 ast_log(LOG_WARNING, "Unable to write header\n");
287 if (fwrite(&chans, 1, 2, f) != 2) {
288 ast_log(LOG_WARNING, "Unable to write header\n");
291 if (fwrite(&hz, 1, 4, f) != 4) {
292 ast_log(LOG_WARNING, "Unable to write header\n");
295 if (fwrite(&bhz, 1, 4, f) != 4) {
296 ast_log(LOG_WARNING, "Unable to write header\n");
299 if (fwrite(&bysam, 1, 2, f) != 2) {
300 ast_log(LOG_WARNING, "Unable to write header\n");
303 if (fwrite(&bisam, 1, 2, f) != 2) {
304 ast_log(LOG_WARNING, "Unable to write header\n");
307 if (fwrite("data", 1, 4, f) != 4) {
308 ast_log(LOG_WARNING, "Unable to write header\n");
311 if (fwrite(&size, 1, 4, f) != 4) {
312 ast_log(LOG_WARNING, "Unable to write header\n");
318 static int wav_open(struct ast_filestream *s)
320 /* We don't have any header to read or anything really, but
321 if we did, it would go here. We also might want to check
322 and be sure it's a valid file. */
323 struct wav_desc *tmp = (struct wav_desc *)s->private;
324 if ((tmp->maxlen = check_header(s->f)) < 0)
329 static int wav_rewrite(struct ast_filestream *s, const char *comment)
331 /* We don't have any header to read or anything really, but
332 if we did, it would go here. We also might want to check
333 and be sure it's a valid file. */
335 if (write_header(s->f))
340 static void wav_close(struct ast_filestream *s)
343 struct wav_desc *fs = (struct wav_desc *)s->private;
344 /* Pad to even length */
346 fwrite(&zero, 1, 1, s->f);
349 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
352 int samples; /* actual samples read */
353 #if __BYTE_ORDER == __BIG_ENDIAN
357 int bytes = WAV_BUF_SIZE; /* in bytes */
359 /* Send a frame from the file to the appropriate channel */
360 struct wav_desc *fs = (struct wav_desc *)s->private;
363 if (fs->maxlen - here < bytes) /* truncate if necessary */
364 bytes = fs->maxlen - here;
367 /* ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
368 s->fr.frametype = AST_FRAME_VOICE;
369 s->fr.subclass = AST_FORMAT_SLINEAR;
371 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
373 if ( (res = fread(s->fr.data, 1, s->fr.datalen, s->f)) <= 0 ) {
375 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
379 s->fr.samples = samples = res / 2;
381 tmp = (short *)(s->fr.data);
382 #if __BYTE_ORDER == __BIG_ENDIAN
383 /* file format is little endian so we need to swap */
384 for( x = 0; x < samples; x++)
385 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
392 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
394 #if __BYTE_ORDER == __BIG_ENDIAN
396 short tmp[8000], *tmpi;
398 struct wav_desc *s = (struct wav_desc *)fs->private;
401 if (f->frametype != AST_FRAME_VOICE) {
402 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
405 if (f->subclass != AST_FORMAT_SLINEAR) {
406 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
412 #if __BYTE_ORDER == __BIG_ENDIAN
414 if (f->datalen > sizeof(tmp)) {
415 ast_log(LOG_WARNING, "Data length is too long\n");
419 for (x=0; x < f->datalen/2; x++)
420 tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
422 if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
425 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen ) {
427 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
431 s->bytes += f->datalen;
432 update_header(fs->f);
438 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
440 off_t min, max, cur, offset = 0, samples;
442 samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
443 min = 44; /* wav header is 44 bytes */
445 fseeko(fs->f, 0, SEEK_END);
447 if (whence == SEEK_SET)
448 offset = samples + min;
449 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
450 offset = samples + cur;
451 else if (whence == SEEK_END)
452 offset = max - samples;
453 if (whence != SEEK_FORCECUR) {
454 offset = (offset > max)?max:offset;
456 /* always protect the header space. */
457 offset = (offset < min)?min:offset;
458 return fseeko(fs->f, offset, SEEK_SET);
461 static int wav_trunc(struct ast_filestream *fs)
463 if (ftruncate(fileno(fs->f), ftello(fs->f)))
465 return update_header(fs->f);
468 static off_t wav_tell(struct ast_filestream *fs)
471 offset = ftello(fs->f);
472 /* subtract header size to get samples, then divide by 2 for 16 bit samples */
473 return (offset - 44)/2;
476 static const struct ast_format wav_f = {
479 .format = AST_FORMAT_SLINEAR,
481 .rewrite = wav_rewrite,
488 .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
489 .desc_size = sizeof(struct wav_desc),
492 static int load_module(void)
494 return ast_format_register(&wav_f);
497 static int unload_module(void)
499 return ast_format_unregister(wav_f.name);
502 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Microsoft WAV format (8000Hz Signed Linear)");