Skip to content

Commit 7c28ef4

Browse files
cboulaytstenner
authored andcommitted
lsl_cpp - cache sample_rate to avoid expensive call to info() in push_chunk
1 parent 646a76b commit 7c28ef4

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

include/lsl_cpp.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ class stream_outlet {
416416
*/
417417
stream_outlet(const stream_info &info, int32_t chunk_size = 0, int32_t max_buffered = 360)
418418
: channel_count(info.channel_count()),
419+
sample_rate(info.nominal_srate()),
419420
obj(lsl_create_outlet(info.handle(), chunk_size, max_buffered)) {}
420421

421422
// ========================================
@@ -578,8 +579,8 @@ class stream_outlet {
578579
const std::vector<T> &samples, double timestamp = 0.0, bool pushthrough = true) {
579580
if (!samples.empty()) {
580581
if (timestamp == 0.0) timestamp = local_clock();
581-
if (info().nominal_srate() != IRREGULAR_RATE)
582-
timestamp = timestamp - (samples.size() - 1) / info().nominal_srate();
582+
if (sample_rate != IRREGULAR_RATE)
583+
timestamp = timestamp - (samples.size() - 1) / sample_rate;
583584
push_sample(samples[0], timestamp, pushthrough && samples.size() == 1);
584585
for (std::size_t k = 1; k < samples.size(); k++)
585586
push_sample(samples[k], DEDUCED_TIMESTAMP, pushthrough && k == samples.size() - 1);
@@ -618,8 +619,8 @@ class stream_outlet {
618619
const std::vector<T> &samples, double timestamp = 0.0, bool pushthrough = true) {
619620
if (!samples.empty()) {
620621
if (timestamp == 0.0) timestamp = local_clock();
621-
if (info().nominal_srate() != IRREGULAR_RATE)
622-
timestamp = timestamp - (samples.size() - 1) / info().nominal_srate();
622+
if (sample_rate != IRREGULAR_RATE)
623+
timestamp = timestamp - (samples.size() - 1) / sample_rate;
623624
push_numeric_struct(samples[0], timestamp, pushthrough && samples.size() == 1);
624625
for (std::size_t k = 1; k < samples.size(); k++)
625626
push_numeric_struct(
@@ -901,12 +902,13 @@ class stream_outlet {
901902

902903
#if LSL_CPP11
903904
/// stream_outlet move constructor
904-
stream_outlet(stream_outlet &&res) noexcept : channel_count(res.channel_count), obj(res.obj) {
905+
stream_outlet(stream_outlet &&res) noexcept : channel_count(res.channel_count), sample_rate(res.sample_rate), obj(res.obj) {
905906
res.obj = nullptr;
906907
}
907908

908909
stream_outlet &operator=(stream_outlet &&rhs) noexcept {
909910
channel_count = rhs.channel_count;
911+
sample_rate = rhs.sample_rate;
910912
obj = rhs.obj;
911913
rhs.obj = nullptr;
912914
return *this;
@@ -927,6 +929,7 @@ class stream_outlet {
927929
}
928930

929931
int32_t channel_count;
932+
double sample_rate;
930933
lsl_outlet obj;
931934
};
932935

0 commit comments

Comments
 (0)