2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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 2 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/>.
30 #include "analogsegment.hpp"
32 using std::lock_guard;
33 using std::recursive_mutex;
36 using std::max_element;
38 using std::min_element;
44 const int AnalogSegment::EnvelopeScalePower = 4;
45 const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
46 const float AnalogSegment::LogEnvelopeScaleFactor = logf(EnvelopeScaleFactor);
47 const uint64_t AnalogSegment::EnvelopeDataUnit = 64 * 1024; // bytes
49 AnalogSegment::AnalogSegment(Analog& owner, uint64_t samplerate) :
50 Segment(samplerate, sizeof(float)),
55 lock_guard<recursive_mutex> lock(mutex_);
56 memset(envelope_levels_, 0, sizeof(envelope_levels_));
59 AnalogSegment::~AnalogSegment()
61 lock_guard<recursive_mutex> lock(mutex_);
62 for (Envelope &e : envelope_levels_)
66 void AnalogSegment::append_interleaved_samples(const float *data,
67 size_t sample_count, size_t stride)
69 assert(unit_size_ == sizeof(float));
71 lock_guard<recursive_mutex> lock(mutex_);
73 uint64_t prev_sample_count = sample_count_;
75 for (uint32_t i = 0; i < sample_count; i++) {
76 append_single_sample((void*)data);
80 // Generate the first mip-map from the data
81 append_payload_to_envelope_levels();
84 owner_.notify_samples_added(this, prev_sample_count + 1,
85 prev_sample_count + 1 + sample_count);
87 owner_.notify_samples_added(this, prev_sample_count + 1,
88 prev_sample_count + 1);
91 const float* AnalogSegment::get_samples(
92 int64_t start_sample, int64_t end_sample) const
94 assert(start_sample >= 0);
95 assert(start_sample < (int64_t)sample_count_);
96 assert(end_sample >= 0);
97 assert(end_sample < (int64_t)sample_count_);
98 assert(start_sample <= end_sample);
100 lock_guard<recursive_mutex> lock(mutex_);
102 return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
105 const pair<float, float> AnalogSegment::get_min_max() const
107 return make_pair(min_value_, max_value_);
110 SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start)
112 return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
115 void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase)
117 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
120 void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it)
122 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
125 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
126 uint64_t start, uint64_t end, float min_length) const
128 assert(end <= get_sample_count());
129 assert(start <= end);
130 assert(min_length > 0);
132 lock_guard<recursive_mutex> lock(mutex_);
134 const unsigned int min_level = max((int)floorf(logf(min_length) /
135 LogEnvelopeScaleFactor) - 1, 0);
136 const unsigned int scale_power = (min_level + 1) *
138 start >>= scale_power;
141 s.start = start << scale_power;
142 s.scale = 1 << scale_power;
143 s.length = end - start;
144 s.samples = new EnvelopeSample[s.length];
145 memcpy(s.samples, envelope_levels_[min_level].samples + start,
146 s.length * sizeof(EnvelopeSample));
149 void AnalogSegment::reallocate_envelope(Envelope &e)
151 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
152 EnvelopeDataUnit) * EnvelopeDataUnit;
153 if (new_data_length > e.data_length) {
154 e.data_length = new_data_length;
155 e.samples = (EnvelopeSample*)realloc(e.samples,
156 new_data_length * sizeof(EnvelopeSample));
160 void AnalogSegment::append_payload_to_envelope_levels()
162 Envelope &e0 = envelope_levels_[0];
163 uint64_t prev_length;
164 EnvelopeSample *dest_ptr;
165 SegmentRawDataIterator* it;
167 // Expand the data buffer to fit the new samples
168 prev_length = e0.length;
169 e0.length = sample_count_ / EnvelopeScaleFactor;
171 // Calculate min/max values in case we have too few samples for an envelope
172 if (sample_count_ < EnvelopeScaleFactor) {
173 it = begin_raw_sample_iteration(0);
174 for (uint64_t i = 0; i < sample_count_; i++) {
175 const float sample = *((float*)it->value);
176 if (sample < min_value_)
178 if (sample > max_value_)
180 continue_raw_sample_iteration(it, 1);
182 end_raw_sample_iteration(it);
185 // Break off if there are no new samples to compute
186 if (e0.length == prev_length)
189 reallocate_envelope(e0);
191 dest_ptr = e0.samples + prev_length;
193 // Iterate through the samples to populate the first level mipmap
194 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
195 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
197 it = begin_raw_sample_iteration(start_sample);
198 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
199 const float* samples = (float*)it->value;
201 const EnvelopeSample sub_sample = {
202 *min_element(samples, samples + EnvelopeScaleFactor),
203 *max_element(samples, samples + EnvelopeScaleFactor),
206 if (sub_sample.min < min_value_)
207 min_value_ = sub_sample.min;
208 if (sub_sample.max > max_value_)
209 max_value_ = sub_sample.max;
211 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
212 *dest_ptr++ = sub_sample;
214 end_raw_sample_iteration(it);
216 // Compute higher level mipmaps
217 for (unsigned int level = 1; level < ScaleStepCount; level++) {
218 Envelope &e = envelope_levels_[level];
219 const Envelope &el = envelope_levels_[level - 1];
221 // Expand the data buffer to fit the new samples
222 prev_length = e.length;
223 e.length = el.length / EnvelopeScaleFactor;
225 // Break off if there are no more samples to be computed
226 if (e.length == prev_length)
229 reallocate_envelope(e);
231 // Subsample the lower level
232 const EnvelopeSample *src_ptr =
233 el.samples + prev_length * EnvelopeScaleFactor;
234 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
236 for (dest_ptr = e.samples + prev_length;
237 dest_ptr < end_dest_ptr; dest_ptr++) {
238 const EnvelopeSample *const end_src_ptr =
239 src_ptr + EnvelopeScaleFactor;
241 EnvelopeSample sub_sample = *src_ptr++;
242 while (src_ptr < end_src_ptr) {
243 sub_sample.min = min(sub_sample.min, src_ptr->min);;
244 sub_sample.max = max(sub_sample.max, src_ptr->max);
248 *dest_ptr = sub_sample;