Cosmetics: Rename samplerate to sample_rate.
[paraslash.git] / wav_filter.c
1 /*
2 * Copyright (C) 2005-2010 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 /** A wav header is always 44 bytes. */
23 #define WAV_HEADER_LEN 44
24 /** Always write 16 bit header. */
25 #define BITS 16
26
27 static void make_wav_header(unsigned int channels, unsigned int sample_rate,
28 char *headbuf)
29 {
30
31 unsigned int size = 0x7fffffff;
32 int bytespersec = channels * sample_rate * BITS / 8;
33 int align = channels * BITS / 8;
34
35 PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, sample_rate);
36 memset(headbuf, 0, WAV_HEADER_LEN);
37 memcpy(headbuf, "RIFF", 4);
38 write_u32(headbuf + 4, size - 8);
39 memcpy(headbuf + 8, "WAVE", 4);
40 memcpy(headbuf + 12, "fmt ", 4);
41 write_u32(headbuf + 16, 16); /* 16 + extra format bytes (zero) */
42 write_u16(headbuf + 20, 1); /* format (1 == PCM/uncompressed) */
43 write_u16(headbuf + 22, channels);
44 write_u32(headbuf + 24, sample_rate);
45 write_u32(headbuf + 28, bytespersec);
46 write_u16(headbuf + 32, align); /* number of bytes per sample slice */
47 write_u16(headbuf + 34, BITS); /* significant bits per sample */
48 memcpy(headbuf + 36, "data", 4); /* chunk ID */
49 write_u32(headbuf + 40, size - 44); /* chunk size */
50 }
51
52 static void wav_close(struct filter_node *fn)
53 {
54 free(fn->private_data);
55 fn->private_data = NULL;
56 }
57
58 static void wav_open(struct filter_node *fn)
59 {
60 int *bof;
61
62 fn->private_data = para_malloc(sizeof(int));
63 bof = fn->private_data;
64 *bof = 1;
65 }
66
67 static void wav_pre_select(struct sched *s, struct task *t)
68 {
69 struct filter_node *fn = container_of(t, struct filter_node, task);
70 size_t iqs = btr_get_input_queue_size(fn->btrn);
71
72 t->error = 0;
73 if (iqs == 0)
74 return;
75 s->timeout.tv_sec = 0;
76 s->timeout.tv_usec = 1;
77 }
78
79 static void wav_post_select(__a_unused struct sched *s, struct task *t)
80 {
81 struct filter_node *fn = container_of(t, struct filter_node, task);
82 struct btr_node *btrn = fn->btrn;
83 size_t iqs = btr_get_input_queue_size(btrn);
84 int ret;
85 char *header, *buf;
86 int32_t rate, ch;
87
88 t->error = 0;
89 if (iqs == 0) {
90 ret = -E_WAV_EOF;
91 if (btr_no_parent(btrn))
92 goto err;
93 return;
94 }
95 ret = btr_exec_up(btrn, "sample_rate", &buf);
96 if (ret < 0) {
97 ret = -E_WAV_BAD_FC;
98 goto err;
99 }
100 ret = para_atoi32(buf, &rate);
101 free(buf);
102 if (ret < 0)
103 goto err;
104 ret = btr_exec_up(btrn, "channels", &buf);
105 if (ret < 0) {
106 ret = -E_WAV_BAD_FC;
107 goto err;
108 }
109 ret = para_atoi32(buf, &ch);
110 free(buf);
111 if (ret < 0)
112 goto err;
113 header = para_malloc(WAV_HEADER_LEN);
114 make_wav_header(ch, rate, header);
115 btr_add_output(header, WAV_HEADER_LEN, btrn);
116 ret = -E_WAV_SUCCESS;
117 err:
118 t->error = ret;
119 if (ret == -E_WAV_SUCCESS)
120 btr_splice_out_node(btrn);
121 else {
122 btr_remove_node(btrn);
123 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
124 }
125 }
126
127 /**
128 * The init function of the wav filter.
129 *
130 * \param f Structure to initialize.
131 */
132 void wav_filter_init(struct filter *f)
133 {
134 f->close = wav_close;
135 f->open = wav_open;
136 f->pre_select = wav_pre_select;
137 f->post_select = wav_post_select;
138 }