gpt4 book ai didi

c++ - 如何以每秒 44100 次的速度计算用户定义的表达式

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:12:48 27 4
gpt4 key购买 nike

我正在使用 C++ 开发一个 VST 插件。该插件将允许用户输入一个数学表达式,然后每秒运行 44100 次以产生声音。我对这样的实时内容以及解释用户输入的表达式不熟悉。

问题是我找不到一种方法来评估可以快速运行的用户定义函数。我最好的尝试是在输入时将用户输入的表达式转换为 RPN,然后使用函数计算 RPN 表达式以生成音频。我实现了 RPN 评估函数并硬编码了一个 RPN 表达式来测试它。虽然它似乎评估正确,但似乎执行得不够快。

除了几个 RPN 表达式之外,这是我的评估函数:

#include <string>
#include <stack>
#include <deque>


/*
* an RPN expression is stored as a deque of strings
*
* each string is either an operator, a number, or the single variable t
*
* the deque is read from front to back
*/

std::deque<std::string> simpleCase, complexCase;

//simple expression, just the variable t
simpleCase.push_back("t");

//more complex expression, t*(42&(t>>11))
complexCase.push_back("t");
complexCase.push_back("42");
complexCase.push_back("t");
complexCase.push_back("11");
complexCase.push_back(">>");
complexCase.push_back("&");
complexCase.push_back("*");

/*
* The evalRPN function takes an RPN deque, plugs in a supplied t,
* and evaluates it.
*
* The idea is that t increases continually, and that the integer overflow
* causes the output to oscillate between 0 and 255.
*
* t is a double, but I convert it to a uint32_t.
*
* Allowed operators: bitwise logic (&, |, ^), bitshifts (<<, >>),
* and math (+, -, *, /, %)
*
* Allowed vars: t
*
* Supplied numbers are converted from string to char arrays then to an int
*
* This also assumes the RPN is not ill-formatted.
*/
uint8_t evalRPN(std::deque<std::string> rpnExpr, double tVal)
{
std::stack<uint8_t> numberStack;
std::string token;

while(rpnExpr.size() > 0)
{

token = rpnExpr.front();
rpnExpr.pop_front();

if(token.find_first_not_of("0123456789") == std::string::npos)
{
//if token is a number
numberStack.push((uint8_t)atoi(token.c_str()));
}
else if (token == "t")
{
numberStack.push((uint8_t)tVal);
}
else
{
uint8_t last = numberStack.top();
numberStack.pop();
uint8_t first = numberStack.top();
numberStack.pop();

if(token == "^")
{
numberStack.push(first ^ last);
}
else if (token == "&")
{
numberStack.push(first & last);
}
else if (token == "|")
{
numberStack.push(first | last);
}
else if (token == "<<")
{
numberStack.push(first >> last);
}
else if (token == ">>")
{
numberStack.push(first >> last);
}
else if (token == "+")
{
numberStack.push(first + last);
}
else if (token == "-")
{
numberStack.push(first - last);
}
else if (token == "*")
{
numberStack.push(first * last);
}
else if (token == "/")
{
numberStack.push(first / last);
}
else if (token == "%")
{
numberStack.push(first % last);
}
}
}

//assume one left in numberStack
return(numberStack.top());
}

我可以在我的 RPN 处理中进行任何优化以使其运行得足够快吗?或者是否有另一种更有效的处理 RPN 计算的方法?

此外,是否有另一种与 C++ 兼容的方法,用于获取用户输入的表示标准数学表达式的字符串,然后以足够快的速度运行该表达式以在 1/44100 秒内完成?

最佳答案

这是一个很好的问题。

将你的表达式编译成 RPN 是一个好的开始,事实上在我看来你的代码应该能够每秒执行超过 88K 的表达式,除非它们很长。

但是,您当然可以在没有太多麻烦的情况下做得更好。

我会做一个这样的界面:

class Expression
{
public:
virtual uint32_t eval(uint32_t tVal) = 0;
};

然后您将表达式编译为该接口(interface)的实现。

你可以有常量的实现:

class ConstExpression : public Expression
{
private:
uint32_t m_constVal;

public:
// ...

uint32_t eval(uint32_t tVal)
{
return m_constVal;
}
};

...t

引用的实现
class RefExpression : public Expression
{
public:
// ...

uint32_t eval(uint32_t tVal)
{
return m_tVal;
}
};

...和二元运算符的实现

class AddExpression : public Expression
{
private:
auto_ptr<Expression> m_left;
auto_ptr<Expression> m_right;

public:
// ...

uint32_t eval(uint32_t tVal)
{
return m_left->eval(tVal) + m_right->eval(tVal);
}
};

...也许您想做一些模板魔术来避免编写这么多操作符类的代码。

无论如何,在将你的表达式编译成一个表达式之后,你可以像 theExpression->eval(t) 一样简单地计算它,并且所有代码都以相当有效的方式执行,没有解析、字符串比较、堆栈操作等。

关于c++ - 如何以每秒 44100 次的速度计算用户定义的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38256985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com