sonnum/zigsonnum/activity.zig

117 lines
No EOL
3.1 KiB
Zig

const std = @import("std");
const math = std.math;
const print = std.debug.print;
const Allocator = std.mem.Allocator;
const SoundNode = @import("soundnode.zig").SoundNode;
const FreqAmpBuffer = @import("freqamp.zig").FreqAmpBuffer;
const utility = @import("utility.zig");
const basicsynths = @import ("activities/basicsynths.zig");
const spatial = @import ("activities/spatial.zig");
const generators = @import ("activities/generators.zig");
pub const Activity = struct {
start_tick: u32,
end_tick: u32,
opcode: u16,
soundnode: *SoundNode,
operands: [6]f64 = std.mem.zeroes([6]f64),
pub fn create(allocator: Allocator, start_tick: u32, end_tick: u32, opcode: u16, soundnode: *SoundNode, operands: [6]f64) !*Activity {
const a = try allocator.create(Activity);
a.* = .{
.start_tick = start_tick,
.end_tick = end_tick,
.opcode = opcode,
.soundnode = soundnode,
.operands = operands,
};
return a;
}
pub fn do(self: *Activity) !void {
switch (self.opcode) {
4 => { self.relay(); },
5 => { self.bridge(); },
10 => { spatial.setpos(self); },
50 => { basicsynths.sine(self); },
51 => { basicsynths.triangle(self); },
100 => { generators.singenN(self); },
else => {},
}
}
pub fn relay(self: *Activity) void {
self.soundnode.add_r_amp(self.soundnode.in_wire);
self.soundnode.add_r_amp(self.soundnode.in_air);
}
pub fn bridge(self: *Activity) void {
const in_pin: usize = @intFromFloat(self.operands[0]);
const out_pin: usize = @intFromFloat(self.operands[1]);
var in_value: f64 = 0;
switch (in_pin) {
0 => { in_value = self.soundnode.in_wire; },
1 => { in_value = self.soundnode.in_air; },
2 => { in_value = self.soundnode.in_gain; },
3 => { in_value = self.soundnode.in_basephase; },
4 => { in_value = self.soundnode.in_phase; },
5 => { in_value = self.soundnode.in_basefreq; },
6 => { in_value = self.soundnode.in6; },
7 => { in_value = self.soundnode.in7; },
8 => { in_value = self.soundnode.in8; },
9 => { in_value = self.soundnode.in9; },
10 => { in_value = self.soundnode.in10; },
11 => { in_value = self.soundnode.in11; },
else => {},
}
switch (out_pin) {
0 => { self.soundnode.r_amp = in_value; },
1 => { self.soundnode.out1 = in_value; },
2 => { self.soundnode.gain = in_value; },
3 => { self.soundnode.basephase = in_value; },
4 => { self.soundnode.phase = in_value; },
5 => {
const fcurrent_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
const prevphase = self.soundnode.phase;
const prevfreq = self.soundnode.basefreq;
self.soundnode.basefreq = in_value;
const c = fcurrent_tick / 44100;
const pp = prevphase + c * (self.soundnode.basefreq - prevfreq) + @floor(c * prevfreq - prevphase);
self.soundnode.phase = pp - @floor(pp);
},
6 => { self.soundnode.out6 = in_value; },
7 => { self.soundnode.out7 = in_value; },
8 => { self.soundnode.out8 = in_value; },
9 => { self.soundnode.out9 = in_value; },
10 => { self.soundnode.out10 = in_value; },
11 => { self.soundnode.out11 = in_value; },
else => {},
}
}
};