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

Applies a function to a signal (input-output) More...

#include <klang.h>

+ Inheritance diagram for klang::Generic::Function< SIGNAL, Args >:

Public Member Functions

virtual ~Function ()
 
 operator SIGNAL ()
 
 operator param ()
 
unsigned int args () const
 
template<typename First , typename... Rest>
std::tuple< Rest... > tail (const std::tuple< First, Rest... > &t) const
 
template<typename FunctionPtr >
 Function ()
 
template<typename FunctionPtr >
 Function (FunctionPtr function)
 
template<typename FunctionPtr , typename... OtherArgs>
 Function (FunctionPtr function, OtherArgs... args)
 
void input () override
 
template<typename First , typename... Rest>
First first (First first, Rest...)
 
template<typename... FuncArgs>
Function< SIGNAL, Args... > & operator() (const FuncArgs &... args)
 
template<typename... FuncArgs>
const float operator() (const FuncArgs &... args) const
 
template<typename... FuncArgs>
Function< SIGNAL, Args... > & with (FuncArgs... args)
 
signal evaluate () const
 
virtual void process () override
 
klang::Graphoperator>> (klang::Graph &graph)
 
klang::GraphPtr & operator>> (klang::GraphPtr &graph)
 
 operator const SIGNAL & () override
 
 operator const SIGNAL & () const override
 
template<typename... params>
Modifier< SIGNAL > & operator() (params... p)
 
virtual const SIGNAL & input () const
 
virtual void input (const SIGNAL &source)
 
virtual void operator<< (const SIGNAL &source)
 
virtual const SIGNAL & output () const
 
template<typename TYPE >
TYPE & operator>> (TYPE &destination)
 
template<typename TYPE >
SIGNAL operator+ (TYPE &other)
 
template<typename TYPE >
SIGNAL operator* (TYPE &other)
 
template<typename TYPE >
SIGNAL operator- (TYPE &other)
 
template<typename TYPE >
SIGNAL operator/ (TYPE &other)
 

Public Attributes

std::function< float(Args...)> function
 
std::tuple< Args... > inputs
 
SIGNAL in = { 0.f }
 
SIGNAL out = { 0.f }
 

Static Public Attributes

static constexpr unsigned int ARGS = sizeof...(Args)
 

Detailed Description

template<typename SIGNAL, typename... Args>
struct klang::Generic::Function< SIGNAL, Args >

Definition at line 1476 of file klang.h.

Constructor & Destructor Documentation

◆ ~Function()

template<typename SIGNAL , typename... Args>
virtual klang::Generic::Function< SIGNAL, Args >::~Function ( )
inlinevirtual

Definition at line 1477 of file klang.h.

1477{ }

◆ Function() [1/3]

template<typename SIGNAL , typename... Args>
template<typename FunctionPtr >
klang::Generic::Function< SIGNAL, Args >::Function ( )
inline

Definition at line 1532 of file klang.h.

1532: function(nullptr) { }
std::function< float(Args...)> function
Definition klang.h:1518

◆ Function() [2/3]

template<typename SIGNAL , typename... Args>
template<typename FunctionPtr >
klang::Generic::Function< SIGNAL, Args >::Function ( FunctionPtr function)
inline

Definition at line 1535 of file klang.h.

1536 : function(std::forward<FunctionPtr>(function)) {}

◆ Function() [3/3]

template<typename SIGNAL , typename... Args>
template<typename FunctionPtr , typename... OtherArgs>
klang::Generic::Function< SIGNAL, Args >::Function ( FunctionPtr function,
OtherArgs... args )
inline

Definition at line 1539 of file klang.h.

1540 : function(std::forward<FunctionPtr>(function)) {
1541 with(args...);
1542 }
unsigned int args() const
Definition klang.h:1521
Function< SIGNAL, Args... > & with(FuncArgs... args)
Definition klang.h:1601

Member Function Documentation

◆ args()

template<typename SIGNAL , typename... Args>
unsigned int klang::Generic::Function< SIGNAL, Args >::args ( ) const
inline

Definition at line 1521 of file klang.h.

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

◆ evaluate()

template<typename SIGNAL , typename... Args>
signal klang::Generic::Function< SIGNAL, Args >::evaluate ( ) const
inline

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 }
std::tuple< Args... > inputs
Definition klang.h:1523

References klang::Generic::Function< SIGNAL, Args >::ARGS, and klang::Generic::Function< SIGNAL, Args >::inputs.

Referenced by klang::Generic::Function< SIGNAL, Args >::process().

◆ first()

template<typename SIGNAL , typename... Args>
template<typename First , typename... Rest>
First klang::Generic::Function< SIGNAL, Args >::first ( First first,
Rest...  )
inline

Definition at line 1550 of file klang.h.

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

◆ input() [1/3]

template<typename SIGNAL , typename... Args>
void klang::Generic::Function< SIGNAL, Args >::input ( )
inlineoverridevirtual

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

Definition at line 1544 of file klang.h.

1544 {
1545 std::get<0>(inputs) = in.value;
1546 }

References klang::Generic::Function< SIGNAL, Args >::inputs.

◆ input() [2/3]

template<typename SIGNAL >
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; }

References klang::Generic::Input< SIGNAL >::in.

◆ input() [3/3]

template<typename SIGNAL >
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

References klang::Generic::Input< SIGNAL >::in.

Referenced by klang::Effect::process(), and klang::Stereo::Effect::process().

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

template<typename SIGNAL >
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]

template<typename SIGNAL >
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

References klang::Generic::Modifier< SIGNAL >::process().

◆ operator param()

template<typename SIGNAL , typename... Args>
klang::Generic::Function< SIGNAL, Args >::operator param ( )
inline

Definition at line 1488 of file klang.h.

1488 {
1489 if(function)
1490 return evaluate();
1491 return 2.f;
1492 }
signal evaluate() const
Definition klang.h:1608

◆ operator SIGNAL()

template<typename SIGNAL , typename... Args>
klang::Generic::Function< SIGNAL, Args >::operator SIGNAL ( )
inline

Definition at line 1482 of file klang.h.

1482 {
1483 if (function)
1484 return evaluate();
1485 process();
1486 return out;
1487 };
virtual void process() override
Definition klang.h:1617

◆ operator()() [1/3]

template<typename SIGNAL , typename... Args>
template<typename... FuncArgs>
Function< SIGNAL, Args... > & klang::Generic::Function< SIGNAL, Args >::operator() ( const FuncArgs &... args)
inline

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 }

References klang::Generic::Function< SIGNAL, Args >::ARGS, and klang::Generic::Function< SIGNAL, Args >::inputs.

◆ operator()() [2/3]

template<typename SIGNAL , typename... Args>
template<typename... FuncArgs>
const float klang::Generic::Function< SIGNAL, Args >::operator() ( const FuncArgs &... args) const
inline

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 }
signals< 2 > signal
Stereo audio signal.
Definition klang.h:3308

◆ operator()() [3/3]

template<typename SIGNAL >
template<typename... params>
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*()

template<typename SIGNAL >
template<typename TYPE >
SIGNAL klang::Generic::Output< SIGNAL >::operator* ( TYPE & other)
inlineinherited

Definition at line 1372 of file klang.h.

1372{ process(); return out * (other); }
virtual void process()=0

◆ operator+()

template<typename SIGNAL >
template<typename TYPE >
SIGNAL klang::Generic::Output< SIGNAL >::operator+ ( TYPE & other)
inlineinherited

Definition at line 1371 of file klang.h.

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

◆ operator-()

template<typename SIGNAL >
template<typename TYPE >
SIGNAL klang::Generic::Output< SIGNAL >::operator- ( TYPE & other)
inlineinherited

Definition at line 1373 of file klang.h.

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

◆ operator/()

template<typename SIGNAL >
template<typename TYPE >
SIGNAL klang::Generic::Output< SIGNAL >::operator/ ( TYPE & other)
inlineinherited

Definition at line 1374 of file klang.h.

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

◆ operator<<()

template<typename SIGNAL >
virtual void klang::Generic::Input< SIGNAL >::operator<< ( const SIGNAL & source)
inlinevirtualinherited

Definition at line 1346 of file klang.h.

1346{ in = source; input(); }

References klang::Generic::Input< SIGNAL >::in.

◆ operator>>() [1/3]

template<typename SIGNAL , typename... Args>
Graph & klang::Generic::Function< SIGNAL, Args >::operator>> ( klang::Graph & graph)
inline

Definition at line 2137 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]

template<typename SIGNAL , typename... Args>
GraphPtr & klang::Generic::Function< SIGNAL, Args >::operator>> ( klang::GraphPtr & graph)
inline

Definition at line 2125 of file klang.h.

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

◆ operator>>() [3/3]

template<typename SIGNAL >
template<typename TYPE >
TYPE & klang::Generic::Output< SIGNAL >::operator>> ( TYPE & destination)
inlineinherited

Definition at line 1364 of file klang.h.

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

References klang::Generic::Output< SIGNAL >::out, and klang::Generic::Output< SIGNAL >::process().

◆ output()

template<typename SIGNAL >
virtual const SIGNAL & klang::Generic::Output< SIGNAL >::output ( ) const
inlinevirtualinherited

Definition at line 1360 of file klang.h.

1360{ return out; }

References klang::Generic::Output< SIGNAL >::out.

◆ process()

template<typename SIGNAL , typename... Args>
virtual void klang::Generic::Function< SIGNAL, Args >::process ( )
inlineoverridevirtual

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

Definition at line 1617 of file klang.h.

1617 {
1618 out = evaluate();
1619 }

References klang::Generic::Function< SIGNAL, Args >::evaluate().

◆ tail()

template<typename SIGNAL , typename... Args>
template<typename First , typename... Rest>
std::tuple< Rest... > klang::Generic::Function< SIGNAL, Args >::tail ( const std::tuple< First, Rest... > & t) const
inline

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()

template<typename SIGNAL , typename... Args>
template<typename... FuncArgs>
Function< SIGNAL, Args... > & klang::Generic::Function< SIGNAL, Args >::with ( FuncArgs... args)
inline

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

template<typename SIGNAL , typename... Args>
unsigned int klang::Generic::Function< SIGNAL, Args >::ARGS = sizeof...(Args)
staticconstexpr

◆ function

template<typename SIGNAL , typename... Args>
std::function<float(Args...)> klang::Generic::Function< SIGNAL, Args >::function

Definition at line 1518 of file klang.h.

◆ in

template<typename SIGNAL >
SIGNAL klang::Generic::Input< SIGNAL >::in = { 0.f }
inherited

◆ inputs

template<typename SIGNAL , typename... Args>
std::tuple<Args...> klang::Generic::Function< SIGNAL, Args >::inputs

◆ out