]> sigrok.org Git - libsigrok.git/blame - src/output/wav.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / output / wav.c
CommitLineData
afaa75b9
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 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
0605f874 20#include <string.h>
c1aae900 21#include <libsigrok/libsigrok.h>
afaa75b9
BV
22#include "libsigrok-internal.h"
23
24#define LOG_PREFIX "output/wav"
25
0605f874
BV
26/* Minimum/maximum number of samples per channel to put in a data chunk */
27#define MIN_DATA_CHUNK_SAMPLES 10
28
29struct out_context {
7ea75009 30 double scale;
0605f874
BV
31 gboolean header_done;
32 uint64_t samplerate;
33 int num_channels;
34 GSList *channels;
35 int chanbuf_size;
36 int *chanbuf_used;
37 uint8_t **chanbuf;
38};
39
40static int realloc_chanbufs(const struct sr_output *o, int size)
41{
42 struct out_context *outc;
43 int i;
44
45 outc = o->priv;
46 for (i = 0; i < outc->num_channels; i++) {
47 if (!(outc->chanbuf[i] = g_try_realloc(outc->chanbuf[i], sizeof(float) * size))) {
48 sr_err("Unable to allocate enough output buffer memory.");
49 return SR_ERR;
50 }
51 outc->chanbuf_used[i] = 0;
52 }
53 outc->chanbuf_size = size;
54
55 return SR_OK;
56}
57
58static int flush_chanbufs(const struct sr_output *o, GString *out)
59{
60 struct out_context *outc;
364859ac
BV
61 int num_samples, i, j;
62 char *buf, *bufp;
0605f874
BV
63
64 outc = o->priv;
65
66 /* Any one of them will do. */
364859ac
BV
67 num_samples = outc->chanbuf_used[0];
68 if (!(buf = g_try_malloc(4 * num_samples * outc->num_channels))) {
0605f874
BV
69 sr_err("Unable to allocate enough interleaved output buffer memory.");
70 return SR_ERR;
71 }
0605f874 72
364859ac
BV
73 bufp = buf;
74 for (i = 0; i < num_samples; i++) {
0605f874 75 for (j = 0; j < outc->num_channels; j++) {
364859ac
BV
76 memcpy(bufp, outc->chanbuf[j] + i * 4, 4);
77 bufp += 4;
0605f874
BV
78 }
79 }
364859ac 80 g_string_append_len(out, buf, 4 * num_samples * outc->num_channels);
0605f874
BV
81 g_free(buf);
82
83 for (i = 0; i < outc->num_channels; i++)
84 outc->chanbuf_used[i] = 0;
85
86 return SR_OK;
87}
88
afaa75b9
BV
89static int init(struct sr_output *o, GHashTable *options)
90{
0605f874
BV
91 struct out_context *outc;
92 struct sr_channel *ch;
93 GSList *l;
afaa75b9 94
0605f874
BV
95 outc = g_malloc0(sizeof(struct out_context));
96 o->priv = outc;
dcc55fe9 97 outc->scale = g_variant_get_double(g_hash_table_lookup(options, "scale"));
7ea75009 98
0605f874
BV
99 for (l = o->sdi->channels; l; l = l->next) {
100 ch = l->data;
101 if (ch->type != SR_CHANNEL_ANALOG)
102 continue;
103 if (!ch->enabled)
104 continue;
105 outc->channels = g_slist_append(outc->channels, ch);
106 outc->num_channels++;
107 }
108
109 outc->chanbuf = g_malloc0(sizeof(float *) * outc->num_channels);
110 outc->chanbuf_used = g_malloc0(sizeof(int) * outc->num_channels);
111
112 /* Start off the interleaved buffer with 100 samples/channel. */
113 realloc_chanbufs(o, 100);
114
afaa75b9
BV
115 return SR_OK;
116}
117
0605f874
BV
118static void add_data_chunk(const struct sr_output *o, GString *gs)
119{
120 struct out_context *outc;
121 char tmp[4];
122
123 outc = o->priv;
124 g_string_append(gs, "fmt ");
125 /* Remaining chunk size */
126 WL32(tmp, 0x12);
127 g_string_append_len(gs, tmp, 4);
128 /* Format code 3 = IEEE float */
129 WL16(tmp, 0x0003);
130 g_string_append_len(gs, tmp, 2);
131 /* Number of channels */
132 WL16(tmp, outc->num_channels);
133 g_string_append_len(gs, tmp, 2);
134 /* Samplerate */
135 WL32(tmp, outc->samplerate);
136 g_string_append_len(gs, tmp, 4);
137 /* Byterate, using 32-bit floats. */
138 WL32(tmp, outc->samplerate * outc->num_channels * 4);
139 g_string_append_len(gs, tmp, 4);
140 /* Blockalign */
141 WL16(tmp, outc->num_channels * 4);
142 g_string_append_len(gs, tmp, 2);
143 /* Bits per sample */
144 WL16(tmp, 32);
145 g_string_append_len(gs, tmp, 2);
146 WL16(tmp, 0);
147 g_string_append_len(gs, tmp, 2);
148
149 g_string_append(gs, "data");
150 /* Data chunk size, max it out. */
151 WL32(tmp, 0xffffffff);
152 g_string_append_len(gs, tmp, 4);
153}
154
155static GString *gen_header(const struct sr_output *o)
156{
157 struct out_context *outc;
158 GVariant *gvar;
159 GString *header;
160 char tmp[4];
161
162 outc = o->priv;
163 if (outc->samplerate == 0) {
164 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
165 &gvar) == SR_OK) {
166 outc->samplerate = g_variant_get_uint64(gvar);
167 g_variant_unref(gvar);
168 }
169 }
170
171 header = g_string_sized_new(512);
172 g_string_append(header, "RIFF");
173 /* Total size. Max out the field. */
174 WL32(tmp, 0xffffffff);
175 g_string_append_len(header, tmp, 4);
176 g_string_append(header, "WAVE");
177 add_data_chunk(o, header);
178
179 return header;
180}
181
182/*
183 * Stores the float in little-endian BINARY32 IEEE-754 2008 format.
184 */
185static void float_to_le(uint8_t *buf, float value)
186{
187 char *old;
188
189 old = (char *)&value;
190#ifdef WORDS_BIGENDIAN
191 buf[0] = old[3];
192 buf[1] = old[2];
193 buf[2] = old[1];
194 buf[3] = old[0];
195#else
196 buf[0] = old[0];
197 buf[1] = old[1];
198 buf[2] = old[2];
199 buf[3] = old[3];
200#endif
201}
202
203/*
204 * Returns the number of samples used in the current channel buffers,
205 * or -1 if they're not all the same.
206 */
207static int check_chanbuf_size(const struct sr_output *o)
208{
209 struct out_context *outc;
210 int size, i;
211
212 outc = o->priv;
213 size = 0;
214 for (i = 0; i < outc->num_channels; i++) {
215 if (size == 0) {
216 if (outc->chanbuf_used[i] == 0) {
217 /* Nothing in all the buffers yet. */
218 size = -1;
219 break;
220 } else
221 /* New high water mark. */
222 size = outc->chanbuf_used[i];
223 } else if (outc->chanbuf_used[i] != size) {
224 /* All channel buffers are not equally full yet. */
225 size = -1;
226 break;
227 }
228 }
229
230 return size;
231}
441e9eae 232
afaa75b9
BV
233static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
234 GString **out)
235{
0605f874
BV
236 struct out_context *outc;
237 const struct sr_datafeed_meta *meta;
238 const struct sr_datafeed_analog *analog;
239 const struct sr_config *src;
240 struct sr_channel *ch;
241 GSList *l;
7ea75009 242 float f;
0605f874
BV
243 int num_channels, size, *chan_idx, idx, i, j;
244 uint8_t *buf;
245
246 *out = NULL;
247 if (!o || !o->sdi || !(outc = o->priv))
248 return SR_ERR_ARG;
249
250 switch (packet->type) {
251 case SR_DF_META:
252 meta = packet->payload;
253 for (l = meta->config; l; l = l->next) {
254 src = l->data;
255 if (src->key != SR_CONF_SAMPLERATE)
256 continue;
257 outc->samplerate = g_variant_get_uint64(src->data);
258 }
259 break;
260 case SR_DF_ANALOG:
261 if (!outc->header_done) {
262 *out = gen_header(o);
263 outc->header_done = TRUE;
264 } else
265 *out = g_string_sized_new(512);
266
267 analog = packet->payload;
268 if (analog->num_samples == 0)
269 return SR_OK;
270
271 num_channels = g_slist_length(analog->channels);
272 if (num_channels > outc->num_channels) {
273 sr_err("Packet has %d channels, but only %d were enabled.",
274 num_channels, outc->num_channels);
275 return SR_ERR;
276 }
277
278 if (analog->num_samples > outc->chanbuf_size) {
279 if (realloc_chanbufs(o, analog->num_samples) != SR_OK)
280 return SR_ERR_MALLOC;
281 }
282
283 /* Index the channels in this packet, so we can interleave quicker. */
284 chan_idx = g_malloc(sizeof(int) * outc->num_channels);
285 for (i = 0; i < num_channels; i++) {
286 ch = g_slist_nth_data(analog->channels, i);
287 chan_idx[i] = g_slist_index(outc->channels, ch);
288 }
289
290 for (i = 0; i < analog->num_samples; i++) {
291 for (j = 0; j < num_channels; j++) {
292 idx = chan_idx[j];
293 buf = outc->chanbuf[idx] + outc->chanbuf_used[idx]++ * 4;
7ea75009
BV
294 f = analog->data[i * num_channels + j];
295 if (outc->scale != 0.0)
296 f /= outc->scale;
297 float_to_le(buf, f);
0605f874
BV
298 }
299 }
300 g_free(chan_idx);
301
302 size = check_chanbuf_size(o);
303 if (size > MIN_DATA_CHUNK_SAMPLES)
304 if (flush_chanbufs(o, *out) != SR_OK)
305 return SR_ERR;
306 break;
307 case SR_DF_END:
308 size = check_chanbuf_size(o);
309 if (size > 0) {
310 *out = g_string_sized_new(4 * size * outc->num_channels);
311 if (flush_chanbufs(o, *out) != SR_OK)
312 return SR_ERR;
313 }
314 break;
315 }
afaa75b9
BV
316
317 return SR_OK;
318}
319
320static int cleanup(struct sr_output *o)
321{
0605f874
BV
322 struct out_context *outc;
323 int i;
324
325 outc = o->priv;
326 g_slist_free(outc->channels);
327 for (i = 0; i < outc->num_channels; i++)
328 g_free(outc->chanbuf[i]);
329 g_free(outc->chanbuf_used);
330 g_free(outc->chanbuf);
331 g_free(outc);
332 o->priv = NULL;
afaa75b9
BV
333
334 return SR_OK;
335}
336
7ea75009
BV
337static struct sr_option options[] = {
338 { "scale", "Scale", "Scale values by factor", NULL, NULL },
1685c276 339 ALL_ZERO
7ea75009
BV
340};
341
af7d656d 342static const struct sr_option *get_options(void)
7ea75009 343{
441e9eae
BV
344 if (!options[0].def)
345 options[0].def = g_variant_ref_sink(g_variant_new_double(0.0));
7ea75009
BV
346
347 return options;
348}
349
afaa75b9
BV
350SR_PRIV struct sr_output_module output_wav = {
351 .id = "wav",
352 .name = "WAV",
a82c83c6 353 .desc = "Microsoft WAV file format",
8a174d23 354 .exts = (const char*[]){"wav", NULL},
3cd4b381 355 .flags = 0,
7ea75009 356 .options = get_options,
afaa75b9
BV
357 .init = init,
358 .receive = receive,
359 .cleanup = cleanup,
360};