2 * Copyright (C) 2005-2011 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 /** A wav header is always 44 bytes. */
23 #define WAV_HEADER_LEN 44
24 /** Always write 16 bit header. */
27 static void make_wav_header(unsigned int channels, unsigned int sample_rate,
31 unsigned int size = 0x7fffffff;
32 int bytespersec = channels * sample_rate * BITS / 8;
33 int align = channels * BITS / 8;
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 */
52 static void wav_close(struct filter_node *fn)
54 free(fn->private_data);
55 fn->private_data = NULL;
58 static void wav_open(struct filter_node *fn)
62 fn->private_data = para_malloc(sizeof(int));
63 bof = fn->private_data;
67 static void wav_pre_select(struct sched *s, struct task *t)
69 struct filter_node *fn = container_of(t, struct filter_node, task);
70 size_t iqs = btr_get_input_queue_size(fn->btrn);
78 static void wav_post_select(__a_unused struct sched *s, struct task *t)
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);
90 if (btr_no_parent(btrn))
94 ret = btr_exec_up(btrn, "sample_rate", &buf);
99 ret = para_atoi32(buf, &rate);
103 ret = btr_exec_up(btrn, "channels", &buf);
108 ret = para_atoi32(buf, &ch);
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;
118 if (ret == -E_WAV_SUCCESS)
119 btr_splice_out_node(btrn);
121 btr_remove_node(btrn);
122 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
127 * The init function of the wav filter.
129 * \param f Structure to initialize.
131 void wav_filter_init(struct filter *f)
133 f->close = wav_close;
135 f->pre_select = wav_pre_select;
136 f->post_select = wav_post_select;