summaryrefslogtreecommitdiff
path: root/klangfarbrs/src/lib.rs
blob: 1fdc0fb52ad61cdb70a93c714193516b9ece3f92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//! # Rust Synthesizer for the Godot game engine
//!
//! This crate contains logic for generating samples for audio wave forms which are then
//! used to fill Godot's `AudioStreamPlayback` buffers. Scripts using this code as a dynamic
//! library will be able to request a certain number of frames (represented as a `Vector2`)
//! generated with the current synthesis parameter settings.
//!
//! Because of how the Godot bindings work, the Synth will have a default
//! sample rate at 48kHz. You'll want to set it in your script's `_ready`
//! function to match the sample rate in Godot.

use gdnative::prelude::*;
use gdnative::core_types::TypedArray;
use rand::Rng;
use std::f32::consts::TAU;

mod osc;
use osc::Osc;

pub mod adsr;
use adsr::Envelope;

/// Aliasing some types to distinguish various audio properties.
type Sample = f32;
type SamplesPerSecond = f32;
type Hz = f32;
type Phase = f32;
type Amplitude = f32;
type Millisecond = u32;

/// The various waveforms the `MonoSynth` can generate.
pub enum Waveform {
    Sine,
    Square,
    Triangle,
    Sawtooth,
    WhiteNoise,
    BrownNoise,
}

#[derive(NativeClass)]
#[inherit(Node)]
pub struct MonoSynth {
    pub phasor: Phasor,
    pub waveform: Waveform,
    pub sample_rate: SamplesPerSecond,
    pub frequency: Hz,
    pub apply_bend: bool,
    pub phasor_bend: Vector2,
    pub continuous: bool,
    pub duration: Millisecond,
    // ADSR amplifier
    envelope: Envelope,
    // filter
    pub cutoff: Hz,
    // pub resonance:
    // pub modulator:
    pub frequency_modulation: bool,
    pub fm_frequency: Hz,
    pub fm_depth: Amplitude,
    fm_phasor: Phasor,
    current_envelope_position: usize,
}

pub struct Phasor {
    pub phase: Phase,
}

impl Phasor {
    /// Phase stays between 0.0 and 1.0 and represents position on the axis of time
    /// for a given wave form. Since audio signals are periodic, we can just calculate
    /// the first cycle of a wave repeatedly. This also prevents pitch drift caused by
    /// floating point errors over time.
    pub fn next_phase(&self, frequency: Hz, sample_rate: SamplesPerSecond ) -> Phase {
        (self.phase + (frequency / sample_rate)) % 1.0
    }
}

pub struct Bender {}

impl Bender {
    // for (i = 0; i < nframes; ++i) {
    //     if (in[i] < x0)
    //       out[i] = (y0/x0)*in[i];
    //     else
    //       out[i] = ((1-y0)/(1-x0)) * (in[i] - x0) + y0;
    //   }
    fn bend(phase: Phase, phasor_bend: Vector2) -> f32 {
        if phase < phasor_bend.x {
            (phasor_bend.y / phasor_bend.x) * phase
        } else {
            ((1.0 - phasor_bend.y) / (1.0 - phasor_bend.x)) * (phase - phasor_bend.x) + phasor_bend.y
        }
    }
}

#[methods]
impl MonoSynth {
    /// # Examples
    ///
    /// ```gdscript
    /// var MonoSynth = preload("res://MonoSynth.gdns")
    /// var synth = MonoSynth.new()
    /// synth.set_sample_rate(24000.0)
    /// wave.square() # changes to a square wave
    /// ```
    pub fn new(_owner: &Node) -> Self {
        Self {
            phasor: Phasor { phase: 0.0 },
            waveform: Waveform::Sine,
            sample_rate: 48000.0,
            frequency: 440.0,
            apply_bend: false,
            phasor_bend: Vector2::new(0.0, 0.0),
            continuous: true,
            duration: 0,
            envelope: Envelope::new(500, 1000, 0.5, 4000, 48000.0),
            cutoff: 0.0,
            frequency_modulation: false,
            fm_frequency: 10.0,
            fm_depth: 0.1,
    // Noise,
            fm_phasor: Phasor { phase: 0.0 },
            current_envelope_position: 0,
        }
    }

    #[export]
    fn _ready(&self, _owner: &Node) {
        godot_print!("DAS IST KLANGFARBRS.")
    }

    #[export]
    fn sine(&mut self, _owner: &Node) {
        self.waveform = Waveform::Sine
    }

    #[export]
    fn square(&mut self, _owner: &Node) {
        self.waveform = Waveform::Square
    }

    #[export]
    fn triangle(&mut self, _owner: &Node) {
        self.waveform = Waveform::Triangle
    }

    #[export]
    fn sawtooth(&mut self, _owner: &Node) {
        self.waveform = Waveform::Sawtooth
    }

    #[export]
    fn white_noise(&mut self, _owner: &Node) {
        self.waveform = Waveform::WhiteNoise
    }

    #[export]
    fn brown_noise(&mut self, _owner: &Node) {
        self.waveform = Waveform::BrownNoise
    }

    #[export]
    fn frequency(&mut self, _owner: &Node, frequency: Hz) {
        self.frequency = frequency
    }

    #[export]
    fn continuous(&mut self, _owner: &Node, state: bool) {
        self.continuous = state;
    }

    #[export]
    fn phasor_bend(&mut self, _owner: &Node, phasor_bend: Vector2) {
        self.phasor_bend = phasor_bend
    }

    #[export]
    fn apply_bend(&mut self, _owner: &Node, apply_bend: bool) {
        self.apply_bend = apply_bend
    }

    #[export]
    pub fn set_sample_rate(&mut self, _owner: &Node, sample_rate: f32) {
        self.sample_rate = sample_rate;
    }

    #[export]
    fn frequency_modulation(&mut self, _owner: &Node, frequency_modulation: bool) {
        self.frequency_modulation = frequency_modulation
    }

    #[export]
    fn fm_frequency(&mut self, _owner: &Node, fm_frequency: f32) {
        self.fm_frequency = fm_frequency
    }

    #[export]
    fn fm_depth(&mut self, _owner: &Node, fm_depth: f32) {
        self.fm_depth = fm_depth
    }

    #[export]
    fn envelope(
        &mut self, _owner: &Node,
        attack: Millisecond, decay: Millisecond, sustain: Amplitude, release: Millisecond
    ) {
        self.envelope = Envelope::new(attack, decay, sustain, release, self.sample_rate);
    }

    #[export]
    pub fn frames(&mut self, _owner: &Node, samples: i32) -> TypedArray<Vector2> {
        let mut frames = TypedArray::new();
        let mut rng = rand::thread_rng();
        let mut last_value = (rng.gen::<f32>() * TAU).sin();

        for _i in 0..samples {
            let mut sample = Osc::generate_sample(&self.waveform, self.phasor.phase, last_value);
            last_value = sample;
            let next_phase : f32;

            if self.frequency_modulation {
                let modulation_value = Osc::generate_sample(&Waveform::Sine, self.fm_phasor.phase, last_value) * self.fm_depth;
                self.fm_phasor.phase = self.fm_phasor.next_phase(self.fm_frequency, self.sample_rate);
                next_phase = self.phasor.next_phase(self.frequency + modulation_value, self.sample_rate);
            } else {
                next_phase = self.phasor.next_phase(self.frequency, self.sample_rate);
            }

            if self.apply_bend {
                self.phasor.phase = Bender::bend(next_phase, self.phasor_bend);
            } else {
                self.phasor.phase = next_phase;
            }

            if !self.continuous {
                let pos = self.current_envelope_position;
                let atk = self.envelope.attack.len();
                let atkdcy = atk + self.envelope.decay.len();

                if pos < atk {
                    sample = sample * self.envelope.attack[pos]
                } else if pos >= atk && pos < atkdcy  {
                    sample = sample * self.envelope.decay[pos - atk]
                } else if pos < self.envelope.len() {
                    sample = sample * self.envelope.release[pos - atkdcy]
                }

                self.current_envelope_position += 1;

                if self.current_envelope_position >= self.envelope.len() {
                    self.current_envelope_position = 0;
                }
            }

            frames.push(Vector2::new(sample, sample));
        }

        return frames
    }
}

// Function that registers all exposed classes to Godot
fn init(handle: InitHandle) {
    handle.add_class::<MonoSynth>();
}

// Macro that creates the entry-points of the dynamic library.
godot_init!(init);