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