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