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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "analogsegment.hpp"
32 using std::lock_guard;
33 using std::recursive_mutex;
35 using std::max_element;
37 using std::min_element;
42 const int AnalogSegment::EnvelopeScalePower = 4;
43 const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
44 const float AnalogSegment::LogEnvelopeScaleFactor =
45 logf(EnvelopeScaleFactor);
46 const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024; // bytes
48 AnalogSegment::AnalogSegment(
49 uint64_t samplerate, const uint64_t expected_num_samples) :
50 Segment(samplerate, sizeof(float))
52 set_capacity(expected_num_samples);
54 lock_guard<recursive_mutex> lock(mutex_);
55 memset(envelope_levels_, 0, sizeof(envelope_levels_));
58 AnalogSegment::~AnalogSegment()
60 lock_guard<recursive_mutex> lock(mutex_);
61 for (Envelope &e : envelope_levels_)
65 void AnalogSegment::append_interleaved_samples(const float *data,
66 size_t sample_count, size_t stride)
68 assert(unit_size_ == sizeof(float));
70 lock_guard<recursive_mutex> lock(mutex_);
72 // If we're out of memory, this will throw std::bad_alloc
73 data_.resize((sample_count_ + sample_count) * sizeof(float));
75 float *dst = (float*)data_.data() + sample_count_;
76 const float *dst_end = dst + sample_count;
77 while (dst != dst_end) {
82 sample_count_ += sample_count;
84 // Generate the first mip-map from the data
85 append_payload_to_envelope_levels();
88 const float* AnalogSegment::get_samples(
89 int64_t start_sample, int64_t end_sample) const
91 assert(start_sample >= 0);
92 assert(start_sample < (int64_t)sample_count_);
93 assert(end_sample >= 0);
94 assert(end_sample < (int64_t)sample_count_);
95 assert(start_sample <= end_sample);
97 lock_guard<recursive_mutex> lock(mutex_);
99 float *const data = new float[end_sample - start_sample];
100 memcpy(data, (float*)data_.data() + start_sample, sizeof(float) *
101 (end_sample - start_sample));
105 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
106 uint64_t start, uint64_t end, float min_length) const
108 assert(end <= get_sample_count());
109 assert(start <= end);
110 assert(min_length > 0);
112 lock_guard<recursive_mutex> lock(mutex_);
114 const unsigned int min_level = max((int)floorf(logf(min_length) /
115 LogEnvelopeScaleFactor) - 1, 0);
116 const unsigned int scale_power = (min_level + 1) *
118 start >>= scale_power;
121 s.start = start << scale_power;
122 s.scale = 1 << scale_power;
123 s.length = end - start;
124 s.samples = new EnvelopeSample[s.length];
125 memcpy(s.samples, envelope_levels_[min_level].samples + start,
126 s.length * sizeof(EnvelopeSample));
129 void AnalogSegment::reallocate_envelope(Envelope &e)
131 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
132 EnvelopeDataUnit) * EnvelopeDataUnit;
133 if (new_data_length > e.data_length) {
134 e.data_length = new_data_length;
135 e.samples = (EnvelopeSample*)realloc(e.samples,
136 new_data_length * sizeof(EnvelopeSample));
140 void AnalogSegment::append_payload_to_envelope_levels()
142 Envelope &e0 = envelope_levels_[0];
143 uint64_t prev_length;
144 EnvelopeSample *dest_ptr;
146 // Expand the data buffer to fit the new samples
147 prev_length = e0.length;
148 e0.length = sample_count_ / EnvelopeScaleFactor;
150 // Break off if there are no new samples to compute
151 if (e0.length == prev_length)
154 reallocate_envelope(e0);
156 dest_ptr = e0.samples + prev_length;
158 // Iterate through the samples to populate the first level mipmap
159 const float *const end_src_ptr = (float*)data_.data() +
160 e0.length * EnvelopeScaleFactor;
161 for (const float *src_ptr = (float*)data_.data() +
162 prev_length * EnvelopeScaleFactor;
163 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor) {
164 const EnvelopeSample sub_sample = {
165 *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
166 *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
169 *dest_ptr++ = sub_sample;
172 // Compute higher level mipmaps
173 for (unsigned int level = 1; level < ScaleStepCount; level++) {
174 Envelope &e = envelope_levels_[level];
175 const Envelope &el = envelope_levels_[level-1];
177 // Expand the data buffer to fit the new samples
178 prev_length = e.length;
179 e.length = el.length / EnvelopeScaleFactor;
181 // Break off if there are no more samples to computed
182 if (e.length == prev_length)
185 reallocate_envelope(e);
187 // Subsample the level lower level
188 const EnvelopeSample *src_ptr =
189 el.samples + prev_length * EnvelopeScaleFactor;
190 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
191 for (dest_ptr = e.samples + prev_length;
192 dest_ptr < end_dest_ptr; dest_ptr++) {
193 const EnvelopeSample *const end_src_ptr =
194 src_ptr + EnvelopeScaleFactor;
196 EnvelopeSample sub_sample = *src_ptr++;
197 while (src_ptr < end_src_ptr) {
198 sub_sample.min = min(sub_sample.min, src_ptr->min);
199 sub_sample.max = max(sub_sample.max, src_ptr->max);
203 *dest_ptr = sub_sample;