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/>.
29 #include "analogsegment.hpp"
31 using std::lock_guard;
32 using std::recursive_mutex;
34 using std::max_element;
36 using std::min_element;
41 const int AnalogSegment::EnvelopeScalePower = 4;
42 const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
43 const float AnalogSegment::LogEnvelopeScaleFactor =
44 logf(EnvelopeScaleFactor);
45 const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024; // bytes
47 AnalogSegment::AnalogSegment(uint64_t samplerate) :
48 Segment(samplerate, sizeof(float)),
52 lock_guard<recursive_mutex> lock(mutex_);
53 memset(envelope_levels_, 0, sizeof(envelope_levels_));
56 AnalogSegment::~AnalogSegment()
58 lock_guard<recursive_mutex> lock(mutex_);
59 for (Envelope &e : envelope_levels_)
63 void AnalogSegment::append_interleaved_samples(const float *data,
64 size_t sample_count, size_t stride)
66 assert(unit_size_ == sizeof(float));
68 lock_guard<recursive_mutex> lock(mutex_);
70 for (uint32_t i=0; i < sample_count; i++) {
71 append_single_sample((void*)data);
75 // Generate the first mip-map from the data
76 append_payload_to_envelope_levels();
79 const float* AnalogSegment::get_samples(
80 int64_t start_sample, int64_t end_sample) const
82 assert(start_sample >= 0);
83 assert(start_sample < (int64_t)sample_count_);
84 assert(end_sample >= 0);
85 assert(end_sample < (int64_t)sample_count_);
86 assert(start_sample <= end_sample);
88 lock_guard<recursive_mutex> lock(mutex_);
90 return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
93 const std::pair<float, float> AnalogSegment::get_min_max() const
95 return std::make_pair(min_value_, max_value_);
98 SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const
100 return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
103 void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const
105 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
108 void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const
110 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
113 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
114 uint64_t start, uint64_t end, float min_length) const
116 assert(end <= get_sample_count());
117 assert(start <= end);
118 assert(min_length > 0);
120 lock_guard<recursive_mutex> lock(mutex_);
122 const unsigned int min_level = max((int)floorf(logf(min_length) /
123 LogEnvelopeScaleFactor) - 1, 0);
124 const unsigned int scale_power = (min_level + 1) *
126 start >>= scale_power;
129 s.start = start << scale_power;
130 s.scale = 1 << scale_power;
131 s.length = end - start;
132 s.samples = new EnvelopeSample[s.length];
133 memcpy(s.samples, envelope_levels_[min_level].samples + start,
134 s.length * sizeof(EnvelopeSample));
137 void AnalogSegment::reallocate_envelope(Envelope &e)
139 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
140 EnvelopeDataUnit) * EnvelopeDataUnit;
141 if (new_data_length > e.data_length) {
142 e.data_length = new_data_length;
143 e.samples = (EnvelopeSample*)realloc(e.samples,
144 new_data_length * sizeof(EnvelopeSample));
148 void AnalogSegment::append_payload_to_envelope_levels()
150 Envelope &e0 = envelope_levels_[0];
151 uint64_t prev_length;
152 EnvelopeSample *dest_ptr;
153 SegmentRawDataIterator* it;
155 // Expand the data buffer to fit the new samples
156 prev_length = e0.length;
157 e0.length = sample_count_ / EnvelopeScaleFactor;
159 // Break off if there are no new samples to compute
160 if (e0.length == prev_length)
163 reallocate_envelope(e0);
165 dest_ptr = e0.samples + prev_length;
167 // Iterate through the samples to populate the first level mipmap
168 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
169 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
171 it = begin_raw_sample_iteration(start_sample);
172 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
173 const float* samples = (float*)it->value;
175 const EnvelopeSample sub_sample = {
176 *min_element(samples, samples + EnvelopeScaleFactor),
177 *max_element(samples, samples + EnvelopeScaleFactor),
180 if (sub_sample.min < min_value_) min_value_ = sub_sample.min;
181 if (sub_sample.max > max_value_) max_value_ = sub_sample.max;
183 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
184 *dest_ptr++ = sub_sample;
186 end_raw_sample_iteration(it);
188 // Compute higher level mipmaps
189 for (unsigned int level = 1; level < ScaleStepCount; level++) {
190 Envelope &e = envelope_levels_[level];
191 const Envelope &el = envelope_levels_[level-1];
193 // Expand the data buffer to fit the new samples
194 prev_length = e.length;
195 e.length = el.length / EnvelopeScaleFactor;
197 // Break off if there are no more samples to be computed
198 if (e.length == prev_length)
201 reallocate_envelope(e);
203 // Subsample the lower level
204 const EnvelopeSample *src_ptr =
205 el.samples + prev_length * EnvelopeScaleFactor;
206 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
208 for (dest_ptr = e.samples + prev_length;
209 dest_ptr < end_dest_ptr; dest_ptr++) {
210 const EnvelopeSample *const end_src_ptr =
211 src_ptr + EnvelopeScaleFactor;
213 EnvelopeSample sub_sample = *src_ptr++;
214 while (src_ptr < end_src_ptr) {
215 sub_sample.min = min(sub_sample.min, src_ptr->min);;
216 sub_sample.max = max(sub_sample.max, src_ptr->max);
220 *dest_ptr = sub_sample;