124 lines
No EOL
3 KiB
Zig
124 lines
No EOL
3 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 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.setbasefreq(); },
|
|
7 => { self.setgain(); },
|
|
8 => { self.setbasephase(); },
|
|
9 => { self.sine(); },
|
|
else => {},
|
|
}
|
|
}
|
|
|
|
|
|
pub fn relay_imprecise(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 relay(self: *Activity) void {
|
|
|
|
self.soundnode.add_r_amp(self.soundnode.in_wire);
|
|
self.soundnode.add_r_amp(self.soundnode.in_air);
|
|
|
|
}
|
|
|
|
pub fn setbasefreq(self: *Activity) void {
|
|
self.soundnode.basefreq = self.operands[0];
|
|
}
|
|
|
|
pub fn setgain(self: *Activity) void {
|
|
self.soundnode.gain = self.operands[0];
|
|
}
|
|
|
|
pub fn setbasephase(self: *Activity) void {
|
|
self.soundnode.basephase = self.operands[0];
|
|
}
|
|
|
|
pub fn setpos(self: *Activity) void {
|
|
self.soundnode.x = @as(f32, @floatCast(self.operands[0]));
|
|
self.soundnode.y = @as(f32, @floatCast(self.operands[1]));
|
|
self.soundnode.z = @as(f32, @floatCast(self.operands[2]));
|
|
|
|
}
|
|
|
|
pub fn sine(self: *Activity) void {
|
|
|
|
const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
|
|
|
|
const gainmult = self.operands[0];
|
|
|
|
var gain = self.soundnode.gain;
|
|
|
|
if (gainmult > 0) {
|
|
gain *= gainmult;
|
|
}
|
|
|
|
const amp = gain * math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.basephase * utility.tau);
|
|
self.soundnode.add_r_amp(amp);
|
|
}
|
|
|
|
}; |