Title update
[asterisk/asterisk.git] / formats / format_wav.c
old mode 100755 (executable)
new mode 100644 (file)
index 2282aac..4e24186
@@ -1,64 +1,59 @@
 /*
- * Asterisk -- A telephony toolkit for Linux.
+ * Asterisk -- An open source telephony toolkit.
  *
- * Work with WAV in the proprietary Microsoft format.
- * 
- * Copyright (C) 1999, Mark Spencer
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
- * Mark Spencer <markster@linux-support.net>
+ * Mark Spencer <markster@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
  *
  * This program is free software, distributed under the terms of
- * the GNU General Public License
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Work with WAV in the proprietary Microsoft format.
+ * Microsoft WAV format (8000hz Signed Linear)
+ * \arg File name extension: wav (lower case)
+ * \ingroup formats
  */
  
-#include "asterisk/lock.h"
-#include "asterisk/channel.h"
-#include "asterisk/file.h"
-#include "asterisk/logger.h"
-#include "asterisk/sched.h"
+/*** MODULEINFO
+       <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/mod_format.h"
 #include "asterisk/module.h"
 #include "asterisk/endian.h"
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <stdlib.h>
-#include <sys/time.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
 
 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
 
 /* Portions of the conversion code are by guido@sienanet.it */
 
-struct ast_filestream {
-       void *reserved[AST_RESERVED_POINTERS];
-       /* This is what a filestream means to us */
-       int fd; /* Descriptor */
+#define        WAV_BUF_SIZE    320
+
+#define WAV_HEADER_SIZE 44
+
+struct wav_desc {      /* format-specific parameters */
+       int hz;
        int bytes;
-       int needsgain;
-       struct ast_frame fr;                            /* Frame information */
-       char waste[AST_FRIENDLY_OFFSET];        /* Buffer for sending frames, etc */
-       char empty;                                                     /* Empty character */
-       short buf[160]; 
-       int foffset;
        int lasttimeout;
        int maxlen;
        struct timeval last;
 };
 
-
-AST_MUTEX_DEFINE_STATIC(wav_lock);
-static int glistcnt = 0;
-
-static char *name = "wav";
-static char *desc = "Microsoft WAV format (8000hz Signed Linear)";
-static char *exts = "wav";
-
 #define BLOCKSIZE 160
 
-#define GAIN 2         /* 2^GAIN is the multiple to increase the volume by */
-
 #if __BYTE_ORDER == __LITTLE_ENDIAN
 #define htoll(b) (b)
 #define htols(b) (b)
@@ -82,52 +77,16 @@ static char *exts = "wav";
 #endif
 
 
-static int check_header(int fd)
+static int check_header_fmt(FILE *f, int hsize, int hz)
 {
-       int type, size, formtype;
-       int fmt, hsize;
        short format, chans, bysam, bisam;
        int bysec;
        int freq;
-       int data;
-       if (read(fd, &type, 4) != 4) {
-               ast_log(LOG_WARNING, "Read failed (type)\n");
+       if (hsize < 16) {
+               ast_log(LOG_WARNING, "Unexpected header size %d\n", hsize);
                return -1;
        }
-       if (read(fd, &size, 4) != 4) {
-               ast_log(LOG_WARNING, "Read failed (size)\n");
-               return -1;
-       }
-       size = ltohl(size);
-       if (read(fd, &formtype, 4) != 4) {
-               ast_log(LOG_WARNING, "Read failed (formtype)\n");
-               return -1;
-       }
-       if (memcmp(&type, "RIFF", 4)) {
-               ast_log(LOG_WARNING, "Does not begin with RIFF\n");
-               return -1;
-       }
-       if (memcmp(&formtype, "WAVE", 4)) {
-               ast_log(LOG_WARNING, "Does not contain WAVE\n");
-               return -1;
-       }
-       if (read(fd, &fmt, 4) != 4) {
-               ast_log(LOG_WARNING, "Read failed (fmt)\n");
-               return -1;
-       }
-       if (memcmp(&fmt, "fmt ", 4)) {
-               ast_log(LOG_WARNING, "Does not say fmt\n");
-               return -1;
-       }
-       if (read(fd, &hsize, 4) != 4) {
-               ast_log(LOG_WARNING, "Read failed (formtype)\n");
-               return -1;
-       }
-       if (ltohl(hsize) < 16) {
-               ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
-               return -1;
-       }
-       if (read(fd, &format, 2) != 2) {
+       if (fread(&format, 1, 2, f) != 2) {
                ast_log(LOG_WARNING, "Read failed (format)\n");
                return -1;
        }
@@ -135,7 +94,7 @@ static int check_header(int fd)
                ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format));
                return -1;
        }
-       if (read(fd, &chans, 2) != 2) {
+       if (fread(&chans, 1, 2, f) != 2) {
                ast_log(LOG_WARNING, "Read failed (format)\n");
                return -1;
        }
@@ -143,21 +102,23 @@ static int check_header(int fd)
                ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
                return -1;
        }
-       if (read(fd, &freq, 4) != 4) {
+       if (fread(&freq, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Read failed (freq)\n");
                return -1;
        }
-       if (ltohl(freq) != 8000) {
-               ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
+       if (((ltohl(freq) != 8000) && (ltohl(freq) != 16000)) ||
+           ((ltohl(freq) == 8000) && (hz != 8000)) ||
+           ((ltohl(freq) == 16000) && (hz != 16000))) {
+               ast_log(LOG_WARNING, "Unexpected frequency mismatch %d (expecting %d)\n", ltohl(freq),hz);
                return -1;
        }
        /* Ignore the byte frequency */
-       if (read(fd, &bysec, 4) != 4) {
+       if (fread(&bysec, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n");
                return -1;
        }
        /* Check bytes per sample */
-       if (read(fd, &bysam, 2) != 2) {
+       if (fread(&bysam, 1, 2, f) != 2) {
                ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n");
                return -1;
        }
@@ -165,39 +126,70 @@ static int check_header(int fd)
                ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam));
                return -1;
        }
-       if (read(fd, &bisam, 2) != 2) {
+       if (fread(&bisam, 1, 2, f) != 2) {
                ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam));
                return -1;
        }
        /* Skip any additional header */
-       if ( lseek(fd,ltohl(hsize)-16,SEEK_CUR) == -1 ) {
-               ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 );
+       if (fseek(f,hsize-16,SEEK_CUR) == -1 ) {
+               ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", hsize-16 );
+               return -1;
+       }
+       return 0;
+}
+
+static int check_header(FILE *f, int hz)
+{
+       int type, size, formtype;
+       int data;
+       if (fread(&type, 1, 4, f) != 4) {
+               ast_log(LOG_WARNING, "Read failed (type)\n");
+               return -1;
+       }
+       if (fread(&size, 1, 4, f) != 4) {
+               ast_log(LOG_WARNING, "Read failed (size)\n");
+               return -1;
+       }
+       size = ltohl(size);
+       if (fread(&formtype, 1, 4, f) != 4) {
+               ast_log(LOG_WARNING, "Read failed (formtype)\n");
+               return -1;
+       }
+       if (memcmp(&type, "RIFF", 4)) {
+               ast_log(LOG_WARNING, "Does not begin with RIFF\n");
+               return -1;
+       }
+       if (memcmp(&formtype, "WAVE", 4)) {
+               ast_log(LOG_WARNING, "Does not contain WAVE\n");
                return -1;
        }
        /* Skip any facts and get the first data block */
        for(;;)
        { 
-            char buf[4];
+               char buf[4];
            
            /* Begin data chunk */
-           if (read(fd, &buf, 4) != 4) {
-               ast_log(LOG_WARNING, "Read failed (data)\n");
-               return -1;
+           if (fread(&buf, 1, 4, f) != 4) {
+                       ast_log(LOG_WARNING, "Read failed (block header format)\n");
+                       return -1;
            }
            /* Data has the actual length of data in it */
-           if (read(fd, &data, 4) != 4) {
-               ast_log(LOG_WARNING, "Read failed (data)\n");
-               return -1;
+           if (fread(&data, 1, 4, f) != 4) {
+                       ast_log(LOG_WARNING, "Read failed (block '%.4s' header length)\n", buf);
+                       return -1;
            }
            data = ltohl(data);
-           if( memcmp(buf, "data", 4) == 0 ) break;
-           if( memcmp(buf, "fact", 4) != 0 ) {
-               ast_log(LOG_WARNING, "Unknown block - not fact or data\n");
-               return -1;
-           }
-           if ( lseek(fd,data,SEEK_CUR) == -1 ) {
-               ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data );
-               return -1;
+               if (memcmp(&buf, "fmt ", 4) == 0) {
+                       if (check_header_fmt(f, data, hz))
+                               return -1;
+                       continue;
+               }
+           if(memcmp(buf, "data", 4) == 0 ) 
+                       break;
+               ast_log(LOG_DEBUG, "Skipping unknown block '%.4s'\n", buf);
+           if (fseek(f,data,SEEK_CUR) == -1 ) {
+                       ast_log(LOG_WARNING, "Failed to skip '%.4s' block: %d\n", buf, data);
+                       return -1;
            }
        }
 #if 0
@@ -209,14 +201,14 @@ static int check_header(int fd)
        return data;
 }
 
-static int update_header(int fd)
+static int update_header(FILE *f)
 {
        off_t cur,end;
        int datalen,filelen,bytes;
        
-       
-       cur = lseek(fd, 0, SEEK_CUR);
-       end = lseek(fd, 0, SEEK_END);
+       cur = ftello(f);
+       fseek(f, 0, SEEK_END);
+       end = ftello(f);
        /* data starts 44 bytes in */
        bytes = end - 44;
        datalen = htoll(bytes);
@@ -227,291 +219,260 @@ static int update_header(int fd)
                ast_log(LOG_WARNING, "Unable to find our position\n");
                return -1;
        }
-       if (lseek(fd, 4, SEEK_SET) != 4) {
+       if (fseek(f, 4, SEEK_SET)) {
                ast_log(LOG_WARNING, "Unable to set our position\n");
                return -1;
        }
-       if (write(fd, &filelen, 4) != 4) {
+       if (fwrite(&filelen, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to set write file size\n");
                return -1;
        }
-       if (lseek(fd, 40, SEEK_SET) != 40) {
+       if (fseek(f, 40, SEEK_SET)) {
                ast_log(LOG_WARNING, "Unable to set our position\n");
                return -1;
        }
-       if (write(fd, &datalen, 4) != 4) {
+       if (fwrite(&datalen, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to set write datalen\n");
                return -1;
        }
-       if (lseek(fd, cur, SEEK_SET) != cur) {
+       if (fseeko(f, cur, SEEK_SET)) {
                ast_log(LOG_WARNING, "Unable to return to position\n");
                return -1;
        }
        return 0;
 }
 
-static int write_header(int fd)
+static int write_header(FILE *f, int writehz)
 {
-       unsigned int hz=htoll(8000);
-       unsigned int bhz = htoll(16000);
+       unsigned int hz;
+       unsigned int bhz;
        unsigned int hs = htoll(16);
        unsigned short fmt = htols(1);
        unsigned short chans = htols(1);
        unsigned short bysam = htols(2);
        unsigned short bisam = htols(16);
        unsigned int size = htoll(0);
+
+       if (writehz == 16000) {
+               hz = htoll(16000);
+               bhz = htoll(32000);
+       } else {
+               hz = htoll(8000);
+               bhz = htoll(16000);
+       }
        /* Write a wav header, ignoring sizes which will be filled in later */
-       lseek(fd,0,SEEK_SET);
-       if (write(fd, "RIFF", 4) != 4) {
+       fseek(f,0,SEEK_SET);
+       if (fwrite("RIFF", 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &size, 4) != 4) {
+       if (fwrite(&size, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, "WAVEfmt ", 8) != 8) {
+       if (fwrite("WAVEfmt ", 1, 8, f) != 8) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &hs, 4) != 4) {
+       if (fwrite(&hs, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &fmt, 2) != 2) {
+       if (fwrite(&fmt, 1, 2, f) != 2) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &chans, 2) != 2) {
+       if (fwrite(&chans, 1, 2, f) != 2) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &hz, 4) != 4) {
+       if (fwrite(&hz, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &bhz, 4) != 4) {
+       if (fwrite(&bhz, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &bysam, 2) != 2) {
+       if (fwrite(&bysam, 1, 2, f) != 2) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &bisam, 2) != 2) {
+       if (fwrite(&bisam, 1, 2, f) != 2) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, "data", 4) != 4) {
+       if (fwrite("data", 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
-       if (write(fd, &size, 4) != 4) {
+       if (fwrite(&size, 1, 4, f) != 4) {
                ast_log(LOG_WARNING, "Unable to write header\n");
                return -1;
        }
        return 0;
 }
 
-static struct ast_filestream *wav_open(int fd)
+static int wav_open(struct ast_filestream *s)
 {
        /* We don't have any header to read or anything really, but
           if we did, it would go here.  We also might want to check
           and be sure it's a valid file.  */
-       struct ast_filestream *tmp;
-       if ((tmp = malloc(sizeof(struct ast_filestream)))) {
-               memset(tmp, 0, sizeof(struct ast_filestream));
-               if ((tmp->maxlen = check_header(fd)) < 0) {
-                       free(tmp);
-                       return NULL;
-               }
-               if (ast_mutex_lock(&wav_lock)) {
-                       ast_log(LOG_WARNING, "Unable to lock wav list\n");
-                       free(tmp);
-                       return NULL;
-               }
-               tmp->fd = fd;
-               tmp->needsgain = 1;
-               tmp->fr.data = tmp->buf;
-               tmp->fr.frametype = AST_FRAME_VOICE;
-               tmp->fr.subclass = AST_FORMAT_SLINEAR;
-               /* datalen will vary for each frame */
-               tmp->fr.src = name;
-               tmp->fr.mallocd = 0;
-               tmp->bytes = 0;
-               glistcnt++;
-               ast_mutex_unlock(&wav_lock);
-               ast_update_use_count();
-       }
-       return tmp;
+       struct wav_desc *tmp = (struct wav_desc *)s->_private;
+       if ((tmp->maxlen = check_header(s->f, (s->fmt->format.id == AST_FORMAT_SLINEAR16 ? 16000 : 8000))) < 0)
+               return -1;
+       return 0;
 }
 
-static struct ast_filestream *wav_rewrite(int fd, const char *comment)
+static int wav_rewrite(struct ast_filestream *s, const char *comment)
 {
        /* We don't have any header to read or anything really, but
           if we did, it would go here.  We also might want to check
           and be sure it's a valid file.  */
-       struct ast_filestream *tmp;
-       if ((tmp = malloc(sizeof(struct ast_filestream)))) {
-               memset(tmp, 0, sizeof(struct ast_filestream));
-               if (write_header(fd)) {
-                       free(tmp);
-                       return NULL;
-               }
-               if (ast_mutex_lock(&wav_lock)) {
-                       ast_log(LOG_WARNING, "Unable to lock wav list\n");
-                       free(tmp);
-                       return NULL;
-               }
-               tmp->fd = fd;
-               glistcnt++;
-               ast_mutex_unlock(&wav_lock);
-               ast_update_use_count();
-       } else
-               ast_log(LOG_WARNING, "Out of memory\n");
-       return tmp;
+
+       struct wav_desc *tmp = (struct wav_desc *)s->_private;
+       tmp->hz = (s->fmt->format.id == AST_FORMAT_SLINEAR16 ? 16000 : 8000);
+       if (write_header(s->f,tmp->hz))
+               return -1;
+       return 0;
 }
 
 static void wav_close(struct ast_filestream *s)
 {
        char zero = 0;
-       if (ast_mutex_lock(&wav_lock)) {
-               ast_log(LOG_WARNING, "Unable to lock wav list\n");
+       struct wav_desc *fs = (struct wav_desc *)s->_private;
+
+       if (s->mode == O_RDONLY) {
                return;
        }
-       glistcnt--;
-       ast_mutex_unlock(&wav_lock);
-       ast_update_use_count();
+
+       if (s->filename) {
+               update_header(s->f);
+       }
+
        /* Pad to even length */
-       if (s->bytes & 0x1)
-               write(s->fd, &zero, 1);
-       close(s->fd);
-       free(s);
-       s = NULL;
+       if (fs->bytes & 0x1) {
+               if (!fwrite(&zero, 1, 1, s->f)) {
+                       ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
+               }
+       }
 }
 
 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
 {
        int res;
-       int delay;
+       int samples;    /* actual samples read */
+#if __BYTE_ORDER == __BIG_ENDIAN
        int x;
-       short tmp[sizeof(s->buf) / 2];
-       int bytes = sizeof(tmp);
+       short *tmp;
+#endif
+       int bytes;
        off_t here;
        /* Send a frame from the file to the appropriate channel */
-       here = lseek(s->fd, 0, SEEK_CUR);
-       if ((s->maxlen - here) < bytes)
-               bytes = s->maxlen - here;
+       struct wav_desc *fs = (struct wav_desc *)s->_private;
+
+       bytes = (fs->hz == 16000 ? (WAV_BUF_SIZE * 2) : WAV_BUF_SIZE);
+
+       here = ftello(s->f);
+       if (fs->maxlen - here < bytes)          /* truncate if necessary */
+               bytes = fs->maxlen - here;
        if (bytes < 0)
                bytes = 0;
-/*     ast_log(LOG_DEBUG, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
+/*     ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
+       s->fr.frametype = AST_FRAME_VOICE;
+       ast_format_set(&s->fr.subclass.format, (fs->hz == 16000 ? AST_FORMAT_SLINEAR16 : AST_FORMAT_SLINEAR), 0);
+       s->fr.mallocd = 0;
+       AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
        
-       if ( (res = read(s->fd, tmp, bytes)) <= 0 ) {
-               if (res) {
+       if ( (res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) <= 0 ) {
+               if (res)
                        ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
-               }
                return NULL;
        }
+       s->fr.datalen = res;
+       s->fr.samples = samples = res / 2;
 
 #if __BYTE_ORDER == __BIG_ENDIAN
-       for( x = 0; x < sizeof(tmp)/2; x++) tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
+       tmp = (short *)(s->fr.data.ptr);
+       /* file format is little endian so we need to swap */
+       for( x = 0; x < samples; x++)
+               tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
 #endif
 
-       if (s->needsgain) {
-               for (x=0;x<sizeof(tmp)/2;x++)
-                       if (tmp[x] & ((1 << GAIN) - 1)) {
-                               /* If it has data down low, then it's not something we've artificially increased gain
-                                  on, so we don't need to gain adjust it */
-                               s->needsgain = 0;
-                       }
-       }
-       if (s->needsgain) {
-               for (x=0;x<sizeof(tmp)/2;x++) {
-                       s->buf[x] = tmp[x] >> GAIN;
-               }
-       } else {
-               memcpy(s->buf, tmp, sizeof(s->buf));
-       }
-                       
-       delay = res / 2;
-       s->fr.frametype = AST_FRAME_VOICE;
-       s->fr.subclass = AST_FORMAT_SLINEAR;
-       s->fr.offset = AST_FRIENDLY_OFFSET;
-       s->fr.datalen = res;
-       s->fr.data = s->buf;
-       s->fr.mallocd = 0;
-       s->fr.samples = delay;
-       *whennext = delay;
+       *whennext = samples;
        return &s->fr;
 }
 
 static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
 {
-       int res = 0;
+#if __BYTE_ORDER == __BIG_ENDIAN
        int x;
-       short tmp[8000], *tmpi;
-       float tmpf;
+       short tmp[16000], *tmpi;
+#endif
+       struct wav_desc *s = (struct wav_desc *)fs->_private;
+       int res;
+
        if (f->frametype != AST_FRAME_VOICE) {
                ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
                return -1;
        }
-       if (f->subclass != AST_FORMAT_SLINEAR) {
-               ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass);
+       if ((f->subclass.format.id != AST_FORMAT_SLINEAR) && (f->subclass.format.id != AST_FORMAT_SLINEAR16)) {
+               ast_log(LOG_WARNING, "Asked to write non-SLINEAR%s frame (%s)!\n", s->hz == 16000 ? "16" : "", ast_getformatname(&f->subclass.format));
                return -1;
        }
-       if (f->datalen > sizeof(tmp)) {
-               ast_log(LOG_WARNING, "Data length is too long\n");
+       if (ast_format_cmp(&f->subclass.format, &fs->fmt->format) == AST_FORMAT_CMP_NOT_EQUAL) {
+               ast_log(LOG_WARNING, "Can't change SLINEAR frequency during write\n");
                return -1;
        }
        if (!f->datalen)
                return -1;
 
-#if 0
-       printf("Data Length: %d\n", f->datalen);
-#endif 
-
-       if (fs->buf) {
-               tmpi = f->data;
-               /* Volume adjust here to accomodate */
-               for (x=0;x<f->datalen/2;x++) {
-                       tmpf = ((float)tmpi[x]) * ((float)(1 << GAIN));
-                       if (tmpf > 32767.0)
-                               tmpf = 32767.0;
-                       if (tmpf < -32768.0)
-                               tmpf = -32768.0;
-                       tmp[x] = tmpf;
-                       tmp[x] &= ~((1 << GAIN) - 1);
-
 #if __BYTE_ORDER == __BIG_ENDIAN
-                       tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8);
-#endif
+       /* swap and write */
+       if (f->datalen > sizeof(tmp)) {
+               ast_log(LOG_WARNING, "Data length is too long\n");
+               return -1;
+       }
+       tmpi = f->data.ptr;
+       for (x=0; x < f->datalen/2; x++) 
+               tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8);
 
-               }
-               if ((write (fs->fd, tmp, f->datalen) != f->datalen) ) {
-                       ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
-                       return -1;
-               }
-       } else {
-               ast_log(LOG_WARNING, "Cannot write data to file.\n");
+       if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) {
+#else
+       /* just write */
+       if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen ) {
+#endif
+               ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno));
                return -1;
        }
-       
-       fs->bytes += f->datalen;
-       update_header(fs->fd);
+
+       s->bytes += f->datalen;
                
        return 0;
 
 }
 
-static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
+static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
 {
-       off_t min,max,cur;
-       long offset=0,samples;
-       
+       off_t min = WAV_HEADER_SIZE, max, cur, offset = 0, samples;
+
        samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */
-       min = 44; /* wav header is 44 bytes */
-       cur = lseek(fs->fd, 0, SEEK_CUR);
-       max = lseek(fs->fd, 0, SEEK_END);
+
+       if ((cur = ftello(fs->f)) < 0) {
+               ast_log(AST_LOG_WARNING, "Unable to determine current position in wav filestream %p: %s\n", fs, strerror(errno));
+               return -1;
+       }
+
+       if (fseeko(fs->f, 0, SEEK_END) < 0) {
+               ast_log(AST_LOG_WARNING, "Unable to seek to end of wav filestream %p: %s\n", fs, strerror(errno));
+               return -1;
+       }
+
+       if ((max = ftello(fs->f)) < 0) {
+               ast_log(AST_LOG_WARNING, "Unable to determine max position in wav filestream %p: %s\n", fs, strerror(errno));
+               return -1;
+       }
+
        if (whence == SEEK_SET)
                offset = samples + min;
        else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
@@ -523,69 +484,85 @@ static int wav_seek(struct ast_filestream *fs, long sample_offset, int whence)
        }
        /* always protect the header space. */
        offset = (offset < min)?min:offset;
-       return lseek(fs->fd,offset,SEEK_SET);
+       return fseeko(fs->f, offset, SEEK_SET);
 }
 
 static int wav_trunc(struct ast_filestream *fs)
 {
-       if(ftruncate(fs->fd, lseek(fs->fd,0,SEEK_CUR)))
+       int fd;
+       off_t cur;
+
+       if ((fd = fileno(fs->f)) < 0) {
+               ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for wav filestream %p: %s\n", fs, strerror(errno));
+               return -1;
+       }
+       if ((cur = ftello(fs->f)) < 0) {
+               ast_log(AST_LOG_WARNING, "Unable to determine current position in wav filestream %p: %s\n", fs, strerror(errno));
+               return -1;
+       }
+       /* Truncate file to current length */
+       if (ftruncate(fd, cur)) {
                return -1;
-       return update_header(fs->fd);
+       }
+       return update_header(fs->f);
 }
 
-static long wav_tell(struct ast_filestream *fs)
+static off_t wav_tell(struct ast_filestream *fs)
 {
        off_t offset;
-       offset = lseek(fs->fd, 0, SEEK_CUR);
+       offset = ftello(fs->f);
        /* subtract header size to get samples, then divide by 2 for 16 bit samples */
        return (offset - 44)/2;
 }
 
-static char *wav_getcomment(struct ast_filestream *s)
-{
-       return NULL;
-}
-
-int load_module()
-{
-       return ast_format_register(name, exts, AST_FORMAT_SLINEAR,
-                                                               wav_open,
-                                                               wav_rewrite,
-                                                               wav_write,
-                                                               wav_seek,
-                                                               wav_trunc,
-                                                               wav_tell,
-                                                               wav_read,
-                                                               wav_close,
-                                                               wav_getcomment);
-                                                               
-                                                               
-}
+static struct ast_format_def wav16_f = {
+       .name = "wav16",
+       .exts = "wav16",
+       .open = wav_open,
+       .rewrite = wav_rewrite,
+       .write = wav_write,
+       .seek = wav_seek,
+       .trunc = wav_trunc,
+       .tell = wav_tell,
+       .read = wav_read,
+       .close = wav_close,
+       .buf_size = (WAV_BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
+       .desc_size = sizeof(struct wav_desc),
+};
 
-int unload_module()
-{
-       return ast_format_unregister(name);
-}      
+static struct ast_format_def wav_f = {
+       .name = "wav",
+       .exts = "wav",
+       .open = wav_open,
+       .rewrite = wav_rewrite,
+       .write = wav_write,
+       .seek = wav_seek,
+       .trunc = wav_trunc,
+       .tell = wav_tell,
+       .read = wav_read,
+       .close = wav_close,
+       .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET,
+       .desc_size = sizeof(struct wav_desc),
+};
 
-int usecount()
+static int load_module(void)
 {
-       int res;
-       if (ast_mutex_lock(&wav_lock)) {
-               ast_log(LOG_WARNING, "Unable to lock wav list\n");
-               return -1;
-       }
-       res = glistcnt;
-       ast_mutex_unlock(&wav_lock);
-       return res;
+       ast_format_set(&wav_f.format, AST_FORMAT_SLINEAR, 0);
+       ast_format_set(&wav16_f.format, AST_FORMAT_SLINEAR16, 0);
+       if (ast_format_def_register(&wav_f)
+               || ast_format_def_register(&wav16_f))
+               return AST_MODULE_LOAD_FAILURE;
+       return AST_MODULE_LOAD_SUCCESS;
 }
 
-char *description()
+static int unload_module(void)
 {
-       return desc;
+       return ast_format_def_unregister(wav_f.name)
+               || ast_format_def_unregister(wav16_f.name);
 }
 
-
-char *key()
-{
-       return ASTERISK_GPL_KEY;
-}
+AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV/WAV16 format (8kHz/16kHz Signed Linear)",
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_APP_DEPEND
+);