File: analogsegment.cpp

package info (click to toggle)
pulseview 0.4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,064 kB
  • sloc: cpp: 25,958; xml: 215; java: 42; makefile: 2
file content (260 lines) | stat: -rw-r--r-- 7,472 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * This file is part of the PulseView project.
 *
 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <extdef.h>

#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <memory>

#include <algorithm>

#include "analog.hpp"
#include "analogsegment.hpp"

using std::lock_guard;
using std::recursive_mutex;
using std::make_pair;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::pair;
using std::unique_ptr;

namespace pv {
namespace data {

const int AnalogSegment::EnvelopeScalePower = 4;
const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
const float AnalogSegment::LogEnvelopeScaleFactor = logf(EnvelopeScaleFactor);
const uint64_t AnalogSegment::EnvelopeDataUnit = 64 * 1024;	// bytes

AnalogSegment::AnalogSegment(Analog& owner, uint32_t segment_id, uint64_t samplerate) :
	Segment(segment_id, samplerate, sizeof(float)),
	owner_(owner),
	min_value_(0),
	max_value_(0)
{
	lock_guard<recursive_mutex> lock(mutex_);
	memset(envelope_levels_, 0, sizeof(envelope_levels_));
}

AnalogSegment::~AnalogSegment()
{
	lock_guard<recursive_mutex> lock(mutex_);
	for (Envelope &e : envelope_levels_)
		free(e.samples);
}

void AnalogSegment::append_interleaved_samples(const float *data,
	size_t sample_count, size_t stride)
{
	assert(unit_size_ == sizeof(float));

	lock_guard<recursive_mutex> lock(mutex_);

	uint64_t prev_sample_count = sample_count_;

	// Deinterleave the samples and add them
	unique_ptr<float[]> deint_data(new float[sample_count]);
	float *deint_data_ptr = deint_data.get();
	for (uint32_t i = 0; i < sample_count; i++) {
		*deint_data_ptr = (float)(*data);
		deint_data_ptr++;
		data += stride;
	}

	append_samples(deint_data.get(), sample_count);

	// Generate the first mip-map from the data
	append_payload_to_envelope_levels();

	if (sample_count > 1)
		owner_.notify_samples_added(this, prev_sample_count + 1,
			prev_sample_count + 1 + sample_count);
	else
		owner_.notify_samples_added(this, prev_sample_count + 1,
			prev_sample_count + 1);
}

void AnalogSegment::get_samples(int64_t start_sample, int64_t end_sample,
	float* dest) const
{
	assert(start_sample >= 0);
	assert(start_sample < (int64_t)sample_count_);
	assert(end_sample >= 0);
	assert(end_sample <= (int64_t)sample_count_);
	assert(start_sample <= end_sample);
	assert(dest != nullptr);

	lock_guard<recursive_mutex> lock(mutex_);

	get_raw_samples(start_sample, (end_sample - start_sample), (uint8_t*)dest);
}

const pair<float, float> AnalogSegment::get_min_max() const
{
	return make_pair(min_value_, max_value_);
}

float* AnalogSegment::get_iterator_value_ptr(SegmentDataIterator* it)
{
	assert(it->sample_index <= (sample_count_ - 1));

	return (float*)(it->chunk + it->chunk_offs);
}

void AnalogSegment::get_envelope_section(EnvelopeSection &s,
	uint64_t start, uint64_t end, float min_length) const
{
	assert(end <= get_sample_count());
	assert(start <= end);
	assert(min_length > 0);

	lock_guard<recursive_mutex> lock(mutex_);

	const unsigned int min_level = max((int)floorf(logf(min_length) /
		LogEnvelopeScaleFactor) - 1, 0);
	const unsigned int scale_power = (min_level + 1) *
		EnvelopeScalePower;
	start >>= scale_power;
	end >>= scale_power;

	s.start = start << scale_power;
	s.scale = 1 << scale_power;
	s.length = end - start;
	s.samples = new EnvelopeSample[s.length];
	memcpy(s.samples, envelope_levels_[min_level].samples + start,
		s.length * sizeof(EnvelopeSample));
}

void AnalogSegment::reallocate_envelope(Envelope &e)
{
	const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
		EnvelopeDataUnit) * EnvelopeDataUnit;
	if (new_data_length > e.data_length) {
		e.data_length = new_data_length;
		e.samples = (EnvelopeSample*)realloc(e.samples,
			new_data_length * sizeof(EnvelopeSample));
	}
}

void AnalogSegment::append_payload_to_envelope_levels()
{
	Envelope &e0 = envelope_levels_[0];
	uint64_t prev_length;
	EnvelopeSample *dest_ptr;
	SegmentDataIterator* it;

	// Expand the data buffer to fit the new samples
	prev_length = e0.length;
	e0.length = sample_count_ / EnvelopeScaleFactor;

	// Calculate min/max values in case we have too few samples for an envelope
	const float old_min_value = min_value_, old_max_value = max_value_;
	if (sample_count_ < EnvelopeScaleFactor) {
		it = begin_sample_iteration(0);
		for (uint64_t i = 0; i < sample_count_; i++) {
			const float sample = *get_iterator_value_ptr(it);
			if (sample < min_value_)
				min_value_ = sample;
			if (sample > max_value_)
				max_value_ = sample;
			continue_sample_iteration(it, 1);
		}
		end_sample_iteration(it);
	}

	// Break off if there are no new samples to compute
	if (e0.length == prev_length)
		return;

	reallocate_envelope(e0);

	dest_ptr = e0.samples + prev_length;

	// Iterate through the samples to populate the first level mipmap
	uint64_t start_sample = prev_length * EnvelopeScaleFactor;
	uint64_t end_sample = e0.length * EnvelopeScaleFactor;

	it = begin_sample_iteration(start_sample);
	for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
		const float* samples = get_iterator_value_ptr(it);

		const EnvelopeSample sub_sample = {
			*min_element(samples, samples + EnvelopeScaleFactor),
			*max_element(samples, samples + EnvelopeScaleFactor),
		};

		if (sub_sample.min < min_value_)
			min_value_ = sub_sample.min;
		if (sub_sample.max > max_value_)
			max_value_ = sub_sample.max;

		continue_sample_iteration(it, EnvelopeScaleFactor);
		*dest_ptr++ = sub_sample;
	}
	end_sample_iteration(it);

	// Compute higher level mipmaps
	for (unsigned int level = 1; level < ScaleStepCount; level++) {
		Envelope &e = envelope_levels_[level];
		const Envelope &el = envelope_levels_[level - 1];

		// Expand the data buffer to fit the new samples
		prev_length = e.length;
		e.length = el.length / EnvelopeScaleFactor;

		// Break off if there are no more samples to be computed
		if (e.length == prev_length)
			break;

		reallocate_envelope(e);

		// Subsample the lower level
		const EnvelopeSample *src_ptr =
			el.samples + prev_length * EnvelopeScaleFactor;
		const EnvelopeSample *const end_dest_ptr = e.samples + e.length;

		for (dest_ptr = e.samples + prev_length;
				dest_ptr < end_dest_ptr; dest_ptr++) {
			const EnvelopeSample *const end_src_ptr =
				src_ptr + EnvelopeScaleFactor;

			EnvelopeSample sub_sample = *src_ptr++;
			while (src_ptr < end_src_ptr) {
				sub_sample.min = min(sub_sample.min, src_ptr->min);;
				sub_sample.max = max(sub_sample.max, src_ptr->max);
				src_ptr++;
			}

			*dest_ptr = sub_sample;
		}
	}

	// Notify if the min or max value changed
	if ((old_min_value != min_value_) || (old_max_value != max_value_))
		owner_.min_max_changed(min_value_, max_value_);
}

} // namespace data
} // namespace pv