Fixed segfault caused by bad printf.
[aftubes.git] / ringmod.c
blob8f48b9852c0e42cc20986ee31a8dd24aac560d3b
1 #include "modules.h"
2 #include "graph.h"
3 #include "errors.h"
4 #include "math.h"
6 #define M_PI 3.1415926535897931
8 struct ringmod {
9 struct graphpin *in_pin, *out_pin;
10 float pos;
13 static bool is_acceptable_input_format(struct graphnode *node, struct graphpin *pin, const struct aformat *af)
15 return af->media == MT_AUDIO_32F;
18 static err_t get_ideal_input_format(struct graphnode *node, struct graphpin *pin, struct aformat *af)
20 af->media = MT_AUDIO_32F;
21 af->srate = 44100;
22 af->channels = 2;
23 return EOK;
26 static err_t run(struct graphnode *node)
28 struct ringmod *rm = node->extra;
29 float *src, *dest, pos;
30 err_t err;
31 int i;
33 err = buffer_alloc(&rm->out_pin->edge->buf, rm->in_pin->edge->buf.n_samples);
34 if (err != EOK){
35 return err;
38 src = rm->in_pin->edge->buf.data;
39 dest = rm->out_pin->edge->buf.data;
40 pos = rm->pos;
42 for (i = rm->in_pin->edge->buf.n_samples * rm->in_pin->edge->buf.format.channels;
43 i; --i){
44 *dest = *src * cos((pos * M_PI * 2) / 441.0);
45 ++dest, ++src, ++pos;
48 rm->pos = pos;
50 return EOK;
53 static const struct graphnode_functab functab = {
54 is_acceptable_input_format,
55 get_ideal_input_format,
56 NULL, // set_output_format
57 NULL, // set_buffer
58 run,
61 err_t ringmod_create(struct graphnode **node_out)
63 struct graphnode *node;
64 struct ringmod *restrict rm;
65 err_t err;
67 err = graphnode_create(&node, &functab, sizeof *rm);
68 if (err != EOK){
69 return err;
72 rm = node->extra;
73 rm->pos = 0.0;
75 err = graphnode_add_pin(node, &rm->in_pin);
76 if (err != EOK){
77 graphnode_free(node);
78 return err;
80 rm->in_pin->dir = DIR_IN;
81 rm->in_pin->name = "in";
83 err = graphnode_add_pin(node, &rm->out_pin);
84 if (err != EOK){
85 graphnode_free(node);
86 return err;
88 rm->out_pin->dir = DIR_OUT;
89 rm->out_pin->name = "out";
91 *node_out = node;
92 return EOK;