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 ssize_t
wav_convert(char *inbuf
, size_t len
, struct filter_node
*fn
)
57 int *bof
= fn
->private_data
;
62 if (!fn
->fc
->channels
|| !fn
->fc
->samplerate
) {
63 PARA_ERROR_LOG("%s\n", para_strerror(E_WAV_BAD_FC
));
66 make_wav_header(fn
->fc
->channels
, fn
->fc
->samplerate
, fn
->buf
);
67 fn
->loaded
= WAV_HEADER_LEN
;
71 copy
= PARA_MIN(len
, fn
->bufsize
- fn
->loaded
);
72 memmove(fn
->buf
+ fn
->loaded
, inbuf
, copy
);
74 // PARA_DEBUG_LOG("len = %d, copy = %d\n", len, copy);
78 static void wav_close(struct filter_node
*fn
)
82 free(fn
->private_data
);
83 fn
->private_data
= NULL
;
86 static void wav_open(struct filter_node
*fn
)
90 fn
->bufsize
= WAV_OUTBUF_SIZE
;
91 fn
->buf
= para_malloc(fn
->bufsize
);
92 fn
->private_data
= para_malloc(sizeof(int));
93 bof
= fn
->private_data
;
96 PARA_INFO_LOG("wav filter node: %p, output buffer: %p, loaded: %zd\n",
97 fn
, fn
->buf
, fn
->loaded
);
100 static void wav_pre_select(struct sched
*s
, struct task
*t
)
102 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
103 size_t iqs
= btr_get_input_queue_size(fn
->btrn
);
108 s
->timeout
.tv_sec
= 0;
109 s
->timeout
.tv_usec
= 1;
112 static void wav_post_select(__a_unused
struct sched
*s
, struct task
*t
)
114 struct filter_node
*fn
= container_of(t
, struct filter_node
, task
);
115 struct btr_node
*btrn
= fn
->btrn
;
116 size_t iqs
= btr_get_input_queue_size(btrn
);
125 if (btr_no_parent(btrn
))
129 ret
= btr_exec_up(btrn
, "samplerate", &buf
);
134 ret
= para_atoi32(buf
, &rate
);
138 ret
= btr_exec_up(btrn
, "channels", &buf
);
143 ret
= para_atoi32(buf
, &ch
);
147 header
= para_malloc(WAV_HEADER_LEN
);
148 make_wav_header(ch
, rate
, header
);
149 btr_add_output(header
, WAV_HEADER_LEN
, btrn
);
150 ret
= -E_WAV_SUCCESS
;
154 if (ret
== -E_WAV_SUCCESS
)
155 btr_splice_out_node(btrn
);
158 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
163 * the init function of the wav filter
165 * \param f struct to initialize
167 void wav_filter_init(struct filter
*f
)
169 f
->convert
= wav_convert
;
170 f
->close
= wav_close
;
172 f
->pre_select
= wav_pre_select
;
173 f
->post_select
= wav_post_select
;