2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
24 #define LOG_PREFIX "output/wav"
26 /* Minimum/maximum number of samples per channel to put in a data chunk */
27 #define MIN_DATA_CHUNK_SAMPLES 10
40 static int realloc_chanbufs(const struct sr_output *o, int size)
42 struct out_context *outc;
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.");
51 outc->chanbuf_used[i] = 0;
53 outc->chanbuf_size = size;
58 static int flush_chanbufs(const struct sr_output *o, GString *out)
60 struct out_context *outc;
61 int num_samples, i, j;
66 /* Any one of them will do. */
67 num_samples = outc->chanbuf_used[0];
68 if (!(buf = g_try_malloc(4 * num_samples * outc->num_channels))) {
69 sr_err("Unable to allocate enough interleaved output buffer memory.");
74 for (i = 0; i < num_samples; i++) {
75 for (j = 0; j < outc->num_channels; j++) {
76 memcpy(bufp, outc->chanbuf[j] + i * 4, 4);
80 g_string_append_len(out, buf, 4 * num_samples * outc->num_channels);
83 for (i = 0; i < outc->num_channels; i++)
84 outc->chanbuf_used[i] = 0;
89 static int init(struct sr_output *o, GHashTable *options)
91 struct out_context *outc;
92 struct sr_channel *ch;
97 outc = g_malloc0(sizeof(struct out_context));
102 g_hash_table_iter_init(&iter, options);
103 while (g_hash_table_iter_next(&iter, &key, &value)) {
104 if (!strcmp(key, "scale")) {
105 if (!g_variant_is_of_type(value, G_VARIANT_TYPE_DOUBLE)) {
106 sr_err("Invalid type for 'scale' option.");
109 outc->scale = g_variant_get_double(value);
111 sr_err("Unknown option '%s'.", key);
117 for (l = o->sdi->channels; l; l = l->next) {
119 if (ch->type != SR_CHANNEL_ANALOG)
123 outc->channels = g_slist_append(outc->channels, ch);
124 outc->num_channels++;
127 outc->chanbuf = g_malloc0(sizeof(float *) * outc->num_channels);
128 outc->chanbuf_used = g_malloc0(sizeof(int) * outc->num_channels);
130 /* Start off the interleaved buffer with 100 samples/channel. */
131 realloc_chanbufs(o, 100);
136 static void add_data_chunk(const struct sr_output *o, GString *gs)
138 struct out_context *outc;
142 g_string_append(gs, "fmt ");
143 /* Remaining chunk size */
145 g_string_append_len(gs, tmp, 4);
146 /* Format code 3 = IEEE float */
148 g_string_append_len(gs, tmp, 2);
149 /* Number of channels */
150 WL16(tmp, outc->num_channels);
151 g_string_append_len(gs, tmp, 2);
153 WL32(tmp, outc->samplerate);
154 g_string_append_len(gs, tmp, 4);
155 /* Byterate, using 32-bit floats. */
156 WL32(tmp, outc->samplerate * outc->num_channels * 4);
157 g_string_append_len(gs, tmp, 4);
159 WL16(tmp, outc->num_channels * 4);
160 g_string_append_len(gs, tmp, 2);
161 /* Bits per sample */
163 g_string_append_len(gs, tmp, 2);
165 g_string_append_len(gs, tmp, 2);
167 g_string_append(gs, "data");
168 /* Data chunk size, max it out. */
169 WL32(tmp, 0xffffffff);
170 g_string_append_len(gs, tmp, 4);
173 static GString *gen_header(const struct sr_output *o)
175 struct out_context *outc;
181 if (outc->samplerate == 0) {
182 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
184 outc->samplerate = g_variant_get_uint64(gvar);
185 g_variant_unref(gvar);
189 header = g_string_sized_new(512);
190 g_string_append(header, "RIFF");
191 /* Total size. Max out the field. */
192 WL32(tmp, 0xffffffff);
193 g_string_append_len(header, tmp, 4);
194 g_string_append(header, "WAVE");
195 add_data_chunk(o, header);
201 * Stores the float in little-endian BINARY32 IEEE-754 2008 format.
203 static void float_to_le(uint8_t *buf, float value)
207 old = (char *)&value;
208 #ifdef WORDS_BIGENDIAN
222 * Returns the number of samples used in the current channel buffers,
223 * or -1 if they're not all the same.
225 static int check_chanbuf_size(const struct sr_output *o)
227 struct out_context *outc;
232 for (i = 0; i < outc->num_channels; i++) {
234 if (outc->chanbuf_used[i] == 0) {
235 /* Nothing in all the buffers yet. */
239 /* New high water mark. */
240 size = outc->chanbuf_used[i];
241 } else if (outc->chanbuf_used[i] != size) {
242 /* All channel buffers are not equally full yet. */
250 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
253 struct out_context *outc;
254 const struct sr_datafeed_meta *meta;
255 const struct sr_datafeed_analog *analog;
256 const struct sr_config *src;
257 struct sr_channel *ch;
260 int num_channels, size, *chan_idx, idx, i, j;
264 if (!o || !o->sdi || !(outc = o->priv))
267 switch (packet->type) {
269 meta = packet->payload;
270 for (l = meta->config; l; l = l->next) {
272 if (src->key != SR_CONF_SAMPLERATE)
274 outc->samplerate = g_variant_get_uint64(src->data);
278 if (!outc->header_done) {
279 *out = gen_header(o);
280 outc->header_done = TRUE;
282 *out = g_string_sized_new(512);
284 analog = packet->payload;
285 if (analog->num_samples == 0)
288 num_channels = g_slist_length(analog->channels);
289 if (num_channels > outc->num_channels) {
290 sr_err("Packet has %d channels, but only %d were enabled.",
291 num_channels, outc->num_channels);
295 if (analog->num_samples > outc->chanbuf_size) {
296 if (realloc_chanbufs(o, analog->num_samples) != SR_OK)
297 return SR_ERR_MALLOC;
300 /* Index the channels in this packet, so we can interleave quicker. */
301 chan_idx = g_malloc(sizeof(int) * outc->num_channels);
302 for (i = 0; i < num_channels; i++) {
303 ch = g_slist_nth_data(analog->channels, i);
304 chan_idx[i] = g_slist_index(outc->channels, ch);
307 for (i = 0; i < analog->num_samples; i++) {
308 for (j = 0; j < num_channels; j++) {
310 buf = outc->chanbuf[idx] + outc->chanbuf_used[idx]++ * 4;
311 f = analog->data[i * num_channels + j];
312 if (outc->scale != 0.0)
319 size = check_chanbuf_size(o);
320 if (size > MIN_DATA_CHUNK_SAMPLES)
321 if (flush_chanbufs(o, *out) != SR_OK)
325 size = check_chanbuf_size(o);
327 *out = g_string_sized_new(4 * size * outc->num_channels);
328 if (flush_chanbufs(o, *out) != SR_OK)
337 static int cleanup(struct sr_output *o)
339 struct out_context *outc;
343 g_slist_free(outc->channels);
344 for (i = 0; i < outc->num_channels; i++)
345 g_free(outc->chanbuf[i]);
346 g_free(outc->chanbuf_used);
347 g_free(outc->chanbuf);
354 static struct sr_option options[] = {
355 { "scale", "Scale", "Scale values by factor", NULL, NULL },
359 static struct sr_option *get_options(gboolean cached)
364 options[0].def = g_variant_new_double(0);
365 g_variant_ref_sink(options[0].def);
370 SR_PRIV struct sr_output_module output_wav = {
373 .desc = "WAVE file format",
374 .options = get_options,