shithub: sox

Download patch

ref: 215f6dd53c3dc8bea469841f0ddf45f807e5666f
parent: d089cf02858b6a3cc178dcb412c6563cc50fc86a
author: cbagwell <cbagwell>
date: Sun Nov 14 22:10:29 EST 1999

More libary updates.  Bugfixes in raw handler

--- a/libst.c
+++ b/libst.c
@@ -1,10 +1,10 @@
 /* libst.c - portable sound tools library
 */
 
+#include "st.h"
 #include "libst.h"
 
 #ifndef FAST_ULAW_CONVERSION
-
 /*
 ** This routine converts from linear to ulaw.
 **
--- a/libst.h
+++ b/libst.h
@@ -1,6 +1,6 @@
 /* libst.h - include file for portable sound tools library
 **
-** Copyright (C) 1989 by Jef Poskanzer.
+** Copyright (C) 1989
 **
 ** Permission to use, copy, modify, and distribute this software and its
 ** documentation for any purpose and without fee is hereby granted, provided
@@ -10,24 +10,49 @@
 ** implied warranty.
 */
 
-/* These do not round data.  Caller must round appropriately. */
+/*******************************************************************/
+/* Common function prototypes used internal to Sound Tools library */
+/*******************************************************************/
 
+/******************************************************************/
+/* Define u-law and a-law functionsto convert to linear PCM data  */
+/* Can use faster lookup tables by using the appropriate defines. */
+/******************************************************************/
 #ifdef FAST_ULAW_CONVERSION
-extern int ulaw_exp_table[256];
+extern int           ulaw_exp_table[256];
 extern unsigned char ulaw_comp_table[16384];
-#define st_ulaw_to_linear(ulawbyte) ulaw_exp_table[ulawbyte]
+#define st_ulaw_to_linear(ulawbyte)   ulaw_exp_table[ulawbyte]
 #define st_linear_to_ulaw(linearword) ulaw_comp_table[((short)linearword / 4) & 0x3fff]
 #else
 unsigned char st_linear_to_ulaw( /* short sample */ );
-int st_ulaw_to_linear( /* unsigned char ulawbyte */ );
+int           st_ulaw_to_linear( /* unsigned char ulawbyte */ );
 #endif
 
 #ifdef FAST_ALAW_CONVERSION
-extern int Alaw_exp_table[256];
+extern int           Alaw_exp_table[256];
 extern unsigned char Alaw_comp_table[16384];
-#define st_Alaw_to_linear(Alawbyte) Alaw_exp_table[Alawbyte]
+#define st_Alaw_to_linear(Alawbyte)   Alaw_exp_table[Alawbyte]
 #define st_linear_to_Alaw(linearword) Alaw_comp_table[((short)linearword / 4) & 0x3fff]
 #else
 unsigned char st_linear_to_Alaw( /* short sample */ );
-int st_Alaw_to_linear( /* unsigned char ulawbyte */ );
+int           st_Alaw_to_linear( /* unsigned char ulawbyte */ );
+#endif
+
+/* Greatest Common Demoninator */
+LONG st_gcd(/* a, b */);
+/* Least Common Multiple */
+LONG st_lcm(/* a, b */);
+
+/****************************************************/
+/* Prototypes for internal cross-platform functions */
+/****************************************************/
+
+#ifndef HAVE_RAND
+extern int rand();
+extern void srand(/* seed */);
+#endif
+extern void initrand();
+
+#ifndef HAVE_STRERROR
+char *strerror(/*errorcode*/);
 #endif
--- a/src/8svx.c
+++ b/src/8svx.c
@@ -8,10 +8,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-#ifdef	VMS
-#include <perror.h>
-#endif
-
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>	/* For SEEK_* defines if not found in stdio */
 #endif
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -59,7 +59,7 @@
 ALSAOBJ_1   = alsa.o
 EXTRAOBJS   = $(OSSOBJ_$(NEED_OSS)) $(SUNAUOBJ_$(NEED_SUNAU)) $(ALSAOBJ_$(NEED_ALSA))
 
-LIBOBJS = $(FOBJ) $(EOBJ) handlers.o libst.o misc.o util.o $(EXTRAOBJS)
+LIBOBJS = $(FOBJ) $(EOBJ) handlers.o libst.o misc.o util.o getopt.o $(EXTRAOBJS)
 
 VPATH	= @srcdir@
 
--- a/src/auto.c
+++ b/src/auto.c
@@ -17,7 +17,7 @@
 #include "st.h"
 #include <string.h>
 
-IMPORT void gettype();
+void gettype();
 
 void autostartread(ft)
 ft_t ft;
--- a/src/handlers.c
+++ b/src/handlers.c
@@ -317,7 +317,7 @@
 extern void nothing();
 extern LONG nothing_success();
 
-EXPORT format_t formats[] = {
+format_t formats[] = {
 	{aiffnames, FILE_STEREO,
 		aiffstartread, aiffread, aiffstopread,	   /* SGI/Apple AIFF */
 		aiffstartwrite, aiffwrite, aiffstopwrite},
@@ -594,7 +594,7 @@
  * EFF_MCHAN just means that the effect is coded for multiple channels.
  */
 
-EXPORT effect_t effects[] = {
+effect_t effects[] = {
 	{"null", 0, 			/* stand-in, never gets called */
 		nothing, nothing, nothing, null_drain, nothing},
 	{"avg", EFF_CHAN | EFF_MCHAN, 
--- a/src/mask.c
+++ b/src/mask.c
@@ -14,12 +14,10 @@
 
 #include <math.h>
 #include "st.h"
+#include "libst.h"
 
 #define HALFABIT 1.44			/* square root of 2 */
 
-void newrand15();
-ULONG rand15();
-
 /*
  * Problems:
  * 	1) doesn't allow specification of noise depth
@@ -39,7 +37,7 @@
 		fail("Mask effect takes no options.");
 	/* should take # of bits */
 
-	newrand15();
+	initrand();
 }
 
 /*
@@ -62,7 +60,7 @@
 		case ULAW:
 		case ALAW:
 			for(done = 0; done < len; done++) {
-				tri16 = (rand15() + rand15()) - 32767;
+				tri16 = (rand() + rand()) - 32767;
 
 				l = *ibuf++ + tri16*16*HALFABIT;  /* 2^4.5 */
 				*obuf++ = l;
@@ -72,7 +70,7 @@
 		switch (effp->outinfo.size) {
 			case BYTE:
 			for(done = 0; done < len; done++) {
-				tri16 = (rand15() + rand15()) - 32767;
+				tri16 = (rand() + rand()) - 32767;
 
 				l = *ibuf++ + tri16*256*HALFABIT;  /* 2^8.5 */
 				*obuf++ = l;
@@ -80,7 +78,7 @@
 			break;
 			case WORD:
 			for(done = 0; done < len; done++) {
-				tri16 = (rand15() + rand15()) - 32767;
+				tri16 = (rand() + rand()) - 32767;
 
 				l = *ibuf++ + tri16*HALFABIT;  /* 2^.5 */
 				*obuf++ = l;
--- a/src/misc.c
+++ b/src/misc.c
@@ -17,7 +17,7 @@
 #include <stdio.h>
 #include <time.h>
 
-EXPORT char *sizes[] = {
+char *sizes[] = {
 	"NONSENSE!",
 	"bytes",
 	"shorts",
@@ -28,7 +28,7 @@
 	"IEEE floats"
 };
 
-EXPORT char *styles[] = {
+char *styles[] = {
 	"NONSENSE!",
 	"unsigned",
 	"signed (2's complement)",
@@ -60,13 +60,9 @@
 
 /* Write short. */
 unsigned short
-#if	defined(__STDC__)
-wshort(ft_t ft, unsigned short us)
-#else
 wshort(ft, us)
 ft_t ft;
 unsigned short us;
-#endif
 {
 	if (ft->swap)
 		us = swapw(us);
@@ -167,12 +163,8 @@
 /* Byte swappers */
 
 unsigned short
-#if	defined(__STDC__)
-swapw(unsigned short us)
-#else
 swapw(us)
 unsigned short us;
-#endif
 {
 	return ((us >> 8) | (us << 8)) & 0xffff;
 }
@@ -186,12 +178,7 @@
 
 /* return swapped 32-bit float */
 float
-#if	defined(__STDC__)
-swapf(float uf)
-#else
-swapf(uf)
-float uf;
-#endif
+swapf(float f)
 {
 	union {
 	    ULONG l;
@@ -198,7 +185,7 @@
 	    float f;
 	} u;
 
-	u.f= uf;
+	u.f= f;
 	u.l= (u.l>>24) | ((u.l>>8)&0xff00) | ((u.l<<8)&0xff0000L) | (u.l<<24);
 	return u.f;
 }
@@ -243,31 +230,34 @@
     return a * (b / st_gcd(a, b));
 }
 
+/* FIXME: Need to add to autoconf to check for random */
+#ifndef HAVE_RAND
 /* 
- * Cribbed from Unix SVR3 programmer's manual 
+ * Portable random generator as defined by ANSI C Standard.
+ * Don't ask me why not all C libraries include it.
  */
 
-static ULONG rand15_seed;
+static int rand_seed;
 
-ULONG rand15() {
-	rand15_seed = (rand15_seed * 1103515245L) + 12345L;
-	return (ULONG) ((rand15_seed/65536L) % 32768L);
+int rand() {
+	rand_seed = (rand_seed * 1103515245L) + 12345L;
+	return ((rand_seed/65536L) % 32768L);
 }
 
-void srand15(seed) 
-ULONG seed;
+void srand(seed) 
+int seed;
 {
-	rand15_seed = seed;
+	rand_seed = seed;
 }
+#endif
 
-void newrand15() {
+/* Util to set initial seed so that we are a little less non-random */
+void initrand() {
 	time_t t;
 
 	time(&t);
-	srand15(t);
+	srand(t);
 }
-
-/* sine wave gen should be here, also */
 
 char *
 version()
--- a/src/polyphas.c
+++ b/src/polyphas.c
@@ -29,6 +29,7 @@
 #include <malloc.h>
 #endif
 #include "st.h"
+#include "libst.h"
 
 #define Float float/*double*/
 #define ISCALE 0x10000
@@ -194,7 +195,7 @@
   
   for (k=ct; k>1; ) {
     int tmp, j;
-    j = random() % k;
+    j = rand() % k;
     k--;
     if (j != k) {
       tmp = m[k]; m[k]=m[j]; m[j]=tmp;
@@ -204,7 +205,7 @@
   p = q = m;
   n = *q++;
   while ((k=*q++)) {
-    if ((n * k <= amalg) && (random() & 1)) {
+    if ((n * k <= amalg) && (rand() & 1)) {
       n *= k;
     } else {
       *p++ = n;
@@ -376,7 +377,7 @@
 		int total, size, uprate;
     int k;
 
-    extern long st_lcm();
+    initrand();
 
     rate->lcmrate = st_lcm((long)effp->ininfo.rate, (long)effp->outinfo.rate);
 
--- a/src/rate.c
+++ b/src/rate.c
@@ -18,6 +18,7 @@
 
 #include <math.h>
 #include "st.h"
+#include "libst.h"
 
 /*
  * Linear Interpolation.
@@ -231,7 +232,6 @@
 eff_t effp;
 {
 	rate_t rate = (rate_t) effp->priv;
-	IMPORT LONG lcm();
 	
 	rate->lcmrate = lcm((LONG)effp->ininfo.rate, (LONG)effp->outinfo.rate);
 	/* Cursory check for LCM overflow.  
--- a/src/raw.c
+++ b/src/raw.c
@@ -76,6 +76,7 @@
 	return (rval);
 }
 
+/* Util to swap every 2 chars up to 'n' imes. */
 static void swapn(p, n)
 char *p;
 int n;
@@ -91,6 +92,7 @@
 	}
 }
 
+/* Reads a block of 'n' characters and possibly byte swaps */
 static void blockr(p0, n, ft)
 ft_t ft;
 int n;
@@ -116,6 +118,11 @@
 	}
 	memcpy(p, ft->file.buf + ft->file.pos, n);
 	ft->file.pos += n;
+	/* FIXME: For speed, there should be a version of this funciton
+	 * for WORDS, LONGS, and FLOATS.  This is because swapping
+	 * bytes is an expensive operation and should be done in batch
+	 * mode.
+	 */
 	if (ft->swap)
 		swapn(p,n);
 }
@@ -280,8 +287,12 @@
 {
 	if (ft->file.pos > ft->file.size-n) blockflush(ft);
 	memcpy(ft->file.buf + ft->file.pos, p0, n);
+	/* FIXME: Should be a version for every data type.  This
+	 * is because swap is an expensive operation.  We should
+	 * only swap the buffer after its full.
+	 */
 	if (ft->swap)
-		swapn(ft->file.pos, n);
+		swapn(p0, n);
 	ft->file.pos += n;
 }
 
--- a/src/reverse.c
+++ b/src/reverse.c
@@ -21,8 +21,6 @@
 
 #include "st.h"
 
-IMPORT FILE *tmpfile();
-
 /* Private data */
 typedef struct reversestuff {
 	FILE *fp;
--- a/src/sox.c
+++ b/src/sox.c
@@ -48,15 +48,12 @@
 #else
 #ifndef HAVE_GETOPT
 int getopt(P3(int,char **,char *));
+extern char *optarg;
+extern int optind;
 #endif
 #endif
 
-#ifdef VMS
-#include <perror.h>
-#define LASTCHAR        ']'
-#else
 #define LASTCHAR        '/'
-#endif
 
 /*
  * SOX main program.
@@ -96,8 +93,6 @@
 				/* efftab[0] is the input stream */
 int neffects;			/* # of effects */
 char *ifile, *ofile, *itype, *otype;
-IMPORT char *optarg;
-IMPORT int optind;
 
 int main(argc, argv)
 int argc;
--- a/src/st.h
+++ b/src/st.h
@@ -11,30 +11,6 @@
  * the consequences of using this software.
  */
 
-/* FIXME: The following are not ST library related.  Move to a seperate
- * file that internal formats can use.
- */
-#ifdef VAXC
-#define IMPORT  globalref
-#define EXPORT  globaldef
-/*
- * use the VAX C optimized functions 
- */ 
-#define calloc  VAXC$CALLOC_OPT
-#define cfree   VAXC$CFREE_OPT
-#define free    VAXC$FREE_OPT
-#define malloc  VAXC$MALLOC_OPT
-#define realloc VAXC$REALLOC_OPT
-#else
-#define IMPORT  extern
-#define EXPORT 
-#endif
-
-
-/*
- * Sound Tools sources header file.
- */
-
 #include <stdio.h>
 
 /* FIXME: Move to seperate header */
@@ -63,7 +39,7 @@
 } format_t;
 
 /* FIXME: Does this need to be here? */ 
-IMPORT format_t formats[];
+extern format_t formats[];
 
 /* Signal parameters */
 
@@ -147,7 +123,8 @@
 };
 
 /* This shoul not be here.  Only needed in sox.c */
-IMPORT struct soundstream informat, outformat;
+extern struct soundstream informat;
+extern struct soundstream outformat;
 
 typedef struct soundstream *ft_t;
 
@@ -176,7 +153,8 @@
 /* FIXME: This shouldn't be defined inside library.  Only needed
  * by sox.c itself.  Delete from raw.c and misc.c.
  */
-IMPORT char *sizes[], *styles[];
+extern char *sizes[];
+extern char *styles[];
 
 /*
  * Handler structure for each effect.
@@ -192,7 +170,7 @@
 	void	(*stop)();		/* finish up effect */
 } effect_t;
 
-IMPORT effect_t effects[];
+extern effect_t effects[];
 
 #define	EFF_CHAN	1		/* Effect can mix channels up/down */
 #define EFF_RATE	2		/* Effect can alter data rate */
@@ -267,21 +245,21 @@
 float  	       swapf(P1(float f));			/* Swap float */
 double 	       swapd(P1(double d));			/* Swap double */
 
-IMPORT void report(P2(char *, ...)),  warn(P2(char *, ...)),
+void report(P2(char *, ...)),  warn(P2(char *, ...)),
 	 fail(P2(char *, ...));
 
 /* util.c */
-IMPORT void geteffect(P1(eff_t));
-IMPORT void gettype(P1(ft_t));
-IMPORT void checkformat(P1(ft_t));
-IMPORT void copyformat(P2(ft_t, ft_t));
-IMPORT void cmpformats(P2(ft_t, ft_t));
+void geteffect(P1(eff_t));
+void gettype(P1(ft_t));
+void checkformat(P1(ft_t));
+void copyformat(P2(ft_t, ft_t));
+void cmpformats(P2(ft_t, ft_t));
 
 /* FIXME: Recording hacks shouldn't display a "sigint" style interface.
  * Instead we should provide a function to call when done playing/recording.
  * sox.c should be responsible for registering to sigint.
  */
-IMPORT void sigintreg(P1(ft_t));
+void sigintreg(P1(ft_t));
 
 /* FIXME: Move to sox header file. */
 typedef	unsigned int u_i;
@@ -288,20 +266,17 @@
 typedef	ULONG u_l;
 typedef	unsigned short u_s;
 
-IMPORT float volume;	/* expansion coefficient */
-IMPORT int dovolume;
+extern float volume;	/* expansion coefficient */
+extern int dovolume;
 
-IMPORT float amplitude;	/* Largest sample so far */
+extern int writing;	/* are we writing to a file? */
 
-IMPORT int writing;	/* are we writing to a file? */
-
 /* export flags */
-IMPORT int verbose;	/* be noisy on stderr */
-IMPORT int summary;	/* just print summary of information */
+extern int verbose;	/* be noisy on stderr */
 
-IMPORT char *myname;
+extern char *myname;
 
-IMPORT int soxpreview;	/* Preview mode: be fast and ugly */
+extern int soxpreview;	/* Preview mode: be fast and ugly */
 
 /* FIXME: Not externally visible currently.  Its a per-effect value. */
 #define	MAXRATE	50L * 1024			/* maximum sample rate */
@@ -314,14 +289,8 @@
 #define M_PI	3.14159265358979323846
 #endif
 
-/* FIXME: Move to sox.h */
-#ifdef	VMS
-#define READBINARY      "r", "mbf=16", "ctx=stm" 
-#define WRITEBINARY     "w", "ctx=stm"
-#else
 #define READBINARY	"rb"
 #define WRITEBINARY	"wb"
-#endif
 
 #define REMOVE unlink
 
--- a/src/stat.c
+++ b/src/stat.c
@@ -180,7 +180,7 @@
 eff_t effp;
 {
 	stat_t stat = (stat_t) effp->priv;
-	double amp, scale, srms, freq;
+	double amp, scale, srms = 0, freq;
 	double x, ct;
 
 	ct = stat->read;
--- a/src/tx16w.c
+++ b/src/tx16w.c
@@ -48,9 +48,6 @@
 	LONG	rest;			/* bytes remaining in sample file */
 } *txw_t;
 
-IMPORT float volume, amplitude;
-IMPORT int summary, verbose;
-
 struct WaveHeader_ {
   char filetype[6]; /* = "LM8953", */
   unsigned char
--- a/src/util.c
+++ b/src/util.c
@@ -29,44 +29,29 @@
  */
 
 
-EXPORT float volume = 1.0;	/* expansion coefficient */
-EXPORT int dovolume = 0;
+float volume = 1.0;	/* expansion coefficient */
+int dovolume = 0;
 
-EXPORT float amplitude = 1.0;	/* Largest sample so far */
+int writing = 0;	/* are we writing to a file? */
 
-EXPORT int writing = 0;	/* are we writing to a file? */
-
 /* export flags */
-EXPORT int verbose = 0;	/* be noisy on stderr */
-EXPORT int summary = 0;	/* just print summary of information */
+int verbose = 0;	/* be noisy on stderr */
 
-EXPORT char *myname = 0;
+char *myname = 0;
 
-EXPORT int soxpreview = 0;	/* preview mode */
+int soxpreview = 0;	/* preview mode */
 
 
 void
-#if	defined(__STDC__)
-report(char *fmt, ...)
-#else
-report(va_alist) 
-va_dcl
-#endif
+report(char *fmt, ...) 
 {
 	va_list args;
-#if	!defined(__STDC__)
-	char *fmt;
-#endif
 
 	if (! verbose)
 		return;
+
 	fprintf(stderr, "%s: ", myname);
-#if	!defined(__STDC__)
-	va_start(args);
-	fmt = va_arg(args, char *);
-#else
 	va_start(args, fmt);
-#endif
 	vfprintf(stderr, fmt, args);
 	va_end(args);
 	fprintf(stderr, "\n");
@@ -74,25 +59,13 @@
 
 
 void
-#if	defined(__STDC__)
-warn(char *fmt, ...)
-#else
-warn(va_alist) 
-va_dcl
-#endif
+warn(char *fmt, ...) 
 {
 	va_list args;
-#if	!defined(__STDC__)
-	char *fmt;
-#endif
 
 	fprintf(stderr, "%s: ", myname);
-#if	!defined(__STDC__)
-	va_start(args);
-	fmt = va_arg(args, char *);
-#else
 	va_start(args, fmt);
-#endif
+
 	vfprintf(stderr, fmt, args);
 	va_end(args);
 	fprintf(stderr, "\n");
@@ -99,27 +72,14 @@
 }
 
 void
-#if	defined(__STDC__)
-fail(char *fmt, ...)
-#else
-fail(va_alist) 
-va_dcl
-#endif
+fail(char *fmt, ...) 
 {
 	va_list args;
-#if	!defined(__STDC__)
-	char *fmt;
-#endif
 	extern void cleanup();
 
 	fprintf(stderr, "%s: ", myname);
 
-#if	!defined(__STDC__)
-	va_start(args);
-	fmt = va_arg(args, char *);
-#else
 	va_start(args, fmt);
-#endif
 	vfprintf(stderr, fmt, args);
 	va_end(args);
 	fprintf(stderr, "\n");