refactored, working core

This commit is contained in:
aprilnightk 2025-09-09 11:51:14 +03:00
parent d33bd771f8
commit 866b9e66c1
5 changed files with 158 additions and 144 deletions

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const print = std.debug.print; const print = std.debug.print;
const SoundNode = @import("soundnode.zig").SoundNode; const SoundNode = @import("soundnode.zig").SoundNode;
const FreqAmpList = @import("freqamp.zig").FreqAmpList;
pub const Activity = struct { pub const Activity = struct {
@ -8,7 +9,7 @@ pub const Activity = struct {
end_tick: u32, end_tick: u32,
opcode: u16, opcode: u16,
soundnode: *SoundNode, soundnode: *SoundNode,
operands: [10]f64 = std.mem.zeroes([10]f64), operands: [6]f64 = std.mem.zeroes([6]f64),
pub fn do(self: *Activity) !void { pub fn do(self: *Activity) !void {
switch (self.opcode) { switch (self.opcode) {
@ -21,47 +22,66 @@ pub const Activity = struct {
} }
pub fn reset(self: *Activity) !void { pub fn reset(self: *Activity) !void {
try self.soundnode.freqamp.reset(); try self.soundnode.fab.reset();
} }
pub fn setfreq(self: *Activity) !void { pub fn setfreq(self: *Activity) !void {
try self.soundnode.freqamp.setfreq(self.operands[0], self.operands[1]); try self.soundnode.fab.setfreq(self.operands[0], self.operands[1]);
} }
pub fn slide_freq(self: *Activity) !void { pub fn slide_freq(self: *Activity) !void {
try self.soundnode.freqamp.reset(); try self.soundnode.fab.reset();
const init_freq: f64 = self.operands[0]; const init_freq: f64 = self.operands[0];
const final_freq: f64 = self.operands[1]; const final_freq: f64 = self.operands[1];
const timediff: u32 = self.end_tick - self.start_tick; const timediff: u32 = self.end_tick - self.start_tick;
const elapsed: u32 = self.soundnode.freqamp.current_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 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)) )); const res_freq: f64 = init_freq + @as(f64, @floatCast(freqdiff * @as(f128, @floatFromInt(elapsed)) ));
try self.soundnode.freqamp.setfreq(res_freq, self.operands[2]); try self.soundnode.fab.setfreq(res_freq, self.operands[2]);
if (self.soundnode.freqamp.current_tick % 44100 == 0) { if (self.soundnode.fab.current_tick % 1000 == 0) {
print("TIMEDIFF {d}\n", .{timediff}); print("TIMEDIFF {d}\n", .{timediff});
print("FREQDIFF {d}\n", .{@as(f64, @floatCast(freqdiff))}); print("FREQDIFF {d}\n", .{@as(f64, @floatCast(freqdiff))});
print("RESFREQ {d}\n", .{res_freq}); print("RESFREQ {d}\n", .{res_freq});
self.soundnode.freqamp.prnt();
} }
} }
pub fn relay(self: *Activity) !void { pub fn relay(self: *Activity) !void {
try self.soundnode.freqamp.reset(); try self.soundnode.fab.reset();
const current_tick: u32 = self.soundnode.fab.current_tick;
const current_index: u32 = self.soundnode.fab.current_index;
var current_fal: *FreqAmpList = undefined;
for (self.soundnode.wire_in.items, 0..) |wired_sn, i| { for (self.soundnode.wire_in.items, 0..) |wired_sn, i| {
const current_index: u32 = self.soundnode.freqamp.current_index(); current_fal = wired_sn.fab.fal_array[current_index];
const clist = wired_sn.freqamp.freqamps[current_index];
for (clist.lst.items) |freqamp| { for (current_fal.arraylist.items) |fa| {
try self.soundnode.freqamp.addfreq(freqamp.freq, freqamp.r_amp); try self.soundnode.fab.addfreq(fa.freq, fa.r_amp);
}
_ = i;
}
for (self.soundnode.air_in.items, 0..) |aired_sn, i| {
const dist: f32 = self.soundnode.distance(aired_sn);
const sample_tick: u32 = @intFromFloat(@floor(@as(f32, @floatFromInt(current_tick)) - @floor(128.571428 * dist)));
const sample_index = aired_sn.fab.index_by_tick(sample_tick);
const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0)));
current_fal = aired_sn.fab.fal_array[sample_index];
for (current_fal.arraylist.items) |fa| {
try self.soundnode.fab.addfreq(fa.freq, fa.r_amp * attenuation);
} }
_ = i; _ = i;

View file

@ -27,38 +27,79 @@ pub const FreqAmpList = struct {
allocator: Allocator, allocator: Allocator,
refcount: u64, refcount: u64,
lst: ArrayList(*FreqAmp), arraylist: ArrayList(*FreqAmp),
pub fn create(allocator: Allocator) !*FreqAmpList { pub fn create(allocator: Allocator) !*FreqAmpList {
const lst = ArrayList(*FreqAmp).init(allocator); const arraylist = ArrayList(*FreqAmp).init(allocator);
const fal = try allocator.create(FreqAmpList); const fal = try allocator.create(FreqAmpList);
fal.* = .{ fal.* = .{
.allocator = allocator, .allocator = allocator,
.refcount = 0, .refcount = 0,
.lst = lst, .arraylist = arraylist,
}; };
return fal; return fal;
} }
pub fn setfreq(self: *FreqAmpList, freq: f64, r_amp: f64) !void {
for (self.arraylist.items) |fa| {
if (fa.freq == freq) {
fa.r_amp = r_amp;
return;
}
}
const fa = try FreqAmp.create(self.allocator, freq, r_amp);
try self.arraylist.append(fa);
}
pub fn addfreq(self: *FreqAmpList, freq: f64, r_amp: f64) !void {
for (self.arraylist.items) |fa| {
if (fa.freq == freq) {
fa.r_amp = r_amp;
if (fa.r_amp > 1) {
fa.r_amp = 1;
}
return;
}
}
const fa = try FreqAmp.create(self.allocator, freq, r_amp);
try self.arraylist.append(fa);
}
pub fn deinit(self: *FreqAmpList) void { pub fn deinit(self: *FreqAmpList) void {
for (self.lst.items) |freqamp| { for (self.arraylist.items) |freqamp| {
self.allocator.destroy(freqamp); self.allocator.destroy(freqamp);
} }
self.lst.deinit(); self.arraylist.deinit();
} }
pub fn prnt(self: *FreqAmpList) void { pub fn prnt(self: *FreqAmpList) void {
print("<Freqamp List (referenced {d} times)>\n", .{self.refcount}); print("<Freqamp List (referenced {d} times)>\n", .{self.refcount});
for (self.lst.items) |freqamp| { for (self.arraylist.items) |freqamp| {
freqamp.prnt(); freqamp.prnt();
} }
@ -69,52 +110,56 @@ pub const FreqAmpList = struct {
pub const FreqAmpBuffer = struct { pub const FreqAmpBuffer = struct {
const buffer_ticks_default = 44100; const buffer_ticks_default = 44100 * 2;
allocator: Allocator, allocator: Allocator,
buffer_ticks: u32 = buffer_ticks_default, buffer_ticks: u32 = buffer_ticks_default,
current_tick: u32, current_tick: u32,
freqamps: [buffer_ticks_default]*FreqAmpList, current_index: u32,
fal_array: [buffer_ticks_default]*FreqAmpList,
pub fn init(allocator: Allocator) !FreqAmpBuffer { pub fn init(allocator: Allocator) !FreqAmpBuffer {
const current_tick = 0; //Creating an empty filler FreaAmpList
var initial_list = try FreqAmpList.create(allocator); var initial_list = try FreqAmpList.create(allocator);
initial_list.refcount = buffer_ticks_default; initial_list.refcount = buffer_ticks_default;
initial_list.prnt();
var freqamps: [buffer_ticks_default]*FreqAmpList = undefined; //Initializing the array with empry FreaAmpLists
var fal_array: [buffer_ticks_default]*FreqAmpList = undefined;
@memset(&freqamps, initial_list); @memset(&fal_array, initial_list);
return .{ return .{
.allocator = allocator, .allocator = allocator,
.current_tick = current_tick, .current_tick = 0,
.freqamps = freqamps, .current_index = 0,
.fal_array = fal_array,
}; };
} }
pub fn deinit(self: *FreqAmpBuffer) void { pub fn deinit(self: *FreqAmpBuffer) void {
//Need to redo this
_ = self; _ = self;
} }
pub fn increment_tick(self: *FreqAmpBuffer) void { pub fn increment_tick(self: *FreqAmpBuffer) void {
const prev_index = self.index_by_tick(self.current_tick); const prev_index = self.current_index;
self.current_tick += 1; self.current_tick += 1;
const cur_index = self.index_by_tick(self.current_tick); const cur_index = self.index_by_tick(self.current_tick);
self.current_index = cur_index;
self.freqamps[cur_index].refcount -= 1; self.fal_array[cur_index].refcount -= 1;
self.freqamps[prev_index].refcount += 1; self.fal_array[prev_index].refcount += 1;
if (self.freqamps[cur_index].refcount == 0) { if (self.fal_array[cur_index].refcount == 0) {
defer self.freqamps[cur_index].deinit(); defer self.fal_array[cur_index].deinit();
} }
self.freqamps[cur_index] = self.freqamps[prev_index]; self.fal_array[cur_index] = self.fal_array[prev_index];
} }
@ -122,14 +167,10 @@ pub const FreqAmpBuffer = struct {
return tick % self.buffer_ticks; return tick % self.buffer_ticks;
} }
pub fn current_index(self: *FreqAmpBuffer) u32 {
return self.index_by_tick(self.current_tick);
}
pub fn current_list(self: *FreqAmpBuffer) *FreqAmpList { pub fn current_list(self: *FreqAmpBuffer) *FreqAmpList {
const cur_index = self.index_by_tick(self.current_tick); const cur_index = self.index_by_tick(self.current_tick);
return self.freqamps[cur_index]; return self.fal_array[cur_index];
} }
@ -138,25 +179,25 @@ pub const FreqAmpBuffer = struct {
print("CURRENT STATE:\n", .{}); print("CURRENT STATE:\n", .{});
const prev_index = self.index_by_tick(self.current_tick-%1); const prev_index = self.index_by_tick(self.current_tick-%1);
const cur_index = self.index_by_tick(self.current_tick); const cur_index = self.current_index;
const next_index = self.index_by_tick(self.current_tick+%1); const next_index = self.index_by_tick(self.current_tick+%1);
print("{d}: {*}\n", .{self.current_tick-%1, self.freqamps[prev_index]}); print("{d}: {*}\n", .{self.current_tick-%1, self.fal_array[prev_index]});
self.freqamps[prev_index].prnt(); self.fal_array[prev_index].prnt();
print("{d}: {*}\n", .{self.current_tick, self.freqamps[cur_index]}); print("{d}: {*}\n", .{self.current_tick, self.fal_array[cur_index]});
self.freqamps[cur_index].prnt(); self.fal_array[cur_index].prnt();
print("{d}: {*}\n", .{self.current_tick+%1, self.freqamps[next_index]}); print("{d}: {*}\n", .{self.current_tick+%1, self.fal_array[next_index]});
self.freqamps[next_index].prnt(); self.fal_array[next_index].prnt();
} }
pub fn reset(self: *FreqAmpBuffer) !void { pub fn reset(self: *FreqAmpBuffer) !void {
const prev_index = self.index_by_tick(self.current_tick -% 1); const prev_index = self.index_by_tick(self.current_tick -% 1);
const cur_index = self.index_by_tick(self.current_tick); const cur_index = self.current_index;
if (self.freqamps[cur_index] == self.freqamps[prev_index]) { if (self.fal_array[cur_index] == self.fal_array[prev_index]) {
var clist = self.freqamps[cur_index]; var clist = self.fal_array[cur_index];
clist.refcount -= 1; clist.refcount -= 1;
@ -167,89 +208,29 @@ pub const FreqAmpBuffer = struct {
var new_list = try FreqAmpList.create(self.allocator); var new_list = try FreqAmpList.create(self.allocator);
new_list.refcount += 1; new_list.refcount += 1;
self.freqamps[cur_index] = new_list; self.fal_array[cur_index] = new_list;
} }
} }
pub fn setfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64) !void { pub fn setfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64) !void {
const cur_index = self.index_by_tick(self.current_tick); var current_fal = self.fal_array[self.current_index];
var clist = self.freqamps[cur_index]; try current_fal.setfreq(freq, r_amp);
// If this freq already is set, sum the r_amps
for (clist.lst.items) |freqamp| {
if (freqamp.freq == freq) {
freqamp.r_amp = r_amp;
return;
}
}
// Otherwise, append new freqamp
const fa = try FreqAmp.create(self.allocator, freq, r_amp);
try clist.lst.append(fa);
} }
pub fn addfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64) !void { pub fn addfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64) !void {
const cur_index = self.index_by_tick(self.current_tick); var current_fal = self.fal_array[self.current_index];
var clist = self.freqamps[cur_index]; try current_fal.addfreq(freq, r_amp);
// If this freq already is set, sum the r_amps
for (clist.lst.items) |freqamp| {
if (freqamp.freq == freq) {
freqamp.r_amp += r_amp;
if (freqamp.r_amp > 1) {
freqamp.r_amp = 1;
}
return;
}
}
// Otherwise, append new freqamp
const fa = try FreqAmp.create(self.allocator, freq, r_amp);
try clist.lst.append(fa);
} }
pub fn get_fal(self: *FreqAmpBuffer, tick: u32) *FreqAmpList {
return self.fal_array[self.index_by_tick(tick)];
}
}; };
pub fn nmain() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var fab = try FreqAmpBuffer.init(allocator);
defer fab.deinit();
try fab.reset();
try fab.setfreq(440.0, 0.84);
try fab.setfreq(440, 0.854);
try fab.setfreq(441.3, 0.8);
try fab.reset();
fab.prnt();
fab.increment_tick();
print("\nINCREMENT TICK\n\n", .{});
fab.prnt();
}

View file

@ -15,7 +15,7 @@ pub const Pnt = struct {
}; };
pub fn distanceBetweenPoints(pt1: *const Pnt, pt2: *const Pnt) f32 { pub fn distanceBetweenPoints(pt1: Pnt, pt2: Pnt) f32 {
if ((pt1.x == pt2.x) and (pt1.y == pt2.y) and (pt1.z == pt2.z)) { if ((pt1.x == pt2.x) and (pt1.y == pt2.y) and (pt1.z == pt2.z)) {
return 0; return 0;

View file

@ -26,7 +26,7 @@ pub fn main() !void {
var settings: SoundSettings = SoundSettings{}; var settings: SoundSettings = SoundSettings{};
const start_tick: u32 = 0; const start_tick: u32 = 0;
const end_tick: u32 = 44100 * 3; const end_tick: u32 = 44100 * 10;
var left: SoundNode = try SoundNode.init(allocator, "left_sink"); var left: SoundNode = try SoundNode.init(allocator, "left_sink");
defer left.deinit(); defer left.deinit();
@ -46,7 +46,7 @@ pub fn main() !void {
.soundnode = &sine, .soundnode = &sine,
}; };
a.operands[0] = 410.0; a.operands[0] = 820.0;
a.operands[1] = 0.5; a.operands[1] = 0.5;
var sine2: SoundNode = try SoundNode.init(allocator, "sine2"); var sine2: SoundNode = try SoundNode.init(allocator, "sine2");
@ -54,7 +54,7 @@ pub fn main() !void {
var a4 = Activity{ var a4 = Activity{
.start_tick = 0, .start_tick = 0,
.end_tick = end_tick, .end_tick = end_tick-44100,
.opcode = 3, .opcode = 3,
.soundnode = &sine2, .soundnode = &sine2,
}; };
@ -91,8 +91,8 @@ pub fn main() !void {
try soundnodes.append(&left); try soundnodes.append(&left);
try soundnodes.append(&right); try soundnodes.append(&right);
try left.wire_in.append(&sine); try left.air_in.append(&sine);
try right.wire_in.append(&sine2); try right.air_in.append(&sine2);
var tick: u32 = start_tick; var tick: u32 = start_tick;
@ -152,18 +152,18 @@ pub fn main() !void {
} }
soundnode.freqamp.increment_tick(); soundnode.fab.increment_tick();
_ = i; _ = i;
} }
var amp: f64 = 0; var amp: f64 = 0;
const current_index = left.freqamp.current_index(); const current_index = left.fab.current_index;
const clist_left = left.freqamp.freqamps[current_index]; const current_fal_left = left.fab.fal_array[current_index];
for (clist_left.lst.items) |freqamp| { for (current_fal_left.arraylist.items) |fa| {
amp += freqamp.r_amp * singleSineTick(&settings, freqamp.freq, tick); amp += fa.r_amp * singleSineTick(&settings, fa.freq, tick);
} }
sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp))); sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp)));
@ -171,10 +171,10 @@ pub fn main() !void {
amp = 0; amp = 0;
const clist_right = right.freqamp.freqamps[current_index]; const current_fal_right = right.fab.fal_array[current_index];
for (clist_right.lst.items) |freqamp| { for (current_fal_right.arraylist.items) |fa| {
amp += freqamp.r_amp * singleSineTick(&settings, freqamp.freq, tick); amp += fa.r_amp * singleSineTick(&settings, fa.freq, tick);
} }
sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp))); sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp)));

View file

@ -7,7 +7,8 @@ const ArrayList = std.ArrayList;
const AutoHashMap = std.AutoHashMap; const AutoHashMap = std.AutoHashMap;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const Pnt = @import("point.zig").Pnt; const pnt = @import("point.zig");
const Pnt = pnt.Pnt;
const Activity = @import("activity.zig").Activity; const Activity = @import("activity.zig").Activity;
const SoundSettings = @import("settings.zig").SoundSettings; const SoundSettings = @import("settings.zig").SoundSettings;
const FreqAmpBuffer = @import("freqamp.zig").FreqAmpBuffer; const FreqAmpBuffer = @import("freqamp.zig").FreqAmpBuffer;
@ -27,14 +28,14 @@ pub const SoundNode = struct {
wire_in: ArrayList(*SoundNode), wire_in: ArrayList(*SoundNode),
activities: ArrayList(*Activity), activities: ArrayList(*Activity),
freqamp: FreqAmpBuffer, fab: FreqAmpBuffer,
pub fn init(allocator: Allocator, name: []const u8) !SoundNode { pub fn init(allocator: Allocator, name: []const u8) !SoundNode {
const air_in = ArrayList(*SoundNode).init(allocator); const air_in = ArrayList(*SoundNode).init(allocator);
const wire_in = ArrayList(*SoundNode).init(allocator); const wire_in = ArrayList(*SoundNode).init(allocator);
const activities = ArrayList(*Activity).init(allocator); const activities = ArrayList(*Activity).init(allocator);
const freqamp = try FreqAmpBuffer.init(allocator); const fab = try FreqAmpBuffer.init(allocator);
return .{ return .{
.allocator = allocator, .allocator = allocator,
@ -42,7 +43,7 @@ pub const SoundNode = struct {
.air_in = air_in, .air_in = air_in,
.wire_in = wire_in, .wire_in = wire_in,
.activities = activities, .activities = activities,
.freqamp = freqamp, .fab = fab,
}; };
} }
@ -52,7 +53,19 @@ pub const SoundNode = struct {
self.air_in.deinit(); self.air_in.deinit();
self.wire_in.deinit(); self.wire_in.deinit();
self.activities.deinit(); self.activities.deinit();
self.freqamp.deinit(); self.fab.deinit();
}
pub fn current_tick(self: *SoundNode) u32 {
return self.fab.current_tick;
}
pub fn distance(self: *SoundNode, other: *SoundNode) f32 {
return pnt.distanceBetweenPoints(self.location, other.location);
} }