sonnum/zigsonnum/soundnode.zig
2025-09-16 23:16:48 +03:00

320 lines
5.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 Pin = @import("pin.zig").Pin;
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,
// Input pins
in_wire: f64,
in_air: f64,
in_gain: f64,
in_basephase: f64,
in_phase: f64,
in_basefreq: f64,
in6: f64,
in7: f64,
in8: f64,
in9: f64,
in10: f64,
in11: f64,
// Output pins
r_amp: f64,
out1: f64,
gain: f64,
basephase: f64,
phase: f64,
basefreq: f64,
out6: f64,
out7: f64,
out8: f64,
out9: f64,
out10: f64,
out11: f64,
activities: ArrayList(*Activity),
pins: ArrayList(*Pin),
props: StringHashMap(f64),
x: f32 = 0,
y: f32 = 0,
z: f32 = 0,
fab: FreqAmpBuffer,
active: u8 = 1,
pub fn nullizeStartTick(self: *SoundNode) void {
self.in_wire = 0;
self.in_air = 0;
self.in_gain = 0;
self.in_basephase = 0;
self.in_phase = 0;
self.in_basefreq = 0;
self.in6 = 0;
self.in7 = 0;
self.in8 = 0;
self.in9 = 0;
self.in10 = 0;
self.in11 = 0;
self.r_amp = 0;
}
pub fn create(allocator: Allocator, uid: usize) !*SoundNode {
const activities = ArrayList(*Activity).init(allocator);
const pins = ArrayList(*Pin).init(allocator);
const fab = FreqAmpBuffer{};
const props = StringHashMap(f64).init(allocator);
const sn = try allocator.create(SoundNode);
sn.* = .{
.uid = uid,
.allocator = allocator,
.in_wire = 0,
.in_air = 0,
.in_gain = 0,
.in_basephase = 0,
.in_phase = 0,
.in_basefreq = 0,
.in6 = 0,
.in7 = 0,
.in8 = 0,
.in9 = 0,
.in10 = 0,
.in11 = 0,
.r_amp = 0,
.out1 = 0,
.gain = 0,
.basephase = 0,
.phase = 0,
.basefreq = 0,
.out6 = 0,
.out7 = 0,
.out8 = 0,
.out9 = 0,
.out10 = 0,
.out11 = 0,
.activities = activities,
.pins = pins,
.fab = fab,
.props = props,
.x = 0,
.y = 0,
.z = 0,
.active = 0,
};
return sn;
}
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.r_amp += 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.x == other.x) and (self.y == other.y) and (self.z == other.z)) {
return 0;
}
const dx: f32 = self.x - other.x;
const dy: f32 = self.y - other.y;
const dz: f32 = self.z - other.z;
return math.sqrt(dx*dx + dy*dy + dz*dz);
}
};
pub const SoundNodeOld = struct {
allocator: Allocator,
name: []const u8 = "soundnode",
uid: usize = 0,
air_in: ArrayList(*SoundNode),
wire_in: ArrayList(*SoundNode),
air_targets: ArrayList(*SoundNode),
wire_targets: ArrayList(*SoundNode),
activities: ArrayList(*Activity),
props: StringHashMap(f64),
x: f32 = 0,
y: f32 = 0,
z: f32 = 0,
fab: FreqAmpBuffer,
active: u8 = 1,
pub fn create(allocator: Allocator, name: []const u8, uid: usize) !*SoundNode {
const air_in = ArrayList(*SoundNode).init(allocator);
const wire_in = ArrayList(*SoundNode).init(allocator);
const air_targets = ArrayList(*SoundNode).init(allocator);
const wire_targets = ArrayList(*SoundNode).init(allocator);
const activities = ArrayList(*Activity).init(allocator);
const fab = FreqAmpBuffer{};
const props = StringHashMap(f64).init(allocator);
const sn = try allocator.create(SoundNode);
sn.* = .{
.uid = uid,
.allocator = allocator,
.name = name,
.air_in = air_in,
.wire_in = wire_in,
.air_targets = air_targets,
.wire_targets = wire_targets,
.activities = activities,
.fab = fab,
.props = props,
.x = 0,
.y = 0,
.z = 0,
.active = 0,
};
return sn;
}
pub fn init(allocator: Allocator, name: []const u8) !SoundNode {
const air_in = ArrayList(*SoundNode).init(allocator);
const wire_in = ArrayList(*SoundNode).init(allocator);
const activities = ArrayList(*Activity).init(allocator);
const fab = try FreqAmpBuffer.init(allocator);
const props = StringHashMap(f64).init(allocator);
return .{
.allocator = allocator,
.name = name,
.air_in = air_in,
.wire_in = wire_in,
.activities = activities,
.fab = fab,
.props = props,
.x = 0,
.y = 0,
.z = 0,
};
}
pub fn deinit(self: *SoundNode) void {
self.air_in.deinit();
self.wire_in.deinit();
self.activities.deinit();
self.fab.deinit();
self.props.deinit();
}
pub fn current_tick(self: *SoundNode) u32 {
return self.fab.current_tick;
}
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.x == other.x) and (self.y == other.y) and (self.z == other.z)) {
return 0;
}
const dx: f32 = self.x - other.x;
const dy: f32 = self.y - other.y;
const dz: f32 = self.z - other.z;
return math.sqrt(dx*dx + dy*dy + dz*dz);
}
};