History

Restructure code so that restarting the effects chain can completely re-create the effects without memory leaks. In mult-output mode, only restart effects that do not have samples left. Framework now can support using different effects and options for each file processed. Also, removed attempt to optimatize when rate/mixer were auto-inserted because it was unpredicatable/confusing at how it affected longer chains.

cbagwell committed 18 years ago

Commit 6fa0f571 parent 7e27d2e7 files · +255 −110Browse files

Unified · Split

7 files
  1. ChangeLog +1 −0
  2. libsox.3 +46 −12
  3. src/effects.c +26 −8
  4. src/example0.c +1 −2
  5. src/example1.c +2 −3
  6. src/sox.c +174 −82
  7. src/sox.h +5 −3
ChangeLog+1−0
@@ -49,6 +49,7 @@ Other bug fixes: · 48 unmodified lines
4949 Internal improvements:
5050
5151 o Fixed all compiler warnings (with gcc 4.3.1, 64-bit arch.). (robs)
52+ o Updates to internal effects chain API.
5253
5354
5455 sox-14.1.0 2008-7-29
libsox.3+46−12
@@ -38,6 +38,8 @@ libsox \- SoX, an audio file-format and effect library · 37 unmodified lines
3838 .P
3939 .B int sox_format_init(void);
4040 .P
41+.B void sox_format_quit(void);
42+.P
4143 .B sox_format_t sox_open_read(const char *\fIpath\fB, const sox_signalinfo_t *\fIinfo\fB, const char *\fIfiletype\fB);
4244 .P
4345 .B sox_format_t sox_open_write(sox_bool (*\fIoverwrite_permitted\fB)(const char *\fIfilename\fB), const char *\fIpath\fB, const sox_signalinfo_t *\fIinfo\fB, const char *\fIfiletype\fB, const char *\fIcomment\fB, sox_size_t \fIlength\fB, const sox_instrinfo_t *\fIinstr\fB, const sox_loopinfo_t *\fIloops\fB);
@@ -52,7 +54,15 @@ libsox \- SoX, an audio file-format and effect library · 8 unmodified lines
5254 .P
5355 .B sox_effect_handler_t const *sox_find_effect(char const *\fIname\fB);
5456 .P
55−.B void sox_create_effect(sox_effect_t \fIeffp\fB, sox_effect_handler_t const *\fIe\fB);
57+.B sox_effect_t *sox_create_effect(sox_effect_handler_t const *\fIeh\fB);
58+.P
59+.B int sox_effect_options(sox_effect_t *\fIeffp\fB, int \fIargc\fB, char * const \fIargv[]\fB);
60+.P
61+.B sox_effects_chain_t *sox_create_effects_chain(sox_encodinginfo_t const *\fIin_enc\fB, sox_encodinginfo_t const *\fIout_enc\fB);
62+.P
63+.B void sox_delete_effects_chain(sox_effects_chain_t *\fIecp\fB);
64+.P
65+.B int sox_add_effect(sox_effects_chaint_t *\fIchain\fB, sox_effect_t*\fIeffp\fB, sox_signalinfo_t *\fIin\fB, sox_signalinfo-t const *\fIout\fB);
5666 .P
5767 .B cc \fIfile.c\fB -o \fIfile \f-lsox
5868 .fi
@@ -68,6 +78,9 @@ library support then this will detect and initialize all external · 9 unmodified lines
6878 libraries. This should be called before any other file operations
6979 are performed.
7080 .P
81+\fBsox_format_quit\fR function performs some required cleanup
82+related to all file format handlers.
83+.P
7184 \fBsox_open_input\fR function opens the file for reading whose name is
7285 the string pointed to by \fIpath\fR and associates an sox_format_t with it. If
7386 \fIinfo\fR is non-NULL then it will be used to specify the data format
@@ -109,9 +122,6 @@ The \fBsox_close\fR function dissociates the named \fIsox_format_t\fR from its · 35 unmodified lines
109122 underlying file or set of functions. If the format handler was being
110123 used for output, any buffered data is written first.
111124 .P
112−\fBsox_format_quite\fR function performs some required cleanup
113−related to all file format handlers.
114−.P
115125 The function \fBsox_find_effect\fR finds effect \fIname\fR, returning
116126 a pointer to its \fIsox_effect_handler_t\fR if it exists, and NULL
117127 otherwise.
@@ -121,16 +131,40 @@ The function \fBsox_create_effect\fR instantiates an effect into a · 3 unmodified lines
121131 methods are automatically set to the corresponding \fBnothing\fR
122132 method.
123133 .P
124−The \fBsox_update_effect\fR function copies input and output signal
125−info into effect structures. The \fIeffect_mask\fR parameter is the
126−return value of the previous call to this function; for the first
127−call, pass 0. The function returns the updated effect mask.
134+The function \fBsox_effect_options\fR allows passing options into the effect to control its behavior. It will return SOX_EOF if there were any invalid options passed in. On success, the \fIeffp->in_signal\fR will optional contain the rate and channel count it requires input data from and \fIeffp->out_signal\fR will optionally contain the rate and channel count it outputs in. When present, this information should be used to make sure appropriate effects are placed in the effects chain to handle any needed conversions.
135+.P
136+Passing in options is currently only supported when they are passed in before the effect is ever started. The behavior is undefined if its called once the effect is started.
137+.P
138+\fBsox_create_effects_chain\fR will instantiate an effects chain that
139+effects can be added to. \fIin_enc\fR and \fIout_enc\fR are the
140+signal encoding of the input and output of the chain respectively.
141+The pointers to \fIin_enc\fR and \fIout_enc\fR
142+are stored internally and so their memory should not be freed. Also,
143+it is OK if their values change over time to reflect new input or
144+output encodings as they are referenced only as effects
145+start up or are restarted.
146+.P
147+\fBsox_delete_effects_chain\fR will release any resources reserved during
148+the creation of the chain. This will also call \fBsox_delete_effects\fR
149+if any effects are still in the chain.
150+.P
151+\fBsox_add_effect\fR adds an effect to the chain. \fIin\fR specifies the input
152+signal info for this effect. \fIout\fR is a suggestion
153+as to what the output signal should be but depending on the effects
154+given options and on \fIin\fR the effect can choose to do differently.
155+Whatever output rate and channels the effect does produce are written
156+back to \fIin\fR. It is meant that \fIin\fR be stored and passed to each
157+new call to \fBsox_add_effect\fR so that changes will be propagated to each new effect.
128158 .P
129159 SoX includes skeleton C files to assist you in writing new
130−formats (skelform.c) and effects (skeleff.c). sox.c itself is a good
131−starting point for new programs. Note that new formats can often just
132−deal with the header and then use raw.c's routines for reading and
133−writing.
160+formats (skelform.c) and effects (skeleff.c). Note that new formats
161+can often just deal with the header and then use raw.c's routines
162+for reading and writing.
163+
164+example0.c and example1.c are a good starting point to see how
165+to write applications using libsox. sox.c itself is also a good
166+reference.
167+
134168 .SH RETURN VALUE
135169 Upon successful completion \fBsox_open_input\fR and
136170 \fBsox_open_output\fR return an \fIsox_format_t\fR (which is a pointer).
src/effects.c+26−8
@@ -30,7 +30,7 @@ · 29 unmodified lines
3030
3131 #define DEBUG_EFFECTS_CHAIN 0
3232
33−
33+/* FIXME: Not thread safe using globals */
3434 sox_effects_globals_t sox_effects_globals =
3535 {sox_plot_off, 1, &sox_globals};
3636
@@ -66,7 +66,9 @@ static int default_getopts(sox_effect_t * effp, int argc, char **argv UNUSED) · 29 unmodified lines
6666 sox_effect_t * sox_create_effect(sox_effect_handler_t const * eh)
6767 {
6868 sox_effect_t * effp = lsx_calloc(1, sizeof(*effp));
69− assert(eh);
69+
70+ if (!effp) return NULL;
71+
7072 effp->global_info = &sox_effects_globals;
7173 effp->handler = *eh;
7274 if (!effp->handler.getopts) effp->handler.getopts = default_getopts;
@@ -75,24 +77,40 @@ sox_effect_t * sox_create_effect(sox_effect_handler_t const * eh) · 2 unmodified lines
7577 if (!effp->handler.drain ) effp->handler.drain = default_drain;
7678 if (!effp->handler.stop ) effp->handler.stop = default_function;
7779 if (!effp->handler.kill ) effp->handler.kill = default_function;
80+
7881 effp->priv = lsx_calloc(1, effp->handler.priv_size);
79− return effp;
80−}
82+ if (effp->handler.priv_size && !effp->priv)
83+ {
84+ free(effp);
85+ return NULL;
86+ }
8187
88+ return effp;
89+} /* sox_create_effect */
8290
91+int sox_effect_options(sox_effect_t *effp, int argc, char * const argv[])
92+{
93+ return effp->handler.getopts(effp, argc, argv);
94+} /* sox_effect_options */
8395
8496 /* Effects chain: */
8597
8698 sox_effects_chain_t * sox_create_effects_chain(
87− sox_encodinginfo_t const * in_enc,
88− sox_encodinginfo_t const * out_enc)
99+ sox_encodinginfo_t const * in_enc, sox_encodinginfo_t const * out_enc)
89100 {
90101 sox_effects_chain_t * result = lsx_calloc(1, sizeof(sox_effects_chain_t));
91102 result->global_info = sox_effects_globals;
92103 result->in_enc = in_enc;
93104 result->out_enc = out_enc;
94105 return result;
95−}
106+} /* sox_create_effects_chain */
107+
108+void sox_delete_effects_chain(sox_effects_chain_t *ecp)
109+{
110+ if (ecp && ecp->length)
111+ sox_delete_effects(ecp);
112+ free(ecp);
113+} /* sox_delete_effects_chain */
96114
97115 /* Effect can call in start() or flow() to set minimum input size to flow() */
98116 int sox_effect_set_imin(sox_effect_t * effp, size_t imin)
@@ -378,7 +396,7 @@ size_t sox_stop_effect(sox_effect_t *effp) · 279 unmodified lines
378396 * Note: This currently closes down the effect which might
379397 * note be obvious from name.
380398 */
381−static void sox_delete_effect(sox_effect_t *effp)
399+void sox_delete_effect(sox_effect_t *effp)
382400 {
383401 size_t clips;
384402 unsigned f;
src/example0.c+1−2
@@ -83,10 +83,9 @@ int main(int argc, char * argv[]) · 82 unmodified lines
8383 sox_flow_effects(chain, NULL);
8484
8585 /* All done; tidy up: */
86− sox_delete_effects(chain);
86+ sox_delete_effects_chain(chain);
8787 sox_close(out);
8888 sox_close(in);
8989 sox_format_quit();
90− free(chain);
9190 return 0;
9291 }
src/example1.c+2−3
@@ -51,7 +51,7 @@ static int input_drain( · 50 unmodified lines
5151 }
5252
5353 /* The function that will be called to output samples from the effects chain.
54− * In this example, we store the samples in a SoX-openned audio file.
54+ * In this example, we store the samples in a SoX-opened audio file.
5555 * In a different application, they might perhaps be analysed in some way,
5656 * or displayed as a wave-form */
5757 static int output_flow(sox_effect_t *effp UNUSED, sox_sample_t const * ibuf,
@@ -153,10 +153,9 @@ int main(int argc, char * argv[]) · 95 unmodified lines
153153 sox_flow_effects(chain, NULL);
154154
155155 /* All done; tidy up: */
156− sox_delete_effects(chain);
156+ sox_delete_effects_chain(chain);
157157 sox_close(out);
158158 sox_close(in);
159159 sox_format_quit();
160− free(chain);
161160 return 0;
162161 }
src/sox.c+174−82
@@ -130,9 +130,10 @@ static size_t output_count = 0; · 129 unmodified lines
130130 * a resampling effect, a channel mixing effect, the input, and the output.
131131 */
132132 #define MAX_USER_EFF (SOX_MAX_EFFECTS - 4)
133−static sox_effect_t * user_efftab[MAX_USER_EFF], efftab_options[MAX_USER_EFF];
133+static struct { char *name; int argc; char *argv[256]; } user_effargs[MAX_USER_EFF];
134+static sox_effect_t *user_efftab[MAX_USER_EFF];
134135 static unsigned nuser_effects;
135−static sox_effects_chain_t ofile_effects_chain;
136+static sox_effects_chain_t *effects_chain = NULL;
136137
137138
138139 /* Flowing */
@@ -542,26 +543,78 @@ static sox_effect_handler_t const * output_effect_fn(void) · 403 unmodified lines
542543 return &handler;
543544 }
544545
545−static void add_auto_effect(sox_effects_chain_t * chain, char const * name, char * arg, sox_signalinfo_t * signal)
546+static void add_effect(sox_effects_chain_t *chain, char const *name,
547+ int argc, char *argv[], sox_signalinfo_t *signal)
546548 {
547549 sox_effect_t * effp;
548− char * * argv = & arg;
549550
550551 effp = sox_create_effect(sox_find_effect(name)); /* Should always succeed. */
551552
552− if (effp->handler.getopts(effp, arg != NULL, argv) == SOX_EOF)
553+ if (!effp)
554+ sox_fail("Failed creating effect. Out of Memory?\n");
555+
556+ if (effp->handler.flags & SOX_EFF_DEPRECATED)
557+ sox_warn("effect `%s' is deprecated; see soxeffect(7) for an alternative",
558+ effp->handler.name);
559+
560+ if (sox_effect_options(effp, argc, argv) == SOX_EOF)
553561 exit(1); /* The failing effect should have displayed an error message */
554562
555563 if (sox_add_effect(chain, effp, signal, &ofile->ft->signal) != SOX_SUCCESS)
556564 exit(2); /* The effects chain should have displayed an error message */
557565 }
558566
559−/* If needed effects are not given, auto-add at (performance) optimal point. */
560−static void add_effects(sox_effects_chain_t * chain)
567+/* Creates users effects and passes in user specified options.
568+ * This is done without putting anything into the effects chain
569+ * because an effect may set the effp->in_format and we may want
570+ * to copy that back into the input/combiner before opening and
571+ * inserting it.
572+ * Similarly, we may want to use effp->out_format to override the
573+ * default values of output file before we open it.
574+ * To keep things simple, we create all user effects. Later, when
575+ * we add them, some may already be in the chain and we will need to free
576+ * them.
577+ */
578+static void create_user_effects(void)
579+{
580+ unsigned i;
581+ sox_effect_t *effp;
582+
583+ for (i = 0; i < nuser_effects; i++)
584+ {
585+ effp = sox_create_effect(sox_find_effect(user_effargs[i].name));
586+
587+ if (!effp)
588+ sox_fail("Failed creating effect. Out of Memory?\n");
589+
590+ if (effp->handler.flags & SOX_EFF_DEPRECATED)
591+ sox_warn("effect `%s' is deprecated; see soxeffect(7) for an alternative",
592+ effp->handler.name);
593+
594+ /* The failing effect should have displayed an error message */
595+ if (sox_effect_options(effp, user_effargs[i].argc,
596+ user_effargs[i].argv) == SOX_EOF)
597+ exit(1);
598+
599+ user_efftab[i] = effp;
600+ }
601+}
602+
603+/* Add all user effects to the chain. If the output effect's rate or
604+ * channel count do not match the end of the effects chain then
605+ * insert effects to correct this.
606+ *
607+ * This can also be called on pre-existing effect chains so that it
608+ * will only add effects that are missing off the END of the chain.
609+ * This is useful if an effect went into drain mode and you
610+ * wish to restart it and any effects after it.
611+ */
612+static void add_effects(sox_effects_chain_t *chain)
561613 {
562614 sox_signalinfo_t signal = combiner_signal;
563615 unsigned i, min_chan = 0, min_rate = 0;
564616 sox_effect_t * effp;
617+ unsigned effects_added = 0;
565618 char * rate_arg = sox_mode != sox_play? NULL :
566619 (rate_arg = getenv("PLAY_RATE_ARG"))? rate_arg : "-l";
567620
@@ -572,35 +625,54 @@ static void add_effects(sox_effects_chain_t * chain) · 4 unmodified lines
572625 if (user_efftab[i]->handler.flags & SOX_EFF_RATE)
573626 min_rate = i + 1;
574627 }
575− /* 1st `effect' in the chain is the input combiner_signal */
576− effp = sox_create_effect(input_combiner_effect_fn());
577− sox_add_effect(chain, effp, &signal, &ofile->ft->signal);
628+
629+ effects_added++;
630+ if (effects_added > chain->length)
631+ {
632+ /* 1st `effect' in the chain is the input combiner_signal */
633+ effp = sox_create_effect(input_combiner_effect_fn());
634+ sox_add_effect(chain, effp, &signal, &ofile->ft->signal);
635+ }
578636
579637 /* Add auto effects if appropriate; add user specified effects */
580− for (i = 0; i <= nuser_effects; i++) {
581− /* If reducing channels, it's faster to do so before all other effects: */
582− if (signal.channels > ofile->ft->signal.channels && i >= min_chan)
583− add_auto_effect(chain, "mixer", NULL, &signal);
584−
585− /* If reducing rate, it's faster to do so before all other effects
586− * (except reducing channels): */
587− if (signal.rate > ofile->ft->signal.rate && i >= min_rate)
588− add_auto_effect(chain, "rate", rate_arg, &signal);
589−
590− if (i < nuser_effects)
591− if (sox_add_effect(chain, user_efftab[i], &signal, &ofile->ft->signal) != SOX_SUCCESS)
638+ for (i = 0; i < nuser_effects; i++) {
639+ effects_added++;
640+ if (effects_added > chain->length)
641+ {
642+ /* Effects chain should have displayed an error message */
643+ if (sox_add_effect(chain, user_efftab[i],
644+ &signal, &ofile->ft->signal) != SOX_SUCCESS)
592645 exit(2);
646+ }
647+ else
648+ /* Since already added from previous pass, free the extra copy
649+ * created this pass.
650+ */
651+ sox_delete_effect(user_efftab[i]);
593652 }
653+
594654 /* Add auto effects if still needed at this point */
595655 if (signal.rate != ofile->ft->signal.rate)
596− add_auto_effect(chain, "rate", rate_arg, &signal); /* Must be up-sampling */
656+ {
657+ effects_added++;
658+ if (effects_added > chain->length)
659+ add_effect(chain, "rate", rate_arg != NULL, &rate_arg, &signal); /* Must be up-sampling */
660+ }
597661 if (signal.channels != ofile->ft->signal.channels)
598− add_auto_effect(chain, "mixer", NULL, &signal); /* Must be increasing channels */
599−
600− /* Last `effect' in the chain is the output file */
601− effp = sox_create_effect(output_effect_fn());
602− if (sox_add_effect(chain, effp, &signal, &ofile->ft->signal) != SOX_SUCCESS)
603− exit(2);
662+ {
663+ effects_added++;
664+ if (effects_added > chain->length)
665+ add_effect(chain, "mixer", 0, NULL, &signal); /* Must be increasing channels */
666+ }
667+
668+ effects_added++;
669+ if (effects_added > chain->length)
670+ {
671+ /* Last `effect' in the chain is the output file */
672+ effp = sox_create_effect(output_effect_fn());
673+ if (sox_add_effect(chain, effp, &signal, &ofile->ft->signal) != SOX_SUCCESS)
674+ exit(2);
675+ }
604676
605677 for (i = 0; i < chain->length; ++i) {
606678 sox_effect_t const * effp = &chain->effects[i][0];
@@ -616,7 +688,7 @@ static size_t total_clips(void) · 9 unmodified lines
616688 size_t clips = 0;
617689 for (i = 0; i < file_count; ++i)
618690 clips += files[i]->ft->clips + files[i]->volume_clips;
619− return clips + mixing_clips + sox_effects_clips(&ofile_effects_chain);
691+ return clips + mixing_clips + sox_effects_clips(effects_chain);
620692 }
621693
622694 static sox_bool since(struct timeval * then, double secs, sox_bool always_reset)
@@ -711,15 +783,15 @@ static void optimize_trim(void) · 88 unmodified lines
711783 * to do it for multiple files is complex and problably never used. This
712784 * hack is a huge time savings when trimming gigs of audio data into
713785 * managable chunks. */
714− if (input_count == 1 && ofile_effects_chain.length > 1 && strcmp(ofile_effects_chain.effects[1][0].handler.name, "trim") == 0) {
786+ if (input_count == 1 && effects_chain->length > 1 && strcmp(effects_chain->effects[1][0].handler.name, "trim") == 0) {
715787 if (files[0]->ft->handler.seek && files[0]->ft->seekable){
716− size_t offset = sox_trim_get_start(&ofile_effects_chain.effects[1][0]);
788+ size_t offset = sox_trim_get_start(&effects_chain->effects[1][0]);
717789 if (offset && sox_seek(files[0]->ft, offset, SOX_SEEK_SET) == SOX_SUCCESS) {
718790 read_wide_samples = offset / files[0]->ft->signal.channels;
719791 /* Assuming a failed seek stayed where it was. If the seek worked then
720792 * reset the start location of trim so that it thinks user didn't
721793 * request a skip. */
722− sox_trim_clear_start(&ofile_effects_chain.effects[1][0]);
794+ sox_trim_clear_start(&effects_chain->effects[1][0]);
723795 sox_debug("optimize_trim successful");
724796 }
725797 }
@@ -851,10 +923,24 @@ static void sigint(int s) · 125 unmodified lines
851923 else user_abort = sox_true;
852924 }
853925
854−static void calculate_combiner_and_output_signal_parameters(void)
926+static void calculate_combiner_signal_parameters(void)
855927 {
856− sox_bool known_length = combine_method != sox_sequence;
857− size_t i, olen = 0;
928+ size_t i;
929+
930+ /* If user didn't specify # of channels then see if an effect
931+ * is specifying them. This is of most use currently with the
932+ * synth effect were user can use null input handler and specify
933+ * channel counts directly in effect. Forcing to use -c with
934+ * -n isn't as convenient.
935+ */
936+ for (i = 0; i < input_count; i++) {
937+ unsigned j;
938+ for (j =0; j < nuser_effects && !files[i]->ft->signal.channels; ++j)
939+ files[i]->ft->signal.channels = user_efftab[j]->in_signal.channels;
940+ /* For historical reasons, default to one channel if not specified. */
941+ if (!files[i]->ft->signal.channels)
942+ files[i]->ft->signal.channels = 1;
943+ }
858944
859945 /* Set the combiner output signal attributes to those of the 1st/next input
860946 * file. If we are in sox_sequence mode then we don't need to check the
@@ -884,11 +970,6 @@ static void calculate_combiner_and_output_signal_parameters(void) · 23 unmodified lines
884970 max_channels = max(max_channels, files[i]->ft->signal.channels);
885971 min_rate = min(min_rate , files[i]->ft->signal.rate);
886972 max_rate = max(max_rate , files[i]->ft->signal.rate);
887− known_length = known_length && files[i]->ft->signal.length != 0;
888− if (combine_method == sox_concatenate)
889− olen += files[i]->ft->signal.length / files[i]->ft->signal.channels;
890− else
891− olen = max(olen, files[i]->ft->signal.length / files[i]->ft->signal.channels);
892973 }
893974
894975 /* Check for invalid/unusual rate or channel combinations: */
@@ -910,6 +991,27 @@ static void calculate_combiner_and_output_signal_parameters(void) · 15 unmodified lines
910991 combine_method == sox_merge? total_channels : max_channels;
911992 }
912993
994+ /* Now take account of any net speed change specified by user effects by
995+ * adjusting the nominal sample rate at the output of the combiner: */
996+ combiner_signal.rate *= sox_effects_globals.speed;
997+
998+} /* calculate_combiner_signal_parameters */
999+
1000+static void calculate_output_signal_parameters(void)
1001+{
1002+ sox_bool known_length = combine_method != sox_sequence;
1003+ size_t i, olen = 0;
1004+
1005+ /* Report all input files and gather info on differing rates & numbers of
1006+ * channels, and on the resulting output audio length: */
1007+ for (i = 0; i < input_count; i++) {
1008+ known_length = known_length && files[i]->ft->signal.length != 0;
1009+ if (combine_method == sox_concatenate)
1010+ olen += files[i]->ft->signal.length / files[i]->ft->signal.channels;
1011+ else
1012+ olen = max(olen, files[i]->ft->signal.length / files[i]->ft->signal.channels);
1013+ }
1014+
9131015 /* Determine the output file signal attributes; set from user options
9141016 * if given: */
9151017 ofile->signal = ofile_signal_options;
@@ -928,10 +1030,6 @@ static void calculate_combiner_and_output_signal_parameters(void) · 12 unmodified lines
9281030 /* FIXME: comment this: */
9291031 ofile->signal.precision = combiner_signal.precision;
9301032
931− /* Now take account of any net speed change specified by user effects by
932− * adjusting the nominal sample rate at the output of the combiner: */
933− combiner_signal.rate *= sox_effects_globals.speed;
934−
9351033 /* If any given user effect modifies the audio length, then we assume that
9361034 * we don't know what the output length will be. FIXME: in most cases,
9371035 * an effect that modifies length will be able to determine by how much from
@@ -971,28 +1069,26 @@ static void set_combiner_and_output_encoding_parameters(void) · 33 unmodified lines
9711069
9721070 static int process(void)
9731071 { /* Input(s) -> Balancing -> Combiner -> Effects -> Output */
974− unsigned i;
9751072 int flow_status;
9761073
977− for (i = 0; i < nuser_effects; ++i)
978− *user_efftab[i] = efftab_options[i];
1074+ create_user_effects();
9791075
980− calculate_combiner_and_output_signal_parameters();
1076+ calculate_combiner_signal_parameters();
9811077 set_combiner_and_output_encoding_parameters();
982− open_output_file();
9831078
984− ofile_effects_chain.length = 0;
1079+ calculate_output_signal_parameters();
1080+ open_output_file();
9851081
986− ofile_effects_chain.global_info = sox_effects_globals;
987− ofile_effects_chain.in_enc = &combiner_encoding;
988− ofile_effects_chain.out_enc = &ofile->ft->encoding;
989− add_effects(&ofile_effects_chain);
1082+ if (!effects_chain)
1083+ effects_chain = sox_create_effects_chain(&combiner_encoding,
1084+ &ofile->ft->encoding);
1085+ add_effects(effects_chain);
9901086
9911087 optimize_trim();
9921088
9931089 signal(SIGTERM, sigint); /* Stop gracefully, as soon as we possibly can. */
9941090 signal(SIGINT , sigint); /* Either skip current input or behave as SIGTERM. */
995− flow_status = sox_flow_effects(&ofile_effects_chain, update_status);
1091+ flow_status = sox_flow_effects(effects_chain, update_status);
9961092
9971093 /* When in sox_multiple mode, changing to a new output file is
9981094 * based on effects return ST_EOF. In that case, don't return
@@ -1674,7 +1770,6 @@ static void parse_options_and_filenames(int argc, char **argv) · 675 unmodified lines
16741770 static void parse_effects(int argc, char **argv)
16751771 {
16761772 for (nuser_effects = 0; optind < argc; ++nuser_effects) {
1677− sox_effect_t * e;
16781773 int i;
16791774
16801775 if (nuser_effects >= MAX_USER_EFF) {
@@ -1683,17 +1778,12 @@ static void parse_effects(int argc, char **argv) · 2 unmodified lines
16831778 }
16841779
16851780 /* Name should always be correct! */
1686− e = sox_create_effect(sox_find_effect(argv[optind++]));
1687−
1688− for (i = 0; i < argc - optind && !sox_find_effect(argv[optind + i]); ++i);
1689− if (e->handler.getopts(e, i, &argv[optind]) == SOX_EOF)
1690− exit(1); /* The failing effect should have displayed an error message */
1781+ user_effargs[nuser_effects].name = strdup(argv[optind++]);
1782+ for (i = 0; i < argc - optind && !sox_find_effect(argv[optind + i]); ++i)
1783+ user_effargs[nuser_effects].argv[i] = strdup(argv[optind + i]);
1784+ user_effargs[nuser_effects].argc = i;
16911785
16921786 optind += i; /* Skip past the effect arguments */
1693−
1694− if (e->handler.flags & SOX_EFF_DEPRECATED)
1695− sox_warn("effect `%s' is deprecated; see soxeffect(7) for an alternative", e->handler.name);
1696− user_efftab[nuser_effects] = e;
16971787 }
16981788 }
16991789
@@ -1898,15 +1988,6 @@ int main(int argc, char **argv) · 198 unmodified lines
18981988 exit(0);
18991989 }
19001990
1901− /* Bit of a hack: input files can get # of chans from an effect */
1902− for (i = 0; i < input_count; i++) {
1903− unsigned j;
1904− for (j =0; j < nuser_effects && !files[i]->ft->signal.channels; ++j)
1905− files[i]->ft->signal.channels = user_efftab[j]->in_signal.channels;
1906− if (!files[i]->ft->signal.channels)
1907− ++files[i]->ft->signal.channels;
1908− }
1909−
19101991 if (sox_globals.repeatable)
19111992 sox_debug("Not reseeding PRNG; randomness is repeatable");
19121993 else {
@@ -1920,8 +2001,6 @@ int main(int argc, char **argv) · 7 unmodified lines
19202001 * block of input files. Also used by sox_multiple output mode. */
19212002 ofile_signal_options = ofile->signal;
19222003 ofile_encoding_options = ofile->encoding;
1923− for (i = 0; i < nuser_effects; ++i)
1924− efftab_options[i] = *user_efftab[i];
19252004
19262005 if (combine_method == sox_sequence || output_method == sox_multiple) do {
19272006 /* If ofile->ft is set then we've called this at least once.
@@ -1935,16 +2014,29 @@ int main(int argc, char **argv) · 7 unmodified lines
19352014 * If input reached EOF then restart all effects.
19362015 */
19372016 if (output_method == sox_multiple && !input_eof)
1938− /* FIXME: Make a function that does that */
1939− ;
1940− else if (output_method == sox_multiple)
1941− sox_delete_effects(&ofile_effects_chain);
2017+ {
2018+ size_t e;
2019+
2020+ /* Scan backwards until data is found and do not free input effect */
2021+ for (e = effects_chain->length-1; e > 0; --e)
2022+ {
2023+ if (effects_chain->effects[e]->obeg !=
2024+ effects_chain->effects[e]->oend)
2025+ break;
2026+ sox_delete_effect(effects_chain->effects[e]);
2027+ effects_chain->effects[e] = NULL;
2028+ effects_chain->length--;
2029+ }
2030+ }
2031+ else
2032+ sox_delete_effects(effects_chain);
19422033 sox_close(ofile->ft);
19432034 ofile->ft = NULL;
19442035 }
19452036 } while (process() != SOX_EOF && !user_abort && current_input < input_count);
19462037 else process();
1947− sox_delete_effects(&ofile_effects_chain);
2038+
2039+ sox_delete_effects_chain(effects_chain);
19482040
19492041 for (i = 0; i < file_count; ++i)
19502042 if (files[i]->ft->clips != 0)
src/sox.h+5−3
@@ -476,6 +476,7 @@ struct sox_effect { · 475 unmodified lines
476476
477477 sox_effect_handler_t const * sox_find_effect(char const * name);
478478 sox_effect_t * sox_create_effect(sox_effect_handler_t const * eh);
479+int sox_effect_options(sox_effect_t *effp, int argc, char * const argv[]);
479480
480481 /* Effects chain */
481482
@@ -493,13 +494,14 @@ struct sox_effects_chain { · 11 unmodified lines
493494 };
494495 typedef struct sox_effects_chain sox_effects_chain_t;
495496 sox_effects_chain_t * sox_create_effects_chain(
496− sox_encodinginfo_t const * in_enc,
497− sox_encodinginfo_t const * out_enc);
497+ sox_encodinginfo_t const * in_enc, sox_encodinginfo_t const * out_enc);
498+void sox_delete_effects_chain(sox_effects_chain_t *ecp);
498499 int sox_add_effect( sox_effects_chain_t * chain, sox_effect_t * effp, sox_signalinfo_t * in, sox_signalinfo_t const * out);
499500 int sox_flow_effects(sox_effects_chain_t *, int (* callback)(sox_bool all_done));
500501 size_t sox_effects_clips(sox_effects_chain_t *);
501502 size_t sox_stop_effect(sox_effect_t *effp);
502−void sox_delete_effects(sox_effects_chain_t *);
503+void sox_delete_effect(sox_effect_t *effp);
504+void sox_delete_effects(sox_effects_chain_t *chain);
503505
504506 /* The following routines are unique to the trim effect.
505507 * sox_trim_get_start can be used to find what is the start

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