sonnum/zigsonnum/activities/basicsynths.zig
2025-09-17 16:19:02 +03:00

107 lines
No EOL
3.1 KiB
Zig

const std = @import("std");
const math = std.math;
const print = std.debug.print;
const Activity = @import ("../activity.zig").Activity;
const SoundNode = @import("../soundnode.zig").SoundNode;
const utility = @import("../utility.zig");
const dbg = utility.dbg;
const idbg = utility.idbg;
// Produces a sine wave to r_amp pin
pub fn sine(self: *Activity) void {
const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
const gain = self.soundnode.corrGain(self.operands[0]);
const amp = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau);
const final_amp = gain * amp;
self.soundnode.add_r_amp(final_amp);
}
// Produces a triangle wave to r_amp pin
pub fn triangle(self: *Activity) void {
const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
const gain = self.soundnode.corrGain(self.operands[0]);
const period = 44100 / self.soundnode.basefreq;
const tp = (current_tick - (period * self.soundnode.totalPhase())) / period;
const amp = @abs(2 * (2 * ( tp - @floor(tp + 0.5) ) )) - 1;
const final_amp = gain * amp;
self.soundnode.add_r_amp(final_amp);
}
// Produces a square wave to r_amp pin
pub fn square(self: *Activity) void {
const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
const gain = self.soundnode.corrGain(self.operands[0]);
const sin = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau);
if (sin > 0) {
self.soundnode.add_r_amp(gain);
} else {
self.soundnode.add_r_amp(-gain);
}
}
// Produces a sawtooth wave to r_amp pin
pub fn sawtooth(self: *Activity) void {
const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
const gain = self.soundnode.corrGain(self.operands[0]);
const period = 44100 / self.soundnode.basefreq;
const tp = (current_tick - (period * self.soundnode.totalPhase())) / period;
const amp = 2 * (tp - @floor(0.5+tp));
const final_amp = gain * amp;
self.soundnode.add_r_amp(final_amp);
}
// Produces a skewed sine wave to R_AMP pin
// Skeweness factor is taken from OUT8 pin
pub fn skewsine(self: *Activity) void {
const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
const gain = self.soundnode.corrGain(self.operands[0]);
const skew = self.soundnode.out8;
var amp: f64 = 0;
if (skew == 0) {
amp = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau);
} else if (skew > 0) {
const m = (utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau);
const sincos = skew * math.sin(m) / (1 - skew*math.cos(m));
amp = (1/skew) * math.atan(sincos);
} else if (skew < 0) {
const m = (utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.basephase * utility.tau);
const sincos = skew * math.cos(m) / (1 + skew*math.sin(m));
amp = (1/skew) * math.atan(sincos);
}
const final_amp = gain * amp;
self.soundnode.add_r_amp(final_amp);
}