2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/types.h>
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "input/wav"
33 /* How many bytes at a time to process and send to the session bus. */
34 #define CHUNK_SIZE (1 * 1024 * 1024 * sizeof(float))
36 /* Minimum size of header + 1 8-bit mono PCM sample. */
37 #define MIN_DATA_CHUNK_OFFSET 45
39 /* Expect to find the "data" chunk within this offset from the start. */
40 #define MAX_DATA_CHUNK_OFFSET 1024
42 #define WAVE_FORMAT_PCM_ 0x0001
43 #define WAVE_FORMAT_IEEE_FLOAT_ 0x0003
44 #define WAVE_FORMAT_EXTENSIBLE_ 0xfffe
54 gboolean create_channels;
55 GSList *prev_sr_channels;
58 static int parse_wav_header(GString *buf, struct context *inc)
61 unsigned int fmt_code, samplesize, num_channels, unitsize;
63 if (buf->len < MIN_DATA_CHUNK_OFFSET)
66 fmt_code = RL16(buf->str + 20);
67 samplerate = RL32(buf->str + 24);
69 samplesize = RL16(buf->str + 32);
70 num_channels = RL16(buf->str + 22);
71 if (num_channels == 0)
73 unitsize = samplesize / num_channels;
74 if (unitsize != 1 && unitsize != 2 && unitsize != 4) {
75 sr_err("Only 8, 16 or 32 bits per sample supported.");
79 if (fmt_code == WAVE_FORMAT_PCM_) {
80 } else if (fmt_code == WAVE_FORMAT_IEEE_FLOAT_) {
82 sr_err("only 32-bit floats supported.");
85 } else if (fmt_code == WAVE_FORMAT_EXTENSIBLE_) {
87 /* Not enough for extensible header and next chunk. */
90 if (RL16(buf->str + 16) != 40) {
91 sr_err("WAV extensible format chunk must be 40 bytes.");
94 if (RL16(buf->str + 36) != 22) {
95 sr_err("WAV extension must be 22 bytes.");
98 if (RL16(buf->str + 34) != RL16(buf->str + 38)) {
99 sr_err("Reduced valid bits per sample not supported.");
102 /* Real format code is the first two bytes of the GUID. */
103 fmt_code = RL16(buf->str + 44);
104 if (fmt_code != WAVE_FORMAT_PCM_ && fmt_code != WAVE_FORMAT_IEEE_FLOAT_) {
105 sr_err("Only PCM and floating point samples are supported.");
108 if (fmt_code == WAVE_FORMAT_IEEE_FLOAT_ && unitsize != 4) {
109 sr_err("only 32-bit floats supported.");
113 sr_err("Only PCM and floating point samples are supported.");
118 inc->fmt_code = fmt_code;
119 inc->samplerate = samplerate;
120 inc->samplesize = samplesize;
121 inc->num_channels = num_channels;
122 inc->unitsize = unitsize;
123 inc->found_data = FALSE;
129 static int format_match(GHashTable *metadata, unsigned int *confidence)
134 buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
135 if (strncmp(buf->str, "RIFF", 4))
137 if (strncmp(buf->str + 8, "WAVE", 4))
139 if (strncmp(buf->str + 12, "fmt ", 4))
142 * Only gets called when we already know this is a WAV file, so
143 * this parser can log error messages.
145 if ((ret = parse_wav_header(buf, NULL)) != SR_OK)
153 static int init(struct sr_input *in, GHashTable *options)
159 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
160 in->priv = g_malloc0(sizeof(struct context));
163 inc->create_channels = TRUE;
168 static int find_data_chunk(GString *buf, int initial_offset)
170 unsigned int offset, i;
172 offset = initial_offset;
173 while (offset < MIN(MAX_DATA_CHUNK_OFFSET, buf->len)) {
174 if (!memcmp(buf->str + offset, "data", 4))
175 /* Skip into the samples. */
177 for (i = 0; i < 4; i++) {
178 if (!isalnum(buf->str[offset + i])
179 && !isblank(buf->str[offset + i]))
180 /* Doesn't look like a chunk ID. */
183 /* Skip past this chunk. */
184 offset += 8 + RL32(buf->str + offset + 4);
187 if (offset > MAX_DATA_CHUNK_OFFSET)
193 static void send_chunk(const struct sr_input *in, int offset, int num_samples)
195 struct sr_datafeed_packet packet;
196 struct sr_datafeed_analog analog;
197 struct sr_analog_encoding encoding;
198 struct sr_analog_meaning meaning;
199 struct sr_analog_spec spec;
202 int total_samples, samplenum;
207 total_samples = num_samples * inc->num_channels;
208 fdata = g_malloc0(total_samples * sizeof(float));
209 s = in->buf->str + offset;
212 for (samplenum = 0; samplenum < total_samples; samplenum++) {
213 if (inc->fmt_code == WAVE_FORMAT_PCM_) {
214 switch (inc->unitsize) {
216 /* 8-bit PCM samples are unsigned. */
217 fdata[samplenum] = *(uint8_t*)(s) / (float)255;
220 fdata[samplenum] = RL16S(s) / (float)INT16_MAX;
223 fdata[samplenum] = RL32S(s) / (float)INT32_MAX;
228 #ifdef WORDS_BIGENDIAN
230 for (i = 0; i < inc->unitsize; i++)
231 d[i] = s[inc->unitsize - 1 - i];
233 memcpy(d, s, inc->unitsize);
240 /* TODO: Use proper 'digits' value for this device (and its modes). */
241 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
242 packet.type = SR_DF_ANALOG;
243 packet.payload = &analog;
244 analog.num_samples = num_samples;
246 analog.meaning->channels = in->sdi->channels;
247 analog.meaning->mq = 0;
248 analog.meaning->mqflags = 0;
249 analog.meaning->unit = 0;
250 sr_session_send(in->sdi, &packet);
254 static int process_buffer(struct sr_input *in)
257 int offset, chunk_samples, total_samples, processed, max_chunk_samples;
262 std_session_send_df_header(in->sdi);
263 (void)sr_session_send_meta(in->sdi, SR_CONF_SAMPLERATE,
264 g_variant_new_uint64(inc->samplerate));
268 if (!inc->found_data) {
269 /* Skip past size of 'fmt ' chunk. */
270 i = 20 + RL32(in->buf->str + 16);
271 offset = find_data_chunk(in->buf, i);
273 if (in->buf->len > MAX_DATA_CHUNK_OFFSET) {
274 sr_err("Couldn't find data chunk.");
278 inc->found_data = TRUE;
282 /* Round off up to the last channels * unitsize boundary. */
283 chunk_samples = (in->buf->len - offset) / inc->samplesize;
284 max_chunk_samples = CHUNK_SIZE / inc->samplesize;
286 total_samples = chunk_samples;
287 while (processed < total_samples) {
288 if (chunk_samples > max_chunk_samples)
289 num_samples = max_chunk_samples;
291 num_samples = chunk_samples;
292 send_chunk(in, offset, num_samples);
293 offset += num_samples * inc->samplesize;
294 chunk_samples -= num_samples;
295 processed += num_samples;
298 if ((unsigned int)offset < in->buf->len) {
300 * The incoming buffer wasn't processed completely. Stash
301 * the leftover data for next time.
303 g_string_erase(in->buf, 0, offset);
305 g_string_truncate(in->buf, 0);
311 * Check the channel list for consistency across file re-import. See
312 * the VCD input module for more details and motivation.
315 static void keep_header_for_reread(const struct sr_input *in)
320 g_slist_free_full(inc->prev_sr_channels, sr_channel_free_cb);
321 inc->prev_sr_channels = in->sdi->channels;
322 in->sdi->channels = NULL;
325 static int check_header_in_reread(const struct sr_input *in)
334 if (!inc->prev_sr_channels)
337 if (sr_channel_lists_differ(inc->prev_sr_channels, in->sdi->channels)) {
338 sr_err("Channel list change not supported for file re-read.");
341 g_slist_free_full(in->sdi->channels, sr_channel_free_cb);
342 in->sdi->channels = inc->prev_sr_channels;
343 inc->prev_sr_channels = NULL;
348 static int receive(struct sr_input *in, GString *buf)
352 char channelname[16];
354 g_string_append_len(in->buf, buf->str, buf->len);
356 if (in->buf->len < MIN_DATA_CHUNK_OFFSET) {
358 * Don't even try until there's enough room
359 * for the data segment to start.
365 if (!in->sdi_ready) {
366 if ((ret = parse_wav_header(in->buf, inc)) == SR_ERR_NA)
367 /* Not enough data yet. */
369 else if (ret != SR_OK)
372 if (inc->create_channels) {
373 for (int i = 0; i < inc->num_channels; i++) {
374 snprintf(channelname, sizeof(channelname), "CH%d", i + 1);
375 sr_channel_new(in->sdi, i, SR_CHANNEL_ANALOG, TRUE, channelname);
377 if (!check_header_in_reread(in))
380 inc->create_channels = FALSE;
382 /* sdi is ready, notify frontend. */
383 in->sdi_ready = TRUE;
387 ret = process_buffer(in);
392 static int end(struct sr_input *in)
398 ret = process_buffer(in);
404 std_session_send_df_end(in->sdi);
409 static int reset(struct sr_input *in)
414 memset(inc, 0, sizeof(*inc));
417 * Create, and re-create channels for every iteration of file
418 * import. Other logic will enforce a consistent set of channels
419 * across re-import, or an appropriate error message when file
420 * properties should change.
422 keep_header_for_reread(in);
423 inc->create_channels = TRUE;
425 g_string_truncate(in->buf, 0);
430 SR_PRIV struct sr_input_module input_wav = {
433 .desc = "Microsoft WAV file format data",
434 .exts = (const char*[]){"wav", NULL},
435 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
436 .format_match = format_match,