Klang C++
Language Reference (draft)
Loading...
Searching...
No Matches
klang::Function< Args >

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 ()
 
unsigned int args () const
 
std::tuple< Rest... > tail (const std::tuple< First, Rest... > &t) const
 
void input () override
 
virtual const signalinput () const
 
virtual void input (const signal &source)
 
First first (First first, Rest...)
 
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::Graphoperator>> (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 signaloutput () const
 
signal operator+ (TYPE &other)
 
signal operator* (TYPE &other)
 
signal operator- (TYPE &other)
 
signal operator/ (TYPE &other)
 

Public Attributes

std::function< float(Args...)> function
 
std::tuple< Args... > inputs
 
signal in
 
signal out
 

Static Public Attributes

static constexpr unsigned int ARGS
 

Detailed Description

template<typename... Args>
struct klang::Function< Args >

Definition at line 2028 of file klang.h.

Constructor & Destructor Documentation

◆ Function() [1/2]

template<typename... Args>
klang::Function< Args >::Function ( )
inline

Definition at line 2034 of file klang.h.

2034: Generic::Function<signal, Args...>(nullptr) { }
Function() -> Function< signal, Args... >
signals< 2 > signal
Stereo audio signal.
Definition klang.h:3308

◆ Function() [2/2]

template<typename... Args>
klang::Function< Args >::Function ( std::function< float(Args...)> function)
inline

Definition at line 2036 of file klang.h.

2036: Generic::Function<signal, Args...>(function) { }
std::function< float(Args...)> function
Definition klang.h:1518

Member Function Documentation

◆ args()

unsigned int klang::Generic::Function< signal, Args >::args ( ) const
inlineinherited

Definition at line 1521 of file klang.h.

1521{ return ARGS; }
static constexpr unsigned int ARGS
Definition klang.h:1520

◆ evaluate()

signal klang::Generic::Function< signal, Args >::evaluate ( ) const
inlineinherited

Definition at line 1608 of file klang.h.

1608 {
1609 if (!function)
1610 return 0.f;
1611 if constexpr (ARGS > 1)
1612 return std::apply(function, inputs);
1613 else
1614 return function(in);
1615 }

◆ first()

First klang::Generic::Function< signal, Args >::first ( First first,
Rest...  )
inlineinherited

Definition at line 1550 of file klang.h.

1550{ return first; }
First first(First first, Rest...)
Definition klang.h:1550

◆ input() [1/3]

void klang::Generic::Function< signal, Args >::input ( )
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Input< signal >.

Definition at line 1544 of file klang.h.

1544 {
1545 std::get<0>(inputs) = in.value;
1546 }
float value
Definition klang.h:363

◆ input() [2/3]

virtual const signal & klang::Generic::Input< signal >::input ( ) const
inlinevirtualinherited

Reimplemented in klang::Generic::Graph< SIZE >::Series.

Definition at line 1343 of file klang.h.

1343{ return in; }

◆ input() [3/3]

virtual void klang::Generic::Input< signal >::input ( const signal & source)
inlinevirtualinherited

Definition at line 1347 of file klang.h.

1347{ in = source; input(); }
virtual const signal & input() const
Definition klang.h:1343

◆ operator const signal &() [1/2]

klang::Generic::Modifier< signal >::operator const signal & ( ) const
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Output< signal >.

Definition at line 1450 of file klang.h.

1450{ return out; } // return last output

◆ operator const signal &() [2/2]

klang::Generic::Modifier< signal >::operator const signal & ( )
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Output< signal >.

Definition at line 1449 of file klang.h.

1449{ process(); return out; } // return processed output
virtual void process() override
Definition klang.h:1453

◆ operator param()

klang::Generic::Function< signal, Args >::operator param ( )
inlineinherited

Definition at line 1488 of file klang.h.

1488 {
1489 if(function)
1490 return evaluate();
1491 return 2.f;
1492 }

◆ operator signal()

klang::Generic::Function< signal, Args >::operator signal ( )
inlineinherited

Definition at line 1482 of file klang.h.

1482 {
1483 if (function)
1484 return evaluate();
1485 process();
1486 return out;
1487 };

◆ operator()() [1/3]

Function< signal, Args... > & klang::Generic::Function< signal, Args >::operator() ( const FuncArgs &... args)
inlineinherited

Definition at line 1554 of file klang.h.

1554 {
1555 if constexpr (ARGS > 1 && sizeof...(FuncArgs) == 1){
1556 in = first(args...);
1557 std::get<0>(inputs) = in.value;
1558 return *this;
1559 } else if constexpr (ARGS == sizeof...(FuncArgs)){
1560 in = first(args...);
1561 inputs = std::tuple<Args...>(args...);
1562 return *this;
1563 } else if constexpr (sizeof...(FuncArgs) == (ARGS - 1)){
1564 inputs = std::tuple<Args...>(in.value, args...);
1565 return *this;
1566 } else {
1567 in = first(args...);
1568 std::get<0>(inputs) = in.value;
1569 return *this;
1570 }
1571 }

◆ operator()() [2/3]

const float klang::Generic::Function< signal, Args >::operator() ( const FuncArgs &... args) const
inlineinherited

Definition at line 1575 of file klang.h.

1575 {
1576 signal in = this->in;
1577 std::tuple<Args...> inputs = this->inputs;
1578
1579 // configure inputs
1580 if constexpr (ARGS > 1 && sizeof...(FuncArgs) == 1) {
1581 in = first(args...);
1582 std::get<0>(inputs) = in.value;
1583 } else if constexpr (ARGS == sizeof...(FuncArgs)) {
1584 in = first(args...);
1585 inputs = std::tuple<Args...>(args...);
1586 } else if constexpr (sizeof...(FuncArgs) == (ARGS - 1)) {
1587 inputs = std::tuple<Args...>(in.value, args...);
1588 } else {
1589 in = first(args...);
1590 std::get<0>(inputs) = in.value;
1591 }
1592
1593 // return outputs
1594 if constexpr (ARGS > 1)
1595 return std::apply(function, inputs);
1596 else
1597 return function(in);
1598 }

◆ operator()() [3/3]

Modifier< signal > & klang::Generic::Modifier< signal >::operator() ( params... p)
inlineinherited

Definition at line 1457 of file klang.h.

1457 {
1458 set(p...); return *this;
1459 }

◆ operator*()

signal klang::Generic::Output< signal >::operator* ( TYPE & other)
inlineinherited

Definition at line 1372 of file klang.h.

1372{ process(); return out * (other); }

◆ operator+()

signal klang::Generic::Output< signal >::operator+ ( TYPE & other)
inlineinherited

Definition at line 1371 of file klang.h.

1371{ process(); return out + (other); }

◆ operator-()

signal klang::Generic::Output< signal >::operator- ( TYPE & other)
inlineinherited

Definition at line 1373 of file klang.h.

1373{ process(); return out - (other); }

◆ operator/()

signal klang::Generic::Output< signal >::operator/ ( TYPE & other)
inlineinherited

Definition at line 1374 of file klang.h.

1374{ process(); return out / (other); }

◆ operator<<()

virtual void klang::Generic::Input< signal >::operator<< ( const signal & source)
inlinevirtualinherited

Definition at line 1346 of file klang.h.

1346{ in = source; input(); }

◆ operator>>() [1/3]

Graph & klang::Generic::Function< signal, Args >::operator>> ( klang::Graph & graph)
inlineinherited

Definition at line 1621 of file klang.h.

2137 {
2138 graph.plot(*this);
2139 return graph;
2140 }
static THREAD_LOCAL GraphPtr graph
Definition klang.h:2122

◆ operator>>() [2/3]

GraphPtr & klang::Generic::Function< signal, Args >::operator>> ( klang::GraphPtr & graph)
inlineinherited

Definition at line 1622 of file klang.h.

2125 {
2126 graph.plot(*this);
2127 return graph;
2128 }

◆ operator>>() [3/3]

TYPE & klang::Generic::Output< signal >::operator>> ( TYPE & destination)
inlineinherited

Definition at line 1364 of file klang.h.

1364{ process(); return destination = out; }

◆ output()

virtual const signal & klang::Generic::Output< signal >::output ( ) const
inlinevirtualinherited

Definition at line 1360 of file klang.h.

1360{ return out; }

◆ process()

virtual void klang::Generic::Function< signal, Args >::process ( )
inlineoverridevirtualinherited

Reimplemented from klang::Generic::Modifier< signal >.

Definition at line 1617 of file klang.h.

1617 {
1618 out = evaluate();
1619 }

◆ tail()

std::tuple< Rest... > klang::Generic::Function< signal, Args >::tail ( const std::tuple< First, Rest... > & t) const
inlineinherited

Definition at line 1527 of file klang.h.

1527 {
1528 return std::apply( [](const First&, const Rest&... rest) { return std::make_tuple(rest...); },t);
1529 }

◆ with()

Function< signal, Args... > & klang::Generic::Function< signal, Args >::with ( FuncArgs... args)
inlineinherited

Definition at line 1601 of file klang.h.

1601 {
1602 static_assert(sizeof...(FuncArgs) == (ARGS - 1), "with() arguments must have all but first argument.");
1603 inputs = std::tuple<Args...>(in.value, args...);
1604
1605 return *this;
1606 }

Member Data Documentation

◆ ARGS

unsigned int klang::Generic::Function< signal, Args >::ARGS
staticconstexprinherited

Definition at line 1520 of file klang.h.

◆ function

std::function<float(Args...)> klang::Generic::Function< signal, Args >::function
inherited

Definition at line 1518 of file klang.h.

◆ in

Definition at line 1340 of file klang.h.

1340{ 0.f };

◆ inputs

std::tuple<Args...> klang::Generic::Function< signal, Args >::inputs
inherited

Definition at line 1523 of file klang.h.

◆ out

Definition at line 1357 of file klang.h.

1357{ 0.f };