147 lines
2.9 KiB
Zig
147 lines
2.9 KiB
Zig
const std = @import("std");
|
|
const print = std.debug.print;
|
|
const math = std.math;
|
|
|
|
const Endian = std.builtin.Endian;
|
|
const ArrayList = std.ArrayList;
|
|
const AutoHashMap = std.AutoHashMap;
|
|
const Allocator = std.mem.Allocator;
|
|
const StringHashMap = std.StringHashMap;
|
|
|
|
const pnt = @import("point.zig");
|
|
const Pnt = pnt.Pnt;
|
|
const Link = @import("link.zig").Link;
|
|
const Activity = @import("activity.zig").Activity;
|
|
const SoundSettings = @import("settings.zig").SoundSettings;
|
|
const FreqAmpBuffer = @import("freqamp.zig").FreqAmpBuffer;
|
|
|
|
const utility = @import("utility.zig");
|
|
const prnt = utility.prnt;
|
|
|
|
|
|
pub const SoundNode = struct {
|
|
|
|
allocator: Allocator,
|
|
|
|
uid: usize = 0,
|
|
freq_q: u8,
|
|
|
|
pins: [256]f64,
|
|
|
|
activities: ArrayList(*Activity),
|
|
links: ArrayList(*Link),
|
|
|
|
props: StringHashMap(f64),
|
|
|
|
wantprint: bool = false,
|
|
|
|
fab: FreqAmpBuffer,
|
|
active: u8 = 0,
|
|
|
|
|
|
pub fn create(allocator: Allocator, uid: usize, freq_q: u8) !*SoundNode {
|
|
|
|
const activities = ArrayList(*Activity).init(allocator);
|
|
const links = ArrayList(*Link).init(allocator);
|
|
const fab = FreqAmpBuffer{};
|
|
const props = StringHashMap(f64).init(allocator);
|
|
|
|
const sn = try allocator.create(SoundNode);
|
|
|
|
|
|
sn.* = .{
|
|
.uid = uid,
|
|
.freq_q = freq_q,
|
|
|
|
.pins = std.mem.zeroes([256]f64),
|
|
|
|
.allocator = allocator,
|
|
|
|
.activities = activities,
|
|
.links = links,
|
|
.fab = fab,
|
|
.props = props,
|
|
|
|
.wantprint = false,
|
|
.active = 0,
|
|
};
|
|
|
|
//Gain and per-freq gains must be initialized as 1
|
|
|
|
@memset(sn.pins[112..160], 1);
|
|
|
|
return sn;
|
|
|
|
}
|
|
|
|
pub fn printState(self: *SoundNode) void {
|
|
const fcurrent_tick: f64 = @floatFromInt(self.current_tick());
|
|
|
|
print("\n===SOUNDNODE {d} TICK {d} SECOND {d}===\n", .{self.uid, fcurrent_tick, fcurrent_tick/44100});
|
|
|
|
print("======================\n", .{});
|
|
|
|
}
|
|
|
|
pub fn corrGain(self: *SoundNode, gainmult: f64) f64 {
|
|
|
|
if (gainmult > 0) {
|
|
return self.pins[32] * gainmult;
|
|
} else {
|
|
return self.pins[32];
|
|
}
|
|
}
|
|
|
|
pub fn deinit(self: *SoundNode) void {
|
|
|
|
self.activities.deinit();
|
|
self.fab.deinit();
|
|
self.props.deinit();
|
|
|
|
}
|
|
|
|
pub fn current_tick(self: *SoundNode) u32 {
|
|
|
|
return self.fab.current_tick;
|
|
|
|
}
|
|
|
|
pub fn add_r_amp(self: *SoundNode, r_amp: f64) void {
|
|
|
|
self.fab.add_r_amp(r_amp);
|
|
self.pins[0] += r_amp;
|
|
|
|
}
|
|
|
|
pub fn g(self: *SoundNode, key: []const u8) f64 {
|
|
|
|
const value = self.props.get(key);
|
|
|
|
if (value) |v| {
|
|
return v;
|
|
} else {
|
|
return @as(f64, 0.0);
|
|
}
|
|
}
|
|
|
|
pub fn s(self: *SoundNode, key: []const u8, value: f64) !void {
|
|
|
|
try self.props.put(key, value);
|
|
|
|
}
|
|
|
|
pub fn distance(self: *SoundNode, other: *SoundNode) f64 {
|
|
|
|
if ((self.pins[61] == other.pins[61]) and (self.pins[62] == other.pins[62]) and (self.pins[63] == other.pins[63])) {
|
|
return 0;
|
|
}
|
|
|
|
const dx = self.pins[61] - other.pins[61];
|
|
const dy = self.pins[62] - other.pins[62];
|
|
const dz = self.pins[63] - other.pins[63];
|
|
|
|
return math.sqrt(dx*dx + dy*dy + dz*dz);
|
|
|
|
}
|
|
|
|
};
|