🍑 nectarine
Audio synthesis tools for C23
Loading...
Searching...
No Matches
tgwaveforms.h File Reference

Type-generic waveform functions. More...

#include "waveforms.h"

Go to the source code of this file.

Macros

#define nec_wrap(phase)
 Maps an arbitrary phase into the domain [0, 1).
#define nec_fill_wrap(len, phase, output)
 Maps a buffer of arbitrary phases into the domain [0, 1).
#define nec_analog_sawtooth(phase)
 Analog-like sawtooth waveform.
#define nec_fill_analog_sawtooth(len, phase, output)
 Fills a buffer with an analog-like sawtooth waveform.
#define nec_analog_square(phase, duty)
 Analog-like square waveform.
#define nec_fill_analog_square(len, phase, duty, output)
 Fills a buffer with an analog-like square waveform.
#define nec_analog_triangle(phase)
 Analog-like triangle waveform.
#define nec_fill_analog_triangle(len, phase, output)
 Fills a buffer with an analog-like triangle waveform.
#define nec_parabol(phase)
 Fast sine approximation waveform.
#define nec_fill_parabol(len, phase, output)
 Fills a buffer with a fast sine approximation waveform.
#define nec_sin(phase)
 Accurate sine approximation waveform.
#define nec_fill_sin(len, phase, output)
 Fills a buffer with an accurate sine approximation waveform.
#define nec_circle(phase)
 Circle waveform function.
#define nec_fill_circle(len, phase, output)
 Fills a buffer with a circle waveform.
#define nec_sawtooth(phase)
 Basic sawtooth waveform.
#define nec_fill_sawtooth(len, phase, output)
 Fills a buffer with a sawtooth waveform.
#define nec_square(phase, duty)
 Basic square waveform.
#define nec_fill_square(len, phase, duty, output)
 Fills a buffer with a square waveform.
#define nec_triangle(phase)
 Basic triangle waveform.
#define nec_fill_triangle(len, phase, output)
 Fills a buffer with a triangle waveform.

Detailed Description

Type-generic waveform functions.

Macro Definition Documentation

◆ nec_analog_sawtooth

#define nec_analog_sawtooth ( phase)
Value:
_Generic((phase),\
long double : nec_analog_sawtoothl,\
)(phase)
#define nec_analog_sawtooth(phase)
Analog-like sawtooth waveform.
Definition tgwaveforms.h:42
static float nec_analog_sawtoothf(float phase)
Analog-like sawtooth waveform.
Definition waveforms.h:159
static long double nec_analog_sawtoothl(long double phase)
Analog-like sawtooth waveform.
Definition waveforms.h:145

Analog-like sawtooth waveform.

This sawtooth waveform changes with an exponential curve to emulate a discharging capacitor (or charging, when inverted).

To be more precise, it approximates the exponential curve \(\frac{2e}{e-1}\left(e^{-x}-1\right)+1\) using a rational function.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
Returns
A sample of a sawtooth waveform, with range [-1, 1].

◆ nec_analog_square

#define nec_analog_square ( phase,
duty )
Value:
_Generic((phase),\
long double : nec_analog_squarel,\
double : nec_analog_square,\
)((phase), (duty))
#define nec_analog_square(phase, duty)
Analog-like square waveform.
Definition tgwaveforms.h:56
static float nec_analog_squaref(float phase, float duty)
Analog-like square waveform.
Definition waveforms.h:257
static long double nec_analog_squarel(long double phase, long double duty)
Analog-like square waveform.
Definition waveforms.h:239

Analog-like square waveform.

This square waveform decays with an exponential curve to emulate a discharging capacitor.

To be more precise, it approximates the exponential curve \(\frac{e}{2\left(e-1\right)}\left(e^{-x}-1\right)+1\) using a rational function.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
dutyThe duty cycle of the waveform, in domain [0, 1].
Returns
A sample of a square waveform, with range [-1, 1].

◆ nec_analog_triangle

#define nec_analog_triangle ( phase)
Value:
_Generic((phase),\
long double : nec_analog_trianglel,\
)(phase)
#define nec_analog_triangle(phase)
Analog-like triangle waveform.
Definition tgwaveforms.h:70
static long double nec_analog_trianglel(long double phase)
Analog-like triangle waveform.
Definition waveforms.h:345
static float nec_analog_trianglef(float phase)
Analog-like triangle waveform.
Definition waveforms.h:359

Analog-like triangle waveform.

This triangle waveform changes with an exponential curve to emulate a charging and discharging capacitor.

To be more precise, it approximates the exponential curve \(\frac{2e}{e-1}\left(e^{-x}-1\right)+1\) using a rational function.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
Returns
A sample of a triangle waveform, with range [-1, 1].

◆ nec_circle

#define nec_circle ( phase)
Value:
_Generic((phase),\
long double : nec_circlel,\
double : nec_circle,\
float : nec_circlef\
)(phase)
#define nec_circle(phase)
Circle waveform function.
Definition tgwaveforms.h:112
static long double nec_circlel(long double phase)
Circle waveform function.
Definition waveforms.h:619
static float nec_circlef(float phase)
Circle waveform function.
Definition waveforms.h:633

Circle waveform function.

This waveform looks like an alternating semicircle.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
Returns
A sample of circle waveform, with range [-1, 1].

◆ nec_fill_analog_sawtooth

#define nec_fill_analog_sawtooth ( len,
phase,
output )
Value:
_Generic((output),\
long double* : nec_fill_analog_sawtoothl,\
)((len), (phase), (output))
#define nec_fill_analog_sawtooth(len, phase, output)
Fills a buffer with an analog-like sawtooth waveform.
Definition tgwaveforms.h:49
static void nec_fill_analog_sawtoothf(size_t len, float phase[const restrict static len], float output[restrict static len])
Fills a buffer with an analog-like sawtooth waveform.
Definition waveforms.h:209
static void nec_fill_analog_sawtoothl(size_t len, long double phase[const restrict static len], long double output[restrict static len])
Fills a buffer with an analog-like sawtooth waveform.
Definition waveforms.h:172

Fills a buffer with an analog-like sawtooth waveform.

See also
nec_analog_sawtooth()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[out]outputA buffer to be filled with samples of a sawtooth waveform, with range [-1, 1].

◆ nec_fill_analog_square

#define nec_fill_analog_square ( len,
phase,
duty,
output )
Value:
_Generic((output),\
long double* : nec_fill_analog_squarel,\
)((len), (phase), (duty), (output))
#define nec_fill_analog_square(len, phase, duty, output)
Fills a buffer with an analog-like square waveform.
Definition tgwaveforms.h:63
static void nec_fill_analog_squaref(size_t len, float phase[const restrict static len], float duty[const restrict static len], float output[restrict static len])
Fills a buffer with an analog-like square waveform.
Definition waveforms.h:314
static void nec_fill_analog_squarel(size_t len, long double phase[const restrict static len], long double duty[const restrict static len], long double output[restrict static len])
Fills a buffer with an analog-like square waveform.
Definition waveforms.h:273

Fills a buffer with an analog-like square waveform.

See also
nec_analog_square()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[in]dutyA buffer full of duty cycles in domain [0, 1].
[out]outputA buffer to be filled with samples of a square waveform, with range [-1, 1].

◆ nec_fill_analog_triangle

#define nec_fill_analog_triangle ( len,
phase,
output )
Value:
_Generic((output),\
long double* : nec_fill_analog_trianglel,\
)((len), (phase), (output))
#define nec_fill_analog_triangle(len, phase, output)
Fills a buffer with an analog-like triangle waveform.
Definition tgwaveforms.h:77
static void nec_fill_analog_trianglef(size_t len, float phase[const restrict static len], float output[restrict static len])
Fills a buffer with an analog-like triangle waveform.
Definition waveforms.h:409
static void nec_fill_analog_trianglel(size_t len, long double phase[const restrict static len], long double output[restrict static len])
Fills a buffer with an analog-like triangle waveform.
Definition waveforms.h:372

Fills a buffer with an analog-like triangle waveform.

See also
nec_analog_triangle()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[out]outputA buffer to be filled with samples of a triangle waveform, with range [-1, 1].

◆ nec_fill_circle

#define nec_fill_circle ( len,
phase,
output )
Value:
_Generic((output),\
long double* : nec_fill_circlel,\
double* : nec_fill_circle,\
float* : nec_fill_circlef\
)((len), (phase), (output))
#define nec_fill_circle(len, phase, output)
Fills a buffer with a circle waveform.
Definition tgwaveforms.h:119
static void nec_fill_circlel(size_t len, long double phase[const restrict static len], long double output[restrict static len])
Fills a buffer with a circle waveform.
Definition waveforms.h:646
static void nec_fill_circlef(size_t len, float phase[const restrict static len], float output[restrict static len])
Fills a buffer with a circle waveform.
Definition waveforms.h:683

Fills a buffer with a circle waveform.

See also
nec_circle()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[out]outputA buffer to be filled with samples of a circle waveform, with range [-1, 1].

◆ nec_fill_parabol

#define nec_fill_parabol ( len,
phase,
output )
Value:
_Generic((output),\
long double* : nec_fill_paraboll,\
double* : nec_fill_parabol,\
)((len), (phase), (output))
#define nec_fill_parabol(len, phase, output)
Fills a buffer with a fast sine approximation waveform.
Definition tgwaveforms.h:91
static void nec_fill_paraboll(size_t len, long double phase[const restrict static len], long double output[restrict static len])
Fills a buffer with a fast sine approximation waveform.
Definition waveforms.h:467
static void nec_fill_parabolf(size_t len, float phase[const restrict static len], float output[restrict static len])
Fills a buffer with a fast sine approximation waveform.
Definition waveforms.h:504

Fills a buffer with a fast sine approximation waveform.

See also
nec_parabol()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[out]outputA buffer to be filled with samples of an approximated sine waveform, with range [-1, 1].

◆ nec_fill_sawtooth

#define nec_fill_sawtooth ( len,
phase,
output )
Value:
_Generic((output),\
long double* : nec_fill_sawtoothl,\
double* : nec_fill_sawtooth,\
)((len), (phase), (output))
#define nec_fill_sawtooth(len, phase, output)
Fills a buffer with a sawtooth waveform.
Definition tgwaveforms.h:133
static void nec_fill_sawtoothl(size_t len, long double phase[const restrict static len], long double output[restrict static len])
Fills a buffer with a sawtooth waveform.
Definition waveforms.h:731
static void nec_fill_sawtoothf(size_t len, float phase[const restrict static len], float output[restrict static len])
Fills a buffer with a sawtooth waveform.
Definition waveforms.h:768

Fills a buffer with a sawtooth waveform.

See also
nec_sawtooth()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[out]outputA buffer to be filled with samples of a sawtooth waveform, with range [-1, 1].

◆ nec_fill_sin

#define nec_fill_sin ( len,
phase,
output )
Value:
_Generic((output),\
long double* : nec_fill_sinl,\
double* : nec_fill_sin,\
float* : nec_fill_sinf\
)((len), (phase), (output))
#define nec_fill_sin(len, phase, output)
Fills a buffer with an accurate sine approximation waveform.
Definition tgwaveforms.h:105
static void nec_fill_sinf(size_t len, float phase[const restrict static len], float output[restrict static len])
Fills a buffer with an accurate sine approximation waveform.
Definition waveforms.h:597
static void nec_fill_sinl(size_t len, long double phase[const restrict static len], long double output[restrict static len])
Fills a buffer with an accurate sine approximation waveform.
Definition waveforms.h:560

Fills a buffer with an accurate sine approximation waveform.

See also
nec_sin()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[out]outputA buffer to be filled with samples of a sine waveform, with range [-1, 1].

◆ nec_fill_square

#define nec_fill_square ( len,
phase,
duty,
output )
Value:
_Generic((output),\
long double* : nec_fill_squarel,\
double* : nec_fill_square,\
float* : nec_fill_squaref\
)((len), (phase), (duty), (output))
#define nec_fill_square(len, phase, duty, output)
Fills a buffer with a square waveform.
Definition tgwaveforms.h:147
static void nec_fill_squarel(size_t len, long double phase[const restrict static len], long double duty[const restrict static len], long double output[restrict static len])
Fills a buffer with a square waveform.
Definition waveforms.h:821
static void nec_fill_squaref(size_t len, float phase[const restrict static len], float duty[const restrict static len], float output[restrict static len])
Fills a buffer with a square waveform.
Definition waveforms.h:862

Fills a buffer with a square waveform.

See also
nec_square()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[in]dutyA buffer full of duty cycles in domain [0, 1].
[out]outputA buffer to be filled with samples of a square waveform, with range [-1, 1].

◆ nec_fill_triangle

#define nec_fill_triangle ( len,
phase,
output )
Value:
_Generic((output),\
long double* : nec_fill_trianglel,\
double* : nec_fill_triangle,\
)((len), (phase), (output))
#define nec_fill_triangle(len, phase, output)
Fills a buffer with a triangle waveform.
Definition tgwaveforms.h:161
static void nec_fill_trianglel(size_t len, long double phase[const restrict static len], long double output[restrict static len])
Fills a buffer with a triangle waveform.
Definition waveforms.h:912
static void nec_fill_trianglef(size_t len, float phase[const restrict static len], float output[restrict static len])
Fills a buffer with a triangle waveform.
Definition waveforms.h:949

Fills a buffer with a triangle waveform.

See also
nec_triangle()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of input phases in domain [0, 1).
[out]outputA buffer to be filled with samples of a triangle waveform, with range [-1, 1].

◆ nec_fill_wrap

#define nec_fill_wrap ( len,
phase,
output )
Value:
_Generic((output),\
long double* : nec_fill_wrapl,\
double* : nec_fill_wrap,\
float* : nec_fill_wrapf\
)((len), (phase), (output))
#define nec_fill_wrap(len, phase, output)
Maps a buffer of arbitrary phases into the domain [0, 1).
Definition tgwaveforms.h:35
static void nec_fill_wrapf(size_t len, float phase[const restrict static len], float output[restrict static len])
Maps a buffer of arbitrary phases into the domain [0, 1).
Definition waveforms.h:116
static void nec_fill_wrapl(size_t len, long double phase[const restrict static len], long double output[restrict static len])
Maps a buffer of arbitrary phases into the domain [0, 1).
Definition waveforms.h:79

Maps a buffer of arbitrary phases into the domain [0, 1).

See also
nec_wrap()
Parameters
[in]lenThe length of the input and output buffers, phase and output. Must be at least 1.
[in]phaseA buffer full of arbitrary input phases.
[out]outputA buffer to be filled with equivalent phases to the contents of phase, but within the domain [0, 1).

◆ nec_parabol

#define nec_parabol ( phase)
Value:
_Generic((phase),\
long double : nec_paraboll,\
double : nec_parabol,\
float : nec_parabolf\
)(phase)
#define nec_parabol(phase)
Fast sine approximation waveform.
Definition tgwaveforms.h:84
static float nec_parabolf(float phase)
Fast sine approximation waveform.
Definition waveforms.h:453
static long double nec_paraboll(long double phase)
Fast sine approximation waveform.
Definition waveforms.h:437

Fast sine approximation waveform.

This function generates a fast approximation of a sine waveform. It has a maximum absolute error of ±0.05601, which is quite large and produces audible harmonics, so it is intended for use in oscillators where these harmonics are desirable or LFOs where they aren't perceptible.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
Returns
A sample of an approximated sine waveform, with range [-1, 1].

◆ nec_sawtooth

#define nec_sawtooth ( phase)
Value:
_Generic((phase),\
long double : nec_sawtoothl,\
double : nec_sawtooth,\
float : nec_sawtoothf\
)(phase)
#define nec_sawtooth(phase)
Basic sawtooth waveform.
Definition tgwaveforms.h:126
static float nec_sawtoothf(float phase)
Basic sawtooth waveform.
Definition waveforms.h:718
static long double nec_sawtoothl(long double phase)
Basic sawtooth waveform.
Definition waveforms.h:704

Basic sawtooth waveform.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
Returns
A sample of a sawtooth waveform, with range [-1, 1].

◆ nec_sin

#define nec_sin ( phase)
Value:
_Generic((phase),\
long double : nec_sinl,\
double : nec_sin,\
float : nec_sinf\
)(phase)
#define nec_sin(phase)
Accurate sine approximation waveform.
Definition tgwaveforms.h:98
static float nec_sinf(float phase)
Accurate sine approximation waveform.
Definition waveforms.h:546
static long double nec_sinl(long double phase)
Accurate sine approximation waveform.
Definition waveforms.h:530

Accurate sine approximation waveform.

This function generates a sine approximation. It has a maximum absolute error of ±0.0010903. This error does produce harmonics, but they are very quiet.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
Returns
A sample of sine waveform, with range [-1, 1].

◆ nec_square

#define nec_square ( phase,
duty )
Value:
_Generic((phase),\
long double : nec_squarel,\
double : nec_square,\
float : nec_squaref\
)((phase), (duty))
#define nec_square(phase, duty)
Basic square waveform.
Definition tgwaveforms.h:140
static float nec_squaref(float phase, float duty)
Basic square waveform.
Definition waveforms.h:806
static long double nec_squarel(long double phase, long double duty)
Basic square waveform.
Definition waveforms.h:790

Basic square waveform.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
dutyThe duty cycle of the waveform, in domain [0, 1].
Returns
A sample of a square waveform, with range [-1, 1].

◆ nec_triangle

#define nec_triangle ( phase)
Value:
_Generic((phase),\
long double : nec_trianglel,\
double : nec_triangle,\
float : nec_trianglef\
)(phase)
#define nec_triangle(phase)
Basic triangle waveform.
Definition tgwaveforms.h:154
static float nec_trianglef(float phase)
Basic triangle waveform.
Definition waveforms.h:899
static long double nec_trianglel(long double phase)
Basic triangle waveform.
Definition waveforms.h:885

Basic triangle waveform.

Parameters
phaseThe input phase of the waveform, in domain [0, 1).
Returns
A sample of a triangle waveform, with range [-1, 1].

◆ nec_wrap

#define nec_wrap ( phase)
Value:
_Generic((phase),\
long double : nec_wrapl,\
double : nec_wrap,\
float : nec_wrapf\
)(phase)
#define nec_wrap(phase)
Maps an arbitrary phase into the domain [0, 1).
Definition tgwaveforms.h:28
static float nec_wrapf(float phase)
Maps an arbitrary phase into the domain [0, 1).
Definition waveforms.h:66
static long double nec_wrapl(long double phase)
Maps an arbitrary phase into the domain [0, 1).
Definition waveforms.h:52

Maps an arbitrary phase into the domain [0, 1).

All of nectarine's waveform functions require an input phase within the domain [0, 1) and misbehave outside that domain. This function can be used to produce an equivalent input phase within that domain.

This is desirable when storing an oscillator's phase also, since long-running oscillators can accumulate floating point precision error as the phase increases further and further away from zero. Wrapping an oscillator's stored phase periodically can help prevent this.

Parameters
phaseAn arbitrary input phase.
Returns
An equivalent phase to phase, but within the domain [0, 1).