2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file wav_filter.c A filter that inserts a wave header. */
17 #include "buffer_tree.h"
20 #include "portable_io.h"
22 /** size of the output buffer */
23 #define WAV_OUTBUF_SIZE 81920
24 /** a wav header is always 44 bytes */
25 #define WAV_HEADER_LEN 44
26 /** always write 16 bit header */
29 static void make_wav_header(unsigned int channels, unsigned int samplerate,
33 unsigned int size = 0x7fffffff;
34 int bytespersec = channels * samplerate * BITS / 8;
35 int align = channels * BITS / 8;
37 PARA_DEBUG_LOG("writing wave header: %d channels, %d KHz\n", channels, samplerate);
38 memset(headbuf, 0, WAV_HEADER_LEN);
39 memcpy(headbuf, "RIFF", 4);
40 write_u32(headbuf + 4, size - 8);
41 memcpy(headbuf + 8, "WAVE", 4);
42 memcpy(headbuf + 12, "fmt ", 4);
43 write_u32(headbuf + 16, 16); /* 16 + extra format bytes (zero) */
44 write_u16(headbuf + 20, 1); /* format (1 == PCM/uncompressed) */
45 write_u16(headbuf + 22, channels);
46 write_u32(headbuf + 24, samplerate);
47 write_u32(headbuf + 28, bytespersec);
48 write_u16(headbuf + 32, align); /* number of bytes per sample slice */
49 write_u16(headbuf + 34, BITS); /* significant bits per sample */
50 memcpy(headbuf + 36, "data", 4); /* chunk ID */
51 write_u32(headbuf + 40, size - 44); /* chunk size */
54 static void wav_close(struct filter_node *fn)
58 free(fn->private_data);
59 fn->private_data = NULL;
62 static void wav_open(struct filter_node *fn)
66 fn->bufsize = WAV_OUTBUF_SIZE;
67 fn->buf = para_malloc(fn->bufsize);
68 fn->private_data = para_malloc(sizeof(int));
69 bof = fn->private_data;
72 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
73 fn, fn->buf, fn->loaded);
76 static void wav_pre_select(struct sched *s, struct task *t)
78 struct filter_node *fn = container_of(t, struct filter_node, task);
79 size_t iqs = btr_get_input_queue_size(fn->btrn);
84 s->timeout.tv_sec = 0;
85 s->timeout.tv_usec = 1;
88 static void wav_post_select(__a_unused struct sched *s, struct task *t)
90 struct filter_node *fn = container_of(t, struct filter_node, task);
91 struct btr_node *btrn = fn->btrn;
92 size_t iqs = btr_get_input_queue_size(btrn);
100 if (btr_no_parent(btrn))
104 ret = btr_exec_up(btrn, "samplerate", &buf);
109 ret = para_atoi32(buf, &rate);
113 ret = btr_exec_up(btrn, "channels", &buf);
118 ret = para_atoi32(buf, &ch);
122 header = para_malloc(WAV_HEADER_LEN);
123 make_wav_header(ch, rate, header);
124 btr_add_output(header, WAV_HEADER_LEN, btrn);
125 ret = -E_WAV_SUCCESS;
128 if (ret == -E_WAV_SUCCESS)
129 btr_splice_out_node(btrn);
131 btr_remove_node(btrn);
132 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
137 * The init function of the wav filter.
139 * \param f Structure to initialize.
141 void wav_filter_init(struct filter *f)
143 f->close = wav_close;
145 f->pre_select = wav_pre_select;
146 f->post_select = wav_post_select;