file_writer: Make file_write_open() a no-op.
[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         sched_min_delay(s);
76 }
77
78 static void wav_post_select(__a_unused struct sched *s, struct task *t)
79 {
80         struct filter_node *fn = container_of(t, struct filter_node, task);
81         struct btr_node *btrn = fn->btrn;
82         size_t iqs = btr_get_input_queue_size(btrn);
83         int ret;
84         char *header, *buf;
85         int32_t rate, ch;
86
87         t->error = 0;
88         if (iqs == 0) {
89                 ret = -E_WAV_EOF;
90                 if (btr_no_parent(btrn))
91                         goto err;
92                 return;
93         }
94         ret = btr_exec_up(btrn, "sample_rate", &buf);
95         if (ret < 0) {
96                 ret = -E_WAV_BAD_FC;
97                 goto err;
98         }
99         ret = para_atoi32(buf, &rate);
100         free(buf);
101         if (ret < 0)
102                 goto err;
103         ret = btr_exec_up(btrn, "channels", &buf);
104         if (ret < 0) {
105                 ret = -E_WAV_BAD_FC;
106                 goto err;
107         }
108         ret = para_atoi32(buf, &ch);
109         free(buf);
110         if (ret < 0)
111                 goto err;
112         header = para_malloc(WAV_HEADER_LEN);
113         make_wav_header(ch, rate, header);
114         btr_add_output(header, WAV_HEADER_LEN, btrn);
115         ret = -E_WAV_SUCCESS;
116 err:
117         t->error = ret;
118         if (ret == -E_WAV_SUCCESS)
119                 btr_splice_out_node(btrn);
120         else {
121                 btr_remove_node(btrn);
122                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
123         }
124 }
125
126 /**
127  * The init function of the wav filter.
128  *
129  * \param f Structure to initialize.
130  */
131 void wav_filter_init(struct filter *f)
132 {
133         f->close = wav_close;
134         f->open = wav_open;
135         f->pre_select = wav_pre_select;
136         f->post_select = wav_post_select;
137 }