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