]> sigrok.org Git - libsigrok.git/blob - src/output/wav.c
output/wav: Fix channel deinterleaving.
[libsigrok.git] / src / output / wav.c
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
20 #include <string.h>
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
23
24 #define LOG_PREFIX "output/wav"
25
26 /* Minimum/maximum number of samples per channel to put in a data chunk */
27 #define MIN_DATA_CHUNK_SAMPLES 10
28
29 struct out_context {
30         gboolean header_done;
31         uint64_t samplerate;
32         int num_channels;
33         GSList *channels;
34         int chanbuf_size;
35         int *chanbuf_used;
36         uint8_t **chanbuf;
37 };
38
39 static int realloc_chanbufs(const struct sr_output *o, int size)
40 {
41         struct out_context *outc;
42         int i;
43
44         outc = o->priv;
45         for (i = 0; i < outc->num_channels; i++) {
46                 if (!(outc->chanbuf[i] = g_try_realloc(outc->chanbuf[i], sizeof(float) * size))) { 
47                         sr_err("Unable to allocate enough output buffer memory.");
48                         return SR_ERR;
49                 }
50                 outc->chanbuf_used[i] = 0;
51         }
52         outc->chanbuf_size = size;
53
54         return SR_OK;
55 }
56
57 static int flush_chanbufs(const struct sr_output *o, GString *out)
58 {
59         struct out_context *outc;
60         int num_samples, i, j;
61         char *buf, *bufp;
62
63         outc = o->priv;
64
65         /* Any one of them will do. */
66         num_samples = outc->chanbuf_used[0];
67         if (!(buf = g_try_malloc(4 * num_samples * outc->num_channels))) {
68                 sr_err("Unable to allocate enough interleaved output buffer memory.");
69                 return SR_ERR;
70         }
71
72         bufp = buf;
73         for (i = 0; i < num_samples; i++) {
74                 for (j = 0; j < outc->num_channels; j++) {
75                         memcpy(bufp, outc->chanbuf[j] + i * 4, 4);
76                         bufp += 4;
77                 }
78         }
79         g_string_append_len(out, buf, 4 * num_samples * outc->num_channels);
80         g_free(buf);
81
82         for (i = 0; i < outc->num_channels; i++)
83                 outc->chanbuf_used[i] = 0;
84
85         return SR_OK;
86 }
87
88 static int init(struct sr_output *o, GHashTable *options)
89 {
90         struct out_context *outc;
91         struct sr_channel *ch;
92         GSList *l;
93
94         (void)options;
95
96         outc = g_malloc0(sizeof(struct out_context));
97         o->priv = outc;
98
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
115         return SR_OK;
116 }
117
118 static 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
155 static 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  */
185 static 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  */
207 static 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 }
232 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
233                 GString **out)
234 {
235         struct out_context *outc;
236         const struct sr_datafeed_meta *meta;
237         const struct sr_datafeed_analog *analog;
238         const struct sr_config *src;
239         struct sr_channel *ch;
240         GSList *l;
241         int num_channels, size, *chan_idx, idx, i, j;
242         uint8_t *buf;
243
244         *out = NULL;
245         if (!o || !o->sdi || !(outc = o->priv))
246                 return SR_ERR_ARG;
247
248         switch (packet->type) {
249         case SR_DF_META:
250                 meta = packet->payload;
251                 for (l = meta->config; l; l = l->next) {
252                         src = l->data;
253                         if (src->key != SR_CONF_SAMPLERATE)
254                                 continue;
255                         outc->samplerate = g_variant_get_uint64(src->data);
256                 }
257                 break;
258         case SR_DF_ANALOG:
259                 if (!outc->header_done) {
260                         *out = gen_header(o);
261                         outc->header_done = TRUE;
262                 } else
263                         *out = g_string_sized_new(512);
264
265                 analog = packet->payload;
266                 if (analog->num_samples == 0)
267                         return SR_OK;
268
269                 num_channels = g_slist_length(analog->channels);
270                 if (num_channels > outc->num_channels) {
271                         sr_err("Packet has %d channels, but only %d were enabled.",
272                                         num_channels, outc->num_channels);
273                         return SR_ERR;
274                 }
275
276                 if (analog->num_samples > outc->chanbuf_size) {
277                         if (realloc_chanbufs(o, analog->num_samples) != SR_OK)
278                                 return SR_ERR_MALLOC;
279                 }
280
281                 /* Index the channels in this packet, so we can interleave quicker. */
282                 chan_idx = g_malloc(sizeof(int) * outc->num_channels);
283                 for (i = 0; i < num_channels; i++) {
284                         ch = g_slist_nth_data(analog->channels, i);
285                         chan_idx[i] = g_slist_index(outc->channels, ch);
286                 }
287
288                 for (i = 0; i < analog->num_samples; i++) {
289                         for (j = 0; j < num_channels; j++) {
290                                 idx = chan_idx[j];
291                                 buf = outc->chanbuf[idx] + outc->chanbuf_used[idx]++ * 4;
292                                 float_to_le(buf, analog->data[i * num_channels + j]);
293                         }
294                 }
295                 g_free(chan_idx);
296
297                 size = check_chanbuf_size(o);
298                 if (size > MIN_DATA_CHUNK_SAMPLES)
299                         if (flush_chanbufs(o, *out) != SR_OK)
300                                 return SR_ERR;
301                 break;
302         case SR_DF_END:
303                 size = check_chanbuf_size(o);
304                 if (size > 0) {
305                         *out = g_string_sized_new(4 * size * outc->num_channels);
306                         if (flush_chanbufs(o, *out) != SR_OK)
307                                 return SR_ERR;
308                 }
309                 break;
310         }
311
312         return SR_OK;
313 }
314
315 static int cleanup(struct sr_output *o)
316 {
317         struct out_context *outc;
318         int i;
319
320         outc = o->priv;
321         g_slist_free(outc->channels);
322         for (i = 0; i < outc->num_channels; i++)
323                 g_free(outc->chanbuf[i]);
324         g_free(outc->chanbuf_used);
325         g_free(outc->chanbuf);
326         g_free(outc);
327         o->priv = NULL;
328
329         return SR_OK;
330 }
331
332 SR_PRIV struct sr_output_module output_wav = {
333         .id = "wav",
334         .name = "WAV",
335         .desc = "WAVE file format",
336         .init = init,
337         .receive = receive,
338         .cleanup = cleanup,
339 };
340