2 * Copyright (C) 2005-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file check_wav.c Detect and delete a wav header. */
15 #include "buffer_tree.h"
17 #include "check_wav.h"
19 /** Length of a standard wav header. */
20 #define WAV_HEADER_LEN 44
22 enum check_wav_state {
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;
40 void check_wav_pre_select(struct sched *s, struct check_wav_context *cwc)
42 int ret = btr_node_status(cwc->btrn, cwc->min_iqs, BTR_NT_INTERNAL);
47 static int check_wav_exec(struct btr_node *btrn, const char *cmd, char **result)
49 struct check_wav_context *cwc = btr_context(btrn);
50 int val, header_val, given, arg;
52 header_val = cwc->channels;
53 arg = cwc->params.channels_arg;
54 given = cwc->params.channels_given;
55 if (!strcmp(cmd, "channels"))
58 header_val = cwc->sample_rate;
59 arg = cwc->params.sample_rate_arg;
60 given = cwc->params.sample_rate_given;
61 if (!strcmp(cmd, "sample_rate"))
64 header_val = cwc->sample_format;
65 arg = cwc->params.sample_format_arg;
66 given = cwc->params.sample_format_given;
67 if (!strcmp(cmd, "sample_format"))
70 return -ERRNO_TO_PARA_ERROR(ENOTSUP);
80 /* Use default value */
87 *result = make_message("%d", val);
91 int check_wav_post_select(struct check_wav_context *cwc)
93 struct btr_node *btrn = cwc->btrn;
97 uint16_t bps; /* bits per sample */
98 const char *sample_formats[] = {SAMPLE_FORMATS};
100 ret = btr_node_status(btrn, cwc->min_iqs, BTR_NT_INTERNAL);
103 if (cwc->state != CWS_NEED_HEADER)
105 btr_merge(btrn, cwc->min_iqs);
106 sz = btr_next_buffer(btrn, (char **)&a);
107 if (sz < cwc->min_iqs) /* file size less than WAV_HEADER_SIZE */
111 * The default byte ordering assumed for WAVE data files is
112 * little-endian. Files written using the big-endian byte ordering
113 * scheme have the identifier RIFX instead of RIFF.
115 if (a[0] != 'R' || a[1] != 'I' || a[2] != 'F' ||
116 (a[3] != 'F' && a[3] != 'X')) {
117 PARA_NOTICE_LOG("wav header not found\n");
118 cwc->state = CWS_NO_HEADER;
121 PARA_INFO_LOG("found wav header\n");
122 cwc->state = CWS_HAVE_HEADER;
123 /* Only set those values which have not already been set. */
124 cwc->channels = (unsigned)a[22];
125 cwc->sample_rate = a[24] + (a[25] << 8) + (a[26] << 16) + (a[27] << 24);
126 bps = a[34] + ((unsigned)a[35] << 8);
127 if (bps != 8 && bps != 16) {
128 PARA_WARNING_LOG("%u bps not supported, assuming 16\n",
133 * 8-bit samples are stored as unsigned bytes, ranging from 0
134 * to 255. 16-bit samples are stored as 2's-complement signed
135 * integers, ranging from -32768 to 32767.
138 cwc->sample_format = SF_U8;
140 cwc->sample_format = (a[3] == 'F')?
141 SF_S16_LE : SF_S16_BE;
142 PARA_NOTICE_LOG("%dHz, %s, %s\n", cwc->sample_rate,
143 cwc->channels == 1? "mono" : "stereo",
144 sample_formats[cwc->sample_format]);
145 btr_consume(btrn, WAV_HEADER_LEN);
150 btr_remove_node(&cwc->btrn);
154 struct check_wav_context *check_wav_init(struct btr_node *parent,
155 struct wav_params *params, struct btr_node **cw_btrn)
157 struct check_wav_context *cwc = para_calloc(sizeof(*cwc));
159 cwc->state = CWS_NEED_HEADER;
160 cwc->min_iqs = WAV_HEADER_LEN;
161 cwc->params = *params;
162 cwc->btrn = btr_new_node(&(struct btr_node_description)
163 EMBRACE(.name = "check_wav", .parent = parent,
164 .handler = check_wav_exec, .context = cwc));
166 *cw_btrn = cwc->btrn;
170 void check_wav_shutdown(struct check_wav_context *cwc)