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 4096
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
56 static int parse_wav_header(GString *buf, struct context *inc)
59 unsigned int fmt_code, samplesize, num_channels, unitsize;
61 if (buf->len < MIN_DATA_CHUNK_OFFSET)
64 fmt_code = RL16(buf->str + 20);
65 samplerate = RL32(buf->str + 24);
67 samplesize = RL16(buf->str + 32);
68 num_channels = RL16(buf->str + 22);
69 if (num_channels == 0)
71 unitsize = samplesize / num_channels;
72 if (unitsize != 1 && unitsize != 2 && unitsize != 4) {
73 sr_err("Only 8, 16 or 32 bits per sample supported.");
77 if (fmt_code == WAVE_FORMAT_PCM_) {
78 } else if (fmt_code == WAVE_FORMAT_IEEE_FLOAT_) {
80 sr_err("only 32-bit floats supported.");
83 } else if (fmt_code == WAVE_FORMAT_EXTENSIBLE_) {
85 /* Not enough for extensible header and next chunk. */
88 if (RL16(buf->str + 16) != 40) {
89 sr_err("WAV extensible format chunk must be 40 bytes.");
92 if (RL16(buf->str + 36) != 22) {
93 sr_err("WAV extension must be 22 bytes.");
96 if (RL16(buf->str + 34) != RL16(buf->str + 38)) {
97 sr_err("Reduced valid bits per sample not supported.");
100 /* Real format code is the first two bytes of the GUID. */
101 fmt_code = RL16(buf->str + 44);
102 if (fmt_code != WAVE_FORMAT_PCM_ && fmt_code != WAVE_FORMAT_IEEE_FLOAT_) {
103 sr_err("Only PCM and floating point samples are supported.");
106 if (fmt_code == WAVE_FORMAT_IEEE_FLOAT_ && unitsize != 4) {
107 sr_err("only 32-bit floats supported.");
111 sr_err("Only PCM and floating point samples are supported.");
116 inc->fmt_code = fmt_code;
117 inc->samplerate = samplerate;
118 inc->samplesize = samplesize;
119 inc->num_channels = num_channels;
120 inc->unitsize = unitsize;
121 inc->found_data = FALSE;
127 static int format_match(GHashTable *metadata)
132 buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
133 if (strncmp(buf->str, "RIFF", 4))
135 if (strncmp(buf->str + 8, "WAVE", 4))
137 if (strncmp(buf->str + 12, "fmt ", 4))
140 * Only gets called when we already know this is a WAV file, so
141 * this parser can log error messages.
143 if ((ret = parse_wav_header(buf, NULL)) != SR_OK)
149 static int init(struct sr_input *in, GHashTable *options)
153 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
154 in->priv = g_malloc0(sizeof(struct context));
159 static int find_data_chunk(GString *buf, int initial_offset)
161 unsigned int offset, i;
163 offset = initial_offset;
164 while (offset < MIN(MAX_DATA_CHUNK_OFFSET, buf->len)) {
165 if (!memcmp(buf->str + offset, "data", 4))
166 /* Skip into the samples. */
168 for (i = 0; i < 4; i++) {
169 if (!isalnum(buf->str[offset + i])
170 && !isblank(buf->str[offset + i]))
171 /* Doesn't look like a chunk ID. */
174 /* Skip past this chunk. */
175 offset += 8 + RL32(buf->str + offset + 4);
178 if (offset > MAX_DATA_CHUNK_OFFSET)
184 static void send_chunk(const struct sr_input *in, int offset, int num_samples)
186 struct sr_datafeed_packet packet;
187 struct sr_datafeed_analog_old analog;
189 float fdata[CHUNK_SIZE];
191 int total_samples, samplenum;
196 s = in->buf->str + offset;
198 memset(fdata, 0, CHUNK_SIZE);
199 total_samples = num_samples * inc->num_channels;
200 for (samplenum = 0; samplenum < total_samples; samplenum++) {
201 if (inc->fmt_code == WAVE_FORMAT_PCM_) {
203 memcpy(&sample, s, inc->unitsize);
204 switch (inc->samplesize) {
206 /* 8-bit PCM samples are unsigned. */
207 fdata[samplenum] = (uint8_t)sample / (float)255;
210 fdata[samplenum] = RL16S(&sample) / (float)INT16_MAX;
213 fdata[samplenum] = RL32S(&sample) / (float)INT32_MAX;
218 #ifdef WORDS_BIGENDIAN
220 for (i = 0; i < inc->unitsize; i++)
221 d[i] = s[inc->unitsize - 1 - i];
223 memcpy(d, s, inc->unitsize);
229 packet.type = SR_DF_ANALOG_OLD;
230 packet.payload = &analog;
231 analog.channels = in->sdi->channels;
232 analog.num_samples = num_samples;
237 sr_session_send(in->sdi, &packet);
240 static int process_buffer(struct sr_input *in)
243 struct sr_datafeed_packet packet;
244 struct sr_datafeed_meta meta;
245 struct sr_config *src;
246 int offset, chunk_samples, total_samples, processed, max_chunk_samples;
252 for (i = 0; i < inc->num_channels; i++) {
253 snprintf(channelname, 8, "CH%d", i + 1);
254 sr_channel_new(in->sdi, i, SR_CHANNEL_ANALOG, TRUE, channelname);
257 std_session_send_df_header(in->sdi, LOG_PREFIX);
259 packet.type = SR_DF_META;
260 packet.payload = &meta;
261 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
262 meta.config = g_slist_append(NULL, src);
263 sr_session_send(in->sdi, &packet);
264 g_slist_free(meta.config);
270 if (!inc->found_data) {
271 /* Skip past size of 'fmt ' chunk. */
272 i = 20 + RL32(in->buf->str + 16);
273 offset = find_data_chunk(in->buf, i);
275 if (in->buf->len > MAX_DATA_CHUNK_OFFSET) {
276 sr_err("Couldn't find data chunk.");
280 inc->found_data = TRUE;
284 /* Round off up to the last channels * unitsize boundary. */
285 chunk_samples = (in->buf->len - offset) / inc->samplesize;
286 max_chunk_samples = CHUNK_SIZE / inc->samplesize;
288 total_samples = chunk_samples;
289 while (processed < total_samples) {
290 if (chunk_samples > max_chunk_samples)
291 num_samples = max_chunk_samples;
293 num_samples = chunk_samples;
294 send_chunk(in, offset, num_samples);
295 offset += num_samples * inc->samplesize;
296 chunk_samples -= num_samples;
297 processed += num_samples;
300 if ((unsigned int)offset < in->buf->len) {
302 * The incoming buffer wasn't processed completely. Stash
303 * the leftover data for next time.
305 g_string_erase(in->buf, 0, offset);
307 g_string_truncate(in->buf, 0);
312 static int receive(struct sr_input *in, GString *buf)
317 g_string_append_len(in->buf, buf->str, buf->len);
319 if (in->buf->len < MIN_DATA_CHUNK_OFFSET) {
321 * Don't even try until there's enough room
322 * for the data segment to start.
328 if (!in->sdi_ready) {
329 if ((ret = parse_wav_header(in->buf, inc)) == SR_ERR_NA)
330 /* Not enough data yet. */
332 else if (ret != SR_OK)
335 /* sdi is ready, notify frontend. */
336 in->sdi_ready = TRUE;
340 ret = process_buffer(in);
345 static int end(struct sr_input *in)
347 struct sr_datafeed_packet packet;
352 ret = process_buffer(in);
358 packet.type = SR_DF_END;
359 sr_session_send(in->sdi, &packet);
365 SR_PRIV struct sr_input_module input_wav = {
369 .exts = (const char*[]){"wav", NULL},
370 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
371 .format_match = format_match,