]> sigrok.org Git - libsigrok.git/blobdiff - src/output/wav.c
kingst-la2016: fix segfault that often occurs when a capture is aborted
[libsigrok.git] / src / output / wav.c
index f991cbfdc68526d9fea8754ad200565b712076a9..732fe976f0df420c2cae8b8e4b9a935ca0201b45 100644 (file)
@@ -46,7 +46,7 @@ static int realloc_chanbufs(const struct sr_output *o, int size)
 
        outc = o->priv;
        for (i = 0; i < outc->num_channels; i++) {
-               if (!(outc->chanbuf[i] = g_try_realloc(outc->chanbuf[i], sizeof(float) * size))) { 
+               if (!(outc->chanbuf[i] = g_try_realloc(outc->chanbuf[i], sizeof(float) * size))) {
                        sr_err("Unable to allocate enough output buffer memory.");
                        return SR_ERR;
                }
@@ -186,9 +186,9 @@ static GString *gen_header(const struct sr_output *o)
  */
 static void float_to_le(uint8_t *buf, float value)
 {
-       char *old;
+       uint8_t *old;
 
-       old = (char *)&value;
+       old = (uint8_t *)&value;
 #ifdef WORDS_BIGENDIAN
        buf[0] = old[3];
        buf[1] = old[2];
@@ -219,9 +219,10 @@ static int check_chanbuf_size(const struct sr_output *o)
                                /* Nothing in all the buffers yet. */
                                size = -1;
                                break;
-                       } else
+                       } else {
                                /* New high water mark. */
                                size = outc->chanbuf_used[i];
+                       }
                } else if (outc->chanbuf_used[i] != size) {
                        /* All channel buffers are not equally full yet. */
                        size = -1;
@@ -237,7 +238,6 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
 {
        struct out_context *outc;
        const struct sr_datafeed_meta *meta;
-       const struct sr_datafeed_analog_old *analog_old;
        const struct sr_datafeed_analog *analog;
        const struct sr_config *src;
        struct sr_channel *ch;
@@ -262,33 +262,24 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
                        outc->samplerate = g_variant_get_uint64(src->data);
                }
                break;
-       case SR_DF_ANALOG_OLD:
        case SR_DF_ANALOG:
                if (!outc->header_done) {
                        *out = gen_header(o);
                        outc->header_done = TRUE;
-               } else
+               } else {
                        *out = g_string_sized_new(512);
+               }
 
-               analog_old = packet->payload;
                analog = packet->payload;
-
-               if (packet->type == SR_DF_ANALOG_OLD) {
-                       num_samples = analog_old->num_samples;
-                       channels = analog_old->channels;
-                       num_channels = g_slist_length(analog_old->channels);
-                       data = analog_old->data;
-               } else {
-                       num_samples = analog->num_samples;
-                       channels = analog->meaning->channels;
-                       num_channels = g_slist_length(analog->meaning->channels);
-                       if (!(data = g_try_realloc(outc->fdata, sizeof(float) * num_samples * num_channels)))
-                               return SR_ERR_MALLOC;
-                       outc->fdata = data;
-                       ret = sr_analog_to_float(analog, data);
-                       if (ret != SR_OK)
-                               return ret;
-               }
+               num_samples = analog->num_samples;
+               channels = analog->meaning->channels;
+               num_channels = g_slist_length(analog->meaning->channels);
+               if (!(data = g_try_realloc(outc->fdata, sizeof(float) * num_samples * num_channels)))
+                       return SR_ERR_MALLOC;
+               outc->fdata = data;
+               ret = sr_analog_to_float(analog, data);
+               if (ret != SR_OK)
+                       return ret;
 
                if (num_samples == 0)
                        return SR_OK;
@@ -300,7 +291,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
                }
 
                if (num_samples > outc->chanbuf_size) {
-                       if (realloc_chanbufs(o, analog_old->num_samples) != SR_OK)
+                       if (realloc_chanbufs(o, analog->num_samples) != SR_OK)
                                return SR_ERR_MALLOC;
                }
 
@@ -316,7 +307,7 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
                                idx = chan_idx[j];
                                buf = outc->chanbuf[idx] + outc->chanbuf_used[idx]++ * 4;
                                f = data[i * num_channels + j];
-                               if (outc->scale != 0.0)
+                               if (outc->scale != 1.0)
                                        f /= outc->scale;
                                float_to_le(buf, f);
                        }
@@ -349,7 +340,7 @@ static struct sr_option options[] = {
 static const struct sr_option *get_options(void)
 {
        if (!options[0].def)
-               options[0].def = g_variant_ref_sink(g_variant_new_double(0.0));
+               options[0].def = g_variant_ref_sink(g_variant_new_double(1.0));
 
        return options;
 }
@@ -376,7 +367,7 @@ static int cleanup(struct sr_output *o)
 SR_PRIV struct sr_output_module output_wav = {
        .id = "wav",
        .name = "WAV",
-       .desc = "Microsoft WAV file format",
+       .desc = "Microsoft WAV file format data",
        .exts = (const char*[]){"wav", NULL},
        .flags = 0,
        .options = get_options,