const std = @import("std"); const print = std.debug.print; const Allocator = std.mem.Allocator; 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 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 => { try self.relay(); }, 5 => { try self.accumulate(); }, 6 => { try self.reset(); }, 7 => { try self.setfreq(); }, 8 => { self.setpos(); }, 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], self.operands[2]); } pub fn slide_freq(self: *Activity) !void { // NEEDS REDO 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], 0); 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; for (self.soundnode.wire_in.items, 0..) |wired_sn, i| { const current_fal = wired_sn.fab.fal_array[current_index]; for (current_fal.arraylist.items) |fa| { try self.soundnode.fab.add_fa(fa); } _ = 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 sample_index = aired_sn.fab.index_by_tick(sample_tick); const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0))); const sample_fal = aired_sn.fab.fal_array[sample_index]; for (sample_fal.arraylist.items) |fa| { try self.soundnode.fab.addfreq(fa.freq, fa.r_amp * attenuation, fa.phase); } } _ = i; } } pub fn accumulate(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; for (self.soundnode.wire_in.items, 0..) |wired_sn, i| { const 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, fa.phase); } _ = 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 sample_index = aired_sn.fab.index_by_tick(sample_tick); const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0))); const sample_fal = aired_sn.fab.fal_array[sample_index]; for (sample_fal.arraylist.items) |fa| { try self.soundnode.fab.addfreq(fa.freq, fa.r_amp * attenuation, fa.phase); } } _ = 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])); } };