From: Soeren Apel Date: Sun, 16 Aug 2020 19:09:11 +0000 (+0200) Subject: Implement single sample value getters X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=27ff2925665ce9c916d68c637827095e27b99ee7;p=pulseview.git Implement single sample value getters --- diff --git a/pv/data/analogsegment.cpp b/pv/data/analogsegment.cpp index e6f9626c..57901309 100644 --- a/pv/data/analogsegment.cpp +++ b/pv/data/analogsegment.cpp @@ -96,6 +96,16 @@ void AnalogSegment::append_interleaved_samples(const float *data, prev_sample_count + 1, prev_sample_count + 1); } +float AnalogSegment::get_sample(int64_t sample_num) const +{ + assert(sample_num >= 0); + assert(sample_num <= (int64_t)sample_count_); + + lock_guard lock(mutex_); // Because of free_unused_memory() + + return *((const float*)get_raw_sample(sample_num)); +} + void AnalogSegment::get_samples(int64_t start_sample, int64_t end_sample, float* dest) const { diff --git a/pv/data/analogsegment.hpp b/pv/data/analogsegment.hpp index 846837ce..63317043 100644 --- a/pv/data/analogsegment.hpp +++ b/pv/data/analogsegment.hpp @@ -81,6 +81,7 @@ public: void append_interleaved_samples(const float *data, size_t sample_count, size_t stride); + float get_sample(int64_t sample_num) const; void get_samples(int64_t start_sample, int64_t end_sample, float* dest) const; const pair get_min_max() const; diff --git a/pv/data/segment.cpp b/pv/data/segment.cpp index 2bac0062..1dfdefd4 100644 --- a/pv/data/segment.cpp +++ b/pv/data/segment.cpp @@ -211,6 +211,20 @@ void Segment::append_samples(void* data, uint64_t samples) sample_count_ += samples; } +const uint8_t* Segment::get_raw_sample(uint64_t sample_num) const +{ + assert(sample_num <= sample_count_); + + uint64_t chunk_num = (sample_num * unit_size_) / chunk_size_; + uint64_t chunk_offs = (sample_num * unit_size_) % chunk_size_; + + lock_guard lock(mutex_); // Because of free_unused_memory() + + const uint8_t* chunk = data_chunks_[chunk_num]; + + return chunk + chunk_offs; +} + void Segment::get_raw_samples(uint64_t start, uint64_t count, uint8_t* dest) const { @@ -219,13 +233,13 @@ void Segment::get_raw_samples(uint64_t start, uint64_t count, assert(count > 0); assert(dest != nullptr); - lock_guard lock(mutex_); - uint8_t* dest_ptr = dest; uint64_t chunk_num = (start * unit_size_) / chunk_size_; uint64_t chunk_offs = (start * unit_size_) % chunk_size_; + lock_guard lock(mutex_); // Because of free_unused_memory() + while (count > 0) { const uint8_t* chunk = data_chunks_[chunk_num]; diff --git a/pv/data/segment.hpp b/pv/data/segment.hpp index 8465949c..9bdc17dd 100644 --- a/pv/data/segment.hpp +++ b/pv/data/segment.hpp @@ -25,12 +25,12 @@ #include #include -#include +#include #include using std::recursive_mutex; -using std::vector; +using std::deque; namespace SegmentTest { struct SmallSize8Single; @@ -84,6 +84,7 @@ public: protected: void append_single_sample(void *data); void append_samples(void *data, uint64_t samples); + const uint8_t* get_raw_sample(uint64_t sample_num) const; void get_raw_samples(uint64_t start, uint64_t count, uint8_t *dest) const; SegmentDataIterator* begin_sample_iteration(uint64_t start); @@ -94,7 +95,7 @@ protected: uint32_t segment_id_; mutable recursive_mutex mutex_; - vector data_chunks_; + deque data_chunks_; uint8_t* current_chunk_; uint64_t used_samples_, unused_samples_; uint64_t sample_count_; diff --git a/pv/data/signalbase.cpp b/pv/data/signalbase.cpp index 7d420ac7..fb687244 100644 --- a/pv/data/signalbase.cpp +++ b/pv/data/signalbase.cpp @@ -574,7 +574,9 @@ void SignalBase::convert_single_segment_range(AnalogSegment *asegment, // Create sigrok::Analog instance float *asamples = new float[ConversionBlockSize]; + assert(asamples); uint8_t *lsamples = new uint8_t[ConversionBlockSize]; + assert(lsamples); vector > channels; if (channel_)