aboutsummaryrefslogtreecommitdiffstats
path: root/source/device_formula.cpp
blob: 5c1ca16e162ea64da81587936256018055995399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "stmdsp.hpp"
#include "exprtk.hpp"

#include <algorithm>
#include <string_view>
#include <vector>

std::vector<stmdsp::dacsample_t> deviceGenLoadFormulaEval(const std::string_view formulaString)
{
    double x = 0;

    exprtk::symbol_table<double> symbol_table;
    exprtk::expression<double> expression;
    exprtk::parser<double> parser;

    symbol_table.add_variable("x", x);
    symbol_table.add_constants();
    expression.register_symbol_table(symbol_table);
    parser.compile(std::string(formulaString), expression);

    std::vector<stmdsp::dacsample_t> samples (stmdsp::SAMPLES_MAX);

    std::generate(samples.begin(), samples.end(),
                  [&] { ++x; return static_cast<stmdsp::dacsample_t>(expression.value()); });

    return samples;
}