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