diff --git a/zigsonnum/freqamp.zig b/zigsonnum/freqamp.zig index a124eec..b5fd075 100644 --- a/zigsonnum/freqamp.zig +++ b/zigsonnum/freqamp.zig @@ -18,7 +18,7 @@ pub const FreqAmp = struct { } pub fn prnt(self: *FreqAmp) void { - print("\n", .{self.freq, self.r_amp}); + print("<{d}::{d}>\n", .{self.freq, self.r_amp}); } }; @@ -29,17 +29,6 @@ pub const FreqAmpList = struct { refcount: u64, lst: ArrayList(*FreqAmp), - pub fn init(allocator: Allocator) FreqAmpList { - - const lst = ArrayList(*FreqAmp).init(allocator); - - return .{ - .allocator = allocator, - .refcount = 0, - .lst = lst, - }; - - } pub fn create(allocator: Allocator) !*FreqAmpList { @@ -57,6 +46,11 @@ pub const FreqAmpList = struct { } pub fn deinit(self: *FreqAmpList) void { + + for (self.lst.items) |freqamp| { + self.allocator.destroy(freqamp); + } + self.lst.deinit(); } @@ -86,9 +80,8 @@ pub const FreqAmpBuffer = struct { const current_tick = 0; - //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(); @@ -107,6 +100,24 @@ pub const FreqAmpBuffer = struct { _ = self; } + pub fn increment_tick(self: *FreqAmpBuffer) void { + + const prev_index = self.index_by_tick(self.current_tick); + + self.current_tick += 1; + const current_index = self.index_by_tick(self.current_tick); + + self.freqamps[current_index].refcount -= 1; + self.freqamps[prev_index].refcount += 1; + + if (self.freqamps[current_index].refcount == 0) { + defer self.freqamps[current_index].deinit(); + } + + self.freqamps[current_index] = self.freqamps[prev_index]; + + } + pub fn index_by_tick(self: *FreqAmpBuffer, tick: u64) u64 { return tick % self.buffer_ticks; } @@ -122,8 +133,11 @@ pub const FreqAmpBuffer = struct { print("CURRENT STATE:\n", .{}); print("0: {*}\n", .{self.freqamps[0]}); + self.freqamps[0].prnt(); print("1: {*}\n", .{self.freqamps[1]}); + self.freqamps[1].prnt(); print("2: {*}\n", .{self.freqamps[2]}); + self.freqamps[2].prnt(); } @@ -131,22 +145,17 @@ pub const FreqAmpBuffer = struct { const current_index = self.index_by_tick(self.current_tick); var clist = self.freqamps[current_index]; + + clist.refcount -= 1; - clist.prnt(); - - if (clist.refcount <= 1) { + if (clist.refcount == 0) { defer clist.deinit(); } - clist.refcount -= 1; - - //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(); } @@ -156,7 +165,7 @@ pub const FreqAmpBuffer = struct { 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| { @@ -172,9 +181,6 @@ pub const FreqAmpBuffer = struct { // Otherwise, append new freqamp 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); @@ -191,18 +197,15 @@ pub fn main() !void { 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); fab.prnt(); + + fab.increment_tick(); + print("\nINCREMENT TICK\n\n", .{}); + + fab.prnt(); - if (0 == 0) { - var clist = fab.current_list(); - print("RESULT\n", .{}); - print("!! {*} !!\n", .{clist}); - clist.prnt(); - } } \ No newline at end of file diff --git a/zigsonnum/soundnode.zig b/zigsonnum/soundnode.zig index b3298dd..fb05a95 100644 --- a/zigsonnum/soundnode.zig +++ b/zigsonnum/soundnode.zig @@ -10,6 +10,7 @@ const Allocator = std.mem.Allocator; const Pnt = @import("point.zig").Pnt; 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; @@ -27,6 +28,7 @@ pub const SoundNode = struct { activities: ArrayList(*Activity), freqmap: AutoHashMap(u16, f16), + freqamp: FreqAmpBuffer, pub fn init(allocator: Allocator, name: []const u8) SoundNode { @@ -34,6 +36,7 @@ pub const SoundNode = struct { const wire_in = ArrayList(*SoundNode).init(allocator); const activities = ArrayList(*Activity).init(allocator); const freqmap = AutoHashMap(u16, f16).init(allocator); //REDO THIS TO INCORPORATE TICKS + const freqamp = FreqAmpBuffer.init(allocator); return .{ .allocator = allocator, @@ -42,6 +45,7 @@ pub const SoundNode = struct { .wire_in = wire_in, .activities = activities, .freqmap = freqmap, + .freqamp = freqamp, }; }