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 <boost/foreach.hpp>
32 #include "analogsnapshot.h"
34 using boost::lock_guard;
35 using boost::recursive_mutex;
37 using std::max_element;
39 using std::min_element;
44 const int AnalogSnapshot::EnvelopeScalePower = 4;
45 const int AnalogSnapshot::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
46 const float AnalogSnapshot::LogEnvelopeScaleFactor =
47 logf(EnvelopeScaleFactor);
48 const uint64_t AnalogSnapshot::EnvelopeDataUnit = 64*1024; // bytes
50 AnalogSnapshot::AnalogSnapshot() :
51 Snapshot(sizeof(float))
53 lock_guard<recursive_mutex> lock(_mutex);
54 memset(_envelope_levels, 0, sizeof(_envelope_levels));
57 AnalogSnapshot::~AnalogSnapshot()
59 lock_guard<recursive_mutex> lock(_mutex);
60 BOOST_FOREACH(Envelope &e, _envelope_levels)
64 void AnalogSnapshot::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 _data = realloc(_data, (_sample_count + sample_count) * sizeof(float));
73 float *dst = (float*)_data + _sample_count;
74 const float *dst_end = dst + sample_count;
75 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* AnalogSnapshot::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 + start_sample, sizeof(float) *
100 (end_sample - start_sample));
104 void AnalogSnapshot::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 AnalogSnapshot::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)
134 e.data_length = new_data_length;
135 e.samples = (EnvelopeSample*)realloc(e.samples,
136 new_data_length * sizeof(EnvelopeSample));
140 void AnalogSnapshot::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 +
160 e0.length * EnvelopeScaleFactor;
161 for (const float *src_ptr = (float*)_data +
162 prev_length * EnvelopeScaleFactor;
163 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor)
165 const EnvelopeSample sub_sample = {
166 *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
167 *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
170 *dest_ptr++ = sub_sample;
173 // Compute higher level mipmaps
174 for (unsigned int level = 1; level < ScaleStepCount; level++)
176 Envelope &e = _envelope_levels[level];
177 const Envelope &el = _envelope_levels[level-1];
179 // Expand the data buffer to fit the new samples
180 prev_length = e.length;
181 e.length = el.length / EnvelopeScaleFactor;
183 // Break off if there are no more samples to computed
184 if (e.length == prev_length)
187 reallocate_envelope(e);
189 // Subsample the level lower level
190 const EnvelopeSample *src_ptr =
191 el.samples + prev_length * EnvelopeScaleFactor;
192 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
193 for (dest_ptr = e.samples + prev_length;
194 dest_ptr < end_dest_ptr; dest_ptr++)
196 const EnvelopeSample *const end_src_ptr =
197 src_ptr + EnvelopeScaleFactor;
199 EnvelopeSample sub_sample = *src_ptr++;
200 while (src_ptr < end_src_ptr)
202 sub_sample.min = min(sub_sample.min, src_ptr->min);
203 sub_sample.max = max(sub_sample.max, src_ptr->max);
207 *dest_ptr = sub_sample;