From ada836c989c04ede49670672999060c21ff737aa Mon Sep 17 00:00:00 2001 From: Kapandaria Date: Fri, 8 Nov 2024 23:15:34 +0200 Subject: [PATCH] Fix crash on Xpressive when using `integrate` function (#7499) * Fixed a bug in the integrate function, that was caused by warning fixes session. * Xpressive - fixed code style issues. --- plugins/Xpressive/ExprSynth.cpp | 43 ++++++++++++++++++++++----------- plugins/Xpressive/Xpressive.cpp | 20 +++++++++------ 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/plugins/Xpressive/ExprSynth.cpp b/plugins/Xpressive/ExprSynth.cpp index c48b94ec8dc..e6783211d8e 100644 --- a/plugins/Xpressive/ExprSynth.cpp +++ b/plugins/Xpressive/ExprSynth.cpp @@ -83,9 +83,10 @@ struct IntegrateFunction : public exprtk::ifunction IntegrateFunction(const unsigned int* frame, unsigned int sample_rate,unsigned int max_counters) : exprtk::ifunction(1), + m_firstValue(0), m_frame(frame), - m_sample_rate(sample_rate), - m_max_counters(max_counters), + m_sampleRate(sample_rate), + m_maxCounters(max_counters), m_nCounters(0), m_nCountersCalls(0), m_cc(0) @@ -96,15 +97,26 @@ struct IntegrateFunction : public exprtk::ifunction inline T operator()(const T& x) override { - if (*m_frame == 0) + if (m_frame) { - ++m_nCountersCalls; - if (m_nCountersCalls > m_max_counters) + if (m_nCountersCalls == 0) + { + m_firstValue = *m_frame; + } + if (m_firstValue == *m_frame) { - return 0; + ++m_nCountersCalls; + if (m_nCountersCalls > m_maxCounters) + { + return 0; + } + m_cc = m_nCounters; + ++m_nCounters; + } + else // we moved to the next frame + { + m_frame = 0; // this will indicate that we are no longer in init phase. } - m_cc = m_nCounters; - ++m_nCounters; } T res = 0; @@ -114,13 +126,16 @@ struct IntegrateFunction : public exprtk::ifunction m_counters[m_cc] += x; } m_cc = (m_cc + 1) % m_nCountersCalls; - return res / m_sample_rate; - } - - const unsigned int* const m_frame; - const unsigned int m_sample_rate; - const unsigned int m_max_counters; + return res / m_sampleRate; + } + unsigned int m_firstValue; + const unsigned int* m_frame; + const unsigned int m_sampleRate; + // number of counters allocated + const unsigned int m_maxCounters; + // number of integrate instances that has counters allocated unsigned int m_nCounters; + // real number of integrate instances unsigned int m_nCountersCalls; unsigned int m_cc; double *m_counters; diff --git a/plugins/Xpressive/Xpressive.cpp b/plugins/Xpressive/Xpressive.cpp index 23a76b22820..5ee7dcf8d89 100644 --- a/plugins/Xpressive/Xpressive.cpp +++ b/plugins/Xpressive/Xpressive.cpp @@ -553,7 +553,7 @@ void XpressiveView::expressionChanged() { ExprFront expr(text.constData(),sample_rate); float t=0; const float f=10,key=5,v=0.5; - unsigned int i; + unsigned int frame_counter = 0; expr.add_variable("t", t); if (m_output_expr) @@ -572,20 +572,24 @@ void XpressiveView::expressionChanged() { expr.add_cyclic_vector("W2",e->graphW2().samples(),e->graphW2().length()); expr.add_cyclic_vector("W3",e->graphW3().samples(),e->graphW3().length()); } - expr.setIntegrate(&i,sample_rate); + expr.setIntegrate(&frame_counter,sample_rate); expr.add_constant("srate",sample_rate); const bool parse_ok=expr.compile(); if (parse_ok) { e->exprValid().setValue(0); - const auto length = static_cast(m_raw_graph->length()); + const unsigned int length = static_cast(m_raw_graph->length()); auto const samples = new float[length]; - for (auto i = std::size_t{0}; i < length; i++) { - t = i / (float) length; - samples[i] = expr.evaluate(); - if (std::isinf(samples[i]) != 0 || std::isnan(samples[i]) != 0) - samples[i] = 0; + // frame_counter's reference is used in the integrate function. + for (frame_counter = 0; frame_counter < length; ++frame_counter) + { + t = frame_counter / (float) length; + samples[frame_counter] = expr.evaluate(); + if (std::isinf(samples[frame_counter]) != 0 || std::isnan(samples[frame_counter]) != 0) + { + samples[frame_counter] = 0; + } } m_raw_graph->setSamples(samples); delete[] samples;