From b542e9e9e8a0aa113b63be1cd4602f1857856b34 Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Sun, 7 Sep 2025 22:32:39 +0300 Subject: [PATCH] somewhat working attempt --- zigsonnum/freqamp.zig | 80 +++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 18 deletions(-) diff --git a/zigsonnum/freqamp.zig b/zigsonnum/freqamp.zig index 2b7c473..a124eec 100644 --- a/zigsonnum/freqamp.zig +++ b/zigsonnum/freqamp.zig @@ -9,6 +9,14 @@ pub const FreqAmp = struct { freq: f16, r_amp: f16, + pub fn create(allocator: Allocator, freq: f16, r_amp: f16) !*FreqAmp { + + const fa = try allocator.create(FreqAmp); + fa.* = .{.freq = freq, .r_amp = r_amp}; + return fa; + + } + pub fn prnt(self: *FreqAmp) void { print("\n", .{self.freq, self.r_amp}); } @@ -33,6 +41,21 @@ pub const FreqAmpList = struct { } + pub fn create(allocator: Allocator) !*FreqAmpList { + + const lst = ArrayList(*FreqAmp).init(allocator); + const fal = try allocator.create(FreqAmpList); + + fal.* = .{ + .allocator = allocator, + .refcount = 0, + .lst = lst, + }; + + return fal; + + } + pub fn deinit(self: *FreqAmpList) void { self.lst.deinit(); } @@ -59,17 +82,19 @@ pub const FreqAmpBuffer = struct { current_tick: u64, freqamps: [buffer_ticks_default]*FreqAmpList, - pub fn init(allocator: Allocator) FreqAmpBuffer { + pub fn init(allocator: Allocator) !FreqAmpBuffer { const current_tick = 0; - var initial_list = FreqAmpList.init(allocator); + //var initial_list = FreqAmpList.init(allocator); + var initial_list = try FreqAmpList.create(allocator); + print("{any}", .{@TypeOf(initial_list)}); initial_list.refcount = buffer_ticks_default; initial_list.prnt(); var freqamps: [buffer_ticks_default]*FreqAmpList = undefined; - @memset(&freqamps, &initial_list); + @memset(&freqamps, initial_list); return .{ .allocator = allocator, @@ -93,7 +118,16 @@ pub const FreqAmpBuffer = struct { } - pub fn reset(self: *FreqAmpBuffer) void { + pub fn prnt(self: *FreqAmpBuffer) void { + + print("CURRENT STATE:\n", .{}); + print("0: {*}\n", .{self.freqamps[0]}); + print("1: {*}\n", .{self.freqamps[1]}); + print("2: {*}\n", .{self.freqamps[2]}); + + } + + pub fn reset(self: *FreqAmpBuffer) !void { const current_index = self.index_by_tick(self.current_tick); var clist = self.freqamps[current_index]; @@ -106,21 +140,25 @@ pub const FreqAmpBuffer = struct { clist.refcount -= 1; - var new_list = FreqAmpList.init(self.allocator); + //var new_list = FreqAmpList.init(self.allocator); + var new_list = try FreqAmpList.create(self.allocator); + print("!! {*} !! {any}\n", .{new_list, @TypeOf(new_list)}); + + new_list.refcount += 1; + self.freqamps[current_index] = new_list; new_list.prnt(); - self.freqamps[current_index] = &new_list; - self.freqamps[current_index].refcount += 1; - new_list.prnt(); } pub fn setfreq(self: *FreqAmpBuffer, freq: f16, r_amp: f16) !void { const current_index = self.index_by_tick(self.current_tick); var clist = self.freqamps[current_index]; + + print("!! {*} !!\n", .{clist}); clist.prnt(); // If this freq already is set, sum the r_amps - + for (clist.lst.items) |freqamp| { if (freqamp.freq == freq) { @@ -133,9 +171,10 @@ pub const FreqAmpBuffer = struct { // Otherwise, append new freqamp - var fa = try self.allocator.create(FreqAmp); - fa.freq = freq; - fa.r_amp = r_amp; + const fa = try FreqAmp.create(self.allocator, freq, r_amp); + //var fa = try self.allocator.create(FreqAmp); + //fa.freq = freq; + //fa.r_amp = r_amp; try clist.lst.append(fa); @@ -149,16 +188,21 @@ pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); - var fab = FreqAmpBuffer.init(allocator); + var fab = try FreqAmpBuffer.init(allocator); defer fab.deinit(); + try fab.reset(); - fab.reset(); try fab.setfreq(440.0, 0.84); - try fab.setfreq(440.0, 0.854); + try fab.setfreq(440, 0.854); + try fab.setfreq(441.3, 0.8); - var clist = fab.current_list(); - print("RESULT\n", .{}); - clist.prnt(); + fab.prnt(); + if (0 == 0) { + var clist = fab.current_list(); + print("RESULT\n", .{}); + print("!! {*} !!\n", .{clist}); + clist.prnt(); + } } \ No newline at end of file