]>
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 { | |
17bfaca6 | 46 | int fmt_code; |
1d36b4d2 BV |
47 | uint64_t samplerate; |
48 | int samplesize; | |
49 | int num_channels; | |
cb41a838 | 50 | int unitsize; |
17bfaca6 | 51 | gboolean found_data; |
1d36b4d2 BV |
52 | }; |
53 | ||
17bfaca6 | 54 | static int parse_wav_header(GString *buf, struct context *inc) |
1d36b4d2 | 55 | { |
17bfaca6 | 56 | uint64_t samplerate; |
cbd9e6e9 | 57 | unsigned int fmt_code, samplesize, num_channels, unitsize; |
1d36b4d2 | 58 | |
cbd9e6e9 | 59 | if (buf->len < MIN_DATA_CHUNK_OFFSET) |
4f979115 | 60 | return SR_OK_CONTINUE; |
1d36b4d2 | 61 | |
962d4344 BV |
62 | fmt_code = RL16(buf->str + 20); |
63 | samplerate = RL32(buf->str + 24); | |
cbd9e6e9 | 64 | |
962d4344 | 65 | samplesize = RL16(buf->str + 32); |
cbd9e6e9 BV |
66 | if (samplesize != 1 && samplesize != 2 && samplesize != 4) { |
67 | sr_err("Only 8, 16 or 32 bits per sample supported."); | |
4f979115 | 68 | return SR_ERR_DATA; |
cbd9e6e9 BV |
69 | } |
70 | ||
962d4344 | 71 | num_channels = RL16(buf->str + 22); |
cbd9e6e9 BV |
72 | if (num_channels == 0) |
73 | return SR_ERR; | |
17bfaca6 | 74 | unitsize = samplesize / num_channels; |
1d36b4d2 | 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. */ | |
85 | return SR_OK_CONTINUE; | |
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 | } |
17bfaca6 BV |
105 | } else { |
106 | sr_err("Only PCM and floating point samples are supported."); | |
4f979115 | 107 | return SR_ERR_DATA; |
17bfaca6 | 108 | } |
1d36b4d2 | 109 | |
17bfaca6 BV |
110 | if (inc) { |
111 | inc->fmt_code = fmt_code; | |
112 | inc->samplerate = samplerate; | |
113 | inc->samplesize = samplesize; | |
114 | inc->num_channels = num_channels; | |
115 | inc->unitsize = unitsize; | |
116 | inc->found_data = FALSE; | |
117 | } | |
1d36b4d2 BV |
118 | |
119 | return SR_OK; | |
120 | } | |
121 | ||
17bfaca6 | 122 | static int format_match(GHashTable *metadata) |
1d36b4d2 | 123 | { |
17bfaca6 | 124 | GString *buf; |
4f979115 | 125 | int ret; |
1d36b4d2 | 126 | |
17bfaca6 BV |
127 | buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER)); |
128 | if (strncmp(buf->str, "RIFF", 4)) | |
4f979115 | 129 | return SR_ERR; |
17bfaca6 | 130 | if (strncmp(buf->str + 8, "WAVE", 4)) |
4f979115 | 131 | return SR_ERR; |
17bfaca6 | 132 | if (strncmp(buf->str + 12, "fmt ", 4)) |
4f979115 | 133 | return SR_ERR; |
17bfaca6 BV |
134 | /* |
135 | * Only gets called when we already know this is a WAV file, so | |
136 | * this parser can log error messages. | |
137 | */ | |
4f979115 BV |
138 | if ((ret = parse_wav_header(buf, NULL)) != SR_OK) |
139 | return ret; | |
1d36b4d2 | 140 | |
4f979115 | 141 | return SR_OK; |
1d36b4d2 BV |
142 | } |
143 | ||
17bfaca6 | 144 | static int init(struct sr_input *in, GHashTable *options) |
1d36b4d2 | 145 | { |
17bfaca6 | 146 | (void)options; |
1d36b4d2 | 147 | |
1d36b4d2 | 148 | in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL); |
1d36b4d2 BV |
149 | |
150 | return SR_OK; | |
151 | } | |
152 | ||
17bfaca6 | 153 | static int find_data_chunk(GString *buf, int initial_offset) |
cb41a838 | 154 | { |
17bfaca6 | 155 | unsigned int offset, i; |
cb41a838 BV |
156 | |
157 | offset = initial_offset; | |
17bfaca6 BV |
158 | while(offset < MIN(MAX_DATA_CHUNK_OFFSET, buf->len)) { |
159 | if (!memcmp(buf->str + offset, "data", 4)) | |
cb41a838 BV |
160 | /* Skip into the samples. */ |
161 | return offset + 8; | |
162 | for (i = 0; i < 4; i++) { | |
17bfaca6 BV |
163 | if (!isalpha(buf->str[offset + i]) |
164 | && !isascii(buf->str[offset + i]) | |
165 | && !isblank(buf->str[offset + i])) | |
cb41a838 BV |
166 | /* Doesn't look like a chunk ID. */ |
167 | return -1; | |
168 | } | |
169 | /* Skip past this chunk. */ | |
962d4344 | 170 | offset += 8 + RL32(buf->str + offset + 4); |
cb41a838 BV |
171 | } |
172 | ||
173 | return offset; | |
174 | } | |
175 | ||
7db06394 | 176 | static int initial_receive(struct sr_input *in) |
1d36b4d2 BV |
177 | { |
178 | struct sr_datafeed_packet packet; | |
179 | struct sr_datafeed_meta meta; | |
17bfaca6 | 180 | struct sr_channel *ch; |
1d36b4d2 | 181 | struct sr_config *src; |
17bfaca6 | 182 | struct context *inc; |
4f979115 | 183 | int ret, i; |
17bfaca6 BV |
184 | char channelname[8]; |
185 | ||
186 | if (!in->buf) | |
187 | /* Shouldn't happen. */ | |
188 | return SR_ERR; | |
1d36b4d2 | 189 | |
7db06394 | 190 | inc = in->priv = g_malloc(sizeof(struct context)); |
4f979115 BV |
191 | if ((ret = parse_wav_header(in->buf, inc)) != SR_OK) |
192 | return ret; | |
17bfaca6 BV |
193 | |
194 | for (i = 0; i < inc->num_channels; i++) { | |
195 | snprintf(channelname, 8, "CH%d", i + 1); | |
196 | ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE, channelname); | |
197 | in->sdi->channels = g_slist_append(in->sdi->channels, ch); | |
198 | } | |
1d36b4d2 | 199 | |
29a27196 | 200 | std_session_send_df_header(in->sdi, LOG_PREFIX); |
1d36b4d2 BV |
201 | |
202 | packet.type = SR_DF_META; | |
203 | packet.payload = &meta; | |
17bfaca6 | 204 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate)); |
1d36b4d2 BV |
205 | meta.config = g_slist_append(NULL, src); |
206 | sr_session_send(in->sdi, &packet); | |
ec4063b8 | 207 | sr_config_free(src); |
1d36b4d2 | 208 | |
17bfaca6 BV |
209 | return SR_OK; |
210 | } | |
211 | ||
212 | static void send_chunk(const struct sr_input *in, int offset, int num_samples) | |
213 | { | |
214 | struct sr_datafeed_packet packet; | |
215 | struct sr_datafeed_analog analog; | |
216 | struct context *inc; | |
217 | float fdata[CHUNK_SIZE]; | |
218 | uint64_t sample; | |
219 | int total_samples, samplenum; | |
220 | char *s, *d; | |
1d36b4d2 | 221 | |
7db06394 | 222 | inc = in->priv; |
17bfaca6 BV |
223 | |
224 | s = in->buf->str + offset; | |
225 | d = (char *)fdata; | |
cb41a838 | 226 | memset(fdata, 0, CHUNK_SIZE); |
17bfaca6 BV |
227 | total_samples = num_samples * inc->num_channels; |
228 | for (samplenum = 0; samplenum < total_samples; samplenum++) { | |
229 | if (inc->fmt_code == WAVE_FORMAT_PCM) { | |
230 | sample = 0; | |
231 | memcpy(&sample, s, inc->unitsize); | |
232 | switch (inc->samplesize) { | |
233 | case 1: | |
234 | /* 8-bit PCM samples are unsigned. */ | |
962d4344 | 235 | fdata[samplenum] = (uint8_t)sample / (float)255; |
17bfaca6 BV |
236 | break; |
237 | case 2: | |
962d4344 | 238 | fdata[samplenum] = RL16S(&sample) / (float)INT16_MAX; |
17bfaca6 BV |
239 | break; |
240 | case 4: | |
962d4344 | 241 | fdata[samplenum] = RL32S(&sample) / (float)INT32_MAX; |
17bfaca6 BV |
242 | break; |
243 | } | |
244 | } else { | |
245 | /* BINARY32 float */ | |
cb41a838 | 246 | #ifdef WORDS_BIGENDIAN |
17bfaca6 BV |
247 | for (i = 0; i < inc->unitsize; i++) |
248 | d[i] = s[inc->unitsize - i]; | |
cb41a838 | 249 | #else |
17bfaca6 | 250 | memcpy(d, s, inc->unitsize); |
cb41a838 | 251 | #endif |
1d36b4d2 | 252 | } |
17bfaca6 BV |
253 | s += inc->unitsize; |
254 | d += inc->unitsize; | |
255 | } | |
256 | packet.type = SR_DF_ANALOG; | |
257 | packet.payload = &analog; | |
258 | analog.channels = in->sdi->channels; | |
259 | analog.num_samples = num_samples; | |
260 | analog.mq = 0; | |
261 | analog.mqflags = 0; | |
262 | analog.unit = 0; | |
263 | analog.data = fdata; | |
264 | sr_session_send(in->sdi, &packet); | |
265 | } | |
266 | ||
267 | static int receive(const struct sr_input *in, GString *buf) | |
268 | { | |
17bfaca6 BV |
269 | struct context *inc; |
270 | int offset, chunk_samples, total_samples, processed, max_chunk_samples, num_samples, i; | |
271 | ||
17bfaca6 BV |
272 | g_string_append_len(in->buf, buf->str, buf->len); |
273 | ||
7db06394 BV |
274 | if (!in->priv) { |
275 | if (initial_receive((struct sr_input *)in) != SR_OK) | |
17bfaca6 BV |
276 | return SR_ERR; |
277 | if (in->buf->len < MIN_DATA_CHUNK_OFFSET) { | |
278 | /* | |
279 | * Don't even get started until there's enough room | |
280 | * for the data segment to start. | |
281 | */ | |
282 | return SR_OK; | |
283 | } | |
284 | } | |
7db06394 | 285 | inc = in->priv; |
17bfaca6 BV |
286 | |
287 | if (!inc->found_data) { | |
288 | /* Skip past size of 'fmt ' chunk. */ | |
962d4344 | 289 | i = 20 + RL32(in->buf->str + 16); |
17bfaca6 BV |
290 | offset = find_data_chunk(in->buf, i); |
291 | if (offset < 0) { | |
292 | if (in->buf->len > MAX_DATA_CHUNK_OFFSET) { | |
293 | sr_err("Couldn't find data chunk."); | |
294 | return SR_ERR; | |
295 | } | |
296 | } | |
297 | inc->found_data = TRUE; | |
298 | } else | |
299 | offset = 0; | |
300 | ||
301 | /* Round off up to the last channels * unitsize boundary. */ | |
302 | chunk_samples = (in->buf->len - offset) / inc->num_channels / inc->unitsize; | |
303 | max_chunk_samples = CHUNK_SIZE / inc->num_channels / inc->unitsize; | |
304 | processed = 0; | |
305 | total_samples = chunk_samples; | |
306 | while (processed < total_samples) { | |
307 | if (chunk_samples > max_chunk_samples) | |
308 | num_samples = max_chunk_samples; | |
309 | else | |
310 | num_samples = chunk_samples; | |
311 | send_chunk(in, offset, num_samples); | |
312 | offset += num_samples * inc->unitsize; | |
313 | chunk_samples -= num_samples; | |
314 | processed += num_samples; | |
315 | } | |
316 | ||
317 | if ((unsigned int)offset < in->buf->len) { | |
318 | /* | |
319 | * The incoming buffer wasn't processed completely. Stash | |
320 | * the leftover data for next time. | |
321 | */ | |
322 | g_string_erase(in->buf, 0, offset); | |
323 | } else | |
324 | g_string_truncate(in->buf, 0); | |
1d36b4d2 BV |
325 | |
326 | return SR_OK; | |
327 | } | |
328 | ||
7db06394 BV |
329 | static int cleanup(struct sr_input *in) |
330 | { | |
0a4d68f7 BV |
331 | struct sr_datafeed_packet packet; |
332 | ||
333 | if (in->priv) { | |
334 | /* End of stream. */ | |
335 | packet.type = SR_DF_END; | |
336 | sr_session_send(in->sdi, &packet); | |
337 | ||
338 | g_free(in->priv); | |
339 | in->priv = NULL; | |
340 | } | |
7db06394 BV |
341 | |
342 | return SR_OK; | |
343 | } | |
344 | ||
d4c93774 | 345 | SR_PRIV struct sr_input_module input_wav = { |
1d36b4d2 | 346 | .id = "wav", |
17bfaca6 BV |
347 | .name = "WAV", |
348 | .desc = "WAV file", | |
349 | .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED }, | |
1d36b4d2 BV |
350 | .format_match = format_match, |
351 | .init = init, | |
17bfaca6 | 352 | .receive = receive, |
7db06394 | 353 | .cleanup = cleanup, |
1d36b4d2 BV |
354 | }; |
355 |