#include <extdef.h>
-#include <assert.h>
-#include <string.h>
-#include <stdlib.h>
+#include <cassert>
#include <cmath>
+#include <cstdlib>
+#include <cstring>
#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;
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(uint64_t samplerate) :
- Segment(samplerate, sizeof(float))
+const float AnalogSegment::LogEnvelopeScaleFactor = logf(EnvelopeScaleFactor);
+const uint64_t AnalogSegment::EnvelopeDataUnit = 64 * 1024; // bytes
+
+AnalogSegment::AnalogSegment(Analog& owner, uint64_t samplerate) :
+ Segment(samplerate, sizeof(float)),
+ owner_(owner),
+ min_value_(0),
+ max_value_(0)
{
lock_guard<recursive_mutex> lock(mutex_);
memset(envelope_levels_, 0, sizeof(envelope_levels_));
lock_guard<recursive_mutex> lock(mutex_);
- for (uint32_t i=0; i < sample_count; i++) {
+ uint64_t prev_sample_count = sample_count_;
+
+ for (uint32_t i = 0; i < sample_count; i++) {
append_single_sample((void*)data);
data += stride;
}
// 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);
}
const float* AnalogSegment::get_samples(
return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
}
-SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const
+const pair<float, float> AnalogSegment::get_min_max() const
+{
+ return make_pair(min_value_, max_value_);
+}
+
+SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start)
{
return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
}
-void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const
+void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase)
{
Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
}
-void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const
+void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it)
{
Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
}
prev_length = e0.length;
e0.length = sample_count_ / EnvelopeScaleFactor;
+ // Calculate min/max values in case we have too few samples for an envelope
+ if (sample_count_ < EnvelopeScaleFactor) {
+ it = begin_raw_sample_iteration(0);
+ for (uint64_t i = 0; i < sample_count_; i++) {
+ const float sample = *((float*)it->value);
+ if (sample < min_value_)
+ min_value_ = sample;
+ if (sample > max_value_)
+ max_value_ = sample;
+ continue_raw_sample_iteration(it, 1);
+ }
+ end_raw_sample_iteration(it);
+ }
+
// Break off if there are no new samples to compute
if (e0.length == prev_length)
return;
// Iterate through the samples to populate the first level mipmap
uint64_t start_sample = prev_length * EnvelopeScaleFactor;
- uint64_t end_sample = e0.length * EnvelopeScaleFactor;
+ uint64_t end_sample = e0.length * EnvelopeScaleFactor;
it = begin_raw_sample_iteration(start_sample);
for (uint64_t i = start_sample; i < end_sample; i += 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_raw_sample_iteration(it, EnvelopeScaleFactor);
*dest_ptr++ = sub_sample;
}
// Compute higher level mipmaps
for (unsigned int level = 1; level < ScaleStepCount; level++) {
Envelope &e = envelope_levels_[level];
- const Envelope &el = envelope_levels_[level-1];
+ const Envelope &el = envelope_levels_[level - 1];
// Expand the data buffer to fit the new samples
prev_length = e.length;
EnvelopeSample sub_sample = *src_ptr++;
while (src_ptr < end_src_ptr) {
- sub_sample.min = min(sub_sample.min, src_ptr->min);
+ sub_sample.min = min(sub_sample.min, src_ptr->min);;
sub_sample.max = max(sub_sample.max, src_ptr->max);
src_ptr++;
}