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 * Set select timeout according to the given context.
44 * \param s Contains the timeval that should be set.
45 * \param cwc Contains a pointer to the buffer tree node.
47 * This requests a minimal timeout from the scheduler if btrn of \a cwc is not
50 void check_wav_pre_select(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(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_select(struct check_wav_context
*cwc
)
126 struct btr_node
*btrn
= cwc
->btrn
;
130 uint16_t 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 cwc
->sample_rate
= read_u32(a
+ 24);
161 bps
= read_u16(a
+ 34);
162 if (bps
!= 8 && bps
!= 16) {
163 PARA_WARNING_LOG("%u bps not supported, assuming 16\n",
168 * 8-bit samples are stored as unsigned bytes, ranging from 0
169 * to 255. 16-bit samples are stored as 2's-complement signed
170 * integers, ranging from -32768 to 32767.
173 cwc
->sample_format
= SF_U8
;
175 cwc
->sample_format
= (a
[3] == 'F')?
176 SF_S16_LE
: SF_S16_BE
;
177 PARA_NOTICE_LOG("%uHz, %s, %s\n", cwc
->sample_rate
,
178 cwc
->channels
== 1? "mono" : "stereo",
179 sample_formats
[cwc
->sample_format
]);
180 btr_consume(btrn
, WAV_HEADER_LEN
);
185 btr_remove_node(&cwc
->btrn
);
190 * Allocate and set up a new check_wav instance.
192 * \param parent This buffer tree node will be the parent of the new node.
193 * \param child The child of the new node.
194 * \param params Default values and options.
195 * \param cw_btrn A pointer to the check wav node is returned here.
197 * This function also sets up the ->execute handler of the btrn so that all
198 * children of this node can figure out channel count, sample rate, etc.
200 * \return The (opaque) handle of the newly created check_wav instance. It is
201 * supposed to be passed to \ref check_wav_pre_select() and \ref
202 * check_wav_post_select().
204 * \sa \ref btr_new_node.
206 struct check_wav_context
*check_wav_init(struct btr_node
*parent
,
207 struct btr_node
*child
, struct wav_params
*params
,
208 struct btr_node
**cw_btrn
)
210 struct check_wav_context
*cwc
= para_calloc(sizeof(*cwc
));
212 cwc
->state
= CWS_NEED_HEADER
;
213 cwc
->min_iqs
= WAV_HEADER_LEN
;
214 cwc
->params
= *params
;
215 cwc
->btrn
= btr_new_node(&(struct btr_node_description
)
216 EMBRACE(.name
= "check_wav", .parent
= parent
, .child
= child
,
217 .handler
= check_wav_exec
, .context
= cwc
));
219 *cw_btrn
= cwc
->btrn
;
224 * Dellocate all ressources of a check_wav instance.
226 * \param cwc Determines the instance to shut down.
228 * This function may only be called after check_wav_post_select() has returned
231 void check_wav_shutdown(struct check_wav_context
*cwc
)