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