shithub: sox

Download patch

ref: 19144759ff0bc03f470689c61a15fac88caacd7d
parent: 8e67c9bc9ce4ade4ba27ff4f0bdc9594322373af
author: rrt <rrt>
date: Mon Dec 11 10:37:30 EST 2006

First cut at removing buffering. This update removes it on the read
side only. Warning: I've quite possibly broken some things, but there
is now just one reading macro that is instantiated for all the various
sizes, so it should be easy to fix.

Tested for some basic conversions, and ALSA playback (which needed
some work as it relied on the old behaviour of the buffering
functions, so I've inlined them in alsa.c instead).

The conversion macros are now a bit more regular: many more take a
clip count argument, even when they don't use it.

--- a/src/8svx.c
+++ b/src/8svx.c
@@ -225,7 +225,7 @@
                         if (feof(p->ch[i]))
                                 return done;
                         /* scale signed up to long's range */
-                        *buf++ = ST_SIGNED_BYTE_TO_SAMPLE(datum);
+                        *buf++ = ST_SIGNED_BYTE_TO_SAMPLE(datum,);
                 }
                 done += ft->info.channels;
         }
--- a/src/alsa.c
+++ b/src/alsa.c
@@ -199,7 +199,7 @@
     dir = 0;
     if (snd_pcm_hw_params_set_buffer_size_near(alsa->pcm_handle, hw_params, 
                                                &buffer_size) < 0) {
-        st_fail_errno(ft, ST_EPERM, "Error setting buffersize.");
+        st_fail_errno(ft, ST_EPERM, "Error setting buffer size.");
         goto open_error;
     }
 
@@ -206,7 +206,7 @@
     snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
     if (period_size*2 > buffer_size)
     {
-        st_fail_errno(ft, ST_EPERM, "Buffer to small. Could not use.");
+        st_fail_errno(ft, ST_EPERM, "Buffer too small. Could not use.");
         goto open_error;
     }
 
@@ -290,6 +290,44 @@
     return st_alsasetup(ft, SND_PCM_STREAM_CAPTURE);
 }
 
+static void st_ub_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
+{
+    while (len--)
+        *buf1++ = ST_UNSIGNED_BYTE_TO_SAMPLE(*((unsigned char *)buf2++),);
+}
+
+static void st_sb_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
+{
+    while (len--)
+        *buf1++ = ST_SIGNED_BYTE_TO_SAMPLE(*((int8_t *)buf2++),);
+}
+
+static void st_uw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap, st_size_t * clippedCount UNUSED)
+{
+    while (len--)
+    {
+        uint16_t datum = *((uint16_t *)buf2);
+        buf2++; buf2++;
+        if (swap)
+            datum = st_swapw(datum);
+
+        *buf1++ = ST_UNSIGNED_WORD_TO_SAMPLE(datum,);
+    }
+}
+
+static void st_sw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap, st_size_t * clippedCount UNUSED)
+{
+    while (len--)
+    {
+        int16_t datum = *((int16_t *)buf2);
+        buf2++; buf2++;
+        if (swap)
+            datum = st_swapw(datum);
+
+        *buf1++ = ST_SIGNED_WORD_TO_SAMPLE(datum,);
+    }
+}
+
 static st_size_t st_alsaread(ft_t ft, st_sample_t *buf, st_size_t nsamp)
 {
     st_size_t len;
@@ -341,8 +379,6 @@
         /* ALSA library takes "frame" counts. */
         err = snd_pcm_readi(alsa->pcm_handle, alsa->buf, 
                             (nsamp-len)/ft->info.channels);
-        if (err == -EAGAIN)
-            continue;
         if (err < 0)
         {
             if (xrun_recovery(alsa->pcm_handle, err) < 0)
@@ -377,16 +413,51 @@
     return st_alsasetup(ft, SND_PCM_STREAM_PLAYBACK);
 }
 
+static void st_ub_write_buf(char* buf1, st_sample_t const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount)
+{
+    while (len--)
+        *(uint8_t *)buf1++ = ST_SAMPLE_TO_UNSIGNED_BYTE(*buf2++, *clippedCount);
+}
+
+static void st_sb_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount)
+{
+    while (len--)
+        *(int8_t *)buf1++ = ST_SAMPLE_TO_SIGNED_BYTE(*buf2++, *clippedCount);
+}
+
+static void st_uw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap, st_size_t * clippedCount)
+{
+    while (len--)
+    {
+        uint16_t datum = ST_SAMPLE_TO_UNSIGNED_WORD(*buf2++, *clippedCount);
+        if (swap)
+            datum = st_swapw(datum);
+        *(uint16_t *)buf1 = datum;
+        buf1++; buf1++;
+    }
+}
+
+static void st_sw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap, st_size_t * clippedCount)
+{
+    while (len--)
+    {
+        int16_t datum = ST_SAMPLE_TO_SIGNED_WORD(*buf2++, *clippedCount);
+        if (swap)
+            datum = st_swapw(datum);
+        *(int16_t *)buf1 = datum;
+        buf1++; buf1++;
+    }
+}
+
 static st_size_t st_alsawrite(ft_t ft, const st_sample_t *buf, st_size_t nsamp)
 {
-    st_size_t len;
-    int err;
+    st_size_t osamp, done;
     alsa_priv_t alsa = (alsa_priv_t)ft->priv;
     void (*write_buf)(char *, const st_sample_t *, st_size_t, char, st_size_t *) = 0;
 
     switch(ft->info.size) {
         case ST_SIZE_BYTE:
-            switch(ft->info.encoding)
+            switch (ft->info.encoding)
             {
                 case ST_ENCODING_SIGN2:
                     write_buf = st_sb_write_buf;
@@ -395,12 +466,12 @@
                     write_buf = st_ub_write_buf;
                     break;
                 default:
-                    st_fail_errno(ft,ST_EFMT,"Do not support this encoding for this data size");
-                    return ST_EOF;
+                    st_fail_errno(ft,ST_EFMT,"this encoding is not supported for this data size");
+                    return 0;
             }
             break;
         case ST_SIZE_WORD:
-            switch(ft->info.encoding)
+            switch (ft->info.encoding)
             {
                 case ST_ENCODING_SIGN2:
                     write_buf = st_sw_write_buf;
@@ -409,42 +480,36 @@
                     write_buf = st_uw_write_buf;
                     break;
                 default:
-                    st_fail_errno(ft,ST_EFMT,"Do not support this encoding for this data size");
-                    return ST_EOF;
+                    st_fail_errno(ft,ST_EFMT,"this encoding is not supported for this data size");
+                    return 0;
             }
             break;
         default:
-            st_fail_errno(ft,ST_EFMT,"Do not support this data size for this handler");
-            return ST_EOF;
+            st_fail_errno(ft,ST_EFMT,"this data size is not supported by this handler");
+            return 0;
     }
 
-    /* Prevent overflow */
-    if (nsamp > alsa->buf_size/ft->info.size)
-        nsamp = (alsa->buf_size/ft->info.size);
-    len = 0;
+    for (done = 0; done < nsamp; done += osamp) {
+      int err;
+      st_size_t len;
+      
+      osamp = min(nsamp - done, alsa->buf_size / ft->info.size);
+      write_buf(alsa->buf, buf, osamp, ft->swap, &ft->clippedCount);
+      buf += osamp;
 
-    write_buf(alsa->buf, buf, nsamp, ft->swap, &ft->clippedCount);
-
-    while (len < nsamp)
-    {
+      for (len = 0; len < osamp;) {
         err = snd_pcm_writei(alsa->pcm_handle, 
-                             alsa->buf+(len*ft->info.size), 
-                             (nsamp-len)/ft->info.channels);
-        if (err == -EAGAIN)
-            continue;
-        if (err < 0)
-        {
-            if (xrun_recovery(alsa->pcm_handle, err) < 0)
-            {
-                st_fail_errno(ft, ST_EPERM, "ALSA write error");
-                return ST_EOF;
-            }
-        }
-        else
-            len += err * ft->info.channels;
+                             alsa->buf + (len * ft->info.size), 
+                             (osamp - len) / ft->info.channels);
+        if (err < 0 && xrun_recovery(alsa->pcm_handle, err) < 0) {
+          st_fail_errno(ft, ST_EPERM, "ALSA write error");
+          return 0;
+        } else
+          len += err * ft->info.channels;
+      }
     }
 
-    return len;
+    return nsamp;
 }
 
 
--- a/src/au.c
+++ b/src/au.c
@@ -355,7 +355,7 @@
         while (samp > 0 && unpack_input(ft, &code) >= 0) {
                 *buf++ = ST_SIGNED_WORD_TO_SAMPLE(
                         (*p->dec_routine)(code, AUDIO_ENCODING_LINEAR,
-                                          &p->state));
+                                          &p->state),);
                 samp--;
                 done++;
         }
--- a/src/flac.c
+++ b/src/flac.c
@@ -199,10 +199,10 @@
         FLAC__int32 d = decoder->decoded_wide_samples[channel][decoder->wide_sample_number];
         switch (decoder->bits_per_sample)
         {
-          case  8: *sampleBuffer++ = ST_SIGNED_BYTE_TO_SAMPLE(d); break;
-          case 16: *sampleBuffer++ = ST_SIGNED_WORD_TO_SAMPLE(d); break;
-          case 24: *sampleBuffer++ = ST_SIGNED_24BIT_TO_SAMPLE(d); break;
-          case 32: *sampleBuffer++ = ST_SIGNED_DWORD_TO_SAMPLE(d); break;
+          case  8: *sampleBuffer++ = ST_SIGNED_BYTE_TO_SAMPLE(d,); break;
+          case 16: *sampleBuffer++ = ST_SIGNED_WORD_TO_SAMPLE(d,); break;
+          case 24: *sampleBuffer++ = ST_SIGNED_24BIT_TO_SAMPLE(d,); break;
+          case 32: *sampleBuffer++ = ST_SIGNED_DWORD_TO_SAMPLE(d,); break;
         }
         ++actual;
       }
--- a/src/gsm.c
+++ b/src/gsm.c
@@ -118,7 +118,7 @@
         {
                 while (p->samplePtr < p->sampleTop && done < samp)
                         buf[done++] = 
-                            ST_SIGNED_WORD_TO_SAMPLE(*(p->samplePtr)++);
+                            ST_SIGNED_WORD_TO_SAMPLE(*(p->samplePtr)++,);
 
                 if (done>=samp) break;
 
--- a/src/hcom.c
+++ b/src/hcom.c
@@ -190,7 +190,7 @@
                         return (0);
                 }
                 p->sample = sample_rate;
-                *buf++ = ST_UNSIGNED_BYTE_TO_SAMPLE(p->sample);
+                *buf++ = ST_UNSIGNED_BYTE_TO_SAMPLE(p->sample,);
                 p->huffcount--;
                 p->nrbits = 0;
                 done++;
@@ -226,7 +226,7 @@
                                 p->sample = 0;
                         p->sample = (p->sample + datum) & 0xff;
                         p->huffcount--;
-                        *buf++ = ST_UNSIGNED_BYTE_TO_SAMPLE(p->sample);
+                        *buf++ = ST_UNSIGNED_BYTE_TO_SAMPLE(p->sample,);
                         p->dictentry = 0;
                         done++;
                         len--;
--- a/src/misc.c
+++ b/src/misc.c
@@ -183,7 +183,7 @@
 {
         if (st_readbuf(ft, ub, 1, 1) != 1)
         {
-                st_fail_errno(ft,errno,readerr);
+            st_fail_errno(ft,errno,readerr);
             return(ST_EOF);
         }
         return ST_SUCCESS;
@@ -205,7 +205,7 @@
 {
         if (st_readbuf(ft, uw, 2, 1) != 1)
         {
-                st_fail_errno(ft,errno,readerr);
+            st_fail_errno(ft,errno,readerr);
             return (ST_EOF);
         }
         if (ft->swap)
@@ -226,12 +226,38 @@
         return(ST_SUCCESS);
 }
 
+/* Read three bytes. */
+int st_read3(ft_t ft, uint24_t *u3)
+{
+        if (st_readbuf(ft, u3, 3, 1) != 1)
+        {
+            st_fail_errno(ft,errno,readerr);
+            return (ST_EOF);
+        }
+        if (ft->swap)
+                *u3 = st_swap24(*u3);
+        return ST_SUCCESS;
+}
+
+/* Write three bytes. */
+int st_write3(ft_t ft, uint24_t u3)
+{
+        if (ft->swap)
+                u3 = st_swap24(u3);
+        if (st_writebuf(ft, &u3, 2, 1) != 1)
+        {
+                st_fail_errno(ft,errno,writerr);
+                return (ST_EOF);
+        }
+        return(ST_SUCCESS);
+}
+
 /* Read double word. */
 int st_readdw(ft_t ft, uint32_t *udw)
 {
         if (st_readbuf(ft, udw, 4, 1) != 1)
         {
-                st_fail_errno(ft,errno,readerr);
+            st_fail_errno(ft,errno,readerr);
             return (ST_EOF);
         }
         if (ft->swap)
@@ -257,6 +283,7 @@
 {
         if (st_readbuf(ft, f, sizeof(float), 1) != 1)
         {
+            st_fail_errno(ft,errno,readerr);
             return(ST_EOF);
         }
         if (ft->swap)
@@ -284,6 +311,7 @@
 {
         if (st_readbuf(ft, d, sizeof(double), 1) != 1)
         {
+            st_fail_errno(ft,errno,readerr);
             return(ST_EOF);
         }
         if (ft->swap)
@@ -313,20 +341,10 @@
         f[i]= l[n-i-1];
 }
 
-
-/* Byte swappers, use libc optimized macro's if possible */
+/* Byte swappers, use optimized macros if available */
 #ifndef HAVE_BYTESWAP_H
-
-uint16_t st_swapw(uint16_t uw)
-{
-    return ((uw >> 8) | (uw << 8)) & 0xffff;
-}
-
-uint32_t st_swapdw(uint32_t udw)
-{
-    return (udw >> 24) | ((udw >> 8) & 0xff00) | ((udw << 8) & 0xff0000L) | (udw << 24);
-}
-
+#define st_swapw(uw) (((uw >> 8) | (uw << 8)) & 0xffff)
+#define st_swapdw(udw) ((udw >> 24) | ((udw >> 8) & 0xff00) | ((udw << 8) & 0xff0000L) | (udw << 24))
 #endif
 
 /* return swapped 32-bit float */
--- a/src/raw.c
+++ b/src/raw.c
@@ -2,9 +2,7 @@
  * Sound Tools raw format file.
  *
  * Includes .ub, .uw, .sb, .sw, and .ul formats at end
- */
-
-/*
+ *
  * July 5, 1991
  * Copyright 1991 Lance Norskog And Sundry Contributors
  * This source code is freely redistributable and may be used for
@@ -54,16 +52,16 @@
   0x3F, 0xBF, 0x7F, 0xFF
 };
 
-#define ST_ULAW_BYTE_TO_SAMPLE(d)   ST_SIGNED_WORD_TO_SAMPLE(st_ulaw2linear16(d))
-#define ST_ALAW_BYTE_TO_SAMPLE(d)   ST_SIGNED_WORD_TO_SAMPLE(st_alaw2linear16(d))
+#define ST_ULAW_BYTE_TO_SAMPLE(d,clips)   ST_SIGNED_WORD_TO_SAMPLE(st_ulaw2linear16(d),clips)
+#define ST_ALAW_BYTE_TO_SAMPLE(d,clips)   ST_SIGNED_WORD_TO_SAMPLE(st_alaw2linear16(d),clips)
 #define ST_SAMPLE_TO_ULAW_BYTE(d,c) st_14linear2ulaw(ST_SAMPLE_TO_SIGNED_WORD(d,c) >> 2)
 #define ST_SAMPLE_TO_ALAW_BYTE(d,c) st_13linear2alaw(ST_SAMPLE_TO_SIGNED_WORD(d,c) >> 3)
 
 /* Some hardware sends MSB last. These account for that */
-#define ST_INVERT_ULAW_BYTE_TO_SAMPLE(d) \
-    ST_SIGNED_WORD_TO_SAMPLE(st_ulaw2linear16(cswap[d]))
-#define ST_INVERT_ALAW_BYTE_TO_SAMPLE(d) \
-    ST_SIGNED_WORD_TO_SAMPLE(st_alaw2linear16(cswap[d]))
+#define ST_INVERT_ULAW_BYTE_TO_SAMPLE(d,clips) \
+    ST_SIGNED_WORD_TO_SAMPLE(st_ulaw2linear16(cswap[d]),clips)
+#define ST_INVERT_ALAW_BYTE_TO_SAMPLE(d,clips) \
+    ST_SIGNED_WORD_TO_SAMPLE(st_alaw2linear16(cswap[d]),clips)
 #define ST_SAMPLE_TO_INVERT_ULAW_BYTE(d,c) \
     cswap[st_14linear2ulaw(ST_SAMPLE_TO_SIGNED_WORD(d,c) >> 2)]
 #define ST_SAMPLE_TO_INVERT_ALAW_BYTE(d,c) \
@@ -105,15 +103,6 @@
 
 int st_rawstartread(ft_t ft)
 {
-    ft->file.buf = (char *)malloc(ST_BUFSIZ);
-    if (!ft->file.buf)
-    {
-        st_fail_errno(ft,ST_ENOMEM,"Unable to alloc resources");
-        return ST_EOF;
-    }
-    ft->file.size = ST_BUFSIZ;
-    ft->file.count = 0;
-    ft->file.pos = 0;
     ft->file.eof = 0;
 
     return ST_SUCCESS;
@@ -124,7 +113,7 @@
     ft->file.buf = (char *)malloc(ST_BUFSIZ);
     if (!ft->file.buf)
     {
-        st_fail_errno(ft,ST_ENOMEM,"Unable to alloc resources");
+        st_fail_errno(ft, ST_ENOMEM, "Unable to allocate memory");
         return ST_EOF;
     }
     ft->file.size = ST_BUFSIZ;
@@ -134,383 +123,132 @@
     return ST_SUCCESS;
 }
 
-void st_ub_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        uint8_t datum;
-
-        datum = *((unsigned char *)buf2);
-        buf2++;
-
-        *buf1++ = ST_UNSIGNED_BYTE_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-void st_sb_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        int8_t datum;
-
-        datum = *((int8_t *)buf2);
-        buf2++;
-
-        *buf1++ = ST_SIGNED_BYTE_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-static void st_ulaw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        uint8_t datum;
-
-        datum = *((uint8_t *)buf2);
-        buf2++;
-
-        *buf1++ = ST_ULAW_BYTE_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-static void st_alaw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        uint8_t datum;
-
-        datum = *((uint8_t *)buf2);
-        buf2++;
-
-        *buf1++ = ST_ALAW_BYTE_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-static void st_inv_ulaw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        uint8_t datum;
-
-        datum = *((uint8_t *)buf2);
-        buf2++;
-
-        *buf1++ = ST_INVERT_ULAW_BYTE_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-static void st_inv_alaw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        uint8_t datum;
-
-        datum = *((uint8_t *)buf2);
-        buf2++;
-
-        *buf1++ = ST_INVERT_ALAW_BYTE_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-
-void st_uw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        uint16_t datum;
-
-        datum = *((uint16_t *)buf2);
-        buf2++; buf2++;
-        if (swap)
-            datum = st_swapw(datum);
-
-        *buf1++ = ST_UNSIGNED_WORD_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-void st_sw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        int16_t datum;
-
-        datum = *((int16_t *)buf2);
-        buf2++; buf2++;
-        if (swap)
-            datum = st_swapw(datum);
-
-        *buf1++ = ST_SIGNED_WORD_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-
-
-static int24_t st_24_read_one(char const * * buffer, char swap)
-{
-  /* N.B. overreads an extra byte; however SoX buffers are 2^n bytes long and
-   * since 2^n != 0 (mod 3), there will always be an extra byte to read from. */
-  int24_t datum  = *(int24_t const *)*buffer;
-
-  *buffer += 3;
-
-  if (ST_IS_BIGENDIAN)
-  {
-    datum >>= 8;
+#define READ_FUNC(size, sign, ctype, uctype, cast) \
+  static st_size_t st_ ## sign ## size ## _read_buf(st_sample_t *buf1, ft_t ft, st_size_t len, st_size_t *clippedCount UNUSED) \
+  { \
+    st_size_t n; \
+    for (n = 0; n < len; n++) { \
+      ctype datum; \
+      int ret = st_read ## size(ft, (uctype *)&datum); \
+      if (ret != ST_SUCCESS) \
+        break; \
+      *buf1++ = ST_ ## cast ## _TO_SAMPLE(datum, *clippedCount); \
+    } \
+    return n; \
   }
 
-  return swap ? (int24_t)(st_swap24(datum)) : datum;
-}
+READ_FUNC(b, u, uint8_t, uint8_t, UNSIGNED_BYTE)
+READ_FUNC(b, s, int8_t, uint8_t, SIGNED_BYTE)
+READ_FUNC(b, ulaw, uint8_t, uint8_t, ULAW_BYTE)
+READ_FUNC(b, alaw, uint8_t, uint8_t, ALAW_BYTE)
+READ_FUNC(b, inv_ulaw, uint8_t, uint8_t, INVERT_ULAW_BYTE)
+READ_FUNC(b, inv_alaw, uint8_t, uint8_t, INVERT_ALAW_BYTE)
+READ_FUNC(w, u, uint16_t, uint16_t, UNSIGNED_WORD)
+READ_FUNC(w, s, int16_t, uint16_t, SIGNED_WORD)
+READ_FUNC(3, u, uint24_t, uint24_t, UNSIGNED_24BIT)
+READ_FUNC(3, s, int24_t, uint24_t, SIGNED_24BIT)
+READ_FUNC(dw, u, uint32_t, uint32_t, UNSIGNED_DWORD)
+READ_FUNC(dw, , int32_t, uint32_t, SIGNED_DWORD)
+READ_FUNC(f, , float, float, FLOAT_DWORD)
+READ_FUNC(df, , double, double, FLOAT_DDWORD)
 
-
-
-static void st_u24_read_buf(st_sample_t * buf1, char const * buf2, st_size_t len, char const swap, st_size_t * clippedCount UNUSED)
-{
-  while (len--)
-  {
-    int24_t datum = st_24_read_one(&buf2, swap);
-    *buf1++ = ST_UNSIGNED_24BIT_TO_SAMPLE(datum);
-  }
-}
-
-
-
-static void st_s24_read_buf(st_sample_t * buf1, char const * buf2, st_size_t len, char const swap, st_size_t * clippedCount UNUSED)
-{
-  while (len--)
-  {
-    int24_t datum = st_24_read_one(&buf2, swap);
-    *buf1++ = ST_SIGNED_24BIT_TO_SAMPLE(datum);
-  }
-}
-
-
-
-static void st_udw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        uint32_t datum;
-
-        datum = *((uint32_t *)buf2);
-        buf2++; buf2++; buf2++; buf2++;
-        if (swap)
-            datum = st_swapdw(datum);
-
-        *buf1++ = ST_UNSIGNED_DWORD_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-static void st_dw_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap, st_size_t * clippedCount UNUSED)
-{
-    while (len)
-    {
-        int32_t datum;
-
-        datum = *((int32_t *)buf2);
-        buf2++; buf2++; buf2++; buf2++;
-        if (swap)
-            datum = st_swapdw(datum);
-
-        *buf1++ = ST_SIGNED_DWORD_TO_SAMPLE(datum);
-        len--;
-    }
-}
-
-static void st_f32_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap, st_size_t * clippedCount)
-{
-    while (len)
-    {
-        float datum;
-
-        datum = *((float *)buf2);
-        buf2++; buf2++; buf2++; buf2++;
-        if (swap)
-            datum = st_swapf(datum);
-
-        *buf1++ = ST_FLOAT_DWORD_TO_SAMPLE(datum, *clippedCount);
-        len--;
-    }
-}
-
-static void st_f64_read_buf(st_sample_t *buf1, char const * buf2, st_size_t len, char swap, st_size_t * clippedCount)
-{
-    while (len)
-    {
-        double datum;
-
-        datum = *((double *)buf2);
-        buf2++; buf2++; buf2++; buf2++;
-        buf2++; buf2++; buf2++; buf2++;
-        if (swap)
-            datum = st_swapd(datum);
-
-        *buf1++ = ST_FLOAT_DDWORD_TO_SAMPLE(datum, *clippedCount);
-        len--;
-    }
-}
-
-/* Reads a buffer of different data types into SoX's internal buffer
- * format.
- */
-/* FIXME:  This function adds buffering on top of stdio's buffering.
- * Mixing st_rawreads's and freads or fgetc or even SoX's other util
- * functions will cause a loss of data!  Need to have sox implement
- * a consistent buffering protocol.
- */
+/* Read a stream of some type into SoX's internal buffer format. */
 st_size_t st_rawread(ft_t ft, st_sample_t *buf, st_size_t nsamp)
 {
-    st_size_t len, done = 0;
-    void (*read_buf)(st_sample_t *, char const *, st_size_t, char, st_size_t *) = 0;
-    size_t i;
+    st_size_t done = 0;
+    st_size_t (*read_buf)(st_sample_t *, ft_t ft, st_size_t, st_size_t *) = NULL;
 
-    switch(ft->info.size) {
-        case ST_SIZE_BYTE:
-            switch(ft->info.encoding)
-            {
-                case ST_ENCODING_SIGN2:
-                    read_buf = st_sb_read_buf;
-                    break;
-                case ST_ENCODING_UNSIGNED:
-                    read_buf = st_ub_read_buf;
-                    break;
-                case ST_ENCODING_ULAW:
-                    read_buf = st_ulaw_read_buf;
-                    break;
-                case ST_ENCODING_ALAW:
-                    read_buf = st_alaw_read_buf;
-                    break;
-                case ST_ENCODING_INV_ULAW:
-                    read_buf = st_inv_ulaw_read_buf;
-                    break;
-                case ST_ENCODING_INV_ALAW:
-                    read_buf = st_inv_alaw_read_buf;
-                    break;
-                default:
-                    st_fail_errno(ft,ST_EFMT,"Do not support this encoding for this data size");
-                    return ST_EOF;
-            }
-            break;
+    switch (ft->info.size) {
+    case ST_SIZE_BYTE:
+      switch(ft->info.encoding) {
+      case ST_ENCODING_SIGN2:
+        read_buf = st_sb_read_buf;
+        break;
+      case ST_ENCODING_UNSIGNED:
+        read_buf = st_ub_read_buf;
+        break;
+      case ST_ENCODING_ULAW:
+        read_buf = st_ulawb_read_buf;
+        break;
+      case ST_ENCODING_ALAW:
+        read_buf = st_alawb_read_buf;
+        break;
+      case ST_ENCODING_INV_ULAW:
+        read_buf = st_inv_ulawb_read_buf;
+        break;
+      case ST_ENCODING_INV_ALAW:
+        read_buf = st_inv_alawb_read_buf;
+        break;
+      default:
+        break;
+      }
+      break;
+      
+    case ST_SIZE_WORD: 
+      switch(ft->info.encoding) {
+      case ST_ENCODING_SIGN2:
+        read_buf = st_sw_read_buf;
+        break;
+      case ST_ENCODING_UNSIGNED:
+        read_buf = st_uw_read_buf;
+        break;
+      default:
+        break;
+      }
+      break;
 
-        case ST_SIZE_WORD:
-            switch(ft->info.encoding)
-            {
-                case ST_ENCODING_SIGN2:
-                    read_buf = st_sw_read_buf;
-                    break;
-                case ST_ENCODING_UNSIGNED:
-                    read_buf = st_uw_read_buf;
-                    break;
-                default:
-                    st_fail_errno(ft,ST_EFMT,"Do not support this encoding for this data size");
-                    return ST_EOF;
-            }
-            break;
+    case ST_SIZE_24BIT:
+      switch(ft->info.encoding) {
+      case ST_ENCODING_SIGN2:
+        read_buf = st_s3_read_buf;
+        break;
+      case ST_ENCODING_UNSIGNED:
+        read_buf = st_u3_read_buf;
+        break;
+      default:
+        break;
+      }
+      break;
+      
+    case ST_SIZE_DWORD:
+      switch(ft->info.encoding) {
+      case ST_ENCODING_SIGN2:
+        read_buf = st_dw_read_buf;
+        break;
+      case ST_ENCODING_UNSIGNED:
+        read_buf = st_udw_read_buf;
+        break;
+      case ST_ENCODING_FLOAT:
+        read_buf = st_f_read_buf;
+        break;
+      default:
+        break;
+      }
+      break;
+      
+    case ST_SIZE_DDWORD:
+      switch(ft->info.encoding) {
+      case ST_ENCODING_FLOAT:
+        read_buf = st_df_read_buf;
+        break;
+      default:
+        break;
+      }
+      break;
 
-        case ST_SIZE_24BIT:
-            switch(ft->info.encoding)
-            {
-                case ST_ENCODING_SIGN2:
-                    read_buf = st_s24_read_buf;
-                    break;
-                case ST_ENCODING_UNSIGNED:
-                    read_buf = st_u24_read_buf;
-                    break;
-                default:
-                    st_fail_errno(ft,ST_EFMT,"Do not support this encoding for this data size");
-                    return ST_EOF;
-            }
-            break;
-
-        case ST_SIZE_DWORD:
-            switch(ft->info.encoding)
-            {
-                case ST_ENCODING_SIGN2:
-                    read_buf = st_dw_read_buf;
-                    break;
-                case ST_ENCODING_UNSIGNED:
-                    read_buf = st_udw_read_buf;
-                    break;
-                case ST_ENCODING_FLOAT:
-                    read_buf = st_f32_read_buf;
-                    break;
-                default:
-                    st_fail_errno(ft,ST_EFMT,"Do not support this encoding for this data size");
-                    return ST_EOF;
-            }
-            break;
-
-        case ST_SIZE_DDWORD:
-            switch(ft->info.encoding)
-            {
-                case ST_ENCODING_FLOAT:
-                    read_buf = st_f64_read_buf;
-                    break;
-                default:
-                    st_fail_errno(ft,ST_EFMT,"Do not support this encoding for this data size");
-            }
-            break;
-
-        default:
-            st_fail_errno(ft,ST_EFMT,"Do not support this data size for this handler");
-            return ST_EOF;
+    default:
+      st_fail_errno(ft,ST_EFMT,"this handler does not support this data size");
+      return ST_EOF;
     }
 
-
-    len = min((st_size_t)nsamp,(ft->file.count-ft->file.pos)/ft->info.size);
-    if (len)
-    {
-        read_buf(buf + done, ft->file.buf + ft->file.pos, len, ft->swap, &ft->clippedCount);
-        ft->file.pos += (len*ft->info.size);
-        done += len;
+    if (read_buf == NULL) {
+        st_fail_errno(ft,ST_EFMT,"this encoding is not supported for this data size");
+        return ST_EOF;
     }
+    
+    if (nsamp)
+        done += read_buf(buf + done, ft, nsamp, &ft->clippedCount);
 
-    while (done < nsamp)
-    {
-        /* See if there is not enough data in buffer for any more reads
-         * or if there is no data in the buffer at all.
-         * If not then shift any remaining data down to the beginning
-         * and attempt to fill up the rest of the buffer.
-         */
-        if (!ft->file.eof && (ft->file.count == 0 ||
-                              ft->file.pos >= (ft->file.count-ft->info.size+1)))
-        {
-            for (i = 0; i < (ft->file.count-ft->file.pos); i++)
-                ft->file.buf[i] = ft->file.buf[ft->file.pos+i];
-
-            i = ft->file.count-ft->file.pos;
-            ft->file.pos = 0;
-
-            ft->file.count = st_readbuf(ft, ft->file.buf+i, 1, ft->file.size-i);
-            if (ft->file.count != ft->file.size-i || ft->file.count == 0)
-            {
-                ft->file.eof = 1;
-            }
-            ft->file.count += i;
-        }
-
-        len = min((st_size_t)nsamp - done,(ft->file.count-ft->file.pos)/ft->info.size);
-        if (len)
-        {
-            read_buf(buf + done, ft->file.buf + ft->file.pos, len, ft->swap, &ft->clippedCount);
-            ft->file.pos += (len*ft->info.size);
-            done += len;
-        }
-        if (ft->file.eof)
-            break;
-    }
-
-    if (done == 0 && ft->file.eof)
-        return ST_EOF;
-
     return done;
 }
 
@@ -521,7 +259,7 @@
         return ST_SUCCESS;
 }
 
-void st_ub_write_buf(char* buf1, st_sample_t const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount)
+static void st_ub_write_buf(char* buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -530,7 +268,7 @@
     }
 }
 
-void st_sb_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount)
+static void st_sb_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -539,8 +277,7 @@
     }
 }
 
-static void st_ulaw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len,
-                       char swap UNUSED, st_size_t * clippedCount)
+static void st_ulaw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -549,8 +286,7 @@
     }
 }
 
-static void st_alaw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len,
-                       char swap UNUSED, st_size_t * clippedCount)
+static void st_alaw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -559,8 +295,7 @@
     }
 }
 
-static void st_inv_ulaw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len,
-                           char swap UNUSED, st_size_t * clippedCount)
+static void st_inv_ulaw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -569,8 +304,7 @@
     }
 }
 
-static void st_inv_alaw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len,
-                           char swap UNUSED, st_size_t * clippedCount)
+static void st_inv_alaw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -579,7 +313,7 @@
     }
 }
 
-void st_uw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap, st_size_t * clippedCount)
+static void st_uw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -586,8 +320,6 @@
         uint16_t datum;
 
         datum = ST_SAMPLE_TO_UNSIGNED_WORD(*buf2++, *clippedCount);
-        if (swap)
-            datum = st_swapw(datum);
         *(uint16_t *)buf1 = datum;
         buf1++; buf1++;
 
@@ -595,7 +327,7 @@
     }
 }
 
-void st_sw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap, st_size_t * clippedCount)
+static void st_sw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -602,8 +334,6 @@
         int16_t datum;
 
         datum = ST_SAMPLE_TO_SIGNED_WORD(*buf2++, *clippedCount);
-        if (swap)
-            datum = st_swapw(datum);
         *(int16_t *)buf1 = datum;
         buf1++; buf1++;
 
@@ -613,16 +343,10 @@
 
 
 
-static void st_24_write_one(char * * const buf1, int24_t datum, char const swap)
+static void st_24_write_one(char * * const buf1, int24_t datum)
 {
-  if (swap)
-  {
-    datum = st_swap24(datum);
-  }
   if (ST_IS_BIGENDIAN)
-  {
     datum <<= 8;
-  }
 
   /* N.B. overwrites an extra byte; however SoX buffers are 2^n bytes long and
    * since 2^n != 0 (mod 3), there will always be an extra byte to write to. */
@@ -633,29 +357,29 @@
 
 
 
-static void st_u24_write_buf(char * buf1, st_sample_t const * buf2, st_size_t len, char const swap, st_size_t * clippedCount)
+static void st_u24_write_buf(char * buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
   while (len--)
   {
     int24_t datum = ST_SAMPLE_TO_UNSIGNED_24BIT(*buf2++, *clippedCount);
-    st_24_write_one(&buf1, datum, swap);
+    st_24_write_one(&buf1, datum);
   }
 }
 
 
 
-static void st_s24_write_buf(char * buf1, st_sample_t const * buf2, st_size_t len, char const swap, st_size_t * clippedCount)
+static void st_s24_write_buf(char * buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
   while (len--)
   {
     int24_t datum = ST_SAMPLE_TO_SIGNED_24BIT(*buf2++, *clippedCount);
-    st_24_write_one(&buf1, datum, swap);
+    st_24_write_one(&buf1, datum);
   }
 }
 
 
 
-static void st_udw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap, st_size_t * clippedCount UNUSED)
+static void st_udw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount UNUSED)
 {
     while (len)
     {
@@ -662,8 +386,6 @@
         uint32_t datum;
 
         datum = ST_SAMPLE_TO_UNSIGNED_DWORD(*buf2++);
-        if (swap)
-            datum = st_swapdw(datum);
         *(uint32_t *)buf1 = datum;
         buf1++; buf1++; buf1++; buf1++;
 
@@ -671,7 +393,7 @@
     }
 }
 
-static void st_dw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap UNUSED, st_size_t * clippedCount UNUSED)
+static void st_dw_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount UNUSED)
 {
     while (len)
     {
@@ -678,8 +400,6 @@
         int32_t datum;
 
         datum = ST_SAMPLE_TO_SIGNED_DWORD(*buf2++);
-        if (swap)
-            datum = st_swapdw(datum);
         *(int32_t *)buf1 = datum;
         buf1++; buf1++; buf1++; buf1++;
 
@@ -687,7 +407,7 @@
     }
 }
 
-static void st_f32_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap, st_size_t * clippedCount)
+static void st_f32_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -694,8 +414,6 @@
         float datum;
 
         datum = ST_SAMPLE_TO_FLOAT_DWORD(*buf2++, *clippedCount);
-        if (swap)
-            datum = st_swapf(datum);
         *(float *)buf1 = datum;
         buf1++; buf1++; buf1++; buf1++;
 
@@ -703,7 +421,7 @@
     }
 }
 
-static void st_f64_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, char swap, st_size_t * clippedCount)
+static void st_f64_write_buf(char *buf1, st_sample_t const * buf2, st_size_t len, st_size_t * clippedCount)
 {
     while (len)
     {
@@ -710,8 +428,6 @@
         double datum;
 
         datum = ST_SAMPLE_TO_FLOAT_DDWORD(*buf2++, *clippedCount);
-        if (swap)
-            datum = st_swapf(datum);
         *(double *)buf1 = datum;
         buf1++; buf1++; buf1++; buf1++;
         buf1++; buf1++; buf1++; buf1++;
@@ -741,7 +457,7 @@
 st_size_t st_rawwrite(ft_t ft, const st_sample_t *buf, st_size_t nsamp)
 {
     st_size_t len, done = 0;
-    void (*write_buf)(char *, st_sample_t const *, st_size_t, char, st_size_t *) = 0;
+    void (*write_buf)(char *, st_sample_t const *, st_size_t, st_size_t *) = 0;
 
     switch(ft->info.size) {
         case ST_SIZE_BYTE:
@@ -846,7 +562,7 @@
         len = min(nsamp-done,(ft->file.size-ft->file.pos)/ft->info.size);
         if (len)
         {
-            write_buf(ft->file.buf + ft->file.pos, buf+done, len, ft->swap, &ft->clippedCount);
+            write_buf(ft->file.buf + ft->file.pos, buf+done, len, &ft->clippedCount);
             ft->file.pos += (len*ft->info.size);
             done += len;
         }
@@ -934,16 +650,16 @@
 };
 
 static st_format_t st_raw_format = {
-   rawnames,
-   NULL,
-   ST_FILE_STEREO | ST_FILE_SEEK,
-   st_rawstartread,
-   st_rawread,
-   st_rawstopread,
-   st_rawstartwrite,
-   st_rawwrite,
-   st_rawstopwrite,
-   st_rawseek
+  rawnames,
+  NULL,
+  ST_FILE_STEREO | ST_FILE_SEEK,
+  st_rawstartread,
+  st_rawread,
+  st_rawstopread,
+  st_rawstartwrite,
+  st_rawwrite,
+  st_rawstopwrite,
+  st_rawseek
 };
 
 const st_format_t *st_raw_format_fn(void)
@@ -1181,16 +897,16 @@
 };
 
 static st_format_t st_ul_format = {
-   ulnames,
-   NULL,
-   ST_FILE_STEREO,
-   st_ulstartread,
-   st_rawread,
-   st_rawstopread,
-   st_ulstartwrite,
-   st_rawwrite,
-   st_rawstopwrite,
-   st_format_nothing_seek
+  ulnames,
+  NULL,
+  ST_FILE_STEREO,
+  st_ulstartread,
+  st_rawread,
+  st_rawstopread,
+  st_ulstartwrite,
+  st_rawwrite,
+  st_rawstopwrite,
+  st_format_nothing_seek
 };
 
 const st_format_t *st_ul_format_fn(void)
--- a/src/smp.c
+++ b/src/smp.c
@@ -335,7 +335,7 @@
         for(; done < len && smp->NoOfSamps; done++, smp->NoOfSamps--) {
                 st_readw(ft, &datum);
                 /* scale signed up to long's range */
-                *buf++ = ST_SIGNED_WORD_TO_SAMPLE(datum);
+                *buf++ = ST_SIGNED_WORD_TO_SAMPLE(datum,);
         }
         return done;
 }
--- a/src/sox.c
+++ b/src/sox.c
@@ -1310,7 +1310,7 @@
           }
       }
 
-      /* If outputing and output data was generated then write it */
+      /* If outputting and output data was generated then write it */
       if (writing && (efftab[neffects-1].olen>efftab[neffects-1].odone))
       {
           /* Change the volume of this output data if needed. */
@@ -1334,8 +1334,7 @@
 
               if (len != efftab[neffects-1].olen-total || file_desc[file_count-1]->file.eof)
               {
-                  st_warn("Error writing: %s",
-                          file_desc[file_count-1]->st_errstr);
+                  st_warn("Error writing: %s", file_desc[file_count-1]->st_errstr);
                   return ST_EOF;
               }
               total += len;
--- a/src/st.h
+++ b/src/st.h
@@ -103,14 +103,14 @@
 #define ST_SIGNED_TO_SAMPLE(bits,d)((st_sample_t)(d)<<(32-bits))
 #define ST_UNSIGNED_TO_SAMPLE(bits,d)(ST_SIGNED_TO_SAMPLE(bits,d)^ST_SAMPLE_NEG)
 
-#define ST_UNSIGNED_BYTE_TO_SAMPLE(d) ST_UNSIGNED_TO_SAMPLE(8,d)
-#define ST_SIGNED_BYTE_TO_SAMPLE(d) ST_SIGNED_TO_SAMPLE(8,d)
-#define ST_UNSIGNED_WORD_TO_SAMPLE(d) ST_UNSIGNED_TO_SAMPLE(16,d)
-#define ST_SIGNED_WORD_TO_SAMPLE(d) ST_SIGNED_TO_SAMPLE(16,d)
-#define ST_UNSIGNED_24BIT_TO_SAMPLE(d) ST_UNSIGNED_TO_SAMPLE(24,d)
-#define ST_SIGNED_24BIT_TO_SAMPLE(d) ST_SIGNED_TO_SAMPLE(24,d)
-#define ST_UNSIGNED_DWORD_TO_SAMPLE(d) (st_sample_t)((d)^ST_SAMPLE_NEG)
-#define ST_SIGNED_DWORD_TO_SAMPLE(d) (st_sample_t)(d)
+#define ST_UNSIGNED_BYTE_TO_SAMPLE(d,clips) ST_UNSIGNED_TO_SAMPLE(8,d)
+#define ST_SIGNED_BYTE_TO_SAMPLE(d,clips) ST_SIGNED_TO_SAMPLE(8,d)
+#define ST_UNSIGNED_WORD_TO_SAMPLE(d,clips) ST_UNSIGNED_TO_SAMPLE(16,d)
+#define ST_SIGNED_WORD_TO_SAMPLE(d,clips) ST_SIGNED_TO_SAMPLE(16,d)
+#define ST_UNSIGNED_24BIT_TO_SAMPLE(d,clips) ST_UNSIGNED_TO_SAMPLE(24,d)
+#define ST_SIGNED_24BIT_TO_SAMPLE(d,clips) ST_SIGNED_TO_SAMPLE(24,d)
+#define ST_UNSIGNED_DWORD_TO_SAMPLE(d,clips) (st_sample_t)((d)^ST_SAMPLE_NEG)
+#define ST_SIGNED_DWORD_TO_SAMPLE(d,clips) (st_sample_t)(d)
 #define ST_FLOAT_DWORD_TO_SAMPLE ST_FLOAT_DDWORD_TO_SAMPLE
 #define ST_FLOAT_DDWORD_TO_SAMPLE(d,clips) (st_macro_temp_double=d,st_macro_temp_double<-1?++(clips),(-ST_SAMPLE_MAX):st_macro_temp_double>1?++(clips),ST_SAMPLE_MAX:(st_sample_t)((uint32_t)((double)(st_macro_temp_double)*ST_SAMPLE_MAX+(ST_SAMPLE_MAX+.5))-ST_SAMPLE_MAX))
 #define ST_SAMPLE_TO_UNSIGNED_BYTE(d,clips) ST_SAMPLE_TO_UNSIGNED(8,d,clips)
--- a/src/st_i.h
+++ b/src/st_i.h
@@ -68,6 +68,8 @@
 int st_writeb(ft_t ft, uint8_t ub);
 int st_readw(ft_t ft, uint16_t *uw);
 int st_writew(ft_t ft, uint16_t uw);
+int st_read3(ft_t ft, uint24_t *u3);
+int st_write3(ft_t ft, uint24_t u3);
 int st_readdw(ft_t ft, uint32_t *udw);
 int st_writedw(ft_t ft, uint32_t udw);
 int st_readf(ft_t ft, float *f);
@@ -82,16 +84,6 @@
 int st_error(ft_t ft);
 void st_rewind(ft_t ft);
 void st_clearerr(ft_t ft);
-
-/* defined in raw.c */
-void st_ub_write_buf(char* buf1, const st_sample_t *buf2, st_size_t len, char swap, st_size_t * clippedCount);
-void st_sb_write_buf(char *buf1, const st_sample_t *buf2, st_size_t len, char swap, st_size_t * clippedCount);
-void st_uw_write_buf(char *buf1, const st_sample_t *buf2, st_size_t len, char swap, st_size_t * clippedCount);
-void st_sw_write_buf(char *buf1, const st_sample_t *buf2, st_size_t len, char swap, st_size_t * clippedCount);
-void st_ub_read_buf(st_sample_t *buf1, char const *buf2, st_size_t len, char swap, st_size_t * clippedCount);
-void st_sb_read_buf(st_sample_t *buf1, char const *buf2, st_size_t len, char swap, st_size_t * clippedCount);
-void st_uw_read_buf(st_sample_t *buf1, char const *buf2, st_size_t len, char swap, st_size_t * clippedCount);
-void st_sw_read_buf(st_sample_t *buf1, char const *buf2, st_size_t len, char swap, st_size_t * clippedCount);
 
 /* Utilities to byte-swap values, use libc optimized macros if possible  */
 #ifdef HAVE_BYTESWAP_H
--- a/src/voc.c
+++ b/src/voc.c
@@ -381,7 +381,7 @@
                     } else if (v->format == VOC_FMT_ALAW) {
                         *buf++ =  ST_ALAW_BYTE_TO_SAMPLE(uc);
                     } else {
-                        *buf++ = ST_UNSIGNED_BYTE_TO_SAMPLE(uc);
+                        *buf++ = ST_UNSIGNED_BYTE_TO_SAMPLE(uc,);
                     }
                     break;
                 case ST_SIZE_WORD:
@@ -392,7 +392,7 @@
                             v->rest = 0;
                             return done;
                         }
-                    *buf++ = ST_SIGNED_WORD_TO_SAMPLE(sw);
+                    *buf++ = ST_SIGNED_WORD_TO_SAMPLE(sw,);
                     v->rest--; /* Processed 2 bytes so update */
                     break;
                 }
--- a/src/vox.c
+++ b/src/vox.c
@@ -158,10 +158,10 @@
                      byte      = ft->file.buf[ft->file.pos++];
 
                      word      = devox ((uint8_t) ((byte >> 4) & 0x0F),state);
-                     *buffer++ = ST_SIGNED_WORD_TO_SAMPLE (word * 16);
+                     *buffer++ = ST_SIGNED_WORD_TO_SAMPLE (word * 16,);
 
                      word      = devox ((uint8_t) (byte & 0x0F),state);
-                     *buffer++ = ST_SIGNED_WORD_TO_SAMPLE (word * 16);
+                     *buffer++ = ST_SIGNED_WORD_TO_SAMPLE (word * 16,);
 
                      count += 2;
                        }
--- a/src/wav.c
+++ b/src/wav.c
@@ -233,7 +233,7 @@
 
   /* copy out any samples left from the last call */
     while(wav->gsmindex && (wav->gsmindex<160*2) && (done < len))
-        buf[done++]=ST_SIGNED_WORD_TO_SAMPLE(wav->gsmsample[wav->gsmindex++]);
+        buf[done++]=ST_SIGNED_WORD_TO_SAMPLE(wav->gsmsample[wav->gsmindex++],);
 
   /* read and decode loop, possibly leaving some samples in wav->gsmsample */
     while (done < len) {
@@ -259,7 +259,7 @@
         }
 
         while ((wav->gsmindex <160*2) && (done < len)){
-            buf[done++]=ST_SIGNED_WORD_TO_SAMPLE(wav->gsmsample[(wav->gsmindex)++]);
+            buf[done++]=ST_SIGNED_WORD_TO_SAMPLE(wav->gsmsample[(wav->gsmindex)++],);
         }
     }
 
@@ -1079,7 +1079,7 @@
                     top = p+ct;
                     /* Output is already signed */
                     while (p<top)
-                        *buf++ = ST_SIGNED_WORD_TO_SAMPLE((*p++));
+                        *buf++ = ST_SIGNED_WORD_TO_SAMPLE((*p++),);
 
                     wav->samplePtr = p;
                 }
--- a/src/xa.c
+++ b/src/xa.c
@@ -258,7 +258,7 @@
                 xa->state[i].prevSample = xa->state[i].curSample;
                 xa->state[i].curSample = sample;
                 
-                buf[done++] = ST_SIGNED_WORD_TO_SAMPLE(sample);
+                buf[done++] = ST_SIGNED_WORD_TO_SAMPLE(sample,);
                 xa->bytesDecoded += ft->info.size;
             }
             for (i = 0; i < ft->info.channels && done < len; i++) {
@@ -272,7 +272,7 @@
                 xa->state[i].prevSample = xa->state[i].curSample;
                 xa->state[i].curSample = sample;
                 
-                buf[done++] = ST_SIGNED_WORD_TO_SAMPLE(sample);
+                buf[done++] = ST_SIGNED_WORD_TO_SAMPLE(sample,);
                 xa->bytesDecoded += ft->info.size;
             }