scons --> make
[aftubes.git] / modules / wavesource.c
blob03043026e6fa1f87bcc37bc0152534012576089f
1 #include "graph.h"
2 #include "wavefile.h"
4 struct wavefile_source {
5 struct buffer *buf;
6 struct wavefile wf;
7 off_t pos;
8 };
10 static err_t get_output_format(struct graphnode *node, struct graphpin *pin,
11 struct aformat *af)
13 struct wavefile_source *ws = node->extra;
14 af->media = MT_AUDIO_16I;
15 af->srate = ws->wf.format.srate;
16 af->channels = ws->wf.format.channels;
17 return EOK;
20 static err_t set_buffer(struct graphnode *node, struct graphpin *pin, struct buffer *buf)
22 struct wavefile_source *ws = node->extra;
23 ws->buf = buf;
24 return EOK;
27 static err_t run(struct graphnode *node)
29 struct wavefile_source *ws = node->extra;
30 err_t err;
32 err = buffer_alloc(ws->buf, 4410);
33 if (err != EOK){
34 return err;
37 err = wavefile_read_at(&ws->wf, ws->pos, ws->buf->data, 4410);
38 if (err != EOK){
39 return err;
42 ws->pos += 4410;
44 return EOK;
47 static const struct graphnode_functab functab = {
48 NULL, // is_acceptable_input_format
49 NULL, // get_ideal_input_format
50 get_output_format,
51 set_buffer,
52 run,
55 err_t wavesource_create(struct graphnode **node_out, const char *filename)
57 struct graphnode *node;
58 struct graphpin *pin;
59 struct wavefile_source *ws;
60 err_t err;
62 err = graphnode_create(&node, &functab, sizeof *ws);
63 if (err != EOK){
64 return err;
67 err = graphnode_add_pin(node, &pin);
68 if (err != EOK){
69 return err;
71 pin->dir = DIR_OUT;
72 pin->name = "out";
74 ws = node->extra;
75 err = wavefile_open(&ws->wf, filename);
76 if (err != EOK){
77 graphnode_free(node);
78 return err;
80 ws->pos = 0;
82 *node_out = node;
83 return EOK;