375 lines
7.7 KiB
Zig
375 lines
7.7 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,
|
|
|
|
wantprint: bool = false,
|
|
|
|
fab: FreqAmpBuffer,
|
|
active: u8 = 1,
|
|
|
|
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(">> IN_WIRE = {d}\n", .{self.in_wire});
|
|
print(">> IN_AIR = {d}\n", .{self.in_air});
|
|
print(">> IN_GAIN = {d}\n", .{self.in_gain});
|
|
print(">> IN_BASEPHASE = {d}\n", .{self.in_basephase});
|
|
print(">> IN_PHASE = {d}\n", .{self.in_phase});
|
|
print(">> IN_BASEFREQ = {d}\n", .{self.in_basefreq});
|
|
print(">> IN6 = {d}\n", .{self.in6});
|
|
print(">> IN7 = {d}\n", .{self.in7});
|
|
print(">> IN8 = {d}\n", .{self.in8});
|
|
print(">> IN9 = {d}\n", .{self.in9});
|
|
print(">> IN10 = {d}\n", .{self.in10});
|
|
print(">> IN11 = {d}\n", .{self.in11});
|
|
|
|
print("-----------------------\n", .{});
|
|
|
|
print(">> R_AMP = {d}\n", .{self.r_amp});
|
|
print(">> OUT1 = {d}\n", .{self.out1});
|
|
print(">> GAIN = {d}\n", .{self.gain});
|
|
print(">> BASEPHASE = {d}\n", .{self.basephase});
|
|
print(">> PHASE = {d}\n", .{self.phase});
|
|
print(">> BASEFREQ = {d}\n", .{self.basefreq});
|
|
print(">> OUT6 = {d}\n", .{self.out6});
|
|
print(">> OUT7 = {d}\n", .{self.out7});
|
|
print(">> OUT8 = {d}\n", .{self.out8});
|
|
print(">> OUT9 = {d}\n", .{self.out9});
|
|
print(">> OUT10 = {d}\n", .{self.out10});
|
|
print(">> OUT11 = {d}\n", .{self.out11});
|
|
|
|
print("======================\n", .{});
|
|
|
|
}
|
|
|
|
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 corrGain(self: *SoundNode, gainmult: f64) f64 {
|
|
|
|
if (gainmult > 0) {
|
|
return self.gain * gainmult;
|
|
} else {
|
|
return self.gain;
|
|
}
|
|
}
|
|
|
|
pub fn totalPhase(self: *SoundNode) f64 {
|
|
return self.phase + self.basephase;
|
|
}
|
|
|
|
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 = 1,
|
|
.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,
|
|
.wantprint = false,
|
|
.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);
|
|
|
|
}
|
|
|
|
};
|