somewhat working attempt
This commit is contained in:
parent
2eb9ed0aba
commit
b542e9e9e8
1 changed files with 62 additions and 18 deletions
|
|
@ -9,6 +9,14 @@ pub const FreqAmp = struct {
|
||||||
freq: f16,
|
freq: f16,
|
||||||
r_amp: 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 {
|
pub fn prnt(self: *FreqAmp) void {
|
||||||
print("<Freq::{d} ; R_Amp::{d}>\n", .{self.freq, self.r_amp});
|
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 {
|
pub fn deinit(self: *FreqAmpList) void {
|
||||||
self.lst.deinit();
|
self.lst.deinit();
|
||||||
}
|
}
|
||||||
|
|
@ -59,17 +82,19 @@ pub const FreqAmpBuffer = struct {
|
||||||
current_tick: u64,
|
current_tick: u64,
|
||||||
freqamps: [buffer_ticks_default]*FreqAmpList,
|
freqamps: [buffer_ticks_default]*FreqAmpList,
|
||||||
|
|
||||||
pub fn init(allocator: Allocator) FreqAmpBuffer {
|
pub fn init(allocator: Allocator) !FreqAmpBuffer {
|
||||||
|
|
||||||
const current_tick = 0;
|
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.refcount = buffer_ticks_default;
|
||||||
initial_list.prnt();
|
initial_list.prnt();
|
||||||
|
|
||||||
var freqamps: [buffer_ticks_default]*FreqAmpList = undefined;
|
var freqamps: [buffer_ticks_default]*FreqAmpList = undefined;
|
||||||
|
|
||||||
@memset(&freqamps, &initial_list);
|
@memset(&freqamps, initial_list);
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.allocator = allocator,
|
.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);
|
const current_index = self.index_by_tick(self.current_tick);
|
||||||
var clist = self.freqamps[current_index];
|
var clist = self.freqamps[current_index];
|
||||||
|
|
@ -106,21 +140,25 @@ pub const FreqAmpBuffer = struct {
|
||||||
|
|
||||||
clist.refcount -= 1;
|
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();
|
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 {
|
pub fn setfreq(self: *FreqAmpBuffer, freq: f16, r_amp: f16) !void {
|
||||||
|
|
||||||
const current_index = self.index_by_tick(self.current_tick);
|
const current_index = self.index_by_tick(self.current_tick);
|
||||||
var clist = self.freqamps[current_index];
|
var clist = self.freqamps[current_index];
|
||||||
|
|
||||||
|
print("!! {*} !!\n", .{clist});
|
||||||
clist.prnt();
|
clist.prnt();
|
||||||
// If this freq already is set, sum the r_amps
|
// If this freq already is set, sum the r_amps
|
||||||
|
|
||||||
for (clist.lst.items) |freqamp| {
|
for (clist.lst.items) |freqamp| {
|
||||||
|
|
||||||
if (freqamp.freq == freq) {
|
if (freqamp.freq == freq) {
|
||||||
|
|
@ -133,9 +171,10 @@ pub const FreqAmpBuffer = struct {
|
||||||
|
|
||||||
// Otherwise, append new freqamp
|
// Otherwise, append new freqamp
|
||||||
|
|
||||||
var fa = try self.allocator.create(FreqAmp);
|
const fa = try FreqAmp.create(self.allocator, freq, r_amp);
|
||||||
fa.freq = freq;
|
//var fa = try self.allocator.create(FreqAmp);
|
||||||
fa.r_amp = r_amp;
|
//fa.freq = freq;
|
||||||
|
//fa.r_amp = r_amp;
|
||||||
|
|
||||||
try clist.lst.append(fa);
|
try clist.lst.append(fa);
|
||||||
|
|
||||||
|
|
@ -149,16 +188,21 @@ pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var fab = FreqAmpBuffer.init(allocator);
|
var fab = try FreqAmpBuffer.init(allocator);
|
||||||
defer fab.deinit();
|
defer fab.deinit();
|
||||||
|
|
||||||
|
try fab.reset();
|
||||||
|
|
||||||
fab.reset();
|
|
||||||
try fab.setfreq(440.0, 0.84);
|
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();
|
fab.prnt();
|
||||||
print("RESULT\n", .{});
|
|
||||||
clist.prnt();
|
|
||||||
|
|
||||||
|
if (0 == 0) {
|
||||||
|
var clist = fab.current_list();
|
||||||
|
print("RESULT\n", .{});
|
||||||
|
print("!! {*} !!\n", .{clist});
|
||||||
|
clist.prnt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue