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/>.
31 #include "analogsegment.hpp"
33 using std::lock_guard;
34 using std::recursive_mutex;
37 using std::max_element;
39 using std::min_element;
41 using std::unique_ptr;
46 const int AnalogSegment::EnvelopeScalePower = 4;
47 const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
48 const float AnalogSegment::LogEnvelopeScaleFactor = logf(EnvelopeScaleFactor);
49 const uint64_t AnalogSegment::EnvelopeDataUnit = 64 * 1024; // bytes
51 AnalogSegment::AnalogSegment(Analog& owner, uint32_t segment_id, uint64_t samplerate) :
52 Segment(segment_id, samplerate, sizeof(float)),
57 lock_guard<recursive_mutex> lock(mutex_);
58 memset(envelope_levels_, 0, sizeof(envelope_levels_));
61 AnalogSegment::~AnalogSegment()
63 lock_guard<recursive_mutex> lock(mutex_);
64 for (Envelope &e : envelope_levels_)
68 void AnalogSegment::append_interleaved_samples(const float *data,
69 size_t sample_count, size_t stride)
71 assert(unit_size_ == sizeof(float));
73 lock_guard<recursive_mutex> lock(mutex_);
75 uint64_t prev_sample_count = sample_count_;
77 // Deinterleave the samples and add them
78 unique_ptr<float[]> deint_data(new float[sample_count]);
79 float *deint_data_ptr = deint_data.get();
80 for (uint32_t i = 0; i < sample_count; i++) {
81 *deint_data_ptr = (float)(*data);
86 append_samples(deint_data.get(), sample_count);
88 // Generate the first mip-map from the data
89 append_payload_to_envelope_levels();
92 owner_.notify_samples_added(shared_ptr<Segment>(shared_from_this()),
93 prev_sample_count + 1, prev_sample_count + 1 + sample_count);
95 owner_.notify_samples_added(shared_ptr<Segment>(shared_from_this()),
96 prev_sample_count + 1, prev_sample_count + 1);
99 float AnalogSegment::get_sample(int64_t sample_num) const
101 assert(sample_num >= 0);
102 assert(sample_num <= (int64_t)sample_count_);
104 lock_guard<recursive_mutex> lock(mutex_); // Because of free_unused_memory()
106 return *((const float*)get_raw_sample(sample_num));
109 void AnalogSegment::get_samples(int64_t start_sample, int64_t end_sample,
112 assert(start_sample >= 0);
113 assert(start_sample < (int64_t)sample_count_);
114 assert(end_sample >= 0);
115 assert(end_sample <= (int64_t)sample_count_);
116 assert(start_sample <= end_sample);
117 assert(dest != nullptr);
119 lock_guard<recursive_mutex> lock(mutex_);
121 get_raw_samples(start_sample, (end_sample - start_sample), (uint8_t*)dest);
124 const pair<float, float> AnalogSegment::get_min_max() const
126 return make_pair(min_value_, max_value_);
129 float* AnalogSegment::get_iterator_value_ptr(SegmentDataIterator* it)
131 assert(it->sample_index <= (sample_count_ - 1));
133 return (float*)(it->chunk + it->chunk_offs);
136 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
137 uint64_t start, uint64_t end, float min_length) const
139 assert(end <= get_sample_count());
140 assert(start <= end);
141 assert(min_length > 0);
143 lock_guard<recursive_mutex> lock(mutex_);
145 const unsigned int min_level = max((int)floorf(logf(min_length) /
146 LogEnvelopeScaleFactor) - 1, 0);
147 const unsigned int scale_power = (min_level + 1) *
149 start >>= scale_power;
152 s.start = start << scale_power;
153 s.scale = 1 << scale_power;
154 s.length = end - start;
155 s.samples = new EnvelopeSample[s.length];
156 memcpy(s.samples, envelope_levels_[min_level].samples + start,
157 s.length * sizeof(EnvelopeSample));
160 void AnalogSegment::reallocate_envelope(Envelope &e)
162 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
163 EnvelopeDataUnit) * EnvelopeDataUnit;
164 if (new_data_length > e.data_length) {
165 e.data_length = new_data_length;
166 e.samples = (EnvelopeSample*)realloc(e.samples,
167 new_data_length * sizeof(EnvelopeSample));
171 void AnalogSegment::append_payload_to_envelope_levels()
173 Envelope &e0 = envelope_levels_[0];
174 uint64_t prev_length;
175 EnvelopeSample *dest_ptr;
176 SegmentDataIterator* it;
178 // Expand the data buffer to fit the new samples
179 prev_length = e0.length;
180 e0.length = sample_count_ / EnvelopeScaleFactor;
182 // Calculate min/max values in case we have too few samples for an envelope
183 const float old_min_value = min_value_, old_max_value = max_value_;
184 if (sample_count_ < EnvelopeScaleFactor) {
185 it = begin_sample_iteration(0);
186 for (uint64_t i = 0; i < sample_count_; i++) {
187 const float sample = *get_iterator_value_ptr(it);
188 if (sample < min_value_)
190 if (sample > max_value_)
192 continue_sample_iteration(it, 1);
194 end_sample_iteration(it);
197 // Break off if there are no new samples to compute
198 if (e0.length == prev_length)
201 reallocate_envelope(e0);
203 dest_ptr = e0.samples + prev_length;
205 // Iterate through the samples to populate the first level mipmap
206 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
207 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
209 it = begin_sample_iteration(start_sample);
210 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
211 const float* samples = get_iterator_value_ptr(it);
213 const EnvelopeSample sub_sample = {
214 *min_element(samples, samples + EnvelopeScaleFactor),
215 *max_element(samples, samples + EnvelopeScaleFactor),
218 if (sub_sample.min < min_value_)
219 min_value_ = sub_sample.min;
220 if (sub_sample.max > max_value_)
221 max_value_ = sub_sample.max;
223 continue_sample_iteration(it, EnvelopeScaleFactor);
224 *dest_ptr++ = sub_sample;
226 end_sample_iteration(it);
228 // Compute higher level mipmaps
229 for (unsigned int level = 1; level < ScaleStepCount; level++) {
230 Envelope &e = envelope_levels_[level];
231 const Envelope &el = envelope_levels_[level - 1];
233 // Expand the data buffer to fit the new samples
234 prev_length = e.length;
235 e.length = el.length / EnvelopeScaleFactor;
237 // Break off if there are no more samples to be computed
238 if (e.length == prev_length)
241 reallocate_envelope(e);
243 // Subsample the lower level
244 const EnvelopeSample *src_ptr =
245 el.samples + prev_length * EnvelopeScaleFactor;
246 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
248 for (dest_ptr = e.samples + prev_length;
249 dest_ptr < end_dest_ptr; dest_ptr++) {
250 const EnvelopeSample *const end_src_ptr =
251 src_ptr + EnvelopeScaleFactor;
253 EnvelopeSample sub_sample = *src_ptr++;
254 while (src_ptr < end_src_ptr) {
255 sub_sample.min = min(sub_sample.min, src_ptr->min);;
256 sub_sample.max = max(sub_sample.max, src_ptr->max);
260 *dest_ptr = sub_sample;
264 // Notify if the min or max value changed
265 if ((old_min_value != min_value_) || (old_max_value != max_value_))
266 owner_.min_max_changed(min_value_, max_value_);