History

Bugfixes to trim effect. Allows crossfade script to work without pops.

cbagwell committed 22 years ago

Commit c4a08031 parent d3a8f865 files · +100 −91Browse files

Unified · Split

5 files
  1. Changelog +4 −0
  2. scripts/crossfade_cat.sh +7 −7
  3. src/fade.c +7 −6
  4. src/sox.c +2 −2
  5. src/trim.c +80 −76
Changelog+4−0
@@ -41,6 +41,10 @@ sox-12.17.6 · 40 unmodified lines
4141 to not result in the original file.
4242 o Fixed a possible overflow in lots of effects were MIN
4343 value was treated as -MAX instead of -MAX-1.
44+ o Modifed sox so its OK for effects to not process any
45+ input or output bytes as long as they return ST_EOF.
46+ o Bugfix to trim effect were bytes were left off the end
47+ of the file when output length was specified.
4448
4549 sox-12.17.5
4650 -----------
scripts/crossfade_cat.sh+7−7
@@ -45,21 +45,21 @@ fi · 44 unmodified lines
4545
4646 fade_first_opts=
4747 if [ "$fade_first" == "yes" ]; then
48− fade_first_opts="fade h 0 0:0:$fade_length"
48+ fade_first_opts="fade t 0 0:0:$fade_length"
4949 fi
5050
5151 fade_second_opts=
5252 if [ "$fade_second" == "yes" ]; then
53− fade_second_opts="fade h 0:0:$fade_length"
53+ fade_second_opts="fade t 0:0:$fade_length"
5454 fi
5555
5656 echo "crossfade and concatenate files"
5757 echo
5858 echo "Finding length of $first_file..."
59−first_length=`$SOX "$first_file" 2>&1 -e stat | grep Length | cut -d : -f 2 | cut -d . -f 1 | cut -f 1`
60−echo "Length is $first_length"
59+first_length=`$SOX "$first_file" 2>&1 -e stat | grep Length | cut -d : -f 2 | cut -f 1`
60+echo "Length is $first_length seconds"
6161
62−trim_length=`expr $first_length - $fade_length`
62+trim_length=`echo "$first_length - $fade_length" | bc`
6363
6464 # Get crossfade section from first file and optionally do the fade out
6565 echo "Obtaining $fade_length seconds of fade out portion from $first_file..."
@@ -79,7 +79,7 @@ $SOX song1.wav crossfade.wav song2.wav mix.wav · 13 unmodified lines
7979
8080 echo -e "Removing temporary files...\n"
8181 rm fadeout.wav fadein.wav crossfade.wav song1.wav song2.wav
82−mins=`expr $trim_length / 60`
83−secs=`expr $trim_length % 60`
82+mins=`echo "$trim_length / 60" | bc`
83+secs=`echo "$trim_length % 60" | bc`
8484 echo "The crossfade in mix.wav occurs at around $mins mins $secs secs"
8585
src/fade.c+7−6
@@ -230,8 +230,9 @@ int st_fade_flow(eff_t effp, st_sample_t *ibuf, st_sample_t *obuf, · 229 unmodified lines
230230 t_ibuf = (fade->samplesdone < 0 ? 0 : *ibuf);
231231
232232 if ((fade->samplesdone >= fade->in_start) &&
233− (fade->out_stop == 0 || fade->samplesdone < fade->out_stop))
233+ (!fade->do_out || fade->samplesdone < fade->out_stop))
234234 { /* something to generate output */
235+
235236 if (fade->samplesdone < fade->in_stop)
236237 { /* fade-in phase, increase gain */
237238 *obuf = t_ibuf *
@@ -239,14 +240,11 @@ int st_fade_flow(eff_t effp, st_sample_t *ibuf, st_sample_t *obuf, · 1 unmodified line
239240 fade->in_stop - fade->in_start,
240241 fade->in_fadetype);
241242 } /* endif fade-in */
242−
243− if (fade->samplesdone >= fade->in_stop &&
244− (!fade->do_out || fade->samplesdone < fade->out_start))
243+ else if (!fade->do_out || fade->samplesdone < fade->out_start)
245244 { /* steady gain phase */
246245 *obuf = t_ibuf;
247246 } /* endif steady phase */
248−
249− if (fade->do_out && fade->samplesdone >= fade->out_start)
247+ else
250248 { /* fade-out phase, decrease gain */
251249 *obuf = t_ibuf *
252250 fade_gain(fade->out_stop - fade->samplesdone,
@@ -261,6 +259,9 @@ int st_fade_flow(eff_t effp, st_sample_t *ibuf, st_sample_t *obuf, · 8 unmodified lines
261259 t_output = 0;
262260 } /* endif something to output */
263261
262+ /* samplesdone < 0 means we are inventing samples right now
263+ * and so not consuming (happens when in_start < 0).
264+ */
264265 if (fade->samplesdone >= 0 )
265266 { /* Something to input */
266267 *isamp += 1;
src/sox.c+2−2
@@ -988,10 +988,10 @@ int e; · 987 unmodified lines
988988 efftab[e].olen = odonel + odoner;
989989 done = idonel + idoner + odonel + odoner;
990990 }
991− if (done == 0)
992− st_fail("Effect took & gave no samples!");
993991 if (effstatus == ST_EOF)
994992 return -1;
993+ if (done == 0)
994+ st_fail("Effect took & gave no samples!");
995995 return 1;
996996 }
997997
src/trim.c+80−76
@@ -45,41 +45,41 @@ int st_trim_getopts(eff_t effp, int n, char **argv) · 44 unmodified lines
4545 * time related strings.
4646 */
4747 switch (n) {
48− case 2:
49− trim->length_str = malloc(strlen(argv[1])+1);
50− if (!trim->length_str)
51− {
52− st_fail("Could not allocate memory");
53− return(ST_EOF);
54− }
55− strcpy(trim->length_str,argv[1]);
56− /* Do a dummy parse to see if it will fail */
57− if (st_parsesamples(0, trim->length_str,
58− &trim->length, 't') != ST_SUCCESS)
59− {
60− st_fail(TRIM_USAGE);
61− return(ST_EOF);
62− }
63− case 1:
64− trim->start_str = malloc(strlen(argv[0])+1);
65− if (!trim->start_str)
66− {
67− st_fail("Could not allocate memory");
68− return(ST_EOF);
69− }
70− strcpy(trim->start_str,argv[0]);
71− /* Do a dummy parse to see if it will fail */
72− if (st_parsesamples(0, trim->start_str,
73− &trim->start, 't') != ST_SUCCESS)
74− {
75− st_fail(TRIM_USAGE);
76− return(ST_EOF);
77− }
78− break;
79− default:
80− st_fail(TRIM_USAGE);
81− return ST_EOF;
82− break;
48+ case 2:
49+ trim->length_str = malloc(strlen(argv[1])+1);
50+ if (!trim->length_str)
51+ {
52+ st_fail("Could not allocate memory");
53+ return(ST_EOF);
54+ }
55+ strcpy(trim->length_str,argv[1]);
56+ /* Do a dummy parse to see if it will fail */
57+ if (st_parsesamples(0, trim->length_str,
58+ &trim->length, 't') != ST_SUCCESS)
59+ {
60+ st_fail(TRIM_USAGE);
61+ return(ST_EOF);
62+ }
63+ case 1:
64+ trim->start_str = malloc(strlen(argv[0])+1);
65+ if (!trim->start_str)
66+ {
67+ st_fail("Could not allocate memory");
68+ return(ST_EOF);
69+ }
70+ strcpy(trim->start_str,argv[0]);
71+ /* Do a dummy parse to see if it will fail */
72+ if (st_parsesamples(0, trim->start_str,
73+ &trim->start, 't') != ST_SUCCESS)
74+ {
75+ st_fail(TRIM_USAGE);
76+ return(ST_EOF);
77+ }
78+ break;
79+ default:
80+ st_fail(TRIM_USAGE);
81+ return ST_EOF;
82+ break;
8383
8484 }
8585 return (ST_SUCCESS);
@@ -93,24 +93,25 @@ int st_trim_start(eff_t effp) · 7 unmodified lines
9393 trim_t trim = (trim_t) effp->priv;
9494
9595 if (st_parsesamples(effp->ininfo.rate, trim->start_str,
96− &trim->start, 't') != ST_SUCCESS)
96+ &trim->start, 't') != ST_SUCCESS)
9797 {
98− st_fail(TRIM_USAGE);
99− return(ST_EOF);
98+ st_fail(TRIM_USAGE);
99+ return(ST_EOF);
100100 }
101101 /* Account for # of channels */
102102 trim->start *= effp->ininfo.channels;
103+
103104 if (trim->length_str)
104105 {
105− if (st_parsesamples(effp->ininfo.rate, trim->length_str,
106− &trim->length, 't') != ST_SUCCESS)
107− {
108− st_fail(TRIM_USAGE);
109− return(ST_EOF);
110− }
106+ if (st_parsesamples(effp->ininfo.rate, trim->length_str,
107+ &trim->length, 't') != ST_SUCCESS)
108+ {
109+ st_fail(TRIM_USAGE);
110+ return(ST_EOF);
111+ }
111112 }
112113 else
113− trim->length = 0;
114+ trim->length = 0;
114115
115116 /* Account for # of channels */
116117 trim->length *= effp->ininfo.channels;
@@ -144,35 +145,35 @@ int st_trim_flow(eff_t effp, st_sample_t *ibuf, st_sample_t *obuf, · 27 unmodified lines
144145 * If so then we can skip trimming from the front side.
145146 */
146147 if (!trim->trimmed) {
147− if ((trim->index+done) <= trim->start) {
148− /* If we haven't read more then "start" samples, return that
149− * we've read all this buffer without outputing anything
150− */
151− *osamp = 0;
152− *isamp = done;
153− trim->index += done;
154− return (ST_SUCCESS);
155− } else {
156− start_trim = 1;
157− /* We've read at least "start" samples. Now find
158− * out where our target data begins and subtract that
159− * from the total to be copied this round.
160− */
161− offset = trim->start - trim->index;
162− done -= offset;
163− }
148+ if ((trim->index+done) <= trim->start) {
149+ /* If we haven't read more then "start" samples, return that
150+ * we've read all this buffer without outputing anything
151+ */
152+ *osamp = 0;
153+ *isamp = done;
154+ trim->index += done;
155+ return (ST_SUCCESS);
156+ } else {
157+ start_trim = 1;
158+ /* We've read at least "start" samples. Now find
159+ * out where our target data begins and subtract that
160+ * from the total to be copied this round.
161+ */
162+ offset = trim->start - trim->index;
163+ done -= offset;
164+ }
164165 } /* !trimmed */
165166
166167 if (trim->trimmed || start_trim) {
167− if (trim->length && ((trim->trimmed+done) >= trim->length)) {
168− /* Since we know the end is in this block, we set done
169− * to the desired length less the amount already read.
170− */
171− done = trim->length - trim->trimmed;
172− finished = 1;
173− }
174−
175− trim->trimmed += done;
168+ if (trim->length && ((trim->trimmed+done) >= trim->length)) {
169+ /* Since we know the end is in this block, we set done
170+ * to the desired length less the amount already read.
171+ */
172+ done = trim->length - trim->trimmed;
173+ finished = 1;
174+ }
175+
176+ trim->trimmed += done;
176177 }
177178
178179 memcpy(obuf, ibuf+offset, done * sizeof(st_sample_t));
@@ -181,10 +182,13 @@ int st_trim_flow(eff_t effp, st_sample_t *ibuf, st_sample_t *obuf, · 2 unmodified lines
181182 *isamp = offset + done;
182183 trim->index += done;
183184
184− if (finished)
185− return (ST_EOF);
185+ /* return ST_EOF when nothing consumed and we detect
186+ * we are finished.
187+ */
188+ if (finished && !done)
189+ return (ST_EOF);
186190 else
187− return (ST_SUCCESS);
191+ return (ST_SUCCESS);
188192 }
189193
190194 /*
@@ -196,9 +200,9 @@ int st_trim_stop(eff_t effp) · 5 unmodified lines
196200 trim_t trim = (trim_t) effp->priv;
197201
198202 if (trim->start_str)
199− free(trim->start_str);
203+ free(trim->start_str);
200204 if (trim->length_str)
201− free(trim->length_str);
205+ free(trim->length_str);
202206
203207 return (ST_SUCCESS);
204208 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t