sonnum/zigsonnum/freqamp.zig

208 lines
No EOL
4.1 KiB
Zig

const std = @import("std");
const print = std.debug.print;
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
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});
}
};
pub const FreqAmpList = struct {
allocator: Allocator,
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 {
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();
}
pub fn prnt(self: *FreqAmpList) void {
print("<Freqamp List (referenced {d} times)>\n", .{self.refcount});
for (self.lst.items) |freqamp| {
freqamp.prnt();
}
print("<Freqamp List End>\n", .{});
}
};
pub const FreqAmpBuffer = struct {
const buffer_ticks_default = 44100 * 2;
allocator: Allocator,
buffer_ticks: u64 = buffer_ticks_default,
current_tick: u64,
freqamps: [buffer_ticks_default]*FreqAmpList,
pub fn init(allocator: Allocator) !FreqAmpBuffer {
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();
var freqamps: [buffer_ticks_default]*FreqAmpList = undefined;
@memset(&freqamps, initial_list);
return .{
.allocator = allocator,
.current_tick = current_tick,
.freqamps = freqamps,
};
}
pub fn deinit(self: *FreqAmpBuffer) void {
_ = self;
}
pub fn index_by_tick(self: *FreqAmpBuffer, tick: u64) u64 {
return tick % self.buffer_ticks;
}
pub fn current_list(self: *FreqAmpBuffer) *FreqAmpList {
const current_index = self.index_by_tick(self.current_tick);
return self.freqamps[current_index];
}
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];
clist.prnt();
if (clist.refcount <= 1) {
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();
}
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) {
freqamp.r_amp = r_amp;
return;
}
}
// 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);
}
};
pub fn main() !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);
fab.prnt();
if (0 == 0) {
var clist = fab.current_list();
print("RESULT\n", .{});
print("!! {*} !!\n", .{clist});
clist.prnt();
}
}