const std = @import("std"); const print = std.debug.print; const SoundNode = @import("soundnode.zig").SoundNode; const FreqAmpList = @import("freqamp.zig").FreqAmpList; pub const Activity = struct { start_tick: u32, end_tick: u32, opcode: u16, soundnode: *SoundNode, operands: [6]f64 = std.mem.zeroes([6]f64), pub fn do(self: *Activity) !void { switch (self.opcode) { 0 => { try self.reset(); }, 1 => { try self.setfreq(); }, 2 => { try self.relay(); }, 3 => { try self.slide_freq(); }, else => {}, } } pub fn reset(self: *Activity) !void { try self.soundnode.fab.reset(); } pub fn setfreq(self: *Activity) !void { try self.soundnode.fab.setfreq(self.operands[0], self.operands[1]); } pub fn slide_freq(self: *Activity) !void { try self.soundnode.fab.reset(); const init_freq: f64 = self.operands[0]; const final_freq: f64 = self.operands[1]; const timediff: u32 = self.end_tick - self.start_tick; const elapsed: u32 = self.soundnode.fab.current_tick - self.start_tick; const freqdiff: f128 = ((@as(f128, final_freq - init_freq))) / @as(f128, @floatFromInt(timediff)); const res_freq: f64 = init_freq + @as(f64, @floatCast(freqdiff * @as(f128, @floatFromInt(elapsed)) )); try self.soundnode.fab.setfreq(res_freq, self.operands[2]); if (self.soundnode.fab.current_tick % 1000 == 0) { print("TIMEDIFF {d}\n", .{timediff}); print("FREQDIFF {d}\n", .{@as(f64, @floatCast(freqdiff))}); print("RESFREQ {d}\n", .{res_freq}); } } pub fn relay(self: *Activity) !void { try self.soundnode.fab.reset(); const current_tick: u32 = self.soundnode.fab.current_tick; const current_index: u32 = self.soundnode.fab.current_index; var current_fal: *FreqAmpList = undefined; for (self.soundnode.wire_in.items, 0..) |wired_sn, i| { current_fal = wired_sn.fab.fal_array[current_index]; for (current_fal.arraylist.items) |fa| { try self.soundnode.fab.addfreq(fa.freq, fa.r_amp); } _ = i; } for (self.soundnode.air_in.items, 0..) |aired_sn, i| { const dist: f32 = self.soundnode.distance(aired_sn); const sample_tick: u32 = @intFromFloat(@floor(@as(f32, @floatFromInt(current_tick)) - @floor(128.571428 * dist))); const sample_index = aired_sn.fab.index_by_tick(sample_tick); const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0))); current_fal = aired_sn.fab.fal_array[sample_index]; for (current_fal.arraylist.items) |fa| { try self.soundnode.fab.addfreq(fa.freq, fa.r_amp * attenuation); } _ = i; } } };