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 */
355 int bytes = WAV_BUF_SIZE; /* in bytes */
357 /* Send a frame from the file to the appropriate channel */
358 struct wav_desc *fs = (struct wav_desc *)s->private;
361 if (fs->maxlen - here < bytes) /* truncate if necessary */
362 bytes = fs->maxlen - here;
365 /* ast_log(LOG_DEBUG, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
366 s->fr.frametype = AST_FRAME_VOICE;
367 s->fr.subclass = AST_FORMAT_SLINEAR;
369 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
371 if ( (res = fread(s->fr.data, 1, s->fr.datalen, s->f)) <= 0 ) {
373 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
377 s->fr.samples = samples = res / 2;
379 tmp = (short *)(s->fr.data);
380 #if __BYTE_ORDER == __BIG_ENDIAN
381 /* file format is little endian so we need to swap */
382 for( x = 0; x < samples; x++)
383 tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
390 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
393 #if __BYTE_ORDER == __BIG_ENDIAN
394 short tmp[8000], *tmpi;
396 struct wav_desc *s = (struct wav_desc *)fs->private;
399 if (f->frametype != AST_FRAME_VOICE) {
400 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
403 if (f->subclass != AST_FORMAT_SLINEAR) {
404 ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
410 #if __BYTE_ORDER == __BIG_ENDIAN
412 if (f->datalen > sizeof(tmp)) {
413 ast_log(LOG_WARNING, "Data length is too long\n");
417 for (x=0; x < f->datalen/2; x++)
418 tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
420 if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
423 if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen ) {
425 ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
429 s->bytes += f->datalen;
430 update_header(fs->f);
436 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
438 off_t min, max, cur, offset = 0, samples;
440 samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
441 min = 44; /* wav header is 44 bytes */
443 fseeko(fs->f, 0, SEEK_END);
445 if (whence == SEEK_SET)
446 offset = samples + min;
447 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
448 offset = samples + cur;
449 else if (whence == SEEK_END)
450 offset = max - samples;
451 if (whence != SEEK_FORCECUR) {
452 offset = (offset > max)?max:offset;
454 /* always protect the header space. */
455 offset = (offset < min)?min:offset;
456 return fseeko(fs->f, offset, SEEK_SET);
459 static int wav_trunc(struct ast_filestream *fs)
461 if (ftruncate(fileno(fs->f), ftello(fs->f)))
463 return update_header(fs->f);
466 static off_t wav_tell(struct ast_filestream *fs)
469 offset = ftello(fs->f);
470 /* subtract header size to get samples, then divide by 2 for 16 bit samples */
471 return (offset - 44)/2;
474 static const struct ast_format wav_f = {
477 .format = AST_FORMAT_SLINEAR,
479 .rewrite = wav_rewrite,
486 .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
487 .desc_size = sizeof(struct wav_desc),
490 static int load_module(void)
492 return ast_format_register(&wav_f);
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)");