From 71dd60bcb18c0fe93df9c0e11dcbfdf463753686 Mon Sep 17 00:00:00 2001 From: Joshua Phillips Date: Wed, 7 Jan 2009 11:44:10 +0000 Subject: [PATCH] Mixer now has variable number of inputs. It always keeps a spare input pin for a new connection, and creates pins as they are connected. --- SConstruct | 2 +- errors.c | 1 - graph.c | 62 ++++++++++++++++++++++++++------------------------------------ main.c | 5 ++++- mixer.c | 45 ++++++++++++++++++++++++++++++++------------- modules.h | 2 +- ringmod.c | 2 -- 7 files changed, 64 insertions(+), 55 deletions(-) diff --git a/SConstruct b/SConstruct index d052160..11e04c9 100644 --- a/SConstruct +++ b/SConstruct @@ -17,7 +17,7 @@ Help(opts.GenerateHelpText(env)) opts.Save('scache.conf', env) env['LIBS'] = ['m'] -env['CPPDEFINES'] = {'_POSIX_SOURCE': None} +env['CPPDEFINES'] = {'_POSIX_SOURCE': None, '_GNU_SOURCE': None} # Pretty coloured output if not env['verbose']: diff --git a/errors.c b/errors.c index 54dfae9..b54bc81 100644 --- a/errors.c +++ b/errors.c @@ -1,4 +1,3 @@ -#define _GNU_SOURCE #include "errors.h" #include "stdarg.h" #include "stdio.h" diff --git a/graph.c b/graph.c index 1a7da8f..4bdbdb5 100644 --- a/graph.c +++ b/graph.c @@ -257,48 +257,38 @@ err_t graph_connect(struct graph *graph, struct graphpin *a, struct graphpin *b) // if a is a source node, it will be able to provide us with a media type // if a is not a source node, all its inputs must be connected // otherwise we won't know what output format to use. - if (graphnode_all_inputs_connected(a->node)){ - // get media type from source - if (a->node->functab->get_output_format){ - err = a->node->functab->get_output_format(a->node, a, &af); + if (a->node->functab->get_output_format){ + err = a->node->functab->get_output_format(a->node, a, &af); + if (err != EOK){ + return err; + } + } else { + // assume output format is same as input format + err = guess_output_format(a, &af); + if (err != EOK){ + return err; + } + } + // check media type with b node + if (b->node->functab->is_acceptable_input_format + && !b->node->functab->is_acceptable_input_format(b->node, b, &af)){ + // it doesn't like that input format + // we'll have to add a converter node + if (b->node->functab->get_ideal_input_format){ + err = b->node->functab->get_ideal_input_format(b->node, b, &af2); if (err != EOK){ return err; } + cvt_input = NULL; + return create_converter_node(graph, &af, &af2, + a, b, &cvt_node); } else { - // assume output format is same as input format - err = guess_output_format(a, &af); - if (err != EOK){ - return err; - } + return make_error(ENO_AGREEABLE_FORMAT, graph, "cannot agree on a common media format (\"%s\" won't specify an ideal format)", + b->node->name); } - // check media type with b node - if (b->node->functab->is_acceptable_input_format - && !b->node->functab->is_acceptable_input_format(b->node, b, &af)){ - // it doesn't like that input format - // we'll have to add a converter node - if (b->node->functab->get_ideal_input_format){ - err = b->node->functab->get_ideal_input_format(b->node, b, &af2); - if (err != EOK){ - return err; - } - cvt_input = NULL; - return create_converter_node(graph, &af, &af2, - a, b, &cvt_node); - } else { - return make_error(ENO_AGREEABLE_FORMAT, graph, "cannot agree on a common media format (\"%s\" won't specify an ideal format)", - b->node->name); - } - } - // actually do the connection - return do_connect(graph, a, b, &af); - } else { - // there are unconnected inputs, so we can't connect - // (because we don't know what output format to use - // without an input format) - return make_error(EUNCONNECTED, graph, - "unconnected input pins: cannot determine media type (\"%s\")", - a->node->name); } + // actually do the connection + return do_connect(graph, a, b, &af); } } diff --git a/main.c b/main.c index 2ac130a..15665bc 100644 --- a/main.c +++ b/main.c @@ -51,14 +51,17 @@ int main(int argc, char **argv) node->name = "sink"; sink = node; - moan(mixer_create(&node, 2)); + moan(mixer_create(&node)); assert(node); moan(graph_add_node(graph, node)); node->name = "mixer"; mixer = node; + printf("connecting mixer in0\n"); moan(graph_connect(graph, source_1->pins, mixer->pins->next)); + printf("connecting mixer in1\n"); moan(graph_connect(graph, source_2->pins, mixer->pins->next->next)); + printf("connecting mixer out\n"); moan(graph_connect(graph, mixer->pins, sink->pins)); moan(graph_sort(graph)); diff --git a/mixer.c b/mixer.c index 92be401..9ed3537 100644 --- a/mixer.c +++ b/mixer.c @@ -2,6 +2,7 @@ #include "graph.h" #include "errors.h" #include "assert.h" +#include "stdio.h" ///// Simple additive mixer ///// ///// TODO: handle different input formats ///// @@ -39,6 +40,24 @@ static err_t get_output_format(struct graphnode *node, struct graphpin *pin, str } } +static err_t another_in_pin(struct graphnode *node, struct mixer *m) +{ + // create a new input pin + struct graphpin *new_pin; + err_t err; + char *new_name = NULL; + + err = graphnode_add_pin(node, &new_pin); + if (err != EOK){ + return err; + } + asprintf(&new_name, "in%d", m->n_inputs); + new_pin->name = new_name; + new_pin->dir = DIR_IN; + ++m->n_inputs; + return EOK; +} + static err_t set_buffer(struct graphnode *node, struct graphpin *pin, struct buffer *buf) { struct mixer *m = node->extra; @@ -46,14 +65,14 @@ static err_t set_buffer(struct graphnode *node, struct graphpin *pin, struct buf if (buf->format.media == m->af.media && buf->format.srate == m->af.srate && buf->format.channels == m->af.channels){ - return EOK; + return another_in_pin(node, m); } else { return make_error(EBAD_FORMAT, node, "mixer module is being connected to a bad media format"); } } else { m->af = buf->format; m->format_is_set = true; - return EOK; + return another_in_pin(node, m); } } @@ -63,7 +82,7 @@ static size_t longest_in_buf_len(struct graphnode *restrict node) size_t sz = 0; for (in_pin=node->pins; in_pin; in_pin=in_pin->next){ - if (in_pin->dir != DIR_IN){ + if (in_pin->dir != DIR_IN || !in_pin->edge){ continue; } if (in_pin->edge->buf.n_samples > sz){ @@ -85,6 +104,9 @@ static err_t run(struct graphnode *node) } \ \ for (in_pin=node->pins; in_pin; in_pin=in_pin->next){ \ + if (in_pin->dir != DIR_IN || !in_pin->edge){ \ + continue; \ + } \ s_ptr = in_pin->edge->buf.data; \ d_ptr = d_buf->data; \ for (i=sz; i; --i){ \ @@ -127,13 +149,12 @@ static const struct graphnode_functab functab = { run, }; -err_t mixer_create(struct graphnode **node_out, int n_inputs) +err_t mixer_create(struct graphnode **node_out) { struct graphnode *node; struct mixer *m; struct graphpin *in_pin; err_t err; - int i; err = graphnode_create(&node, &functab, sizeof *m); if (err != EOK){ @@ -141,7 +162,7 @@ err_t mixer_create(struct graphnode **node_out, int n_inputs) } m = node->extra; - m->n_inputs = n_inputs; + m->n_inputs = 1; m->format_is_set = false; // create output pin @@ -153,14 +174,12 @@ err_t mixer_create(struct graphnode **node_out, int n_inputs) m->out_pin->name = "out"; // create input pins - for (i=0; idir = DIR_IN; - in_pin->name = "in"; // how original + err = graphnode_add_pin(node, &in_pin); + if (err != EOK){ + return err; } + in_pin->dir = DIR_IN; + in_pin->name = "in0"; // how original *node_out = node; return EOK; diff --git a/modules.h b/modules.h index b066edb..17a0df3 100644 --- a/modules.h +++ b/modules.h @@ -8,6 +8,6 @@ err_t wavesource_create(struct graphnode **node_out, const char *filename); err_t playsink_create(struct graphnode **node_out); err_t ringmod_create(struct graphnode **node_out); err_t audio_converter_create(struct graphnode **node_out, struct aformat *src_af, struct aformat *dest_af); -err_t mixer_create(struct graphnode **node_out, int n_inputs); +err_t mixer_create(struct graphnode **node_out); #endif diff --git a/ringmod.c b/ringmod.c index 8f48b98..1327328 100644 --- a/ringmod.c +++ b/ringmod.c @@ -3,8 +3,6 @@ #include "errors.h" #include "math.h" -#define M_PI 3.1415926535897931 - struct ringmod { struct graphpin *in_pin, *out_pin; float pos; -- 2.11.4.GIT