somewhat working attempt

This commit is contained in:
aprilnightk 2025-09-07 22:32:39 +03:00
parent 2eb9ed0aba
commit b542e9e9e8

View file

@ -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("<Freq::{d} ; R_Amp::{d}>\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,18 +140,22 @@ 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
@ -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();
}
}