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