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 FreqAmpList = @import("freqamp.zig").FreqAmpList; const utility = @import("utility.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.setpos(); }, 6 => { self.sine(); }, else => {}, } } pub fn sine(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); const freq = self.operands[0]; const r_amp = self.operands[1]; const phase = self.operands[2]; const amp = r_amp * math.sin(utility.corrected_tau * freq * current_tick - phase * utility.tau); self.soundnode.fab.add_r_amp(amp); } pub fn relay(self: *Activity) void { const current_tick: u32 = self.soundnode.fab.current_tick; for (self.soundnode.wire_in.items, 0..) |wired_sn, i| { const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick); self.soundnode.fab.add_r_amp(relayed_r_amp); _ = i; } for (self.soundnode.air_in.items, 0..) |aired_sn, i| { const dist: f64 = self.soundnode.distance(aired_sn); var sample_tick: u32 = 0; const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist); if (tck > 0) { sample_tick = @intFromFloat( @floor(tck) ); const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0))); const relayed_r_amp = aired_sn.fab.get_r_amp(sample_tick) * attenuation; self.soundnode.fab.add_r_amp(relayed_r_amp); } _ = i; } } pub fn setpos(self: *Activity) void { self.soundnode.location.x = @as(f32, @floatCast(self.operands[0])); self.soundnode.location.y = @as(f32, @floatCast(self.operands[1])); self.soundnode.location.z = @as(f32, @floatCast(self.operands[2])); } };