Mixer now has variable number of inputs.
[aftubes.git] / ringmod.c
blob13273285bb26893f12481212d304bc07ba37d290
1 #include "modules.h"
2 #include "graph.h"
3 #include "errors.h"
4 #include "math.h"
6 struct ringmod {
7 struct graphpin *in_pin, *out_pin;
8 float pos;
9 };
11 static bool is_acceptable_input_format(struct graphnode *node, struct graphpin *pin, const struct aformat *af)
13 return af->media == MT_AUDIO_32F;
16 static err_t get_ideal_input_format(struct graphnode *node, struct graphpin *pin, struct aformat *af)
18 af->media = MT_AUDIO_32F;
19 af->srate = 44100;
20 af->channels = 2;
21 return EOK;
24 static err_t run(struct graphnode *node)
26 struct ringmod *rm = node->extra;
27 float *src, *dest, pos;
28 err_t err;
29 int i;
31 err = buffer_alloc(&rm->out_pin->edge->buf, rm->in_pin->edge->buf.n_samples);
32 if (err != EOK){
33 return err;
36 src = rm->in_pin->edge->buf.data;
37 dest = rm->out_pin->edge->buf.data;
38 pos = rm->pos;
40 for (i = rm->in_pin->edge->buf.n_samples * rm->in_pin->edge->buf.format.channels;
41 i; --i){
42 *dest = *src * cos((pos * M_PI * 2) / 441.0);
43 ++dest, ++src, ++pos;
46 rm->pos = pos;
48 return EOK;
51 static const struct graphnode_functab functab = {
52 is_acceptable_input_format,
53 get_ideal_input_format,
54 NULL, // set_output_format
55 NULL, // set_buffer
56 run,
59 err_t ringmod_create(struct graphnode **node_out)
61 struct graphnode *node;
62 struct ringmod *restrict rm;
63 err_t err;
65 err = graphnode_create(&node, &functab, sizeof *rm);
66 if (err != EOK){
67 return err;
70 rm = node->extra;
71 rm->pos = 0.0;
73 err = graphnode_add_pin(node, &rm->in_pin);
74 if (err != EOK){
75 graphnode_free(node);
76 return err;
78 rm->in_pin->dir = DIR_IN;
79 rm->in_pin->name = "in";
81 err = graphnode_add_pin(node, &rm->out_pin);
82 if (err != EOK){
83 graphnode_free(node);
84 return err;
86 rm->out_pin->dir = DIR_OUT;
87 rm->out_pin->name = "out";
89 *node_out = node;
90 return EOK;