]>
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 <assert.h> | |
23 | #include <string.h> | |
24 | #include <stdlib.h> | |
25 | #include <cmath> | |
26 | ||
27 | #include <algorithm> | |
28 | ||
29 | #include "analogsegment.hpp" | |
30 | ||
31 | using std::lock_guard; | |
32 | using std::recursive_mutex; | |
33 | using std::max; | |
34 | using std::max_element; | |
35 | using std::min; | |
36 | using std::min_element; | |
37 | ||
38 | namespace pv { | |
39 | namespace data { | |
40 | ||
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 | |
46 | ||
47 | AnalogSegment::AnalogSegment(uint64_t samplerate) : | |
48 | Segment(samplerate, sizeof(float)) | |
49 | { | |
50 | lock_guard<recursive_mutex> lock(mutex_); | |
51 | memset(envelope_levels_, 0, sizeof(envelope_levels_)); | |
52 | } | |
53 | ||
54 | AnalogSegment::~AnalogSegment() | |
55 | { | |
56 | lock_guard<recursive_mutex> lock(mutex_); | |
57 | for (Envelope &e : envelope_levels_) | |
58 | free(e.samples); | |
59 | } | |
60 | ||
61 | void AnalogSegment::append_interleaved_samples(const float *data, | |
62 | size_t sample_count, size_t stride) | |
63 | { | |
64 | assert(unit_size_ == sizeof(float)); | |
65 | ||
66 | lock_guard<recursive_mutex> lock(mutex_); | |
67 | ||
68 | for (uint32_t i=0; i < sample_count; i++) { | |
69 | append_single_sample((void*)data); | |
70 | data += stride; | |
71 | } | |
72 | ||
73 | // Generate the first mip-map from the data | |
74 | append_payload_to_envelope_levels(); | |
75 | } | |
76 | ||
77 | const float* AnalogSegment::get_samples( | |
78 | int64_t start_sample, int64_t end_sample) const | |
79 | { | |
80 | assert(start_sample >= 0); | |
81 | assert(start_sample < (int64_t)sample_count_); | |
82 | assert(end_sample >= 0); | |
83 | assert(end_sample < (int64_t)sample_count_); | |
84 | assert(start_sample <= end_sample); | |
85 | ||
86 | lock_guard<recursive_mutex> lock(mutex_); | |
87 | ||
88 | return (float*)get_raw_samples(start_sample, (end_sample - start_sample)); | |
89 | } | |
90 | ||
91 | SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const | |
92 | { | |
93 | return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start); | |
94 | } | |
95 | ||
96 | void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const | |
97 | { | |
98 | Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase); | |
99 | } | |
100 | ||
101 | void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const | |
102 | { | |
103 | Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it); | |
104 | } | |
105 | ||
106 | void AnalogSegment::get_envelope_section(EnvelopeSection &s, | |
107 | uint64_t start, uint64_t end, float min_length) const | |
108 | { | |
109 | assert(end <= get_sample_count()); | |
110 | assert(start <= end); | |
111 | assert(min_length > 0); | |
112 | ||
113 | lock_guard<recursive_mutex> lock(mutex_); | |
114 | ||
115 | const unsigned int min_level = max((int)floorf(logf(min_length) / | |
116 | LogEnvelopeScaleFactor) - 1, 0); | |
117 | const unsigned int scale_power = (min_level + 1) * | |
118 | EnvelopeScalePower; | |
119 | start >>= scale_power; | |
120 | end >>= scale_power; | |
121 | ||
122 | s.start = start << scale_power; | |
123 | s.scale = 1 << scale_power; | |
124 | s.length = end - start; | |
125 | s.samples = new EnvelopeSample[s.length]; | |
126 | memcpy(s.samples, envelope_levels_[min_level].samples + start, | |
127 | s.length * sizeof(EnvelopeSample)); | |
128 | } | |
129 | ||
130 | void AnalogSegment::reallocate_envelope(Envelope &e) | |
131 | { | |
132 | const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) / | |
133 | EnvelopeDataUnit) * EnvelopeDataUnit; | |
134 | if (new_data_length > e.data_length) { | |
135 | e.data_length = new_data_length; | |
136 | e.samples = (EnvelopeSample*)realloc(e.samples, | |
137 | new_data_length * sizeof(EnvelopeSample)); | |
138 | } | |
139 | } | |
140 | ||
141 | void AnalogSegment::append_payload_to_envelope_levels() | |
142 | { | |
143 | Envelope &e0 = envelope_levels_[0]; | |
144 | uint64_t prev_length; | |
145 | EnvelopeSample *dest_ptr; | |
146 | SegmentRawDataIterator* it; | |
147 | ||
148 | // Expand the data buffer to fit the new samples | |
149 | prev_length = e0.length; | |
150 | e0.length = sample_count_ / EnvelopeScaleFactor; | |
151 | ||
152 | // Break off if there are no new samples to compute | |
153 | if (e0.length == prev_length) | |
154 | return; | |
155 | ||
156 | reallocate_envelope(e0); | |
157 | ||
158 | dest_ptr = e0.samples + prev_length; | |
159 | ||
160 | // Iterate through the samples to populate the first level mipmap | |
161 | uint64_t start_sample = prev_length * EnvelopeScaleFactor; | |
162 | uint64_t end_sample = e0.length * EnvelopeScaleFactor; | |
163 | ||
164 | it = begin_raw_sample_iteration(start_sample); | |
165 | for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) { | |
166 | const float* samples = (float*)it->value; | |
167 | ||
168 | const EnvelopeSample sub_sample = { | |
169 | *min_element(samples, samples + EnvelopeScaleFactor), | |
170 | *max_element(samples, samples + EnvelopeScaleFactor), | |
171 | }; | |
172 | ||
173 | continue_raw_sample_iteration(it, EnvelopeScaleFactor); | |
174 | *dest_ptr++ = sub_sample; | |
175 | } | |
176 | end_raw_sample_iteration(it); | |
177 | ||
178 | // Compute higher level mipmaps | |
179 | for (unsigned int level = 1; level < ScaleStepCount; level++) { | |
180 | Envelope &e = envelope_levels_[level]; | |
181 | const Envelope &el = envelope_levels_[level-1]; | |
182 | ||
183 | // Expand the data buffer to fit the new samples | |
184 | prev_length = e.length; | |
185 | e.length = el.length / EnvelopeScaleFactor; | |
186 | ||
187 | // Break off if there are no more samples to be computed | |
188 | if (e.length == prev_length) | |
189 | break; | |
190 | ||
191 | reallocate_envelope(e); | |
192 | ||
193 | // Subsample the lower level | |
194 | const EnvelopeSample *src_ptr = | |
195 | el.samples + prev_length * EnvelopeScaleFactor; | |
196 | const EnvelopeSample *const end_dest_ptr = e.samples + e.length; | |
197 | ||
198 | for (dest_ptr = e.samples + prev_length; | |
199 | dest_ptr < end_dest_ptr; dest_ptr++) { | |
200 | const EnvelopeSample *const end_src_ptr = | |
201 | src_ptr + EnvelopeScaleFactor; | |
202 | ||
203 | EnvelopeSample sub_sample = *src_ptr++; | |
204 | while (src_ptr < end_src_ptr) { | |
205 | sub_sample.min = min(sub_sample.min, src_ptr->min); | |
206 | sub_sample.max = max(sub_sample.max, src_ptr->max); | |
207 | src_ptr++; | |
208 | } | |
209 | ||
210 | *dest_ptr = sub_sample; | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
215 | } // namespace data | |
216 | } // namespace pv |