lsynth/
waveform.rs

1//! Contains the formulas for generating all the different types of waveforms. All generated samples are between -1 and 1, and the provided periods are expected to be between 0 and 1.
2
3/// The number of samples in a custom waveform.
4pub const CUSTOM_WIDTH: usize = 32;
5
6/// Custom waveforms only need to contain an array of data. This is a convenience type for arrays that follow the required pattern.
7pub type CustomWaveform = [f32; CUSTOM_WIDTH];
8
9/// Generates a sinewave
10pub(crate) fn sine(period: f32) -> f32 {
11	f32::sin(period * std::f32::consts::TAU)
12}
13
14/// Generates a trianglewave
15pub(crate) fn triangle(period: f32) -> f32 {
16	-(period - 0.5).abs() * 4.0 + 1.0
17}
18
19/// Generates a sinewave where the negative values have been truncated. Scaled to generate values between -1 and 1.
20pub(crate) fn rec_sine(period: f32) -> f32 {
21	if period < 0.5 {
22		f32::sin(period * std::f32::consts::TAU) * 2.0 - 1.0
23	}
24	else {
25		-1.0
26	}
27}
28
29/// Generates a sawwave.
30pub(crate) fn saw(period: f32) -> f32 {
31	period * 2.0 - 1.0
32}
33
34/// Generates a pulse wave with a duty of 50%.
35pub(crate) fn square(period: f32) -> f32 {
36	if period < 0.5 {1.0}
37	else {-1.0}
38}
39
40/// Generates a pulse wave with a duty of 25%.
41pub(crate) fn pulse(period: f32) -> f32 {
42	if period < 0.25 {1.0}
43	else {-1.0}
44}
45
46/// Generates a random number between -1 and 1.
47pub(crate) fn noise() -> f32 {
48	rand::random::<f32>() * 2.0 - 1.0
49}
50
51/// Samples a custom waveform at the given point in the period.
52pub(crate) fn custom(period: f32, data: &CustomWaveform) -> f32{
53	let index = (period * CUSTOM_WIDTH as f32).floor() as usize;
54	data[index]
55}