Klang C++ 0.7.15
Language Reference (draft)
Loading...
Searching...
No Matches
klang::Graph

Language Guide / DSP and Analysis

Graph

klang::Graph

Plots data series and functions in Studio.

Use the model’s graph to display points, a function’s response, or a stream of audio samples.

Example · Add points

Pass a y value to add() to use the point index as x. Supply a pair for explicit coordinates. Here the second series starts with three points at specified x positions.

graph.add(0.4);
graph.add(0.7);
graph[1].add({0, 0});
graph[1].add({0.5, 1});
graph[1].add({2, 0});

Example · Plot a function

Feed a single-argument function such as hardclip straight into the graph.

hardclip >> graph;

For Function<float, float> shaper, the graph sweeps the first argument along x. Supply the second argument inline.

shaper(4) >> graph;

Use with() to express the same binding explicitly.

shaper.with(4) >> graph;

Example · Capture audio

Route the input to the graph to append a sample to series zero. Each point uses the sample index as x.

in >> graph;

See Function for more on wrapping and configuring callables.

Example · Set both axes

The first pair sets the x range and the second pair sets the y range. Two arguments set only the x range.

graph(-2, 2, -1, 1);

A line graph plotter. More...

#include <klang.h>

+ Inheritance diagram for klang::Graph:

Public Member Functions

template<typename... Args>
Graph & operator<< (Function< Args... > &function)
 Plot the given function for x plus additional arguments.
 
size_t capacity () const
 
void clear ()
 
Graph & operator() (double min, double max)
 
Graph & operator() (double x_min, double x_max, double y_min, double y_max)
 
Graph::Series & operator[] (int index)
 
const Graph::Series & operator[] (int index) const
 
 operator Series & ()
 
void plot (Generic::Function< SIGNAL, Args... > &function)
 Plot the given Function.
 
void plot (TYPE(*function)(TYPE))
 Plot the given function of x.
 
void plot (FUNCTION f, VALUES... values)
 Plot the given function for x plus additional arguments.
 
void add (TYPE y)
 Add a data point (incrementing x)
 
void add (const Point pt)
 Add a data point.
 
Graph & operator+= (TYPE y)
 Add a data point (incrementing x)
 
Graph & operator+= (const Point pt)
 Add a data point.
 
Graph & operator+= (std::initializer_list< Point > values)
 Plot the given data points.
 
Graph & operator<< (Function< Args... > &function)
 Plot the given function for x plus additional arguments.
 
Graph & operator<< (TYPE &in)
 
Graph & operator<< (const TYPE &in)
 
const Axes & getAxes () const
 
void getAxes (Axes &axes) const
 
const Data & getData () const
 
bool isDirty () const
 
void setDirty (bool dirty)
 
void truncate (unsigned int count)
 

Protected Attributes

Axes axes
 
Data data
 
bool dirty
 

Detailed Description

Definition at line 3749 of file klang.h.

Member Function Documentation

◆ add() [1/2]

void klang::Generic::Graph< SIZE >::add ( const Point pt)
inlineinherited

Definition at line 3558 of file klang.h.

◆ add() [2/2]

void klang::Generic::Graph< SIZE >::add ( TYPE y)
inlineinherited

Definition at line 3556 of file klang.h.

3556{ data[0].add(y); dirty = true; }

◆ capacity()

size_t klang::Generic::Graph< SIZE >::capacity ( ) const
inlineinherited

Definition at line 3331 of file klang.h.

3331{ return SIZE; }

◆ clear()

void klang::Generic::Graph< SIZE >::clear ( )
inlineinherited

Definition at line 3453 of file klang.h.

3453 {
3454 dirty = true;
3455 axes.clear();
3456 for (int s = 0; s < GRAPH_SERIES; s++)
3457 data[s].clear();
3458 data.clear();
3459 }

◆ getAxes() [1/2]

const Axes & klang::Generic::Graph< SIZE >::getAxes ( ) const
inlineinherited

Definition at line 3595 of file klang.h.

3595{ return axes; }

◆ getAxes() [2/2]

void klang::Generic::Graph< SIZE >::getAxes ( Axes & axes) const
inlineinherited

Definition at line 3598 of file klang.h.

3598 {
3599 if (!axes.x.valid()) {
3600 axes.x.clear();
3601 for (int s = 0; s < GRAPH_SERIES; s++)
3602 axes.x.from(data[s], &Point::x);
3603 if (!axes.y.valid() && axes.y.max != 0)
3604 axes.y.min = 0;
3605 }
3606 if (!axes.y.valid()) {
3607 axes.y.clear();
3608 for (int s = 0; s < GRAPH_SERIES; s++)
3609 axes.y.from(data[s], &Point::y);
3610 if (!axes.y.valid() && axes.y.max != 0)
3611 axes.y.min = 0;
3612 }
3613 }

◆ getData()

const Data & klang::Generic::Graph< SIZE >::getData ( ) const
inlineinherited

Definition at line 3615 of file klang.h.

3615{ return data; }

◆ isDirty()

bool klang::Generic::Graph< SIZE >::isDirty ( ) const
inlineinherited

Definition at line 3616 of file klang.h.

3616{ return dirty; }

◆ operator Series &()

klang::Generic::Graph< SIZE >::operator Series & ( )
inlineinherited

Definition at line 3506 of file klang.h.

3506 {
3507 dirty = true;
3508 return data[0];
3509 }

◆ operator()() [1/2]

Graph & klang::Generic::Graph< SIZE >::operator() ( double min,
double max )
inlineinherited

Definition at line 3484 of file klang.h.

3484 {
3485 dirty = true;
3486 axes.x.min = min; axes.x.max = max;
3487 return *this;
3488 }

◆ operator()() [2/2]

Graph & klang::Generic::Graph< SIZE >::operator() ( double x_min,
double x_max,
double y_min,
double y_max )
inlineinherited

Definition at line 3499 of file klang.h.

3499 {
3500 dirty = true;
3501 axes.x.min = x_min; axes.x.max = x_max;
3502 axes.y.min = y_min; axes.y.max = y_max;
3503 return *this;
3504 }

◆ operator+=() [1/3]

Graph & klang::Generic::Graph< SIZE >::operator+= ( const Point pt)
inlineinherited

Definition at line 3564 of file klang.h.

3564{ add(pt); return *this; }

◆ operator+=() [2/3]

Graph & klang::Generic::Graph< SIZE >::operator+= ( std::initializer_list< Point > values)
inlineinherited

Definition at line 3579 of file klang.h.

3579 {
3580 for (const auto& value : values)
3581 add(value);
3582 return *this;
3583 }

◆ operator+=() [3/3]

Graph & klang::Generic::Graph< SIZE >::operator+= ( TYPE y)
inlineinherited

Definition at line 3562 of file klang.h.

3562{ add(y); return *this; }

◆ operator<<() [1/4]

Graph & klang::Generic::Graph< SIZE >::operator<< ( const TYPE & in)
inlineinherited

Definition at line 3592 of file klang.h.

3592{ add(in); return *this; } // without/after processing

◆ operator<<() [2/4]

Graph & klang::Generic::Graph< SIZE >::operator<< ( Function< Args... > & function)
inlineinherited

Definition at line 3586 of file klang.h.

3586 {
3587 plot(function.with());
3588 return *this;
3589 }
void plot(Generic::Function< SIGNAL, Args... > &function)
Definition klang.h:3513

◆ operator<<() [3/4]

Graph & klang::Generic::Graph< SIZE >::operator<< ( TYPE & in)
inlineinherited

Definition at line 3591 of file klang.h.

3591{ add(in); return *this; } // with processing

◆ operator<<() [4/4]

template<typename... Args>
Graph & klang::Graph::operator<< ( Function< Args... > & function)
inline

Definition at line 3754 of file klang.h.

3754 {
3755 plot(function.with());
3756 return *this;
3757 }

References klang::Generic::Graph< SIZE >::plot(), and klang::Generic::Function< SIGNAL, Args >::with().

◆ operator[]() [1/2]

Graph::Series & klang::Generic::Graph< SIZE >::operator[] ( int index)
inlineinherited

Definition at line 3490 of file klang.h.

3490 {
3491 dirty = true;
3492 return data[index];
3493 }

◆ operator[]() [2/2]

const Graph::Series & klang::Generic::Graph< SIZE >::operator[] ( int index) const
inlineinherited

Definition at line 3495 of file klang.h.

3495 {
3496 return data[index];
3497 }

◆ plot() [1/3]

void klang::Generic::Graph< SIZE >::plot ( FUNCTION f,
VALUES... values )
inlineinherited

Definition at line 3541 of file klang.h.

3541 {
3542 thread_local static Function fun(f);
3543 Graph::Series* series = Graph::data.find((void*)fun);
3544 if (!series)
3545 series = Graph::data.add();
3546 if (series) {
3547 dirty = true;
3548 if (!axes.x.valid())
3549 axes.x = { -1, 1 };
3550 series->plot(fun.with(values...), axes.x);
3551 }
3552 }
Function() -> Function< signal, Args... >
void add(const TYPE &item)
Add the specified item to the end of the array.
Definition klang.h:318
Series * find(void *function)
Definition klang.h:3476

◆ plot() [2/3]

void klang::Generic::Graph< SIZE >::plot ( Generic::Function< SIGNAL, Args... > & function)
inlineinherited

Definition at line 3513 of file klang.h.

3513 {
3514 Graph::Series* series = Graph::data.find((void*)function);
3515 if (!series)
3516 series = Graph::data.add();
3517 if (series) {
3518 dirty = true;
3519 if (!axes.x.valid())
3520 axes.x = { -1, 1 };
3521 series->plot(function, axes.x);
3522 }
3523 }

◆ plot() [3/3]

void klang::Generic::Graph< SIZE >::plot ( TYPE(* function )(TYPE))
inlineinherited

Definition at line 3527 of file klang.h.

3527 {
3528 Graph::Series* series = Graph::data.find((void*)function);
3529 if (!series)
3530 series = Graph::data.add();
3531 if (series) {
3532 dirty = true;
3533 if (!axes.x.valid())
3534 axes.x = { -1, 1 };
3535 series->plot(function, axes.x);
3536 }
3537 }

◆ setDirty()

void klang::Generic::Graph< SIZE >::setDirty ( bool dirty)
inlineinherited

Definition at line 3617 of file klang.h.

3617{ Graph::dirty = dirty; }

◆ truncate()

void klang::Generic::Graph< SIZE >::truncate ( unsigned int count)
inlineinherited

Definition at line 3619 of file klang.h.

3619 {
3620 if (count < data.count)
3621 data.count = count;
3622
3623 for (int s = 0; s < GRAPH_SERIES; s++)
3624 if (count < data[s].count)
3625 data[s].count = count;
3626 }

Member Data Documentation

◆ axes

Axes klang::Generic::Graph< SIZE >::axes
protectedinherited

Definition at line 3629 of file klang.h.

◆ data

Data klang::Generic::Graph< SIZE >::data
protectedinherited

Definition at line 3630 of file klang.h.

◆ dirty

bool klang::Generic::Graph< SIZE >::dirty
protectedinherited

Definition at line 3631 of file klang.h.