]> sigrok.org Git - libsigrok.git/blame - src/input/wav.c
Fix #1167 by not creating sigrok channels twice
[libsigrok.git] / src / input / wav.c
CommitLineData
1d36b4d2 1/*
50985c20 2 * This file is part of the libsigrok project.
1d36b4d2
BV
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
6ec6c43b 20#include <config.h>
1d36b4d2
BV
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <unistd.h>
24#include <fcntl.h>
cb41a838 25#include <ctype.h>
1d36b4d2 26#include <string.h>
962d4344 27#include <stdint.h>
c1aae900 28#include <libsigrok/libsigrok.h>
1d36b4d2
BV
29#include "libsigrok-internal.h"
30
3544f848 31#define LOG_PREFIX "input/wav"
1d36b4d2 32
cb41a838 33/* How many bytes at a time to process and send to the session bus. */
9a4fd01a 34#define CHUNK_SIZE (1 * 1024 * 1024 * sizeof(float))
17bfaca6
BV
35
36/* Minimum size of header + 1 8-bit mono PCM sample. */
37#define MIN_DATA_CHUNK_OFFSET 45
38
cb41a838 39/* Expect to find the "data" chunk within this offset from the start. */
b944e336 40#define MAX_DATA_CHUNK_OFFSET 1024
cb41a838 41
76598cda
UH
42#define WAVE_FORMAT_PCM_ 0x0001
43#define WAVE_FORMAT_IEEE_FLOAT_ 0x0003
44#define WAVE_FORMAT_EXTENSIBLE_ 0xfffe
1d36b4d2
BV
45
46struct context {
d0181813 47 gboolean started;
17bfaca6 48 int fmt_code;
1d36b4d2
BV
49 uint64_t samplerate;
50 int samplesize;
51 int num_channels;
cb41a838 52 int unitsize;
17bfaca6 53 gboolean found_data;
9e850040 54 gboolean create_channels;
1d36b4d2
BV
55};
56
17bfaca6 57static int parse_wav_header(GString *buf, struct context *inc)
1d36b4d2 58{
17bfaca6 59 uint64_t samplerate;
cbd9e6e9 60 unsigned int fmt_code, samplesize, num_channels, unitsize;
1d36b4d2 61
cbd9e6e9 62 if (buf->len < MIN_DATA_CHUNK_OFFSET)
d0181813 63 return SR_ERR_NA;
1d36b4d2 64
962d4344
BV
65 fmt_code = RL16(buf->str + 20);
66 samplerate = RL32(buf->str + 24);
cbd9e6e9 67
962d4344
BV
68 samplesize = RL16(buf->str + 32);
69 num_channels = RL16(buf->str + 22);
cbd9e6e9
BV
70 if (num_channels == 0)
71 return SR_ERR;
17bfaca6 72 unitsize = samplesize / num_channels;
e8779db7
BV
73 if (unitsize != 1 && unitsize != 2 && unitsize != 4) {
74 sr_err("Only 8, 16 or 32 bits per sample supported.");
75 return SR_ERR_DATA;
76 }
77
76598cda
UH
78 if (fmt_code == WAVE_FORMAT_PCM_) {
79 } else if (fmt_code == WAVE_FORMAT_IEEE_FLOAT_) {
17bfaca6
BV
80 if (unitsize != 4) {
81 sr_err("only 32-bit floats supported.");
4f979115 82 return SR_ERR_DATA;
17bfaca6 83 }
76598cda 84 } else if (fmt_code == WAVE_FORMAT_EXTENSIBLE_) {
cbd9e6e9
BV
85 if (buf->len < 70)
86 /* Not enough for extensible header and next chunk. */
d0181813 87 return SR_ERR_NA;
cbd9e6e9
BV
88
89 if (RL16(buf->str + 16) != 40) {
90 sr_err("WAV extensible format chunk must be 40 bytes.");
91 return SR_ERR;
92 }
93 if (RL16(buf->str + 36) != 22) {
94 sr_err("WAV extension must be 22 bytes.");
95 return SR_ERR;
96 }
97 if (RL16(buf->str + 34) != RL16(buf->str + 38)) {
98 sr_err("Reduced valid bits per sample not supported.");
4f979115 99 return SR_ERR_DATA;
cbd9e6e9
BV
100 }
101 /* Real format code is the first two bytes of the GUID. */
102 fmt_code = RL16(buf->str + 44);
76598cda 103 if (fmt_code != WAVE_FORMAT_PCM_ && fmt_code != WAVE_FORMAT_IEEE_FLOAT_) {
cbd9e6e9 104 sr_err("Only PCM and floating point samples are supported.");
4f979115 105 return SR_ERR_DATA;
cbd9e6e9 106 }
76598cda 107 if (fmt_code == WAVE_FORMAT_IEEE_FLOAT_ && unitsize != 4) {
28d9df72
MC
108 sr_err("only 32-bit floats supported.");
109 return SR_ERR_DATA;
110 }
17bfaca6
BV
111 } else {
112 sr_err("Only PCM and floating point samples are supported.");
4f979115 113 return SR_ERR_DATA;
17bfaca6 114 }
1d36b4d2 115
17bfaca6
BV
116 if (inc) {
117 inc->fmt_code = fmt_code;
118 inc->samplerate = samplerate;
119 inc->samplesize = samplesize;
120 inc->num_channels = num_channels;
121 inc->unitsize = unitsize;
122 inc->found_data = FALSE;
123 }
1d36b4d2
BV
124
125 return SR_OK;
126}
127
54ee427d 128static int format_match(GHashTable *metadata, unsigned int *confidence)
1d36b4d2 129{
17bfaca6 130 GString *buf;
4f979115 131 int ret;
1d36b4d2 132
17bfaca6
BV
133 buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
134 if (strncmp(buf->str, "RIFF", 4))
4f979115 135 return SR_ERR;
17bfaca6 136 if (strncmp(buf->str + 8, "WAVE", 4))
4f979115 137 return SR_ERR;
17bfaca6 138 if (strncmp(buf->str + 12, "fmt ", 4))
4f979115 139 return SR_ERR;
17bfaca6
BV
140 /*
141 * Only gets called when we already know this is a WAV file, so
142 * this parser can log error messages.
143 */
4f979115
BV
144 if ((ret = parse_wav_header(buf, NULL)) != SR_OK)
145 return ret;
1d36b4d2 146
54ee427d
GS
147 *confidence = 1;
148
4f979115 149 return SR_OK;
1d36b4d2
BV
150}
151
17bfaca6 152static int init(struct sr_input *in, GHashTable *options)
1d36b4d2 153{
9e850040
SA
154 struct context *inc;
155
17bfaca6 156 (void)options;
1d36b4d2 157
aac29cc1 158 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
d0181813 159 in->priv = g_malloc0(sizeof(struct context));
9e850040
SA
160 inc = in->priv;
161
162 inc->create_channels = TRUE;
1d36b4d2
BV
163
164 return SR_OK;
165}
166
17bfaca6 167static int find_data_chunk(GString *buf, int initial_offset)
cb41a838 168{
17bfaca6 169 unsigned int offset, i;
cb41a838
BV
170
171 offset = initial_offset;
0c5f2abc 172 while (offset < MIN(MAX_DATA_CHUNK_OFFSET, buf->len)) {
17bfaca6 173 if (!memcmp(buf->str + offset, "data", 4))
cb41a838
BV
174 /* Skip into the samples. */
175 return offset + 8;
176 for (i = 0; i < 4; i++) {
94b138a3 177 if (!isalnum(buf->str[offset + i])
17bfaca6 178 && !isblank(buf->str[offset + i]))
cb41a838
BV
179 /* Doesn't look like a chunk ID. */
180 return -1;
181 }
182 /* Skip past this chunk. */
962d4344 183 offset += 8 + RL32(buf->str + offset + 4);
cb41a838
BV
184 }
185
b944e336
SB
186 if (offset > MAX_DATA_CHUNK_OFFSET)
187 return -1;
188
cb41a838
BV
189 return offset;
190}
191
17bfaca6
BV
192static void send_chunk(const struct sr_input *in, int offset, int num_samples)
193{
194 struct sr_datafeed_packet packet;
0f33aaef
UH
195 struct sr_datafeed_analog analog;
196 struct sr_analog_encoding encoding;
197 struct sr_analog_meaning meaning;
198 struct sr_analog_spec spec;
17bfaca6 199 struct context *inc;
2cff7a2b 200 float *fdata;
17bfaca6
BV
201 int total_samples, samplenum;
202 char *s, *d;
1d36b4d2 203
7db06394 204 inc = in->priv;
17bfaca6
BV
205
206 s = in->buf->str + offset;
2cff7a2b 207 fdata = g_malloc0(CHUNK_SIZE * sizeof(float));
17bfaca6 208 d = (char *)fdata;
17bfaca6
BV
209 total_samples = num_samples * inc->num_channels;
210 for (samplenum = 0; samplenum < total_samples; samplenum++) {
76598cda 211 if (inc->fmt_code == WAVE_FORMAT_PCM_) {
8b5aefc6 212 switch (inc->unitsize) {
17bfaca6
BV
213 case 1:
214 /* 8-bit PCM samples are unsigned. */
8b5aefc6 215 fdata[samplenum] = *(uint8_t*)(s) / (float)255;
17bfaca6
BV
216 break;
217 case 2:
8b5aefc6 218 fdata[samplenum] = RL16S(s) / (float)INT16_MAX;
17bfaca6
BV
219 break;
220 case 4:
8b5aefc6 221 fdata[samplenum] = RL32S(s) / (float)INT32_MAX;
17bfaca6
BV
222 break;
223 }
224 } else {
225 /* BINARY32 float */
cb41a838 226#ifdef WORDS_BIGENDIAN
28d9df72 227 int i;
17bfaca6 228 for (i = 0; i < inc->unitsize; i++)
28d9df72 229 d[i] = s[inc->unitsize - 1 - i];
cb41a838 230#else
17bfaca6 231 memcpy(d, s, inc->unitsize);
cb41a838 232#endif
1d36b4d2 233 }
17bfaca6
BV
234 s += inc->unitsize;
235 d += inc->unitsize;
236 }
0f33aaef 237
7dcaddd3
UH
238 /* TODO: Use proper 'digits' value for this device (and its modes). */
239 sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
0f33aaef 240 packet.type = SR_DF_ANALOG;
17bfaca6 241 packet.payload = &analog;
17bfaca6 242 analog.num_samples = num_samples;
17bfaca6 243 analog.data = fdata;
0f33aaef
UH
244 analog.meaning->channels = in->sdi->channels;
245 analog.meaning->mq = 0;
246 analog.meaning->mqflags = 0;
247 analog.meaning->unit = 0;
17bfaca6 248 sr_session_send(in->sdi, &packet);
2cff7a2b 249 g_free(fdata);
17bfaca6
BV
250}
251
7066fd46 252static int process_buffer(struct sr_input *in)
17bfaca6 253{
7066fd46 254 struct context *inc;
d0181813
BV
255 struct sr_datafeed_packet packet;
256 struct sr_datafeed_meta meta;
d0181813 257 struct sr_config *src;
d0181813 258 int offset, chunk_samples, total_samples, processed, max_chunk_samples;
7066fd46 259 int num_samples, i;
17bfaca6 260
d0181813 261 inc = in->priv;
d0181813 262 if (!inc->started) {
bee2b016 263 std_session_send_df_header(in->sdi);
d0181813
BV
264
265 packet.type = SR_DF_META;
266 packet.payload = &meta;
267 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
268 meta.config = g_slist_append(NULL, src);
269 sr_session_send(in->sdi, &packet);
c01378c9 270 g_slist_free(meta.config);
d0181813
BV
271 sr_config_free(src);
272
273 inc->started = TRUE;
17bfaca6 274 }
17bfaca6
BV
275
276 if (!inc->found_data) {
277 /* Skip past size of 'fmt ' chunk. */
962d4344 278 i = 20 + RL32(in->buf->str + 16);
17bfaca6
BV
279 offset = find_data_chunk(in->buf, i);
280 if (offset < 0) {
281 if (in->buf->len > MAX_DATA_CHUNK_OFFSET) {
282 sr_err("Couldn't find data chunk.");
283 return SR_ERR;
284 }
285 }
286 inc->found_data = TRUE;
287 } else
288 offset = 0;
289
290 /* Round off up to the last channels * unitsize boundary. */
288f8ce2
SB
291 chunk_samples = (in->buf->len - offset) / inc->samplesize;
292 max_chunk_samples = CHUNK_SIZE / inc->samplesize;
17bfaca6
BV
293 processed = 0;
294 total_samples = chunk_samples;
295 while (processed < total_samples) {
296 if (chunk_samples > max_chunk_samples)
297 num_samples = max_chunk_samples;
298 else
299 num_samples = chunk_samples;
300 send_chunk(in, offset, num_samples);
288f8ce2 301 offset += num_samples * inc->samplesize;
17bfaca6
BV
302 chunk_samples -= num_samples;
303 processed += num_samples;
304 }
305
306 if ((unsigned int)offset < in->buf->len) {
307 /*
308 * The incoming buffer wasn't processed completely. Stash
309 * the leftover data for next time.
310 */
311 g_string_erase(in->buf, 0, offset);
312 } else
313 g_string_truncate(in->buf, 0);
1d36b4d2
BV
314
315 return SR_OK;
316}
317
7066fd46
BV
318static int receive(struct sr_input *in, GString *buf)
319{
320 struct context *inc;
321 int ret;
74c9a8d2 322 char channelname[8];
7066fd46
BV
323
324 g_string_append_len(in->buf, buf->str, buf->len);
325
326 if (in->buf->len < MIN_DATA_CHUNK_OFFSET) {
327 /*
328 * Don't even try until there's enough room
329 * for the data segment to start.
330 */
331 return SR_OK;
332 }
333
334 inc = in->priv;
335 if (!in->sdi_ready) {
336 if ((ret = parse_wav_header(in->buf, inc)) == SR_ERR_NA)
337 /* Not enough data yet. */
338 return SR_OK;
339 else if (ret != SR_OK)
340 return ret;
341
9e850040
SA
342 if (inc->create_channels) {
343 for (int i = 0; i < inc->num_channels; i++) {
344 snprintf(channelname, sizeof(channelname), "CH%d", i + 1);
345 sr_channel_new(in->sdi, i, SR_CHANNEL_ANALOG, TRUE, channelname);
346 }
74c9a8d2
SB
347 }
348
9e850040
SA
349 inc->create_channels = FALSE;
350
7066fd46
BV
351 /* sdi is ready, notify frontend. */
352 in->sdi_ready = TRUE;
353 return SR_OK;
354 }
355
356 ret = process_buffer(in);
357
18078d05 358 return ret;
7066fd46
BV
359}
360
361static int end(struct sr_input *in)
7db06394 362{
d0181813 363 struct context *inc;
7066fd46
BV
364 int ret;
365
366 if (in->sdi_ready)
367 ret = process_buffer(in);
368 else
369 ret = SR_OK;
0a4d68f7 370
d0181813 371 inc = in->priv;
3be42bc2 372 if (inc->started)
bee2b016 373 std_session_send_df_end(in->sdi);
7db06394 374
7066fd46 375 return ret;
7db06394
BV
376}
377
ab4c27cf
SA
378static int reset(struct sr_input *in)
379{
9e850040
SA
380 memset(in->priv, 0, sizeof(struct context));
381
382 /*
383 * We only want to create the sigrok channels once, so
384 * inc->create_channels won't be set to TRUE this time around.
385 */
ab4c27cf 386
ab4c27cf
SA
387 g_string_truncate(in->buf, 0);
388
389 return SR_OK;
390}
391
d4c93774 392SR_PRIV struct sr_input_module input_wav = {
1d36b4d2 393 .id = "wav",
17bfaca6 394 .name = "WAV",
b20eb520 395 .desc = "Microsoft WAV file format data",
c7bc82ff 396 .exts = (const char*[]){"wav", NULL},
17bfaca6 397 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
1d36b4d2
BV
398 .format_match = format_match,
399 .init = init,
17bfaca6 400 .receive = receive,
7066fd46 401 .end = end,
ab4c27cf 402 .reset = reset,
1d36b4d2 403};