Graph now runs (again??).
[aftubes.git] / wavesource.c
blobabf442c8ba8bb64e373e1ae1d066470a068b484e
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 get_output_format,
50 set_buffer,
51 run,
54 err_t wavesource_create(struct graphnode **node_out, const char *filename)
56 struct graphnode *node;
57 struct graphpin *pin;
58 struct wavefile_source *ws;
59 err_t err;
61 err = graphnode_create(&node, &functab, sizeof *ws);
62 if (err != EOK){
63 return err;
66 err = graphnode_add_pin(node, &pin);
67 if (err != EOK){
68 return err;
70 pin->dir = DIR_OUT;
71 pin->name = "out";
73 ws = node->extra;
74 err = wavefile_open(&ws->wf, filename);
75 if (err != EOK){
76 graphnode_free(node);
77 return err;
79 ws->pos = 0;
81 *node_out = node;
82 return EOK;