shithub: sox

Download patch

ref: 1790c203bebbe868bd4f5b649b32d5367ea28b4a
parent: 987af8fb735b57090361b6ecc5a1916cb386c2f6
author: cbagwell <cbagwell>
date: Fri Aug 31 14:36:07 EDT 2001

Finished moving trim, fade, and synth effect to use new
parsesamples function.  Still need to document.

--- a/src/fade.c
+++ b/src/fade.c
@@ -21,6 +21,7 @@
 #define FADE_PAR        'p'     /* Inverted parabola. */
 
 #include <math.h>
+#include <string.h>
 #include "st.h"
 
 /* Private data for fade file */
@@ -83,6 +84,13 @@
         return (ST_EOF);
     }
     strcpy(fade->in_stop_str,argv[0]);
+    /* Do a dummy parse to see if it will fail */
+    if (st_parsesamples(0, fade->in_stop_str, &fade->in_stop, 't') !=
+	    ST_SUCCESS)
+    {
+	st_fail(FADE_USAGE);
+	return(ST_EOF);
+    }
 
     fade->out_start_str = fade->out_stop_str = 0;
 
@@ -99,6 +107,14 @@
                 return (ST_EOF);
             }
              strcpy(fade->out_start_str,argv[t_argno]);
+
+	     /* Do a dummy parse to see if it will fail */
+	     if (st_parsesamples(0, fade->out_start_str, 
+			 &fade->out_start, 't') != ST_SUCCESS)
+	     {
+		 st_fail(FADE_USAGE);
+		 return(ST_EOF);
+	     }
         }
         else
         {
@@ -109,6 +125,14 @@
                 return (ST_EOF);
             }
              strcpy(fade->out_stop_str,argv[t_argno]);
+
+	     /* Do a dummy parse to see if it will fail */
+	     if (st_parsesamples(0, fade->out_stop_str, 
+			 &fade->out_stop, 't') != ST_SUCCESS)
+	     {
+		 st_fail(FADE_USAGE);
+		 return(ST_EOF);
+	     }
         }
     } /* End for(t_argno) */
 
@@ -145,6 +169,7 @@
             st_fail(FADE_USAGE);
             return(ST_EOF);
         }
+	fade->out_stop += fade->out_start;
     }
     else
         fade->out_stop = fade->out_start;
--- a/src/st.h
+++ b/src/st.h
@@ -338,7 +338,6 @@
 void st_initformat(ft_t ft);
 void st_copyformat(ft_t, ft_t);
 int st_checkformat(ft_t);
-double st_parsetime(char *);
 int st_parsesamples(ULONG rate, char *str, ULONG *samples, char def);
 
 /* FIXME: Recording hacks shouldn't display a "sigint" style interface.
--- a/src/synth.c
+++ b/src/synth.c
@@ -153,7 +153,7 @@
 /* Private data for the synthesizer */
 typedef struct synthstuff {
     /* options */
-    double time; /* length in sec */
+    char *length_str;
     int type[MAXCHAN];
     int mix[MAXCHAN];
     double freq[MAXCHAN];
@@ -162,9 +162,9 @@
 
     /* internal stuff */
     LONG max;
-    LONG samples_done;
+    ULONG samples_done;
     int rate;
-    LONG length; /* length in number of samples */
+    ULONG length; /* length in number of samples */
     double h[MAXCHAN]; /* store values necessary for  creation */
     PinkNoise pinkn[MAXCHAN];
 } *synth_t;
@@ -245,7 +245,7 @@
 
     /* set default parameters */
     synth->length = 0; /* use length of input file */
-    synth->time = 0.0;
+    synth->length_str = 0;
     synth->max = LONG_MAX;
     for(c=0;c<MAXCHAN;c++){
 	synth->freq[c] = 440.0;
@@ -273,11 +273,19 @@
 
     /* read length if given ( if first par starts with digit )*/
     if( isdigit((int)argv[argn][0])) {
-	synth->time = st_parsetime(argv[argn]);
-	if (synth->time < 0.0){
-	    st_warn("synth: illegal time");
+	synth->length_str = malloc(strlen(argv[argn])+1);
+	if (!synth->length_str)
+	{
+	    st_fail("Could not allocate memeory");
+	    return(ST_EOF);
+	}
+	strcpy(synth->length_str,argv[argn]);
+	/* Do a dummy parse of to see if it will fail */
+	if (st_parsesamples(0, synth->length_str, &synth->length, 't') !=
+		ST_SUCCESS)
+	{
 	    st_fail(usstr);
-		return(ST_EOF);
+	    return (ST_EOF);
 	}
 	argn++;
     }
@@ -410,9 +418,6 @@
 	parmcopy(synth,1,3);
     }
 
-
-
-
     return (ST_SUCCESS);
 }
 
@@ -427,13 +432,16 @@
     int i;
     int c;
     synth_t synth = (synth_t) effp->priv;
+    char *usstr=USSTR;
 
-    /* calc length in number of samples... */
-    synth->length = (double)effp->ininfo.rate * synth->time;
-
-    if (synth->length < 0){
-	st_fail("synth: start must be positive");
-	return(ST_EOF);
+    if (synth->length_str)
+    {
+	if (st_parsesamples(effp->ininfo.rate, synth->length_str,
+		            &synth->length, 't') != ST_SUCCESS)
+	{
+	    st_fail(usstr);
+	    return(ST_EOF);
+	}
     }
 
     synth->samples_done=0;
--- a/src/trim.c
+++ b/src/trim.c
@@ -21,16 +21,18 @@
 
 typedef struct
 {
+    /* options here */
+    char *start_str;
+    char *length_str;
 
-        /* options here */
-        LONG start;
-        LONG length;
+    /* options converted to values */
+    ULONG start;
+    ULONG length;
 
-        /* internal stuff */
-        LONG index;
-        LONG trimmed;
-        int done;
-
+    /* internal stuff */
+    ULONG index;
+    ULONG trimmed;
+    int done;
 } * trim_t;
 
 /*
@@ -41,43 +43,53 @@
 int n;
 char **argv;
 {
-        trim_t trim = (trim_t) effp->priv;
-	double time;
+    trim_t trim = (trim_t) effp->priv;
 
-        trim->start = 0;
-        trim->length = 0;
+    trim->start = 0;
+    trim->length = 0;
 
-        switch (n) {
-            case 2:
-			time = st_parsetime(argv[1]);
-			if (time >= 0.0)
-			{
-                            trim->length = time * TIMERES;
-			}
-			else
-			{
-			    st_fail(TRIM_USAGE);
-			    return(ST_EOF);
-			}
-            case 1:
-			time = st_parsetime(argv[0]);
-			if (time >= 0.0)
-			{
-                            trim->start = time * TIMERES;
-			}
-			else
-			{
-			    st_fail(TRIM_USAGE);
-			    return(ST_EOF);
-			}
-                        break;
-                default:
-                        st_fail(TRIM_USAGE);
-                        return ST_EOF;
-                        break;
+    /* Do not know sample rate yet so hold off on completely parsing
+     * time related strings.
+     */
+    switch (n) {
+	case 2:
+	    trim->length_str = malloc(strlen(argv[1])+1);
+	    if (!trim->length_str)
+	    {
+		st_fail("Could not allocate memory");
+		return(ST_EOF);
+	    }
+	    strcpy(trim->length_str,argv[1]);
+	    /* Do a dummy parse to see if it will fail */
+	    if (st_parsesamples(0, trim->length_str,
+			        &trim->length, 't') != ST_SUCCESS)
+	    {
+		st_fail(TRIM_USAGE);
+		return(ST_EOF);
+	    }
+	case 1:
+	    trim->start_str = malloc(strlen(argv[0])+1);
+	    if (!trim->start_str)
+	    {
+		st_fail("Could not allocate memory");
+		return(ST_EOF);
+	    }
+	    strcpy(trim->start_str,argv[0]);
+	    /* Do a dummy parse to see if it will fail */
+	    if (st_parsesamples(0, trim->start_str,
+			        &trim->start, 't') != ST_SUCCESS)
+	    {
+		st_fail(TRIM_USAGE);
+		return(ST_EOF);
+	    }
+	    break;
+	default:
+	    st_fail(TRIM_USAGE);
+	    return ST_EOF;
+	    break;
 
-        }
-        return (ST_SUCCESS);
+    }
+    return (ST_SUCCESS);
 }
 
 /*
@@ -86,25 +98,26 @@
 int st_trim_start(effp)
 eff_t effp;
 {
-        trim_t trim = (trim_t) effp->priv;
+    trim_t trim = (trim_t) effp->priv;
 
 
-        trim->start = (double)effp->ininfo.channels * effp->ininfo.rate * trim->start / TIMERES;
-        if (trim->start < 0) 
-        {
-                st_fail("trim: start must be positive");
-        }
+    if (st_parsesamples(effp->ininfo.rate, trim->start_str,
+		        &trim->start, 't') != ST_SUCCESS)
+    {
+	st_fail(TRIM_USAGE);
+	return(ST_EOF);
+    }
+    if (st_parsesamples(effp->ininfo.rate, trim->length_str,
+		        &trim->length, 't') != ST_SUCCESS)
+    {
+	st_fail(TRIM_USAGE);
+	return(ST_EOF);
+    }
 
-	trim->length = (double)effp->ininfo.channels * effp->ininfo.rate * trim->length / TIMERES;
-        if (trim->length < 0) 
-        {
-                st_fail("trim: length must be positive");
-        }
+    trim->done = 0;
+    trim->index = 0;
+    trim->trimmed = 0;
 
-        trim->done = 0;
-        trim->index = 0;
-        trim->trimmed = 0;
-
     return (ST_SUCCESS);
 }
 
@@ -120,47 +133,47 @@
 LONG *ibuf, *obuf;
 LONG *isamp, *osamp;
 {
-	int done;
-	int offset;
-	int start_trim = 0;
+    int done;
+    int offset;
+    int start_trim = 0;
 
-	trim_t trim = (trim_t) effp->priv;
-	
-	done = ((*isamp < *osamp) ? *isamp : *osamp);
-	*isamp = done; /* always read this much */
+    trim_t trim = (trim_t) effp->priv;
 
-	if (trim->done) {
-		*osamp = 0;
-		return (ST_EOF);
-	}
+    done = ((*isamp < *osamp) ? *isamp : *osamp);
+    *isamp = done; /* always read this much */
 
-	trim->index += done;
-	offset = 0;
-	if (! trim->trimmed) {
-		if (trim->start > trim->index) {
-			*osamp = 0;
-			return (ST_SUCCESS);
-		} else {
-			start_trim = 1;
-			offset =  (trim->start==0? 0: trim->index - trim->start);
-			done -= offset; /* adjust done */
-		}
+    if (trim->done) {
+	*osamp = 0;
+	return (ST_EOF);
+    }
+
+    trim->index += done;
+    offset = 0;
+    if (! trim->trimmed) {
+	if (trim->start > trim->index) {
+	    *osamp = 0;
+	    return (ST_SUCCESS);
+	} else {
+	    start_trim = 1;
+	    offset =  (trim->start==0? 0: trim->index - trim->start);
+	    done -= offset; /* adjust done */
 	}
+    }
 
-	if (trim->trimmed || start_trim ) {
+    if (trim->trimmed || start_trim ) {
 
-		if (trim->length && ( (trim->trimmed+done) > trim->length)) {
-			done = trim->length - trim->trimmed ;
-			*osamp = done;
-			trim->done = 1;
-		}
-
-		trim->trimmed += done;
+	if (trim->length && ( (trim->trimmed+done) > trim->length)) {
+	    done = trim->length - trim->trimmed ;
+	    *osamp = done;
+	    trim->done = 1;
 	}
 
-	memcpy(obuf, ibuf+offset, done * sizeof(LONG));
-	*osamp = done;
-	return (ST_SUCCESS);
+	trim->trimmed += done;
+    }
+
+    memcpy(obuf, ibuf+offset, done * sizeof(LONG));
+    *osamp = done;
+    return (ST_SUCCESS);
 }
 
 /*
@@ -170,8 +183,13 @@
 int st_trim_stop(effp)
 eff_t effp;
 {
+    trim_t trim = (trim_t) effp->priv;
 
-        /* nothing to do */
+    if (trim->start_str)
+	free(trim->start_str);
+    if (trim->length_str)
+	free(trim->length_str);
+
     return (ST_SUCCESS);
 }
 
--- a/src/util.c
+++ b/src/util.c
@@ -490,7 +490,7 @@
     float frac = 0;
 
     if (strchr(str, ':') || strchr(str, '.') || str[strlen(str)-1] == 't')
-        found_time = 0;
+        found_time = 1;
     else if (str[strlen(str)-1] == 's')
         found_samples = 1;
 
@@ -502,12 +502,12 @@
         {
             if (sscanf(str, "%d", &time) != 1)
                 return ST_EOF;
-            samples += time;
+            *samples += time;
 
-            while (*str != ':' && *str != '.' && str != NULL)
+            while (*str != ':' && *str != '.' && *str != 0)
                 str++;
 
-            if (*str == '.' || str == NULL)
+            if (*str == '.' || *str == 0)
                 break;
 
             /* Skip past ':' */
@@ -517,10 +517,8 @@
 
         if (*str == '.')
         {
-            str++;
-            if (sscanf(str, "%d", &time) != 1)
+            if (sscanf(str, "%f", &frac) != 1)
                 return ST_EOF;
-            frac = time / 1000;
         }
 
         *samples *= rate;
@@ -535,29 +533,4 @@
         return ST_SUCCESS;
     }
     return ST_EOF;
-}
-
-/* Parse a time specification in hh:mm:ss.frac format.  Returns -1 */
-/* on failure. */
-double st_parsetime(char *str)
-{
-    double time, moretime;
-    if (sscanf(str, "%lf", &time) != 1)
-        return -1;
-    str = strchr(str, ':');
-    if (str == NULL)
-        return time;
-    str++;                              /* Skip colon */
-    time *= 60.0;
-    if (sscanf(str, "%lf", &moretime) != 1)
-        return -1;
-    time += moretime;
-    str = strchr(str, ':');
-    if (str == NULL)
-        return time;
-    str++;                              /* Skip colon */
-    time *= 60.0;
-    if (sscanf(str, "%lf", &moretime) != 1)
-        return -1;
-    return time + moretime;
 }