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
49Internal improvements:49Internal improvements:
50 50
51 o Fixed all compiler warnings (with gcc 4.3.1, 64-bit arch.). (robs)51 o Fixed all compiler warnings (with gcc 4.3.1, 64-bit arch.). (robs)
52 o Updates to internal effects chain API.
52 53
53 54
54sox-14.1.0 2008-7-2955sox-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
38.P38.P
39.B int sox_format_init(void);39.B int sox_format_init(void);
40.P40.P
41.B void sox_format_quit(void);
42.P
41.B sox_format_t sox_open_read(const char *\fIpath\fB, const sox_signalinfo_t *\fIinfo\fB, const char *\fIfiletype\fB);43.B sox_format_t sox_open_read(const char *\fIpath\fB, const sox_signalinfo_t *\fIinfo\fB, const char *\fIfiletype\fB);
42.P44.P
43.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);45.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
52.P54.P
53.B sox_effect_handler_t const *sox_find_effect(char const *\fIname\fB);55.B sox_effect_handler_t const *sox_find_effect(char const *\fIname\fB);
54.P56.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);
56.P66.P
57.B cc \fIfile.c\fB -o \fIfile \f-lsox67.B cc \fIfile.c\fB -o \fIfile \f-lsox
58.fi68.fi
@@ -68,6 +78,9 @@ library support then this will detect and initialize all external · 9 unmodified lines
68libraries. This should be called before any other file operations78libraries. This should be called before any other file operations
69are performed.79are performed.
70.P80.P
81\fBsox_format_quit\fR function performs some required cleanup
82related to all file format handlers.
83.P
71\fBsox_open_input\fR function opens the file for reading whose name is84\fBsox_open_input\fR function opens the file for reading whose name is
72the string pointed to by \fIpath\fR and associates an sox_format_t with it. If85the string pointed to by \fIpath\fR and associates an sox_format_t with it. If
73\fIinfo\fR is non-NULL then it will be used to specify the data format86\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
109underlying file or set of functions. If the format handler was being122underlying file or set of functions. If the format handler was being
110used for output, any buffered data is written first.123used for output, any buffered data is written first.
111.P124.P
112\fBsox_format_quite\fR function performs some required cleanup
113related to all file format handlers.
114.P
115The function \fBsox_find_effect\fR finds effect \fIname\fR, returning125The function \fBsox_find_effect\fR finds effect \fIname\fR, returning
116a pointer to its \fIsox_effect_handler_t\fR if it exists, and NULL126a pointer to its \fIsox_effect_handler_t\fR if it exists, and NULL
117otherwise.127otherwise.
@@ -121,16 +131,40 @@ The function \fBsox_create_effect\fR instantiates an effect into a · 3 unmodified lines
121methods are automatically set to the corresponding \fBnothing\fR131methods are automatically set to the corresponding \fBnothing\fR
122method.132method.
123.P133.P
124The \fBsox_update_effect\fR function copies input and output signal134The 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.
125info into effect structures. The \fIeffect_mask\fR parameter is the135.P
126return value of the previous call to this function; for the first136Passing 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.
127call, pass 0. The function returns the updated effect mask.137.P
138\fBsox_create_effects_chain\fR will instantiate an effects chain that
139effects can be added to. \fIin_enc\fR and \fIout_enc\fR are the
140signal encoding of the input and output of the chain respectively.
141The pointers to \fIin_enc\fR and \fIout_enc\fR
142are stored internally and so their memory should not be freed. Also,
143it is OK if their values change over time to reflect new input or
144output encodings as they are referenced only as effects
145start up or are restarted.
146.P
147\fBsox_delete_effects_chain\fR will release any resources reserved during
148the creation of the chain. This will also call \fBsox_delete_effects\fR
149if 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
152signal info for this effect. \fIout\fR is a suggestion
153as to what the output signal should be but depending on the effects
154given options and on \fIin\fR the effect can choose to do differently.
155Whatever output rate and channels the effect does produce are written
156back to \fIin\fR. It is meant that \fIin\fR be stored and passed to each
157new call to \fBsox_add_effect\fR so that changes will be propagated to each new effect.
128.P158.P
129SoX includes skeleton C files to assist you in writing new159SoX includes skeleton C files to assist you in writing new
130formats (skelform.c) and effects (skeleff.c). sox.c itself is a good160formats (skelform.c) and effects (skeleff.c). Note that new formats
131starting point for new programs. Note that new formats can often just161can often just deal with the header and then use raw.c's routines
132deal with the header and then use raw.c's routines for reading and162for reading and writing.
133writing.163
164example0.c and example1.c are a good starting point to see how
165to write applications using libsox. sox.c itself is also a good
166reference.
167
134.SH RETURN VALUE168.SH RETURN VALUE
135Upon successful completion \fBsox_open_input\fR and169Upon successful completion \fBsox_open_input\fR and
136\fBsox_open_output\fR return an \fIsox_format_t\fR (which is a pointer).170\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
30 30
31#define DEBUG_EFFECTS_CHAIN 031#define DEBUG_EFFECTS_CHAIN 0
32 32
33 33/* FIXME: Not thread safe using globals */
34sox_effects_globals_t sox_effects_globals =34sox_effects_globals_t sox_effects_globals =
35 {sox_plot_off, 1, &sox_globals};35 {sox_plot_off, 1, &sox_globals};
36 36
@@ -66,7 +66,9 @@ static int default_getopts(sox_effect_t * effp, int argc, char **argv UNUSED) · 29 unmodified lines
66sox_effect_t * sox_create_effect(sox_effect_handler_t const * eh)66sox_effect_t * sox_create_effect(sox_effect_handler_t const * eh)
67{67{
68 sox_effect_t * effp = lsx_calloc(1, sizeof(*effp));68 sox_effect_t * effp = lsx_calloc(1, sizeof(*effp));
69 assert(eh);69
70 if (!effp) return NULL;
71
70 effp->global_info = &sox_effects_globals;72 effp->global_info = &sox_effects_globals;
71 effp->handler = *eh;73 effp->handler = *eh;
72 if (!effp->handler.getopts) effp->handler.getopts = default_getopts;74 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
75 if (!effp->handler.drain ) effp->handler.drain = default_drain;77 if (!effp->handler.drain ) effp->handler.drain = default_drain;
76 if (!effp->handler.stop ) effp->handler.stop = default_function;78 if (!effp->handler.stop ) effp->handler.stop = default_function;
77 if (!effp->handler.kill ) effp->handler.kill = default_function;79 if (!effp->handler.kill ) effp->handler.kill = default_function;
80
78 effp->priv = lsx_calloc(1, effp->handler.priv_size);81 effp->priv = lsx_calloc(1, effp->handler.priv_size);
79 return effp;82 if (effp->handler.priv_size && !effp->priv)
80}83 {
84 free(effp);
85 return NULL;
86 }
81 87
88 return effp;
89} /* sox_create_effect */
82 90
91int 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 */
83 95
84/* Effects chain: */96/* Effects chain: */
85 97
86sox_effects_chain_t * sox_create_effects_chain(98sox_effects_chain_t * sox_create_effects_chain(
87 sox_encodinginfo_t const * in_enc,99 sox_encodinginfo_t const * in_enc, sox_encodinginfo_t const * out_enc)
88 sox_encodinginfo_t const * out_enc)
89{100{
90 sox_effects_chain_t * result = lsx_calloc(1, sizeof(sox_effects_chain_t));101 sox_effects_chain_t * result = lsx_calloc(1, sizeof(sox_effects_chain_t));
91 result->global_info = sox_effects_globals;102 result->global_info = sox_effects_globals;
92 result->in_enc = in_enc;103 result->in_enc = in_enc;
93 result->out_enc = out_enc;104 result->out_enc = out_enc;
94 return result;105 return result;
95}106} /* sox_create_effects_chain */
107
108void 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 */
96 114
97/* Effect can call in start() or flow() to set minimum input size to flow() */115/* Effect can call in start() or flow() to set minimum input size to flow() */
98int sox_effect_set_imin(sox_effect_t * effp, size_t imin)116int 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
378 * Note: This currently closes down the effect which might396 * Note: This currently closes down the effect which might
379 * note be obvious from name.397 * note be obvious from name.
380 */398 */
381static void sox_delete_effect(sox_effect_t *effp)399void sox_delete_effect(sox_effect_t *effp)
382{400{
383 size_t clips;401 size_t clips;
384 unsigned f;402 unsigned f;
src/example0.c+1−2
@@ -83,10 +83,9 @@ int main(int argc, char * argv[]) · 82 unmodified lines
83 sox_flow_effects(chain, NULL);83 sox_flow_effects(chain, NULL);
84 84
85 /* All done; tidy up: */85 /* All done; tidy up: */
86 sox_delete_effects(chain);86 sox_delete_effects_chain(chain);
87 sox_close(out);87 sox_close(out);
88 sox_close(in);88 sox_close(in);
89 sox_format_quit();89 sox_format_quit();
90 free(chain);
91 return 0;90 return 0;
92}91}
src/example1.c+2−3
@@ -51,7 +51,7 @@ static int input_drain( · 50 unmodified lines
51}51}
52 52
53/* The function that will be called to output samples from the effects chain.53/* 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.
55 * In a different application, they might perhaps be analysed in some way,55 * In a different application, they might perhaps be analysed in some way,
56 * or displayed as a wave-form */56 * or displayed as a wave-form */
57static int output_flow(sox_effect_t *effp UNUSED, sox_sample_t const * ibuf,57static 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
153 sox_flow_effects(chain, NULL);153 sox_flow_effects(chain, NULL);
154 154
155 /* All done; tidy up: */155 /* All done; tidy up: */
156 sox_delete_effects(chain);156 sox_delete_effects_chain(chain);
157 sox_close(out);157 sox_close(out);
158 sox_close(in);158 sox_close(in);
159 sox_format_quit();159 sox_format_quit();
160 free(chain);
161 return 0;160 return 0;
162}161}
src/sox.c+174−82
@@ -130,9 +130,10 @@ static size_t output_count = 0; · 129 unmodified lines
130 * a resampling effect, a channel mixing effect, the input, and the output.130 * a resampling effect, a channel mixing effect, the input, and the output.
131 */131 */
132#define MAX_USER_EFF (SOX_MAX_EFFECTS - 4)132#define MAX_USER_EFF (SOX_MAX_EFFECTS - 4)
133static sox_effect_t * user_efftab[MAX_USER_EFF], efftab_options[MAX_USER_EFF];133static struct { char *name; int argc; char *argv[256]; } user_effargs[MAX_USER_EFF];
134static sox_effect_t *user_efftab[MAX_USER_EFF];
134static unsigned nuser_effects;135static unsigned nuser_effects;
135static sox_effects_chain_t ofile_effects_chain;136static sox_effects_chain_t *effects_chain = NULL;
136 137
137 138
138/* Flowing */139/* Flowing */
@@ -542,26 +543,78 @@ static sox_effect_handler_t const * output_effect_fn(void) · 403 unmodified lines
542 return &handler;543 return &handler;
543}544}
544 545
545static void add_auto_effect(sox_effects_chain_t * chain, char const * name, char * arg, sox_signalinfo_t * signal)546static void add_effect(sox_effects_chain_t *chain, char const *name,
547 int argc, char *argv[], sox_signalinfo_t *signal)
546{548{
547 sox_effect_t * effp;549 sox_effect_t * effp;
548 char * * argv = & arg;
549 550
550 effp = sox_create_effect(sox_find_effect(name)); /* Should always succeed. */551 effp = sox_create_effect(sox_find_effect(name)); /* Should always succeed. */
551 552
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)
553 exit(1); /* The failing effect should have displayed an error message */561 exit(1); /* The failing effect should have displayed an error message */
554 562
555 if (sox_add_effect(chain, effp, signal, &ofile->ft->signal) != SOX_SUCCESS)563 if (sox_add_effect(chain, effp, signal, &ofile->ft->signal) != SOX_SUCCESS)
556 exit(2); /* The effects chain should have displayed an error message */564 exit(2); /* The effects chain should have displayed an error message */
557}565}
558 566
559/* If needed effects are not given, auto-add at (performance) optimal point. */567/* Creates users effects and passes in user specified options.
560static void add_effects(sox_effects_chain_t * chain)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 */
578static 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 */
612static void add_effects(sox_effects_chain_t *chain)
561{613{
562 sox_signalinfo_t signal = combiner_signal;614 sox_signalinfo_t signal = combiner_signal;
563 unsigned i, min_chan = 0, min_rate = 0;615 unsigned i, min_chan = 0, min_rate = 0;
564 sox_effect_t * effp;616 sox_effect_t * effp;
617 unsigned effects_added = 0;
565 char * rate_arg = sox_mode != sox_play? NULL :618 char * rate_arg = sox_mode != sox_play? NULL :
566 (rate_arg = getenv("PLAY_RATE_ARG"))? rate_arg : "-l";619 (rate_arg = getenv("PLAY_RATE_ARG"))? rate_arg : "-l";
567 620
@@ -572,35 +625,54 @@ static void add_effects(sox_effects_chain_t * chain) · 4 unmodified lines
572 if (user_efftab[i]->handler.flags & SOX_EFF_RATE)625 if (user_efftab[i]->handler.flags & SOX_EFF_RATE)
573 min_rate = i + 1;626 min_rate = i + 1;
574 }627 }
575 /* 1st `effect' in the chain is the input combiner_signal */628
576 effp = sox_create_effect(input_combiner_effect_fn());629 effects_added++;
577 sox_add_effect(chain, effp, &signal, &ofile->ft->signal);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 }
578 636
579 /* Add auto effects if appropriate; add user specified effects */637 /* Add auto effects if appropriate; add user specified effects */
580 for (i = 0; i <= nuser_effects; i++) {638 for (i = 0; i < nuser_effects; i++) {
581 /* If reducing channels, it's faster to do so before all other effects: */639 effects_added++;
582 if (signal.channels > ofile->ft->signal.channels && i >= min_chan)640 if (effects_added > chain->length)
583 add_auto_effect(chain, "mixer", NULL, &signal);641 {
584 642 /* Effects chain should have displayed an error message */
585 /* If reducing rate, it's faster to do so before all other effects643 if (sox_add_effect(chain, user_efftab[i],
586 * (except reducing channels): */644 &signal, &ofile->ft->signal) != SOX_SUCCESS)
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)
592 exit(2);645 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]);
593 }652 }
653
594 /* Add auto effects if still needed at this point */654 /* Add auto effects if still needed at this point */
595 if (signal.rate != ofile->ft->signal.rate)655 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 }
597 if (signal.channels != ofile->ft->signal.channels)661 if (signal.channels != ofile->ft->signal.channels)
598 add_auto_effect(chain, "mixer", NULL, &signal); /* Must be increasing channels */662 {
599 663 effects_added++;
600 /* Last `effect' in the chain is the output file */664 if (effects_added > chain->length)
601 effp = sox_create_effect(output_effect_fn());665 add_effect(chain, "mixer", 0, NULL, &signal); /* Must be increasing channels */
602 if (sox_add_effect(chain, effp, &signal, &ofile->ft->signal) != SOX_SUCCESS)666 }
603 exit(2);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 }
604 676
605 for (i = 0; i < chain->length; ++i) {677 for (i = 0; i < chain->length; ++i) {
606 sox_effect_t const * effp = &chain->effects[i][0];678 sox_effect_t const * effp = &chain->effects[i][0];
@@ -616,7 +688,7 @@ static size_t total_clips(void) · 9 unmodified lines
616 size_t clips = 0;688 size_t clips = 0;
617 for (i = 0; i < file_count; ++i)689 for (i = 0; i < file_count; ++i)
618 clips += files[i]->ft->clips + files[i]->volume_clips;690 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);
620}692}
621 693
622static sox_bool since(struct timeval * then, double secs, sox_bool always_reset)694static sox_bool since(struct timeval * then, double secs, sox_bool always_reset)
@@ -711,15 +783,15 @@ static void optimize_trim(void) · 88 unmodified lines
711 * to do it for multiple files is complex and problably never used. This783 * to do it for multiple files is complex and problably never used. This
712 * hack is a huge time savings when trimming gigs of audio data into784 * hack is a huge time savings when trimming gigs of audio data into
713 * managable chunks. */785 * 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) {
715 if (files[0]->ft->handler.seek && files[0]->ft->seekable){787 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]);
717 if (offset && sox_seek(files[0]->ft, offset, SOX_SEEK_SET) == SOX_SUCCESS) {789 if (offset && sox_seek(files[0]->ft, offset, SOX_SEEK_SET) == SOX_SUCCESS) {
718 read_wide_samples = offset / files[0]->ft->signal.channels;790 read_wide_samples = offset / files[0]->ft->signal.channels;
719 /* Assuming a failed seek stayed where it was. If the seek worked then791 /* Assuming a failed seek stayed where it was. If the seek worked then
720 * reset the start location of trim so that it thinks user didn't792 * reset the start location of trim so that it thinks user didn't
721 * request a skip. */793 * request a skip. */
722 sox_trim_clear_start(&ofile_effects_chain.effects[1][0]);794 sox_trim_clear_start(&effects_chain->effects[1][0]);
723 sox_debug("optimize_trim successful");795 sox_debug("optimize_trim successful");
724 }796 }
725 }797 }
@@ -851,10 +923,24 @@ static void sigint(int s) · 125 unmodified lines
851 else user_abort = sox_true;923 else user_abort = sox_true;
852}924}
853 925
854static void calculate_combiner_and_output_signal_parameters(void)926static void calculate_combiner_signal_parameters(void)
855{927{
856 sox_bool known_length = combine_method != sox_sequence;928 size_t i;
857 size_t i, olen = 0;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 }
858 944
859 /* Set the combiner output signal attributes to those of the 1st/next input945 /* Set the combiner output signal attributes to those of the 1st/next input
860 * file. If we are in sox_sequence mode then we don't need to check the946 * 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
884 max_channels = max(max_channels, files[i]->ft->signal.channels);970 max_channels = max(max_channels, files[i]->ft->signal.channels);
885 min_rate = min(min_rate , files[i]->ft->signal.rate);971 min_rate = min(min_rate , files[i]->ft->signal.rate);
886 max_rate = max(max_rate , files[i]->ft->signal.rate);972 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);
892 }973 }
893 974
894 /* Check for invalid/unusual rate or channel combinations: */975 /* Check for invalid/unusual rate or channel combinations: */
@@ -910,6 +991,27 @@ static void calculate_combiner_and_output_signal_parameters(void) · 15 unmodified lines
910 combine_method == sox_merge? total_channels : max_channels;991 combine_method == sox_merge? total_channels : max_channels;
911 }992 }
912 993
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
1000static 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
913 /* Determine the output file signal attributes; set from user options1015 /* Determine the output file signal attributes; set from user options
914 * if given: */1016 * if given: */
915 ofile->signal = ofile_signal_options;1017 ofile->signal = ofile_signal_options;
@@ -928,10 +1030,6 @@ static void calculate_combiner_and_output_signal_parameters(void) · 12 unmodified lines
928 /* FIXME: comment this: */1030 /* FIXME: comment this: */
929 ofile->signal.precision = combiner_signal.precision;1031 ofile->signal.precision = combiner_signal.precision;
930 1032
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
935 /* If any given user effect modifies the audio length, then we assume that1033 /* If any given user effect modifies the audio length, then we assume that
936 * we don't know what the output length will be. FIXME: in most cases,1034 * we don't know what the output length will be. FIXME: in most cases,
937 * an effect that modifies length will be able to determine by how much from1035 * 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
971 1069
972static int process(void)1070static int process(void)
973{ /* Input(s) -> Balancing -> Combiner -> Effects -> Output */1071{ /* Input(s) -> Balancing -> Combiner -> Effects -> Output */
974 unsigned i;
975 int flow_status;1072 int flow_status;
976 1073
977 for (i = 0; i < nuser_effects; ++i)1074 create_user_effects();
978 *user_efftab[i] = efftab_options[i];
979 1075
980 calculate_combiner_and_output_signal_parameters();1076 calculate_combiner_signal_parameters();
981 set_combiner_and_output_encoding_parameters();1077 set_combiner_and_output_encoding_parameters();
982 open_output_file();
983 1078
984 ofile_effects_chain.length = 0;1079 calculate_output_signal_parameters();
1080 open_output_file();
985 1081
986 ofile_effects_chain.global_info = sox_effects_globals;1082 if (!effects_chain)
987 ofile_effects_chain.in_enc = &combiner_encoding;1083 effects_chain = sox_create_effects_chain(&combiner_encoding,
988 ofile_effects_chain.out_enc = &ofile->ft->encoding;1084 &ofile->ft->encoding);
989 add_effects(&ofile_effects_chain);1085 add_effects(effects_chain);
990 1086
991 optimize_trim();1087 optimize_trim();
992 1088
993 signal(SIGTERM, sigint); /* Stop gracefully, as soon as we possibly can. */1089 signal(SIGTERM, sigint); /* Stop gracefully, as soon as we possibly can. */
994 signal(SIGINT , sigint); /* Either skip current input or behave as SIGTERM. */1090 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);
996 1092
997 /* When in sox_multiple mode, changing to a new output file is1093 /* When in sox_multiple mode, changing to a new output file is
998 * based on effects return ST_EOF. In that case, don't return1094 * 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
1674static void parse_effects(int argc, char **argv)1770static void parse_effects(int argc, char **argv)
1675{1771{
1676 for (nuser_effects = 0; optind < argc; ++nuser_effects) {1772 for (nuser_effects = 0; optind < argc; ++nuser_effects) {
1677 sox_effect_t * e;
1678 int i;1773 int i;
1679 1774
1680 if (nuser_effects >= MAX_USER_EFF) {1775 if (nuser_effects >= MAX_USER_EFF) {
@@ -1683,17 +1778,12 @@ static void parse_effects(int argc, char **argv) · 2 unmodified lines
1683 }1778 }
1684 1779
1685 /* Name should always be correct! */1780 /* Name should always be correct! */
1686 e = sox_create_effect(sox_find_effect(argv[optind++]));1781 user_effargs[nuser_effects].name = strdup(argv[optind++]);
1687 1782 for (i = 0; i < argc - optind && !sox_find_effect(argv[optind + i]); ++i)
1688 for (i = 0; i < argc - optind && !sox_find_effect(argv[optind + i]); ++i);1783 user_effargs[nuser_effects].argv[i] = strdup(argv[optind + i]);
1689 if (e->handler.getopts(e, i, &argv[optind]) == SOX_EOF)1784 user_effargs[nuser_effects].argc = i;
1690 exit(1); /* The failing effect should have displayed an error message */
1691 1785
1692 optind += i; /* Skip past the effect arguments */1786 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;
1697 }1787 }
1698}1788}
1699 1789
@@ -1898,15 +1988,6 @@ int main(int argc, char **argv) · 198 unmodified lines
1898 exit(0);1988 exit(0);
1899 }1989 }
1900 1990
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
1910 if (sox_globals.repeatable)1991 if (sox_globals.repeatable)
1911 sox_debug("Not reseeding PRNG; randomness is repeatable");1992 sox_debug("Not reseeding PRNG; randomness is repeatable");
1912 else {1993 else {
@@ -1920,8 +2001,6 @@ int main(int argc, char **argv) · 7 unmodified lines
1920 * block of input files. Also used by sox_multiple output mode. */2001 * block of input files. Also used by sox_multiple output mode. */
1921 ofile_signal_options = ofile->signal;2002 ofile_signal_options = ofile->signal;
1922 ofile_encoding_options = ofile->encoding;2003 ofile_encoding_options = ofile->encoding;
1923 for (i = 0; i < nuser_effects; ++i)
1924 efftab_options[i] = *user_efftab[i];
1925 2004
1926 if (combine_method == sox_sequence || output_method == sox_multiple) do {2005 if (combine_method == sox_sequence || output_method == sox_multiple) do {
1927 /* If ofile->ft is set then we've called this at least once.2006 /* 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
1935 * If input reached EOF then restart all effects.2014 * If input reached EOF then restart all effects.
1936 */2015 */
1937 if (output_method == sox_multiple && !input_eof)2016 if (output_method == sox_multiple && !input_eof)
1938 /* FIXME: Make a function that does that */2017 {
1939 ;2018 size_t e;
1940 else if (output_method == sox_multiple)2019
1941 sox_delete_effects(&ofile_effects_chain);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);
1942 sox_close(ofile->ft);2033 sox_close(ofile->ft);
1943 ofile->ft = NULL;2034 ofile->ft = NULL;
1944 }2035 }
1945 } while (process() != SOX_EOF && !user_abort && current_input < input_count);2036 } while (process() != SOX_EOF && !user_abort && current_input < input_count);
1946 else process();2037 else process();
1947 sox_delete_effects(&ofile_effects_chain);2038
2039 sox_delete_effects_chain(effects_chain);
1948 2040
1949 for (i = 0; i < file_count; ++i)2041 for (i = 0; i < file_count; ++i)
1950 if (files[i]->ft->clips != 0)2042 if (files[i]->ft->clips != 0)
src/sox.h+5−3
@@ -476,6 +476,7 @@ struct sox_effect { · 475 unmodified lines
476 476
477sox_effect_handler_t const * sox_find_effect(char const * name);477sox_effect_handler_t const * sox_find_effect(char const * name);
478sox_effect_t * sox_create_effect(sox_effect_handler_t const * eh);478sox_effect_t * sox_create_effect(sox_effect_handler_t const * eh);
479int sox_effect_options(sox_effect_t *effp, int argc, char * const argv[]);
479 480
480/* Effects chain */481/* Effects chain */
481 482
@@ -493,13 +494,14 @@ struct sox_effects_chain { · 11 unmodified lines
493};494};
494typedef struct sox_effects_chain sox_effects_chain_t;495typedef struct sox_effects_chain sox_effects_chain_t;
495sox_effects_chain_t * sox_create_effects_chain(496sox_effects_chain_t * sox_create_effects_chain(
496 sox_encodinginfo_t const * in_enc,497 sox_encodinginfo_t const * in_enc, sox_encodinginfo_t const * out_enc);
497 sox_encodinginfo_t const * out_enc);498void sox_delete_effects_chain(sox_effects_chain_t *ecp);
498int sox_add_effect( sox_effects_chain_t * chain, sox_effect_t * effp, sox_signalinfo_t * in, sox_signalinfo_t const * out);499int sox_add_effect( sox_effects_chain_t * chain, sox_effect_t * effp, sox_signalinfo_t * in, sox_signalinfo_t const * out);
499int sox_flow_effects(sox_effects_chain_t *, int (* callback)(sox_bool all_done));500int sox_flow_effects(sox_effects_chain_t *, int (* callback)(sox_bool all_done));
500size_t sox_effects_clips(sox_effects_chain_t *);501size_t sox_effects_clips(sox_effects_chain_t *);
501size_t sox_stop_effect(sox_effect_t *effp);502size_t sox_stop_effect(sox_effect_t *effp);
502void sox_delete_effects(sox_effects_chain_t *);503void sox_delete_effect(sox_effect_t *effp);
504void sox_delete_effects(sox_effects_chain_t *chain);
503 505
504/* The following routines are unique to the trim effect.506/* The following routines are unique to the trim effect.
505 * sox_trim_get_start can be used to find what is the start507 * 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