shithub: sox

Download patch

ref: b96a305a04172aeaf1acb22c8ba720c4bdbacb88
parent: b427d364b9bcbe7c8707e8a9e4428c00a826fbef
author: rrt <rrt>
date: Mon Dec 11 17:14:17 EST 2006

Remove vestiges of buffering: there's no more file member in ft_t.
However, the st_fileinfo_t type remains, because it is used in vox.c
and oss.c. Also, the ft_t type retains an eof member.

--- a/src/alsa.c
+++ b/src/alsa.c
@@ -229,7 +229,7 @@
     {
         st_fail_errno(ft,ST_ENOMEM,
                       "unable to allocate output buffer of size %d", 
-                      ft->file.size);
+                      alsa->buf_size);
         return(ST_EOF);
     }
 
--- a/src/misc.c
+++ b/src/misc.c
@@ -568,11 +568,8 @@
     }
 
     /* Empty the st file buffer */
-    if (ft->st_errno == ST_SUCCESS) {
-        ft->file.count = 0;
-        ft->file.pos = 0;
-        ft->file.eof = 0;
-    }
+    if (ft->st_errno == ST_SUCCESS)
+        ft->eof = 0;
 
     return ft->st_errno;
 }
--- a/src/oss.c
+++ b/src/oss.c
@@ -40,11 +40,13 @@
 #include <machine/soundcard.h>
 #endif
 #include <sys/ioctl.h>
+
 /* common r/w initialization code */
 static int ossdspinit(ft_t ft)
 {
     int sampletype, samplesize, dsp_stereo;
     int tmp, rc;
+    st_fileinfo_t *file = (st_fileinfo_t *)ft->priv;
 
     if (ft->info.rate == 0.0) ft->info.rate = 8000;
     if (ft->info.size == -1) ft->info.size = ST_SIZE_BYTE;
@@ -173,18 +175,18 @@
     /* Find out block size to use last because the driver could compute
      * its size based on specific rates/formats.
      */
-    ft->file.size = 0;
-    ioctl (fileno(ft->fp), SNDCTL_DSP_GETBLKSIZE, &ft->file.size);
-    if (ft->file.size < 4 || ft->file.size > 65536) {
-            st_fail_errno(ft,ST_EOF,"Invalid audio buffer size %d", ft->file.size);
+    file->size = 0;
+    ioctl (fileno(ft->fp), SNDCTL_DSP_GETBLKSIZE, &file->size);
+    if (file->size < 4 || file->size > 65536) {
+            st_fail_errno(ft,ST_EOF,"Invalid audio buffer size %d", file->size);
             return (ST_EOF);
     }
-    ft->file.count = 0;
-    ft->file.pos = 0;
-    ft->file.eof = 0;
+    file->count = 0;
+    file->pos = 0;
+    file->eof = 0;
 
-    if ((ft->file.buf = (char *)malloc(ft->file.size)) == NULL) {
-        st_fail_errno(ft,ST_EOF,"Unable to allocate input/output buffer of size %d", ft->file.size);
+    if ((file->buf = (char *)malloc(file->size)) == NULL) {
+        st_fail_errno(ft,ST_EOF,"Unable to allocate input/output buffer of size %d", file->size);
         return (ST_EOF);
     }
 
@@ -194,9 +196,10 @@
     }
 
     /* Change to non-buffered I/O */
-    setvbuf(ft->fp, NULL, _IONBF, sizeof(char) * ft->file.size);
+    setvbuf(ft->fp, NULL, _IONBF, sizeof(char) * file->size);
     return(ST_SUCCESS);
 }
+
 /*
  * Do anything required before you start reading samples.
  * Read file header.
--- a/src/raw.c
+++ b/src/raw.c
@@ -103,7 +103,7 @@
 
 int st_rawstartread(ft_t ft)
 {
-    ft->file.eof = 0;
+    ft->eof = 0;
 
     return ST_SUCCESS;
 }
@@ -110,15 +110,7 @@
 
 int st_rawstartwrite(ft_t ft)
 {
-    ft->file.buf = (char *)malloc(ST_BUFSIZ);
-    if (!ft->file.buf)
-    {
-        st_fail_errno(ft, ST_ENOMEM, "Unable to allocate memory");
-        return ST_EOF;
-    }
-    ft->file.size = ST_BUFSIZ;
-    ft->file.pos = 0;
-    ft->file.eof = 0;
+    ft->eof = 0;
 
     return ST_SUCCESS;
 }
@@ -269,16 +261,12 @@
 
 int st_rawstopread(ft_t ft)
 {
-        free(ft->file.buf);
-
-        return ST_SUCCESS;
+  return ST_SUCCESS;
 }
 
 static void writeflush(ft_t ft)
 {
-        if (st_writebuf(ft, ft->file.buf, 1, ft->file.pos) != ft->file.pos)
-            ft->file.eof = ST_EOF;
-        ft->file.pos = 0;
+  ft->eof = ST_EOF;
 }
 
 
@@ -296,7 +284,6 @@
 int st_rawstopwrite(ft_t ft)
 {
         writeflush(ft);
-        free(ft->file.buf);
         return ST_SUCCESS;
 }
 
--- a/src/sox.c
+++ b/src/sox.c
@@ -1332,7 +1332,7 @@
                              &efftab[neffects-1].obuf[total],
                              efftab[neffects-1].olen-total);
 
-              if (len != efftab[neffects-1].olen-total || file_desc[file_count-1]->file.eof)
+              if (len != efftab[neffects-1].olen-total || file_desc[file_count-1]->eof)
               {
                   st_warn("Error writing: %s", file_desc[file_count-1]->st_errstr);
                   return ST_EOF;
--- a/src/st.h
+++ b/src/st.h
@@ -291,7 +291,7 @@
     char            *filetype;            /* type of file */
     char            *comment;             /* comment string */
     FILE            *fp;                  /* File stream pointer */
-    st_fileinfo_t   file;                 /* File data block */
+    unsigned char   eof;                  /* Marker that EOF has been reached */
     int             st_errno;             /* Failure error codes */
     char            st_errstr[256];       /* Extend Failure text */
     const st_format_t *h;                 /* format struct for this file */
--- a/src/vox.c
+++ b/src/vox.c
@@ -49,6 +49,7 @@
                           struct { uint8_t  byte;                       /* write store */
                                    uint8_t  flag;
                                  } store;
+                          st_fileinfo_t file;
                         } *vox_t;
 
 
@@ -84,21 +85,20 @@
 static int  st_voxstartread (ft_t ft) 
      { vox_t state = (vox_t) ft->priv;
 
-
        /* ... setup file info */
 
-       ft->file.buf = (char *)malloc(ST_BUFSIZ);
+       state->file.buf = (char *)malloc(ST_BUFSIZ);
     
-       if (!ft->file.buf)
+       if (!state->file.buf)
           { st_fail_errno (ft,ST_ENOMEM,"Unable to allocate internal buffer memory");
             
             return(ST_EOF);
           }
 
-       ft->file.size     = ST_BUFSIZ;
-       ft->file.count    = 0;
-       ft->file.pos      = 0;
-       ft->file.eof      = 0;
+       state->file.size     = ST_BUFSIZ;
+       state->file.count    = 0;
+       state->file.pos      = 0;
+       state->file.eof      = 0;
 
        ft->info.size     = ST_SIZE_WORD;
        ft->info.encoding = ST_ENCODING_OKI_ADPCM;
@@ -145,17 +145,17 @@
              while (count < N) 
                {     /* ... refill buffer */
 
-                     if (ft->file.pos >= ft->file.count)
-                        { ft->file.count = st_readbuf (ft,ft->file.buf,1,ft->file.size);
-                          ft->file.pos   = 0;
+                     if (state->file.pos >= state->file.count)
+                        { state->file.count = st_readbuf (ft,state->file.buf,1,state->file.size);
+                          state->file.pos   = 0;
 
-                          if (ft->file.count == 0)
+                          if (state->file.count == 0)
                              break;
                         }
 
                      /* ... decode two nybbles stored as a byte */
 
-                     byte      = ft->file.buf[ft->file.pos++];
+                     byte      = state->file.buf[state->file.pos++];
 
                      word      = devox ((uint8_t) ((byte >> 4) & 0x0F),state);
                      *buffer++ = ST_SIGNED_WORD_TO_SAMPLE (word * 16,);
@@ -179,7 +179,8 @@
  ******************************************************************************/
 
 static int  st_voxstopread (ft_t ft) 
-     { free (ft->file.buf);
+     { vox_t    state = (vox_t) ft->priv;
+       free (state->file.buf);
      
        return (ST_SUCCESS);
      }
@@ -204,18 +205,18 @@
 
        /* ... setup file info */
 
-       ft->file.buf = (char *)malloc(ST_BUFSIZ);
+       state->file.buf = (char *)malloc(ST_BUFSIZ);
     
-       if (!ft->file.buf)
+       if (!state->file.buf)
           { st_fail_errno (ft,ST_ENOMEM,"Unable to allocate internal buffer memory");
             
             return(ST_EOF);
           }
 
-       ft->file.size     = ST_BUFSIZ;
-       ft->file.count    = 0;
-       ft->file.pos      = 0;
-       ft->file.eof      = 0;
+       state->file.size     = ST_BUFSIZ;
+       state->file.count    = 0;
+       state->file.pos      = 0;
+       state->file.eof      = 0;
 
            ft->info.size     = ST_SIZE_WORD;
        ft->info.encoding = ST_ENCODING_OKI_ADPCM;
@@ -262,12 +263,12 @@
                      flag %= 2;
 
                      if (flag == 0)
-                        { ft->file.buf[ft->file.count++] = byte;
+                        { state->file.buf[state->file.count++] = byte;
 
-                          if (ft->file.count >= ft->file.size)
-                             { st_writebuf (ft,ft->file.buf,1,ft->file.count);
+                          if (state->file.count >= state->file.size)
+                             { st_writebuf (ft,state->file.buf,1,state->file.count);
 
-                               ft->file.count = 0;
+                               state->file.count = 0;
                              }
                         }
 
@@ -303,13 +304,13 @@
           { byte <<= 4;
             byte  |= envox (0,state) & 0x0F;
 
-            ft->file.buf[ft->file.count++] = byte;
+            state->file.buf[state->file.count++] = byte;
           }
 
-       if (ft->file.count > 0)
-          st_writebuf (ft,ft->file.buf,1,ft->file.count);
+       if (state->file.count > 0)
+          st_writebuf (ft,state->file.buf,1,state->file.count);
 
-       free (ft->file.buf);
+       free (state->file.buf);
      
        return (ST_SUCCESS);
      }