wav_filter: Kill non-btr code.
[paraslash.git] / wav_filter.c
1 /*
2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file wav_filter.c A filter that inserts a wave header. */
8
9 #include <regex.h>
10 #include <stdbool.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "filter.h"
19 #include "string.h"
20 #include "portable_io.h"
21
22 /** size of the output buffer */
23 #define WAV_OUTBUF_SIZE 81920
24 /** a wav header is always 44 bytes */
25 #define WAV_HEADER_LEN 44
26 /** always write 16 bit header */
27 #define BITS 16
28
29 static void make_wav_header(unsigned int channels, unsigned int samplerate,
30 char *headbuf)
31 {
32
33 unsigned int size = 0x7fffffff;
34 int bytespersec = channels * samplerate * BITS / 8;
35 int align = channels * BITS / 8;
36
37 PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
38 memset(headbuf, 0, WAV_HEADER_LEN);
39 memcpy(headbuf, "RIFF", 4);
40 write_u32(headbuf + 4, size - 8);
41 memcpy(headbuf + 8, "WAVE", 4);
42 memcpy(headbuf + 12, "fmt ", 4);
43 write_u32(headbuf + 16, 16); /* 16 + extra format bytes (zero) */
44 write_u16(headbuf + 20, 1); /* format (1 == PCM/uncompressed) */
45 write_u16(headbuf + 22, channels);
46 write_u32(headbuf + 24, samplerate);
47 write_u32(headbuf + 28, bytespersec);
48 write_u16(headbuf + 32, align); /* number of bytes per sample slice */
49 write_u16(headbuf + 34, BITS); /* significant bits per sample */
50 memcpy(headbuf + 36, "data", 4); /* chunk ID */
51 write_u32(headbuf + 40, size - 44); /* chunk size */
52 }
53
54 static void wav_close(struct filter_node *fn)
55 {
56 free(fn->buf);
57 fn->buf = NULL;
58 free(fn->private_data);
59 fn->private_data = NULL;
60 }
61
62 static void wav_open(struct filter_node *fn)
63 {
64 int *bof;
65
66 fn->bufsize = WAV_OUTBUF_SIZE;
67 fn->buf = para_malloc(fn->bufsize);
68 fn->private_data = para_malloc(sizeof(int));
69 bof = fn->private_data;
70 fn->loaded = 0;
71 *bof = 1;
72 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
73 fn, fn->buf, fn->loaded);
74 }
75
76 static void wav_pre_select(struct sched *s, struct task *t)
77 {
78 struct filter_node *fn = container_of(t, struct filter_node, task);
79 size_t iqs = btr_get_input_queue_size(fn->btrn);
80
81 t->error = 0;
82 if (iqs == 0)
83 return;
84 s->timeout.tv_sec = 0;
85 s->timeout.tv_usec = 1;
86 }
87
88 static void wav_post_select(__a_unused struct sched *s, struct task *t)
89 {
90 struct filter_node *fn = container_of(t, struct filter_node, task);
91 struct btr_node *btrn = fn->btrn;
92 size_t iqs = btr_get_input_queue_size(btrn);
93 int ret;
94 char *header, *buf;
95 int32_t rate, ch;
96
97 t->error = 0;
98 if (iqs == 0) {
99 ret = -E_WAV_EOF;
100 if (btr_no_parent(btrn))
101 goto err;
102 return;
103 }
104 ret = btr_exec_up(btrn, "samplerate", &buf);
105 if (ret < 0) {
106 ret = -E_WAV_BAD_FC;
107 goto err;
108 }
109 ret = para_atoi32(buf, &rate);
110 free(buf);
111 if (ret < 0)
112 goto err;
113 ret = btr_exec_up(btrn, "channels", &buf);
114 if (ret < 0) {
115 ret = -E_WAV_BAD_FC;
116 goto err;
117 }
118 ret = para_atoi32(buf, &ch);
119 free(buf);
120 if (ret < 0)
121 goto err;
122 header = para_malloc(WAV_HEADER_LEN);
123 make_wav_header(ch, rate, header);
124 btr_add_output(header, WAV_HEADER_LEN, btrn);
125 ret = -E_WAV_SUCCESS;
126 err:
127 t->error = ret;
128 if (ret == -E_WAV_SUCCESS)
129 btr_splice_out_node(btrn);
130 else {
131 btr_remove_node(btrn);
132 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
133 }
134 }
135
136 /**
137 * The init function of the wav filter.
138 *
139 * \param f Structure to initialize.
140 */
141 void wav_filter_init(struct filter *f)
142 {
143 f->close = wav_close;
144 f->open = wav_open;
145 f->pre_select = wav_pre_select;
146 f->post_select = wav_post_select;
147 }