]> sigrok.org Git - pulseview.git/blobdiff - pv/data/analogsegment.cpp
Segment: Do not alter chunks when there are active iterators
[pulseview.git] / pv / data / analogsegment.cpp
index cfc0f2bb60a9498d9561f10c27b5e22ae84ee896..b3f310a144c5cd7ce2e18fb83ed242bd10526b6c 100644 (file)
@@ -106,17 +106,17 @@ const std::pair<float, float> AnalogSegment::get_min_max() const
        return std::make_pair(min_value_, max_value_);
 }
 
-SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const
+SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start)
 {
        return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
 }
 
-void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const
+void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase)
 {
        Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
 }
 
-void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const
+void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it)
 {
        Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
 }
@@ -167,6 +167,18 @@ void AnalogSegment::append_payload_to_envelope_levels()
        prev_length = e0.length;
        e0.length = sample_count_ / EnvelopeScaleFactor;
 
+       // Calculate min/max values in case we have too few samples for an envelope
+       if (sample_count_ < EnvelopeScaleFactor) {
+               it = begin_raw_sample_iteration(0);
+               for (uint64_t i = 0; i < sample_count_; i++) {
+                       const float sample = *((float*)it->value);
+                       if (sample < min_value_) min_value_ = sample;
+                       if (sample > max_value_) max_value_ = sample;
+                       continue_raw_sample_iteration(it, 1);
+               }
+               end_raw_sample_iteration(it);
+       }
+
        // Break off if there are no new samples to compute
        if (e0.length == prev_length)
                return;