1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file check_wav.c Detect and delete a wav header. */
11 #include "buffer_tree.h"
13 #include "check_wav.h"
14 #include "portable_io.h"
16 /** Length of a standard wav header. */
17 #define WAV_HEADER_LEN 44
19 /** The possible states of a check_wav instance. */
20 enum check_wav_state {
21 /** Initial state, less than \p WAV_HEADER_LEN bytes available. */
23 /** Wav hader was detected. */
25 /** First part of the stream did not look like a wav header. */
29 struct check_wav_context {
30 enum check_wav_state state;
31 struct btr_node *btrn;
33 /* Command line args. */
34 struct wav_params params;
35 /* Extracted from the wav header.*/
37 unsigned sample_format;
42 * Request a minimal timeout if not idle.
44 * \param s The scheduler instance.
45 * \param cwc The buffer tree node is derived from this.
47 * If no data is available and the buffer tree node is not in error state, the
48 * function does nothing.
50 void check_wav_pre_monitor(struct sched *s, struct check_wav_context *cwc)
52 int ret = btr_node_status(cwc->btrn, cwc->min_iqs, BTR_NT_INTERNAL);
57 static int check_wav_exec(const struct btr_node *btrn, const char *cmd, char **result)
59 struct check_wav_context *cwc = btr_context(btrn);
60 int val, header_val, given, arg;
62 header_val = cwc->channels;
63 arg = cwc->params.channels_arg;
64 given = cwc->params.channels_given;
65 if (!strcmp(cmd, "channels"))
68 header_val = cwc->sample_rate;
69 arg = cwc->params.sample_rate_arg;
70 given = cwc->params.sample_rate_given;
71 if (!strcmp(cmd, "sample_rate"))
74 header_val = cwc->sample_format;
75 arg = cwc->params.sample_format_arg;
76 given = cwc->params.sample_format_given;
77 if (!strcmp(cmd, "sample_format"))
80 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
91 * No wav header available and no value specified at
92 * the command line. Maybe one of our parent nodes
95 if (btr_exec_up(btr_parent(cwc->btrn), cmd, result) >= 0)
97 /* Use default value */
101 return -E_BTR_NAVAIL;
104 *result = make_message("%d", val);
109 * Filter out the wav header, pushdown everything else.
111 * \param cwc The context of this instance.
113 * This function looks at the first \p WAV_HEADER_SIZE bytes of the input queue
114 * of the btrn of \a cwc. If they look like a wav header, the function extracts
115 * the information of interest and swallows this part of the stream. Otherwise
116 * it is pushed down to all children. In either case the rest of the input is
117 * pushed down as well.
119 * Once the first part has been processed this way, the state of the instance
120 * changes from \p CWS_NEED_HEADER to \p CWS_HAVE_HEADER or \p CWS_NO_HEADER.
124 int check_wav_post_monitor(struct check_wav_context *cwc)
126 struct btr_node *btrn = cwc->btrn;
130 uint16_t format_code, bps; /* bits per sample */
131 const char *sample_formats[] = {SAMPLE_FORMATS};
135 ret = btr_node_status(btrn, cwc->min_iqs, BTR_NT_INTERNAL);
138 if (cwc->state != CWS_NEED_HEADER)
140 btr_merge(btrn, cwc->min_iqs);
141 sz = btr_next_buffer(btrn, (char **)&a);
142 if (sz < cwc->min_iqs) /* file size less than WAV_HEADER_SIZE */
146 * The default byte ordering assumed for WAVE data files is
147 * little-endian. Files written using the big-endian byte ordering
148 * scheme have the identifier RIFX instead of RIFF.
150 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
151 (a[3] != 'F' && a[3] != 'X')) {
152 PARA_NOTICE_LOG("wav header not found\n");
153 cwc->state = CWS_NO_HEADER;
156 PARA_INFO_LOG("found wav header\n");
157 cwc->state = CWS_HAVE_HEADER;
158 /* Only set those values which have not already been set. */
159 cwc->channels = a[22];
160 format_code = read_u16(a + 20);
161 if (format_code != 1 && format_code != 3) {
162 cwc->state = CWS_NO_HEADER;
165 cwc->sample_rate = read_u32(a + 24);
166 bps = read_u16(a + 34);
167 if (bps != 8 && bps != 16 && bps != 32) {
168 PARA_WARNING_LOG("%u bps not supported, assuming 16\n",
172 if ((bps < 32 && format_code != 1) || (bps == 32 && format_code != 3)) {
173 PARA_WARNING_LOG("invalid bps/format_code (%u/%u)\n",
175 cwc->state = CWS_NO_HEADER;
179 * 8-bit samples are stored as unsigned bytes, ranging from 0
180 * to 255. 16-bit samples are stored as 2's-complement signed
181 * integers, ranging from -32768 to 32767.
184 cwc->sample_format = SF_U8;
186 cwc->sample_format = (a[3] == 'F')?
187 SF_S16_LE : SF_S16_BE;
189 cwc->sample_format = (a[3] == 'F')?
190 SF_FLOAT_LE : SF_FLOAT_BE;
191 PARA_NOTICE_LOG("%uHz, %s, %s\n", cwc->sample_rate,
192 cwc->channels == 1? "mono" : "stereo",
193 sample_formats[cwc->sample_format]);
194 btr_consume(btrn, WAV_HEADER_LEN);
199 btr_remove_node(&cwc->btrn);
204 * Allocate and set up a new check_wav instance.
206 * \param parent This buffer tree node will be the parent of the new node.
207 * \param child The child of the new node.
208 * \param params Default values and options.
209 * \param cw_btrn A pointer to the check wav node is returned here.
211 * This function also sets up the ->execute handler of the btrn so that all
212 * children of this node can figure out channel count, sample rate, etc.
214 * \return The (opaque) handle of the newly created check_wav instance. It is
215 * supposed to be passed to \ref check_wav_pre_monitor() and \ref
216 * check_wav_post_monitor().
218 * \sa \ref btr_new_node.
220 struct check_wav_context *check_wav_init(struct btr_node *parent,
221 struct btr_node *child, struct wav_params *params,
222 struct btr_node **cw_btrn)
224 struct check_wav_context *cwc = zalloc(sizeof(*cwc));
226 cwc->state = CWS_NEED_HEADER;
227 cwc->min_iqs = WAV_HEADER_LEN;
228 cwc->params = *params;
229 cwc->btrn = btr_new_node(&(struct btr_node_description)
230 EMBRACE(.name = "check_wav", .parent = parent, .child = child,
231 .handler = check_wav_exec, .context = cwc));
233 *cw_btrn = cwc->btrn;
238 * Dellocate all ressources of a check_wav instance.
240 * \param cwc Determines the instance to shut down.
242 * This function may only be called after check_wav_post_monitor() has returned
245 void check_wav_shutdown(struct check_wav_context *cwc)