]>
Commit | Line | Data |
---|---|---|
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 | |
46 | struct 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; |
1d36b4d2 BV |
54 | }; |
55 | ||
17bfaca6 | 56 | static int parse_wav_header(GString *buf, struct context *inc) |
1d36b4d2 | 57 | { |
17bfaca6 | 58 | uint64_t samplerate; |
cbd9e6e9 | 59 | unsigned int fmt_code, samplesize, num_channels, unitsize; |
1d36b4d2 | 60 | |
cbd9e6e9 | 61 | if (buf->len < MIN_DATA_CHUNK_OFFSET) |
d0181813 | 62 | return SR_ERR_NA; |
1d36b4d2 | 63 | |
962d4344 BV |
64 | fmt_code = RL16(buf->str + 20); |
65 | samplerate = RL32(buf->str + 24); | |
cbd9e6e9 | 66 | |
962d4344 BV |
67 | samplesize = RL16(buf->str + 32); |
68 | num_channels = RL16(buf->str + 22); | |
cbd9e6e9 BV |
69 | if (num_channels == 0) |
70 | return SR_ERR; | |
17bfaca6 | 71 | unitsize = samplesize / num_channels; |
e8779db7 BV |
72 | if (unitsize != 1 && unitsize != 2 && unitsize != 4) { |
73 | sr_err("Only 8, 16 or 32 bits per sample supported."); | |
74 | return SR_ERR_DATA; | |
75 | } | |
76 | ||
76598cda UH |
77 | if (fmt_code == WAVE_FORMAT_PCM_) { |
78 | } else if (fmt_code == WAVE_FORMAT_IEEE_FLOAT_) { | |
17bfaca6 BV |
79 | if (unitsize != 4) { |
80 | sr_err("only 32-bit floats supported."); | |
4f979115 | 81 | return SR_ERR_DATA; |
17bfaca6 | 82 | } |
76598cda | 83 | } else if (fmt_code == WAVE_FORMAT_EXTENSIBLE_) { |
cbd9e6e9 BV |
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); | |
76598cda | 102 | if (fmt_code != WAVE_FORMAT_PCM_ && fmt_code != WAVE_FORMAT_IEEE_FLOAT_) { |
cbd9e6e9 | 103 | sr_err("Only PCM and floating point samples are supported."); |
4f979115 | 104 | return SR_ERR_DATA; |
cbd9e6e9 | 105 | } |
76598cda | 106 | if (fmt_code == WAVE_FORMAT_IEEE_FLOAT_ && unitsize != 4) { |
28d9df72 MC |
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 | ||
54ee427d | 127 | static int format_match(GHashTable *metadata, unsigned int *confidence) |
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 | |
54ee427d GS |
146 | *confidence = 1; |
147 | ||
4f979115 | 148 | return SR_OK; |
1d36b4d2 BV |
149 | } |
150 | ||
17bfaca6 | 151 | static int init(struct sr_input *in, GHashTable *options) |
1d36b4d2 | 152 | { |
17bfaca6 | 153 | (void)options; |
1d36b4d2 | 154 | |
aac29cc1 | 155 | in->sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
d0181813 | 156 | in->priv = g_malloc0(sizeof(struct context)); |
1d36b4d2 BV |
157 | |
158 | return SR_OK; | |
159 | } | |
160 | ||
17bfaca6 | 161 | static int find_data_chunk(GString *buf, int initial_offset) |
cb41a838 | 162 | { |
17bfaca6 | 163 | unsigned int offset, i; |
cb41a838 BV |
164 | |
165 | offset = initial_offset; | |
0c5f2abc | 166 | while (offset < MIN(MAX_DATA_CHUNK_OFFSET, buf->len)) { |
17bfaca6 | 167 | if (!memcmp(buf->str + offset, "data", 4)) |
cb41a838 BV |
168 | /* Skip into the samples. */ |
169 | return offset + 8; | |
170 | for (i = 0; i < 4; i++) { | |
94b138a3 | 171 | if (!isalnum(buf->str[offset + i]) |
17bfaca6 | 172 | && !isblank(buf->str[offset + i])) |
cb41a838 BV |
173 | /* Doesn't look like a chunk ID. */ |
174 | return -1; | |
175 | } | |
176 | /* Skip past this chunk. */ | |
962d4344 | 177 | offset += 8 + RL32(buf->str + offset + 4); |
cb41a838 BV |
178 | } |
179 | ||
b944e336 SB |
180 | if (offset > MAX_DATA_CHUNK_OFFSET) |
181 | return -1; | |
182 | ||
cb41a838 BV |
183 | return offset; |
184 | } | |
185 | ||
17bfaca6 BV |
186 | static void send_chunk(const struct sr_input *in, int offset, int num_samples) |
187 | { | |
188 | struct sr_datafeed_packet packet; | |
0f33aaef UH |
189 | struct sr_datafeed_analog analog; |
190 | struct sr_analog_encoding encoding; | |
191 | struct sr_analog_meaning meaning; | |
192 | struct sr_analog_spec spec; | |
17bfaca6 | 193 | struct context *inc; |
2cff7a2b | 194 | float *fdata; |
17bfaca6 BV |
195 | int total_samples, samplenum; |
196 | char *s, *d; | |
1d36b4d2 | 197 | |
7db06394 | 198 | inc = in->priv; |
17bfaca6 BV |
199 | |
200 | s = in->buf->str + offset; | |
2cff7a2b | 201 | fdata = g_malloc0(CHUNK_SIZE * sizeof(float)); |
17bfaca6 | 202 | d = (char *)fdata; |
17bfaca6 BV |
203 | total_samples = num_samples * inc->num_channels; |
204 | for (samplenum = 0; samplenum < total_samples; samplenum++) { | |
76598cda | 205 | if (inc->fmt_code == WAVE_FORMAT_PCM_) { |
8b5aefc6 | 206 | switch (inc->unitsize) { |
17bfaca6 BV |
207 | case 1: |
208 | /* 8-bit PCM samples are unsigned. */ | |
8b5aefc6 | 209 | fdata[samplenum] = *(uint8_t*)(s) / (float)255; |
17bfaca6 BV |
210 | break; |
211 | case 2: | |
8b5aefc6 | 212 | fdata[samplenum] = RL16S(s) / (float)INT16_MAX; |
17bfaca6 BV |
213 | break; |
214 | case 4: | |
8b5aefc6 | 215 | fdata[samplenum] = RL32S(s) / (float)INT32_MAX; |
17bfaca6 BV |
216 | break; |
217 | } | |
218 | } else { | |
219 | /* BINARY32 float */ | |
cb41a838 | 220 | #ifdef WORDS_BIGENDIAN |
28d9df72 | 221 | int i; |
17bfaca6 | 222 | for (i = 0; i < inc->unitsize; i++) |
28d9df72 | 223 | d[i] = s[inc->unitsize - 1 - i]; |
cb41a838 | 224 | #else |
17bfaca6 | 225 | memcpy(d, s, inc->unitsize); |
cb41a838 | 226 | #endif |
1d36b4d2 | 227 | } |
17bfaca6 BV |
228 | s += inc->unitsize; |
229 | d += inc->unitsize; | |
230 | } | |
0f33aaef | 231 | |
7dcaddd3 UH |
232 | /* TODO: Use proper 'digits' value for this device (and its modes). */ |
233 | sr_analog_init(&analog, &encoding, &meaning, &spec, 2); | |
0f33aaef | 234 | packet.type = SR_DF_ANALOG; |
17bfaca6 | 235 | packet.payload = &analog; |
17bfaca6 | 236 | analog.num_samples = num_samples; |
17bfaca6 | 237 | analog.data = fdata; |
0f33aaef UH |
238 | analog.meaning->channels = in->sdi->channels; |
239 | analog.meaning->mq = 0; | |
240 | analog.meaning->mqflags = 0; | |
241 | analog.meaning->unit = 0; | |
17bfaca6 | 242 | sr_session_send(in->sdi, &packet); |
2cff7a2b | 243 | g_free(fdata); |
17bfaca6 BV |
244 | } |
245 | ||
7066fd46 | 246 | static int process_buffer(struct sr_input *in) |
17bfaca6 | 247 | { |
7066fd46 | 248 | struct context *inc; |
d0181813 BV |
249 | struct sr_datafeed_packet packet; |
250 | struct sr_datafeed_meta meta; | |
d0181813 | 251 | struct sr_config *src; |
d0181813 | 252 | int offset, chunk_samples, total_samples, processed, max_chunk_samples; |
7066fd46 | 253 | int num_samples, i; |
17bfaca6 | 254 | |
d0181813 | 255 | inc = in->priv; |
d0181813 | 256 | if (!inc->started) { |
bee2b016 | 257 | std_session_send_df_header(in->sdi); |
d0181813 BV |
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); | |
c01378c9 | 264 | g_slist_free(meta.config); |
d0181813 BV |
265 | sr_config_free(src); |
266 | ||
267 | inc->started = TRUE; | |
17bfaca6 | 268 | } |
17bfaca6 BV |
269 | |
270 | if (!inc->found_data) { | |
271 | /* Skip past size of 'fmt ' chunk. */ | |
962d4344 | 272 | i = 20 + RL32(in->buf->str + 16); |
17bfaca6 BV |
273 | offset = find_data_chunk(in->buf, i); |
274 | if (offset < 0) { | |
275 | if (in->buf->len > MAX_DATA_CHUNK_OFFSET) { | |
276 | sr_err("Couldn't find data chunk."); | |
277 | return SR_ERR; | |
278 | } | |
279 | } | |
280 | inc->found_data = TRUE; | |
281 | } else | |
282 | offset = 0; | |
283 | ||
284 | /* Round off up to the last channels * unitsize boundary. */ | |
288f8ce2 SB |
285 | chunk_samples = (in->buf->len - offset) / inc->samplesize; |
286 | max_chunk_samples = CHUNK_SIZE / inc->samplesize; | |
17bfaca6 BV |
287 | processed = 0; |
288 | total_samples = chunk_samples; | |
289 | while (processed < total_samples) { | |
290 | if (chunk_samples > max_chunk_samples) | |
291 | num_samples = max_chunk_samples; | |
292 | else | |
293 | num_samples = chunk_samples; | |
294 | send_chunk(in, offset, num_samples); | |
288f8ce2 | 295 | offset += num_samples * inc->samplesize; |
17bfaca6 BV |
296 | chunk_samples -= num_samples; |
297 | processed += num_samples; | |
298 | } | |
299 | ||
300 | if ((unsigned int)offset < in->buf->len) { | |
301 | /* | |
302 | * The incoming buffer wasn't processed completely. Stash | |
303 | * the leftover data for next time. | |
304 | */ | |
305 | g_string_erase(in->buf, 0, offset); | |
306 | } else | |
307 | g_string_truncate(in->buf, 0); | |
1d36b4d2 BV |
308 | |
309 | return SR_OK; | |
310 | } | |
311 | ||
7066fd46 BV |
312 | static int receive(struct sr_input *in, GString *buf) |
313 | { | |
314 | struct context *inc; | |
315 | int ret; | |
74c9a8d2 | 316 | char channelname[8]; |
7066fd46 BV |
317 | |
318 | g_string_append_len(in->buf, buf->str, buf->len); | |
319 | ||
320 | if (in->buf->len < MIN_DATA_CHUNK_OFFSET) { | |
321 | /* | |
322 | * Don't even try until there's enough room | |
323 | * for the data segment to start. | |
324 | */ | |
325 | return SR_OK; | |
326 | } | |
327 | ||
328 | inc = in->priv; | |
329 | if (!in->sdi_ready) { | |
330 | if ((ret = parse_wav_header(in->buf, inc)) == SR_ERR_NA) | |
331 | /* Not enough data yet. */ | |
332 | return SR_OK; | |
333 | else if (ret != SR_OK) | |
334 | return ret; | |
335 | ||
74c9a8d2 | 336 | for (int i = 0; i < inc->num_channels; i++) { |
00ed77f2 | 337 | snprintf(channelname, sizeof(channelname), "CH%d", i + 1); |
74c9a8d2 SB |
338 | sr_channel_new(in->sdi, i, SR_CHANNEL_ANALOG, TRUE, channelname); |
339 | } | |
340 | ||
7066fd46 BV |
341 | /* sdi is ready, notify frontend. */ |
342 | in->sdi_ready = TRUE; | |
343 | return SR_OK; | |
344 | } | |
345 | ||
346 | ret = process_buffer(in); | |
347 | ||
18078d05 | 348 | return ret; |
7066fd46 BV |
349 | } |
350 | ||
351 | static int end(struct sr_input *in) | |
7db06394 | 352 | { |
d0181813 | 353 | struct context *inc; |
7066fd46 BV |
354 | int ret; |
355 | ||
356 | if (in->sdi_ready) | |
357 | ret = process_buffer(in); | |
358 | else | |
359 | ret = SR_OK; | |
0a4d68f7 | 360 | |
d0181813 | 361 | inc = in->priv; |
3be42bc2 | 362 | if (inc->started) |
bee2b016 | 363 | std_session_send_df_end(in->sdi); |
7db06394 | 364 | |
7066fd46 | 365 | return ret; |
7db06394 BV |
366 | } |
367 | ||
ab4c27cf SA |
368 | static int reset(struct sr_input *in) |
369 | { | |
370 | struct context *inc = in->priv; | |
371 | ||
372 | inc->started = FALSE; | |
373 | g_string_truncate(in->buf, 0); | |
374 | ||
375 | return SR_OK; | |
376 | } | |
377 | ||
d4c93774 | 378 | SR_PRIV struct sr_input_module input_wav = { |
1d36b4d2 | 379 | .id = "wav", |
17bfaca6 | 380 | .name = "WAV", |
b20eb520 | 381 | .desc = "Microsoft WAV file format data", |
c7bc82ff | 382 | .exts = (const char*[]){"wav", NULL}, |
17bfaca6 | 383 | .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED }, |
1d36b4d2 BV |
384 | .format_match = format_match, |
385 | .init = init, | |
17bfaca6 | 386 | .receive = receive, |
7066fd46 | 387 | .end = end, |
ab4c27cf | 388 | .reset = reset, |
1d36b4d2 | 389 | }; |