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