![]() |
Klang C++ 0.7.15
Language Reference (draft)
|
Language Guide / Functions
klang::Function
Wraps a callable as a Klang signal-flow stage.
Use Function<Args...> when a C++ calculation should sit in a signal chain. The template arguments are the callable’s input types, in order. The callable returns a value convertible to float, and the stage exposes its result as a signal. A function pointer can also supply the template arguments by deduction.
This effect constructs a Function<float> from a static C++ function with one input. in >> shape >> out supplies the first argument, evaluates the callable and routes the result. Keep the wrapper as a member and reuse it as samples arrive.
struct Shaper : Effect {
static float hardclip(float x) {
return clamp(x, -1.0, 1.0);
}
Function<float> shape;
Shaper() : shape(hardclip) {
}
void process() {
in >> shape >> out;
}
};For a function such as shape(x, drive), the signal arriving from the left supplies x. Pass drive with shape(drive), or use shape.with(drive) to make the binding explicit. Both forms configure the same stage. Supply one value for each argument after the first.
struct DrivenShape : Effect {
static float softclip(float x, float drive) {
return tanh(x * drive);
}
Function<float, float> shape;
DrivenShape() : shape(softclip) {
}
void process() {
in >> shape(4) >> out;
}
};Use this expression in process() for the equivalent explicit binding.
in >> shape.with(4) >> out;To evaluate a mutable wrapper directly, supply all the arguments and call evaluate() to get the result as a signal. A const wrapper can evaluate a full argument list directly and return a float.
const signal sample = shape(0.25, 4).evaluate();Send a configured wrapper to Graph to see its response. The graph sweeps the first argument across its horizontal axis and uses the arguments supplied through with() for the rest.
Applies a function to a signal (input-output) More...
#include <klang.h>
Inheritance diagram for klang::Function< Args >:Public Member Functions | |
| Function () | |
| Function (std::function< float(Args...)> function) | |
| operator signal () | |
| operator param () | |
| operator float () | |
| unsigned int | args () const |
| std::tuple< Rest... > | tail (const std::tuple< First, Rest... > &t) const |
| void | input () override |
| virtual const signal & | input () const |
| virtual void | input (const signal &source) |
| First & | first (First &first, Rest...) |
| const First & | first (const First &first, Rest...) const |
| Function< signal, Args... > & | operator() (const FuncArgs &... args) |
| const float | operator() (const FuncArgs &... args) const |
| Modifier< signal > & | operator() (params &&... p) |
| Function< signal, Args... > & | with (FuncArgs... args) |
| signal | evaluate () const |
| virtual void | process () override |
| klang::Graph & | operator>> (klang::Graph &graph) |
| klang::GraphPtr & | operator>> (klang::GraphPtr &graph) |
| TYPE & | operator>> (TYPE &destination) |
| operator const signal & () override | |
| operator const signal & () const override | |
| virtual void | operator<< (const signal &source) |
| virtual const signal & | output () const |
| signal | operator+ (TYPE &other) |
| signal | operator* (TYPE &other) |
| signal | operator- (TYPE &other) |
| signal | operator/ (TYPE &other) |
| void | reset () |
Public Attributes | |
| std::function< float(Args...)> | function |
| std::tuple< Args... > | inputs |
| signal | in |
| signal | out |
Static Public Attributes | |
| static constexpr unsigned int | ARGS |
|
inline |
|
inline |
Definition at line 3743 of file klang.h.
|
inlineinherited |
Definition at line 3176 of file klang.h.
|
inlineinherited |
Definition at line 3289 of file klang.h.
|
inlineinherited |
Definition at line 3210 of file klang.h.
|
inlineinherited |
|
inlineoverridevirtualinherited |
Reimplemented from klang::Generic::Input< signal >.
Definition at line 3200 of file klang.h.
|
inlinevirtualinherited |
|
inlinevirtualinherited |
|
inlineoverridevirtualinherited |
|
inlineoverridevirtualinherited |
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
Definition at line 3131 of file klang.h.
|
inlineinherited |
Definition at line 3214 of file klang.h.
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
inlinevirtualinherited |
Reimplemented in klang::Debug.
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
inlinevirtualinherited |
|
inlineoverridevirtualinherited |
Reimplemented from klang::Generic::Modifier< signal >.
|
inlineinherited |
|
inlineinherited |
|
inlineinherited |
|
staticconstexprinherited |
|
inherited |
|
inherited |
Definition at line 2944 of file klang.h.
Referenced by klang::Generic::Graph< SIZE >::Series::input(), klang::Generic::Graph< SIZE >::Series::operator!=(), and klang::Generic::Graph< SIZE >::Series::operator==().
|
inherited |
|
inherited |