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