]> sigrok.org Git - pulseview.git/blobdiff - pv/data/analogsegment.cpp
Segments: Fix iterator access to underlying value
[pulseview.git] / pv / data / analogsegment.cpp
index 6dc2b915b30c4683f766bf97da4f662c44a9c07d..342612aae02e15954671d2044f3bdb337682cd09 100644 (file)
@@ -48,8 +48,8 @@ const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
 const float AnalogSegment::LogEnvelopeScaleFactor = logf(EnvelopeScaleFactor);
 const uint64_t AnalogSegment::EnvelopeDataUnit = 64 * 1024;    // bytes
 
-AnalogSegment::AnalogSegment(Analog& owner, uint64_t samplerate) :
-       Segment(samplerate, sizeof(float)),
+AnalogSegment::AnalogSegment(Analog& owner, uint32_t segment_id, uint64_t samplerate) :
+       Segment(segment_id, samplerate, sizeof(float)),
        owner_(owner),
        min_value_(0),
        max_value_(0)
@@ -75,7 +75,7 @@ void AnalogSegment::append_interleaved_samples(const float *data,
        uint64_t prev_sample_count = sample_count_;
 
        // Deinterleave the samples and add them
-       unique_ptr<float> deint_data(new float[sample_count]);
+       unique_ptr<float[]> deint_data(new float[sample_count]);
        float *deint_data_ptr = deint_data.get();
        for (uint32_t i = 0; i < sample_count; i++) {
                *deint_data_ptr = (float)(*data);
@@ -116,19 +116,11 @@ const pair<float, float> AnalogSegment::get_min_max() const
        return make_pair(min_value_, max_value_);
 }
 
-SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start)
+float* AnalogSegment::get_iterator_value_ptr(SegmentDataIterator* it)
 {
-       return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
-}
-
-void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase)
-{
-       Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
-}
+       assert(it->sample_index <= (sample_count_ - 1));
 
-void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it)
-{
-       Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
+       return (float*)(it->chunk + it->chunk_offs);
 }
 
 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
@@ -171,24 +163,25 @@ void AnalogSegment::append_payload_to_envelope_levels()
        Envelope &e0 = envelope_levels_[0];
        uint64_t prev_length;
        EnvelopeSample *dest_ptr;
-       SegmentRawDataIterator* it;
+       SegmentDataIterator* it;
 
        // Expand the data buffer to fit the new samples
        prev_length = e0.length;
        e0.length = sample_count_ / EnvelopeScaleFactor;
 
        // Calculate min/max values in case we have too few samples for an envelope
+       const float old_min_value = min_value_, old_max_value = max_value_;
        if (sample_count_ < EnvelopeScaleFactor) {
-               it = begin_raw_sample_iteration(0);
+               it = begin_sample_iteration(0);
                for (uint64_t i = 0; i < sample_count_; i++) {
-                       const float sample = *((float*)it->value);
+                       const float sample = *get_iterator_value_ptr(it);
                        if (sample < min_value_)
                                min_value_ = sample;
                        if (sample > max_value_)
                                max_value_ = sample;
-                       continue_raw_sample_iteration(it, 1);
+                       continue_sample_iteration(it, 1);
                }
-               end_raw_sample_iteration(it);
+               end_sample_iteration(it);
        }
 
        // Break off if there are no new samples to compute
@@ -203,9 +196,9 @@ void AnalogSegment::append_payload_to_envelope_levels()
        uint64_t start_sample = prev_length * EnvelopeScaleFactor;
        uint64_t end_sample = e0.length * EnvelopeScaleFactor;
 
-       it = begin_raw_sample_iteration(start_sample);
+       it = begin_sample_iteration(start_sample);
        for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
-               const float* samples = (float*)it->value;
+               const float* samples = get_iterator_value_ptr(it);
 
                const EnvelopeSample sub_sample = {
                        *min_element(samples, samples + EnvelopeScaleFactor),
@@ -217,10 +210,10 @@ void AnalogSegment::append_payload_to_envelope_levels()
                if (sub_sample.max > max_value_)
                        max_value_ = sub_sample.max;
 
-               continue_raw_sample_iteration(it, EnvelopeScaleFactor);
+               continue_sample_iteration(it, EnvelopeScaleFactor);
                *dest_ptr++ = sub_sample;
        }
-       end_raw_sample_iteration(it);
+       end_sample_iteration(it);
 
        // Compute higher level mipmaps
        for (unsigned int level = 1; level < ScaleStepCount; level++) {
@@ -257,6 +250,10 @@ void AnalogSegment::append_payload_to_envelope_levels()
                        *dest_ptr = sub_sample;
                }
        }
+
+       // Notify if the min or max value changed
+       if ((old_min_value != min_value_) || (old_max_value != max_value_))
+               owner_.min_max_changed(min_value_, max_value_);
 }
 
 } // namespace data