ref: 7218a569ea0124a0a25dffd3e93b91bde67bdca7
parent: b03e0534d7d2ab306b584296bf0af011db85bf55
author: robs <robs>
date: Sun Mar 18 03:09:11 EDT 2007
Clawing back some of the performance lost when buffering was removed. This change cuts typical file conversion (cpu user) time to 56%
--- a/configure.ac
+++ b/configure.ac
@@ -17,6 +17,10 @@
AC_SUBST(LIBTOOL_DEPS)
m4_ifdef([PKG_PROG_PKG_CONFIG], [PKG_PROG_PKG_CONFIG])
+if test "$GCC" = "yes"; then
+ CFLAGS="-O3 -fomit-frame-pointer"
+fi
+
dnl Debugging
AC_MSG_CHECKING([if we want a debug build])
AC_ARG_ENABLE(debug,
@@ -37,7 +41,7 @@
dnl Extra CFLAGS if we have gcc
if test "$GCC" = yes; then
- CFLAGS="$CFLAGS -Wall -W -Wmissing-prototypes -Wstrict-prototypes -pedantic"
+ CFLAGS="$CFLAGS -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -pedantic"
fi
dnl Check for system dependent features.
--- a/src/misc.c
+++ b/src/misc.c
@@ -78,11 +78,11 @@
assert_static(array_length(sox_encodings_str) == SOX_ENCODINGS,
SIZE_MISMATCH_BETWEEN_sox_encodings_t_AND_sox_encodings_str);
-static const char readerr[] = "Premature EOF while reading sample file.";
-static const char writerr[] = "Error writing sample file. You are probably out of disk space.";
+const char sox_readerr[] = "Premature EOF while reading sample file.";
+const char sox_writerr[] = "Error writing sample file. You are probably out of disk space.";
/* Lookup table to reverse the bit order of a byte. ie MSB become LSB */
-static uint8_t const cswap[256] = {
+uint8_t const cswap[256] = {
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0,
0x30, 0xB0, 0x70, 0xF0, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, 0x04, 0x84, 0x44, 0xC4,
@@ -204,7 +204,7 @@
if (sox_readbuf(ft, &in, 1, 1) != 1)
{
*sc = 0;
- sox_fail_errno(ft,errno,readerr);
+ sox_fail_errno(ft,errno,sox_readerr);
return (SOX_EOF);
}
if (in == 0 || in == '\n')
@@ -222,98 +222,18 @@
{
if (sox_writebuf(ft, c, 1, strlen(c)) != strlen(c))
{
- sox_fail_errno(ft,errno,writerr);
+ sox_fail_errno(ft,errno,sox_writerr);
return(SOX_EOF);
}
return(SOX_SUCCESS);
}
-/* Read byte. */
-int sox_readb(ft_t ft, uint8_t *ub)
-{
- if (sox_readbuf(ft, ub, 1, 1) != 1) {
- sox_fail_errno(ft,errno,readerr);
- return SOX_EOF;
- }
- if (ft->signal.reverse_bits)
- *ub = cswap[*ub];
- if (ft->signal.reverse_nibbles)
- *ub = ((*ub & 15) << 4) | (*ub >> 4);
- return SOX_SUCCESS;
-}
-
-/* Write byte. */
-int sox_writeb(ft_t ft, int ub)
-{
- if (ft->signal.reverse_nibbles)
- ub = ((ub & 15) << 4) | (ub >> 4);
- if (ft->signal.reverse_bits)
- ub = cswap[ub];
- if (sox_writebuf(ft, &ub, 1, 1) != 1) {
- sox_fail_errno(ft,errno,writerr);
- return SOX_EOF;
- }
- return SOX_SUCCESS;
-}
-
-/* Read word. */
-int sox_readw(ft_t ft, uint16_t *uw)
-{
- if (sox_readbuf(ft, uw, 2, 1) != 1)
- {
- sox_fail_errno(ft,errno,readerr);
- return (SOX_EOF);
- }
- if (ft->signal.reverse_bytes)
- *uw = sox_swapw(*uw);
- return SOX_SUCCESS;
-}
-
-/* Write word. */
-int sox_writew(ft_t ft, int uw)
-{
- if (ft->signal.reverse_bytes)
- uw = sox_swapw(uw);
- if (sox_writebuf(ft, &uw, 2, 1) != 1)
- {
- sox_fail_errno(ft,errno,writerr);
- return (SOX_EOF);
- }
- return(SOX_SUCCESS);
-}
-
-/* Read three bytes. */
-int sox_read3(ft_t ft, uint24_t *u3)
-{
- if (sox_readbuf(ft, u3, 3, 1) != 1)
- {
- sox_fail_errno(ft,errno,readerr);
- return (SOX_EOF);
- }
- if (ft->signal.reverse_bytes)
- *u3 = sox_swap24(*u3);
- return SOX_SUCCESS;
-}
-
-/* Write three bytes. */
-int sox_write3(ft_t ft, uint24_t u3)
-{
- if (ft->signal.reverse_bytes)
- u3 = sox_swap24(u3);
- if (sox_writebuf(ft, &u3, 3, 1) != 1)
- {
- sox_fail_errno(ft,errno,writerr);
- return (SOX_EOF);
- }
- return(SOX_SUCCESS);
-}
-
/* Read double word. */
int sox_readdw(ft_t ft, uint32_t *udw)
{
if (sox_readbuf(ft, udw, 4, 1) != 1)
{
- sox_fail_errno(ft,errno,readerr);
+ sox_fail_errno(ft,errno,sox_readerr);
return (SOX_EOF);
}
if (ft->signal.reverse_bytes)
@@ -328,7 +248,7 @@
udw = sox_swapdw(udw);
if (sox_writebuf(ft, &udw, 4, 1) != 1)
{
- sox_fail_errno(ft,errno,writerr);
+ sox_fail_errno(ft,errno,sox_writerr);
return (SOX_EOF);
}
return(SOX_SUCCESS);
@@ -339,7 +259,7 @@
{
if (sox_readbuf(ft, f, sizeof(float), 1) != 1)
{
- sox_fail_errno(ft,errno,readerr);
+ sox_fail_errno(ft,errno,sox_readerr);
return(SOX_EOF);
}
if (ft->signal.reverse_bytes)
@@ -356,7 +276,7 @@
t = sox_swapf(&t);
if (sox_writebuf(ft, &t, sizeof(float), 1) != 1)
{
- sox_fail_errno(ft,errno,writerr);
+ sox_fail_errno(ft,errno,sox_writerr);
return (SOX_EOF);
}
return (SOX_SUCCESS);
@@ -367,7 +287,7 @@
{
if (sox_readbuf(ft, d, sizeof(double), 1) != 1)
{
- sox_fail_errno(ft,errno,readerr);
+ sox_fail_errno(ft,errno,sox_readerr);
return(SOX_EOF);
}
if (ft->signal.reverse_bytes)
@@ -382,7 +302,7 @@
d = sox_swapd(d);
if (sox_writebuf(ft, &d, sizeof(double), 1) != 1)
{
- sox_fail_errno(ft,errno,writerr);
+ sox_fail_errno(ft,errno,sox_writerr);
return (SOX_EOF);
}
return (SOX_SUCCESS);
--- a/src/sox_i.h
+++ b/src/sox_i.h
@@ -19,6 +19,7 @@
#include "xmalloc.h"
#include <stdarg.h>
+#include <errno.h>
#ifdef HAVE_BYTESWAP_H
#include <byteswap.h>
@@ -103,12 +104,6 @@
size_t sox_writebuf(ft_t ft, void const *buf, size_t size, sox_size_t len);
int sox_reads(ft_t ft, char *c, sox_size_t len);
int sox_writes(ft_t ft, char const * c);
-int sox_readb(ft_t ft, uint8_t *ub);
-int sox_writeb(ft_t ft, int ub);
-int sox_readw(ft_t ft, uint16_t *uw);
-int sox_writew(ft_t ft, int uw);
-int sox_read3(ft_t ft, uint24_t *u3);
-int sox_write3(ft_t ft, uint24_t u3);
int sox_readdw(ft_t ft, uint32_t *udw);
int sox_writedw(ft_t ft, uint32_t udw);
int sox_readf(ft_t ft, float *f);
@@ -168,9 +163,6 @@
void sox_fail_errno(ft_t, int, const char *, ...);
-int sox_is_bigendian(void);
-int sox_is_littleendian(void);
-
#ifdef WORDS_BIGENDIAN
#define SOX_IS_BIGENDIAN 1
#define SOX_IS_LITTLEENDIAN 0
@@ -191,6 +183,92 @@
* data to get max performance.
*/
#define SOX_BUFSIZ 8192
+
+extern const char sox_readerr[];
+extern const char sox_writerr[];
+extern uint8_t const cswap[256];
+
+/* Read byte. */
+UNUSED static int sox_readb(ft_t ft, uint8_t *ub)
+{
+ int ch1;
+ if ((ch1 = getc(ft->fp)) == EOF) {
+ sox_fail_errno(ft,errno,sox_readerr);
+ return SOX_EOF;
+ }
+ *ub = ft->signal.reverse_bits? cswap[ch1]: ch1;
+ if (ft->signal.reverse_nibbles)
+ *ub = ((*ub & 15) << 4) | (*ub >> 4);
+ return SOX_SUCCESS;
+}
+
+/* Write byte. */
+UNUSED static int sox_writeb(ft_t ft, int ub)
+{
+ if (ft->signal.reverse_nibbles)
+ ub = ((ub & 15) << 4) | (ub >> 4);
+ if (ft->signal.reverse_bits)
+ ub = cswap[ub];
+ if (putc(ub, ft->fp) == EOF) {
+ sox_fail_errno(ft,errno,sox_writerr);
+ return SOX_EOF;
+ }
+ return SOX_SUCCESS;
+}
+
+/* Read word. */
+UNUSED static int sox_readw(ft_t ft, uint16_t *uw)
+{
+ char * c = (char *)uw;
+ int ch1, ch2;
+ if ((ch1 = getc(ft->fp)) == EOF || (ch2 = getc(ft->fp)) == EOF) {
+ sox_fail_errno(ft,errno,sox_readerr);
+ return SOX_EOF;
+ }
+ c[SOX_IS_BIGENDIAN ^ ft->signal.reverse_bytes] = ch1;
+ c[SOX_IS_LITTLEENDIAN ^ ft->signal.reverse_bytes] = ch2;
+ return SOX_SUCCESS;
+}
+
+/* Write word. */
+UNUSED static int sox_writew(ft_t ft, int uw)
+{
+ char * c = (char *)&uw;
+ if (putc(c[SOX_IS_BIGENDIAN ^ ft->signal.reverse_bytes], ft->fp) == EOF ||
+ putc(c[SOX_IS_LITTLEENDIAN ^ ft->signal.reverse_bytes], ft->fp) == EOF) {
+ sox_fail_errno(ft,errno,sox_writerr);
+ return SOX_EOF;
+ }
+ return SOX_SUCCESS;
+}
+
+/* Read three bytes. */
+UNUSED static int sox_read3(ft_t ft, uint24_t *u3)
+{
+ char * c = (char *)u3;
+ int ch1, ch2, ch3;
+ if ((ch1 = getc(ft->fp)) == EOF || (ch2 = getc(ft->fp)) == EOF || (ch3 = getc(ft->fp)) == EOF) {
+ sox_fail_errno(ft,errno,sox_readerr);
+ return SOX_EOF;
+ }
+ c[(SOX_IS_BIGENDIAN ^ ft->signal.reverse_bytes)<<1] = ch1;
+ c[1] = ch2;
+ c[(SOX_IS_LITTLEENDIAN ^ ft->signal.reverse_bytes)<<1] = ch3;
+ return SOX_SUCCESS;
+}
+
+/* Write three bytes. */
+UNUSED static int sox_write3(ft_t ft, uint24_t u3)
+{
+ char * c = (char *)&u3;
+ if (putc(c[(SOX_IS_BIGENDIAN ^ ft->signal.reverse_bytes)<<1], ft->fp) == EOF ||
+ putc(c[1], ft->fp) == EOF ||
+ putc(c[(SOX_IS_LITTLEENDIAN ^ ft->signal.reverse_bytes)<<1], ft->fp) == EOF) {
+ sox_fail_errno(ft,errno,sox_writerr);
+ return SOX_EOF;
+ }
+ return SOX_SUCCESS;
+}
/*=============================================================================
* File Handlers