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"
15 /** Length of a standard wav header. */
16 #define WAV_HEADER_LEN 44
18 /** The possible states of a check_wav instance. */
19 enum check_wav_state {
20 /** Initial state, less than \p WAV_HEADER_LEN bytes available. */
22 /** Wav hader was detected. */
24 /** First part of the stream did not look like a wav header. */
28 struct check_wav_context {
29 enum check_wav_state state;
30 struct btr_node *btrn;
32 /* Command line args. */
33 struct wav_params params;
34 /* Extracted from the wav header.*/
36 unsigned sample_format;
41 * Set select timeout according to the given context.
43 * \param s Contains the timeval that should be set.
44 * \param cwc Contains a pointer to the buffer tree node.
46 * This requests a minimal timeout from the scheduler if btrn of \a cwc is not
49 void check_wav_pre_select(struct sched *s, struct check_wav_context *cwc)
51 int ret = btr_node_status(cwc->btrn, cwc->min_iqs, BTR_NT_INTERNAL);
56 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
58 struct check_wav_context *cwc = btr_context(btrn);
59 int val, header_val, given, arg;
61 header_val = cwc->channels;
62 arg = cwc->params.channels_arg;
63 given = cwc->params.channels_given;
64 if (!strcmp(cmd, "channels"))
67 header_val = cwc->sample_rate;
68 arg = cwc->params.sample_rate_arg;
69 given = cwc->params.sample_rate_given;
70 if (!strcmp(cmd, "sample_rate"))
73 header_val = cwc->sample_format;
74 arg = cwc->params.sample_format_arg;
75 given = cwc->params.sample_format_given;
76 if (!strcmp(cmd, "sample_format"))
79 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
90 * No wav header available and no value specified at
91 * the command line. Maybe one of our parent nodes
94 if (btr_exec_up(btr_parent(cwc->btrn), cmd, result) >= 0)
96 /* Use default value */
100 return -E_BTR_NAVAIL;
103 *result = make_message("%d", val);
108 * Filter out the wav header, pushdown everything else.
110 * \param cwc The context of this instance.
112 * This function looks at the first \p WAV_HEADER_SIZE bytes of the input queue
113 * of the btrn of \a cwc. If they look like a wav header, the function extracts
114 * the information of interest and swallows this part of the stream. Otherwise
115 * it is pushed down to all children. In either case the rest of the input is
116 * pushed down as well.
118 * Once the first part has been processed this way, the state of the instance
119 * changes from \p CWS_NEED_HEADER to \p CWS_HAVE_HEADER or \p CWS_NO_HEADER.
123 int check_wav_post_select(struct check_wav_context *cwc)
125 struct btr_node *btrn = cwc->btrn;
129 uint16_t bps; /* bits per sample */
130 const char *sample_formats[] = {SAMPLE_FORMATS};
134 ret = btr_node_status(btrn, cwc->min_iqs, BTR_NT_INTERNAL);
137 if (cwc->state != CWS_NEED_HEADER)
139 btr_merge(btrn, cwc->min_iqs);
140 sz = btr_next_buffer(btrn, (char **)&a);
141 if (sz < cwc->min_iqs) /* file size less than WAV_HEADER_SIZE */
145 * The default byte ordering assumed for WAVE data files is
146 * little-endian. Files written using the big-endian byte ordering
147 * scheme have the identifier RIFX instead of RIFF.
149 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
150 (a[3] != 'F' && a[3] != 'X')) {
151 PARA_NOTICE_LOG("wav header not found\n");
152 cwc->state = CWS_NO_HEADER;
155 PARA_INFO_LOG("found wav header\n");
156 cwc->state = CWS_HAVE_HEADER;
157 /* Only set those values which have not already been set. */
158 cwc->channels = (unsigned)a[22];
159 cwc->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
160 bps = a[34] + ((unsigned)a[35] << 8);
161 if (bps != 8 && bps != 16) {
162 PARA_WARNING_LOG("%u bps not supported, assuming 16\n",
167 * 8-bit samples are stored as unsigned bytes, ranging from 0
168 * to 255. 16-bit samples are stored as 2's-complement signed
169 * integers, ranging from -32768 to 32767.
172 cwc->sample_format = SF_U8;
174 cwc->sample_format = (a[3] == 'F')?
175 SF_S16_LE : SF_S16_BE;
176 PARA_NOTICE_LOG("%uHz, %s, %s\n", cwc->sample_rate,
177 cwc->channels == 1? "mono" : "stereo",
178 sample_formats[cwc->sample_format]);
179 btr_consume(btrn, WAV_HEADER_LEN);
184 btr_remove_node(&cwc->btrn);
189 * Allocate and set up a new check_wav instance.
191 * \param parent This buffer tree node will be the parent of the new node.
192 * \param child The child of the new node.
193 * \param params Default values and options.
194 * \param cw_btrn A pointer to the check wav node is returned here.
196 * This function also sets up the ->execute handler of the btrn so that all
197 * children of this node can figure out channel count, sample rate, etc.
199 * \return The (opaque) handle of the newly created check_wav instance. It is
200 * supposed to be passed to \ref check_wav_pre_select() and \ref
201 * check_wav_post_select().
203 * \sa \ref btr_new_node.
205 struct check_wav_context *check_wav_init(struct btr_node *parent,
206 struct btr_node *child, struct wav_params *params,
207 struct btr_node **cw_btrn)
209 struct check_wav_context *cwc = para_calloc(sizeof(*cwc));
211 cwc->state = CWS_NEED_HEADER;
212 cwc->min_iqs = WAV_HEADER_LEN;
213 cwc->params = *params;
214 cwc->btrn = btr_new_node(&(struct btr_node_description)
215 EMBRACE(.name = "check_wav", .parent = parent, .child = child,
216 .handler = check_wav_exec, .context = cwc));
218 *cw_btrn = cwc->btrn;
223 * Dellocate all ressources of a check_wav instance.
225 * \param cwc Determines the instance to shut down.
227 * This function may only be called after check_wav_post_select() has returned
230 void check_wav_shutdown(struct check_wav_context *cwc)