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