2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
29 #include "analogsegment.hpp"
31 using std::lock_guard;
32 using std::recursive_mutex;
34 using std::max_element;
36 using std::min_element;
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
47 AnalogSegment::AnalogSegment(
48 uint64_t samplerate, const uint64_t expected_num_samples) :
49 Segment(samplerate, sizeof(float))
51 set_capacity(expected_num_samples);
53 lock_guard<recursive_mutex> lock(mutex_);
54 memset(envelope_levels_, 0, sizeof(envelope_levels_));
57 AnalogSegment::~AnalogSegment()
59 lock_guard<recursive_mutex> lock(mutex_);
60 for (Envelope &e : envelope_levels_)
64 void AnalogSegment::append_interleaved_samples(const float *data,
65 size_t sample_count, size_t stride)
67 assert(unit_size_ == sizeof(float));
69 lock_guard<recursive_mutex> lock(mutex_);
71 // If we're out of memory, this will throw std::bad_alloc
72 data_.resize((sample_count_ + sample_count) * sizeof(float));
74 float *dst = (float*)data_.data() + sample_count_;
75 const float *dst_end = dst + sample_count;
76 while (dst != dst_end) {
81 sample_count_ += sample_count;
83 // Generate the first mip-map from the data
84 append_payload_to_envelope_levels();
87 const float* AnalogSegment::get_samples(
88 int64_t start_sample, int64_t end_sample) const
90 assert(start_sample >= 0);
91 assert(start_sample < (int64_t)sample_count_);
92 assert(end_sample >= 0);
93 assert(end_sample < (int64_t)sample_count_);
94 assert(start_sample <= end_sample);
96 lock_guard<recursive_mutex> lock(mutex_);
98 float *const data = new float[end_sample - start_sample];
99 memcpy(data, (float*)data_.data() + start_sample, sizeof(float) *
100 (end_sample - start_sample));
104 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
105 uint64_t start, uint64_t end, float min_length) const
107 assert(end <= get_sample_count());
108 assert(start <= end);
109 assert(min_length > 0);
111 lock_guard<recursive_mutex> lock(mutex_);
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) *
117 start >>= scale_power;
120 s.start = start << scale_power;
121 s.scale = 1 << scale_power;
122 s.length = end - start;
123 s.samples = new EnvelopeSample[s.length];
124 memcpy(s.samples, envelope_levels_[min_level].samples + start,
125 s.length * sizeof(EnvelopeSample));
128 void AnalogSegment::reallocate_envelope(Envelope &e)
130 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
131 EnvelopeDataUnit) * EnvelopeDataUnit;
132 if (new_data_length > e.data_length) {
133 e.data_length = new_data_length;
134 e.samples = (EnvelopeSample*)realloc(e.samples,
135 new_data_length * sizeof(EnvelopeSample));
139 void AnalogSegment::append_payload_to_envelope_levels()
141 Envelope &e0 = envelope_levels_[0];
142 uint64_t prev_length;
143 EnvelopeSample *dest_ptr;
145 // Expand the data buffer to fit the new samples
146 prev_length = e0.length;
147 e0.length = sample_count_ / EnvelopeScaleFactor;
149 // Break off if there are no new samples to compute
150 if (e0.length == prev_length)
153 reallocate_envelope(e0);
155 dest_ptr = e0.samples + prev_length;
157 // Iterate through the samples to populate the first level mipmap
158 const float *const end_src_ptr = (float*)data_.data() +
159 e0.length * EnvelopeScaleFactor;
160 for (const float *src_ptr = (float*)data_.data() +
161 prev_length * EnvelopeScaleFactor;
162 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor) {
163 const EnvelopeSample sub_sample = {
164 *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
165 *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
168 *dest_ptr++ = sub_sample;
171 // Compute higher level mipmaps
172 for (unsigned int level = 1; level < ScaleStepCount; level++) {
173 Envelope &e = envelope_levels_[level];
174 const Envelope &el = envelope_levels_[level-1];
176 // Expand the data buffer to fit the new samples
177 prev_length = e.length;
178 e.length = el.length / EnvelopeScaleFactor;
180 // Break off if there are no more samples to computed
181 if (e.length == prev_length)
184 reallocate_envelope(e);
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;
191 dest_ptr < end_dest_ptr; dest_ptr++) {
192 const EnvelopeSample *const end_src_ptr =
193 src_ptr + EnvelopeScaleFactor;
195 EnvelopeSample sub_sample = *src_ptr++;
196 while (src_ptr < end_src_ptr) {
197 sub_sample.min = min(sub_sample.min, src_ptr->min);
198 sub_sample.max = max(sub_sample.max, src_ptr->max);
202 *dest_ptr = sub_sample;