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;
35 using std::max_element;
37 using std::min_element;
42 const int AnalogSegment::EnvelopeScalePower = 4;
43 const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
44 const float AnalogSegment::LogEnvelopeScaleFactor =
45 logf(EnvelopeScaleFactor);
46 const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024; // bytes
48 AnalogSegment::AnalogSegment(Analog& owner, uint64_t samplerate) :
49 Segment(samplerate, sizeof(float)),
54 lock_guard<recursive_mutex> lock(mutex_);
55 memset(envelope_levels_, 0, sizeof(envelope_levels_));
58 AnalogSegment::~AnalogSegment()
60 lock_guard<recursive_mutex> lock(mutex_);
61 for (Envelope &e : envelope_levels_)
65 void AnalogSegment::append_interleaved_samples(const float *data,
66 size_t sample_count, size_t stride)
68 assert(unit_size_ == sizeof(float));
70 lock_guard<recursive_mutex> lock(mutex_);
72 uint64_t prev_sample_count = sample_count_;
74 for (uint32_t i=0; i < sample_count; i++) {
75 append_single_sample((void*)data);
79 // Generate the first mip-map from the data
80 append_payload_to_envelope_levels();
83 owner_.notify_samples_added(this, prev_sample_count + 1,
84 prev_sample_count + 1 + sample_count);
86 owner_.notify_samples_added(this, prev_sample_count + 1,
87 prev_sample_count + 1);
90 const float* AnalogSegment::get_samples(
91 int64_t start_sample, int64_t end_sample) const
93 assert(start_sample >= 0);
94 assert(start_sample < (int64_t)sample_count_);
95 assert(end_sample >= 0);
96 assert(end_sample < (int64_t)sample_count_);
97 assert(start_sample <= end_sample);
99 lock_guard<recursive_mutex> lock(mutex_);
101 return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
104 const std::pair<float, float> AnalogSegment::get_min_max() const
106 return std::make_pair(min_value_, max_value_);
109 SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start)
111 return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
114 void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase)
116 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
119 void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it)
121 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
124 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
125 uint64_t start, uint64_t end, float min_length) const
127 assert(end <= get_sample_count());
128 assert(start <= end);
129 assert(min_length > 0);
131 lock_guard<recursive_mutex> lock(mutex_);
133 const unsigned int min_level = max((int)floorf(logf(min_length) /
134 LogEnvelopeScaleFactor) - 1, 0);
135 const unsigned int scale_power = (min_level + 1) *
137 start >>= scale_power;
140 s.start = start << scale_power;
141 s.scale = 1 << scale_power;
142 s.length = end - start;
143 s.samples = new EnvelopeSample[s.length];
144 memcpy(s.samples, envelope_levels_[min_level].samples + start,
145 s.length * sizeof(EnvelopeSample));
148 void AnalogSegment::reallocate_envelope(Envelope &e)
150 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
151 EnvelopeDataUnit) * EnvelopeDataUnit;
152 if (new_data_length > e.data_length) {
153 e.data_length = new_data_length;
154 e.samples = (EnvelopeSample*)realloc(e.samples,
155 new_data_length * sizeof(EnvelopeSample));
159 void AnalogSegment::append_payload_to_envelope_levels()
161 Envelope &e0 = envelope_levels_[0];
162 uint64_t prev_length;
163 EnvelopeSample *dest_ptr;
164 SegmentRawDataIterator* it;
166 // Expand the data buffer to fit the new samples
167 prev_length = e0.length;
168 e0.length = sample_count_ / EnvelopeScaleFactor;
170 // Calculate min/max values in case we have too few samples for an envelope
171 if (sample_count_ < EnvelopeScaleFactor) {
172 it = begin_raw_sample_iteration(0);
173 for (uint64_t i = 0; i < sample_count_; i++) {
174 const float sample = *((float*)it->value);
175 if (sample < min_value_) min_value_ = sample;
176 if (sample > max_value_) max_value_ = sample;
177 continue_raw_sample_iteration(it, 1);
179 end_raw_sample_iteration(it);
182 // Break off if there are no new samples to compute
183 if (e0.length == prev_length)
186 reallocate_envelope(e0);
188 dest_ptr = e0.samples + prev_length;
190 // Iterate through the samples to populate the first level mipmap
191 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
192 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
194 it = begin_raw_sample_iteration(start_sample);
195 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
196 const float* samples = (float*)it->value;
198 const EnvelopeSample sub_sample = {
199 *min_element(samples, samples + EnvelopeScaleFactor),
200 *max_element(samples, samples + EnvelopeScaleFactor),
203 if (sub_sample.min < min_value_) min_value_ = sub_sample.min;
204 if (sub_sample.max > max_value_) max_value_ = sub_sample.max;
206 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
207 *dest_ptr++ = sub_sample;
209 end_raw_sample_iteration(it);
211 // Compute higher level mipmaps
212 for (unsigned int level = 1; level < ScaleStepCount; level++) {
213 Envelope &e = envelope_levels_[level];
214 const Envelope &el = envelope_levels_[level-1];
216 // Expand the data buffer to fit the new samples
217 prev_length = e.length;
218 e.length = el.length / EnvelopeScaleFactor;
220 // Break off if there are no more samples to be computed
221 if (e.length == prev_length)
224 reallocate_envelope(e);
226 // Subsample the lower level
227 const EnvelopeSample *src_ptr =
228 el.samples + prev_length * EnvelopeScaleFactor;
229 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
231 for (dest_ptr = e.samples + prev_length;
232 dest_ptr < end_dest_ptr; dest_ptr++) {
233 const EnvelopeSample *const end_src_ptr =
234 src_ptr + EnvelopeScaleFactor;
236 EnvelopeSample sub_sample = *src_ptr++;
237 while (src_ptr < end_src_ptr) {
238 sub_sample.min = min(sub_sample.min, src_ptr->min);;
239 sub_sample.max = max(sub_sample.max, src_ptr->max);
243 *dest_ptr = sub_sample;