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/>.
20 #include <sys/types.h>
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
30 #define LOG_PREFIX "input/wav"
32 /* How many bytes at a time to process and send to the session bus. */
33 #define CHUNK_SIZE 4096
35 /* Minimum size of header + 1 8-bit mono PCM sample. */
36 #define MIN_DATA_CHUNK_OFFSET 45
38 /* Expect to find the "data" chunk within this offset from the start. */
39 #define MAX_DATA_CHUNK_OFFSET 256
41 #define WAVE_FORMAT_PCM 0x0001
42 #define WAVE_FORMAT_IEEE_FLOAT 0x0003
43 #define WAVE_FORMAT_EXTENSIBLE 0xfffe
54 static int parse_wav_header(GString *buf, struct context *inc)
57 unsigned int fmt_code, samplesize, num_channels, unitsize;
59 if (buf->len < MIN_DATA_CHUNK_OFFSET)
60 return SR_OK_CONTINUE;
62 fmt_code = RL16(buf->str + 20);
63 samplerate = RL32(buf->str + 24);
65 samplesize = RL16(buf->str + 32);
66 num_channels = RL16(buf->str + 22);
67 if (num_channels == 0)
69 unitsize = samplesize / num_channels;
70 if (unitsize != 1 && unitsize != 2 && unitsize != 4) {
71 sr_err("Only 8, 16 or 32 bits per sample supported.");
76 if (fmt_code == WAVE_FORMAT_PCM) {
77 } else if (fmt_code == WAVE_FORMAT_IEEE_FLOAT) {
79 sr_err("only 32-bit floats supported.");
82 } else if (fmt_code == WAVE_FORMAT_EXTENSIBLE) {
84 /* Not enough for extensible header and next chunk. */
85 return SR_OK_CONTINUE;
87 if (RL16(buf->str + 16) != 40) {
88 sr_err("WAV extensible format chunk must be 40 bytes.");
91 if (RL16(buf->str + 36) != 22) {
92 sr_err("WAV extension must be 22 bytes.");
95 if (RL16(buf->str + 34) != RL16(buf->str + 38)) {
96 sr_err("Reduced valid bits per sample not supported.");
99 /* Real format code is the first two bytes of the GUID. */
100 fmt_code = RL16(buf->str + 44);
101 if (fmt_code != WAVE_FORMAT_PCM && fmt_code != WAVE_FORMAT_IEEE_FLOAT) {
102 sr_err("Only PCM and floating point samples are supported.");
105 if (fmt_code == WAVE_FORMAT_IEEE_FLOAT && unitsize != 4) {
106 sr_err("only 32-bit floats supported.");
110 sr_err("Only PCM and floating point samples are supported.");
115 inc->fmt_code = fmt_code;
116 inc->samplerate = samplerate;
117 inc->samplesize = samplesize;
118 inc->num_channels = num_channels;
119 inc->unitsize = unitsize;
120 inc->found_data = FALSE;
126 static int format_match(GHashTable *metadata)
131 buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
132 if (strncmp(buf->str, "RIFF", 4))
134 if (strncmp(buf->str + 8, "WAVE", 4))
136 if (strncmp(buf->str + 12, "fmt ", 4))
139 * Only gets called when we already know this is a WAV file, so
140 * this parser can log error messages.
142 if ((ret = parse_wav_header(buf, NULL)) != SR_OK)
148 static int init(struct sr_input *in, GHashTable *options)
152 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
157 static int find_data_chunk(GString *buf, int initial_offset)
159 unsigned int offset, i;
161 offset = initial_offset;
162 while(offset < MIN(MAX_DATA_CHUNK_OFFSET, buf->len)) {
163 if (!memcmp(buf->str + offset, "data", 4))
164 /* Skip into the samples. */
166 for (i = 0; i < 4; i++) {
167 if (!isalpha(buf->str[offset + i])
168 && !isascii(buf->str[offset + i])
169 && !isblank(buf->str[offset + i]))
170 /* Doesn't look like a chunk ID. */
173 /* Skip past this chunk. */
174 offset += 8 + RL32(buf->str + offset + 4);
180 static int initial_receive(struct sr_input *in)
182 struct sr_datafeed_packet packet;
183 struct sr_datafeed_meta meta;
184 struct sr_channel *ch;
185 struct sr_config *src;
191 /* Shouldn't happen. */
194 inc = in->priv = g_malloc(sizeof(struct context));
195 if ((ret = parse_wav_header(in->buf, inc)) != SR_OK)
198 for (i = 0; i < inc->num_channels; i++) {
199 snprintf(channelname, 8, "CH%d", i + 1);
200 ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE, channelname);
201 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
204 std_session_send_df_header(in->sdi, LOG_PREFIX);
206 packet.type = SR_DF_META;
207 packet.payload = &meta;
208 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
209 meta.config = g_slist_append(NULL, src);
210 sr_session_send(in->sdi, &packet);
216 static void send_chunk(const struct sr_input *in, int offset, int num_samples)
218 struct sr_datafeed_packet packet;
219 struct sr_datafeed_analog analog;
221 float fdata[CHUNK_SIZE];
223 int total_samples, samplenum;
228 s = in->buf->str + offset;
230 memset(fdata, 0, CHUNK_SIZE);
231 total_samples = num_samples * inc->num_channels;
232 for (samplenum = 0; samplenum < total_samples; samplenum++) {
233 if (inc->fmt_code == WAVE_FORMAT_PCM) {
235 memcpy(&sample, s, inc->unitsize);
236 switch (inc->samplesize) {
238 /* 8-bit PCM samples are unsigned. */
239 fdata[samplenum] = (uint8_t)sample / (float)255;
242 fdata[samplenum] = RL16S(&sample) / (float)INT16_MAX;
245 fdata[samplenum] = RL32S(&sample) / (float)INT32_MAX;
250 #ifdef WORDS_BIGENDIAN
252 for (i = 0; i < inc->unitsize; i++)
253 d[i] = s[inc->unitsize - 1 - i];
255 memcpy(d, s, inc->unitsize);
261 packet.type = SR_DF_ANALOG;
262 packet.payload = &analog;
263 analog.channels = in->sdi->channels;
264 analog.num_samples = num_samples;
269 sr_session_send(in->sdi, &packet);
272 static int receive(const struct sr_input *in, GString *buf)
275 int offset, chunk_samples, total_samples, processed, max_chunk_samples, num_samples, i;
277 g_string_append_len(in->buf, buf->str, buf->len);
280 if (initial_receive((struct sr_input *)in) != SR_OK)
282 if (in->buf->len < MIN_DATA_CHUNK_OFFSET) {
284 * Don't even get started until there's enough room
285 * for the data segment to start.
292 if (!inc->found_data) {
293 /* Skip past size of 'fmt ' chunk. */
294 i = 20 + RL32(in->buf->str + 16);
295 offset = find_data_chunk(in->buf, i);
297 if (in->buf->len > MAX_DATA_CHUNK_OFFSET) {
298 sr_err("Couldn't find data chunk.");
302 inc->found_data = TRUE;
306 /* Round off up to the last channels * unitsize boundary. */
307 chunk_samples = (in->buf->len - offset) / inc->num_channels / inc->unitsize;
308 max_chunk_samples = CHUNK_SIZE / inc->num_channels / inc->unitsize;
310 total_samples = chunk_samples;
311 while (processed < total_samples) {
312 if (chunk_samples > max_chunk_samples)
313 num_samples = max_chunk_samples;
315 num_samples = chunk_samples;
316 send_chunk(in, offset, num_samples);
317 offset += num_samples * inc->unitsize;
318 chunk_samples -= num_samples;
319 processed += num_samples;
322 if ((unsigned int)offset < in->buf->len) {
324 * The incoming buffer wasn't processed completely. Stash
325 * the leftover data for next time.
327 g_string_erase(in->buf, 0, offset);
329 g_string_truncate(in->buf, 0);
334 static int cleanup(struct sr_input *in)
336 struct sr_datafeed_packet packet;
340 packet.type = SR_DF_END;
341 sr_session_send(in->sdi, &packet);
350 SR_PRIV struct sr_input_module input_wav = {
354 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
355 .format_match = format_match,