sonnum/zigsonnum/activities/basicsynths.zig

147 lines
No EOL
4.2 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
// Skewness 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);
}
// Produces a pulse wave to r_amp pin
// Uses OUT8 as pulse phase
pub fn pulse(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 tp_detract = (current_tick - (period * (self.soundnode.out8 + self.soundnode.totalPhase()))) / period;
const amp = 2 * (tp - @floor(0.5 + tp));
const amp_detract = 2 * (tp_detract - @floor(0.5+tp_detract));
const final_amp = gain * (amp - amp_detract);
self.soundnode.add_r_amp(final_amp);
}
// Whitenoise
// Seed is taken from OUT8
//
pub fn whitenoise(self: *Activity) void {
const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
const seed = self.soundnode.out8;
const gain = self.soundnode.corrGain(self.operands[0]);
const w = (seed*current_tick) / math.sin(current_tick);
const amp = (2 * (w - @floor(w))) - 1;
const final_amp = gain * amp;
self.soundnode.fab.add_r_amp(final_amp);
}