]>
Commit | Line | Data |
---|---|---|
aba1dd16 JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
aba1dd16 JH |
18 | */ |
19 | ||
20 | #include <extdef.h> | |
21 | ||
eb8269e3 UH |
22 | #include <cassert> |
23 | #include <cstring> | |
24 | #include <cstdlib> | |
d9e71737 | 25 | #include <cmath> |
aba1dd16 | 26 | |
fda5b6e0 JH |
27 | #include <algorithm> |
28 | ||
7db61e77 | 29 | #include "analog.hpp" |
f3d66e52 | 30 | #include "analogsegment.hpp" |
aba1dd16 | 31 | |
3b68d03d JH |
32 | using std::lock_guard; |
33 | using std::recursive_mutex; | |
6f925ba9 | 34 | using std::make_pair; |
819f4c25 JH |
35 | using std::max; |
36 | using std::max_element; | |
37 | using std::min; | |
38 | using std::min_element; | |
6f925ba9 | 39 | using std::pair; |
aba1dd16 JH |
40 | |
41 | namespace pv { | |
1b1ec774 | 42 | namespace data { |
aba1dd16 | 43 | |
f3d66e52 JH |
44 | const int AnalogSegment::EnvelopeScalePower = 4; |
45 | const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower; | |
46 | const float AnalogSegment::LogEnvelopeScaleFactor = | |
fda5b6e0 | 47 | logf(EnvelopeScaleFactor); |
f3d66e52 | 48 | const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024; // bytes |
fda5b6e0 | 49 | |
7db61e77 | 50 | AnalogSegment::AnalogSegment(Analog& owner, uint64_t samplerate) : |
73e377fe | 51 | Segment(samplerate, sizeof(float)), |
7db61e77 | 52 | owner_(owner), |
73e377fe SA |
53 | min_value_(0), |
54 | max_value_(0) | |
aba1dd16 | 55 | { |
8dbbc7f0 JH |
56 | lock_guard<recursive_mutex> lock(mutex_); |
57 | memset(envelope_levels_, 0, sizeof(envelope_levels_)); | |
aba1dd16 JH |
58 | } |
59 | ||
f3d66e52 | 60 | AnalogSegment::~AnalogSegment() |
fda5b6e0 | 61 | { |
8dbbc7f0 JH |
62 | lock_guard<recursive_mutex> lock(mutex_); |
63 | for (Envelope &e : envelope_levels_) | |
fda5b6e0 JH |
64 | free(e.samples); |
65 | } | |
66 | ||
f3d66e52 | 67 | void AnalogSegment::append_interleaved_samples(const float *data, |
bb2cdfff | 68 | size_t sample_count, size_t stride) |
aba1dd16 | 69 | { |
8dbbc7f0 | 70 | assert(unit_size_ == sizeof(float)); |
bb2cdfff | 71 | |
8dbbc7f0 | 72 | lock_guard<recursive_mutex> lock(mutex_); |
bb2cdfff | 73 | |
7db61e77 SA |
74 | uint64_t prev_sample_count = sample_count_; |
75 | ||
26a883ed SA |
76 | for (uint32_t i=0; i < sample_count; i++) { |
77 | append_single_sample((void*)data); | |
bb2cdfff JH |
78 | data += stride; |
79 | } | |
80 | ||
fda5b6e0 JH |
81 | // Generate the first mip-map from the data |
82 | append_payload_to_envelope_levels(); | |
7db61e77 SA |
83 | |
84 | if (sample_count > 1) | |
85 | owner_.notify_samples_added(this, prev_sample_count + 1, | |
86 | prev_sample_count + 1 + sample_count); | |
87 | else | |
88 | owner_.notify_samples_added(this, prev_sample_count + 1, | |
89 | prev_sample_count + 1); | |
aba1dd16 JH |
90 | } |
91 | ||
f3d66e52 | 92 | const float* AnalogSegment::get_samples( |
d3758367 | 93 | int64_t start_sample, int64_t end_sample) const |
a8acb46e | 94 | { |
d3758367 | 95 | assert(start_sample >= 0); |
8dbbc7f0 | 96 | assert(start_sample < (int64_t)sample_count_); |
d3758367 | 97 | assert(end_sample >= 0); |
8dbbc7f0 | 98 | assert(end_sample < (int64_t)sample_count_); |
d3758367 JH |
99 | assert(start_sample <= end_sample); |
100 | ||
8dbbc7f0 | 101 | lock_guard<recursive_mutex> lock(mutex_); |
d3758367 | 102 | |
26a883ed SA |
103 | return (float*)get_raw_samples(start_sample, (end_sample - start_sample)); |
104 | } | |
105 | ||
6f925ba9 | 106 | const pair<float, float> AnalogSegment::get_min_max() const |
73e377fe | 107 | { |
6f925ba9 | 108 | return make_pair(min_value_, max_value_); |
73e377fe SA |
109 | } |
110 | ||
c70e3464 | 111 | SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) |
26a883ed SA |
112 | { |
113 | return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start); | |
114 | } | |
115 | ||
c70e3464 | 116 | void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) |
26a883ed SA |
117 | { |
118 | Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase); | |
119 | } | |
120 | ||
c70e3464 | 121 | void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) |
26a883ed SA |
122 | { |
123 | Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it); | |
a8acb46e JH |
124 | } |
125 | ||
f3d66e52 | 126 | void AnalogSegment::get_envelope_section(EnvelopeSection &s, |
9320072d JH |
127 | uint64_t start, uint64_t end, float min_length) const |
128 | { | |
129 | assert(end <= get_sample_count()); | |
130 | assert(start <= end); | |
131 | assert(min_length > 0); | |
132 | ||
8dbbc7f0 | 133 | lock_guard<recursive_mutex> lock(mutex_); |
9320072d JH |
134 | |
135 | const unsigned int min_level = max((int)floorf(logf(min_length) / | |
136 | LogEnvelopeScaleFactor) - 1, 0); | |
137 | const unsigned int scale_power = (min_level + 1) * | |
138 | EnvelopeScalePower; | |
139 | start >>= scale_power; | |
140 | end >>= scale_power; | |
141 | ||
142 | s.start = start << scale_power; | |
143 | s.scale = 1 << scale_power; | |
144 | s.length = end - start; | |
145 | s.samples = new EnvelopeSample[s.length]; | |
8dbbc7f0 | 146 | memcpy(s.samples, envelope_levels_[min_level].samples + start, |
9320072d JH |
147 | s.length * sizeof(EnvelopeSample)); |
148 | } | |
149 | ||
f3d66e52 | 150 | void AnalogSegment::reallocate_envelope(Envelope &e) |
fda5b6e0 JH |
151 | { |
152 | const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) / | |
153 | EnvelopeDataUnit) * EnvelopeDataUnit; | |
2ad82c2e | 154 | if (new_data_length > e.data_length) { |
fda5b6e0 JH |
155 | e.data_length = new_data_length; |
156 | e.samples = (EnvelopeSample*)realloc(e.samples, | |
157 | new_data_length * sizeof(EnvelopeSample)); | |
158 | } | |
159 | } | |
160 | ||
f3d66e52 | 161 | void AnalogSegment::append_payload_to_envelope_levels() |
fda5b6e0 | 162 | { |
8dbbc7f0 | 163 | Envelope &e0 = envelope_levels_[0]; |
fda5b6e0 JH |
164 | uint64_t prev_length; |
165 | EnvelopeSample *dest_ptr; | |
26a883ed | 166 | SegmentRawDataIterator* it; |
fda5b6e0 JH |
167 | |
168 | // Expand the data buffer to fit the new samples | |
169 | prev_length = e0.length; | |
8dbbc7f0 | 170 | e0.length = sample_count_ / EnvelopeScaleFactor; |
fda5b6e0 | 171 | |
67b0df27 SA |
172 | // Calculate min/max values in case we have too few samples for an envelope |
173 | if (sample_count_ < EnvelopeScaleFactor) { | |
174 | it = begin_raw_sample_iteration(0); | |
175 | for (uint64_t i = 0; i < sample_count_; i++) { | |
176 | const float sample = *((float*)it->value); | |
177 | if (sample < min_value_) min_value_ = sample; | |
178 | if (sample > max_value_) max_value_ = sample; | |
179 | continue_raw_sample_iteration(it, 1); | |
180 | } | |
181 | end_raw_sample_iteration(it); | |
182 | } | |
183 | ||
fda5b6e0 JH |
184 | // Break off if there are no new samples to compute |
185 | if (e0.length == prev_length) | |
186 | return; | |
187 | ||
188 | reallocate_envelope(e0); | |
189 | ||
190 | dest_ptr = e0.samples + prev_length; | |
191 | ||
192 | // Iterate through the samples to populate the first level mipmap | |
26a883ed SA |
193 | uint64_t start_sample = prev_length * EnvelopeScaleFactor; |
194 | uint64_t end_sample = e0.length * EnvelopeScaleFactor; | |
195 | ||
196 | it = begin_raw_sample_iteration(start_sample); | |
197 | for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) { | |
198 | const float* samples = (float*)it->value; | |
199 | ||
fda5b6e0 | 200 | const EnvelopeSample sub_sample = { |
26a883ed SA |
201 | *min_element(samples, samples + EnvelopeScaleFactor), |
202 | *max_element(samples, samples + EnvelopeScaleFactor), | |
fda5b6e0 JH |
203 | }; |
204 | ||
73e377fe SA |
205 | if (sub_sample.min < min_value_) min_value_ = sub_sample.min; |
206 | if (sub_sample.max > max_value_) max_value_ = sub_sample.max; | |
207 | ||
26a883ed | 208 | continue_raw_sample_iteration(it, EnvelopeScaleFactor); |
fda5b6e0 JH |
209 | *dest_ptr++ = sub_sample; |
210 | } | |
26a883ed | 211 | end_raw_sample_iteration(it); |
fda5b6e0 JH |
212 | |
213 | // Compute higher level mipmaps | |
2ad82c2e | 214 | for (unsigned int level = 1; level < ScaleStepCount; level++) { |
8dbbc7f0 JH |
215 | Envelope &e = envelope_levels_[level]; |
216 | const Envelope &el = envelope_levels_[level-1]; | |
fda5b6e0 JH |
217 | |
218 | // Expand the data buffer to fit the new samples | |
219 | prev_length = e.length; | |
220 | e.length = el.length / EnvelopeScaleFactor; | |
221 | ||
26a883ed | 222 | // Break off if there are no more samples to be computed |
fda5b6e0 JH |
223 | if (e.length == prev_length) |
224 | break; | |
225 | ||
226 | reallocate_envelope(e); | |
227 | ||
26a883ed | 228 | // Subsample the lower level |
fda5b6e0 JH |
229 | const EnvelopeSample *src_ptr = |
230 | el.samples + prev_length * EnvelopeScaleFactor; | |
231 | const EnvelopeSample *const end_dest_ptr = e.samples + e.length; | |
26a883ed | 232 | |
fda5b6e0 | 233 | for (dest_ptr = e.samples + prev_length; |
2ad82c2e | 234 | dest_ptr < end_dest_ptr; dest_ptr++) { |
fda5b6e0 JH |
235 | const EnvelopeSample *const end_src_ptr = |
236 | src_ptr + EnvelopeScaleFactor; | |
237 | ||
238 | EnvelopeSample sub_sample = *src_ptr++; | |
2ad82c2e | 239 | while (src_ptr < end_src_ptr) { |
73e377fe | 240 | sub_sample.min = min(sub_sample.min, src_ptr->min);; |
fda5b6e0 JH |
241 | sub_sample.max = max(sub_sample.max, src_ptr->max); |
242 | src_ptr++; | |
243 | } | |
244 | ||
245 | *dest_ptr = sub_sample; | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
1b1ec774 | 250 | } // namespace data |
aba1dd16 | 251 | } // namespace pv |