Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on Xpressive when using integrate function #7499

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions plugins/Xpressive/ExprSynth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct IntegrateFunction : public exprtk::ifunction<T>

IntegrateFunction(const unsigned int* frame, unsigned int sample_rate,unsigned int max_counters) :
exprtk::ifunction<T>(1),
m_first_value(0),
m_frame(frame),
m_sample_rate(sample_rate),
m_max_counters(max_counters),
Expand All @@ -96,15 +97,26 @@ struct IntegrateFunction : public exprtk::ifunction<T>

inline T operator()(const T& x) override
{
if (*m_frame == 0)
if (m_frame)
sakertooth marked this conversation as resolved.
Show resolved Hide resolved
{
++m_nCountersCalls;
if (m_nCountersCalls > m_max_counters)
{
return 0;
}
m_cc = m_nCounters;
++m_nCounters;
if (m_nCountersCalls == 0)
{
m_first_value = *m_frame;
}
if (m_first_value == *m_frame)
{
++m_nCountersCalls;
if (m_nCountersCalls > m_max_counters)
{
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.
}
}

T res = 0;
Expand All @@ -116,12 +128,15 @@ struct IntegrateFunction : public exprtk::ifunction<T>
m_cc = (m_cc + 1) % m_nCountersCalls;
return res / m_sample_rate;
}

const unsigned int* const m_frame;
unsigned int m_first_value;
const unsigned int* m_frame;
sakertooth marked this conversation as resolved.
Show resolved Hide resolved
const unsigned int m_sample_rate;
const unsigned int m_max_counters;
unsigned int m_nCounters;
unsigned int m_nCountersCalls;
// number of counters allocated
const unsigned int m_max_counters;
// 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;
};
Expand Down
17 changes: 9 additions & 8 deletions plugins/Xpressive/Xpressive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
sakertooth marked this conversation as resolved.
Show resolved Hide resolved
expr.add_variable("t", t);

if (m_output_expr)
Expand All @@ -572,20 +572,21 @@ 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);
sakertooth marked this conversation as resolved.
Show resolved Hide resolved
expr.add_constant("srate",sample_rate);

const bool parse_ok=expr.compile();

if (parse_ok) {
e->exprValid().setValue(0);
const auto length = static_cast<std::size_t>(m_raw_graph->length());
const unsigned int length = static_cast<unsigned int>(m_raw_graph->length());
sakertooth marked this conversation as resolved.
Show resolved Hide resolved
sakertooth marked this conversation as resolved.
Show resolved Hide resolved
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) {
sakertooth marked this conversation as resolved.
Show resolved Hide resolved
t = frame_counter / (float) length;
Rossmaxx marked this conversation as resolved.
Show resolved Hide resolved
samples[frame_counter] = expr.evaluate();
if (std::isinf(samples[frame_counter]) != 0 || std::isnan(samples[frame_counter]) != 0)
samples[frame_counter] = 0;
sakertooth marked this conversation as resolved.
Show resolved Hide resolved
}
m_raw_graph->setSamples(samples);
delete[] samples;
Expand Down
Loading