91 lines
2.1 KiB
Zig
91 lines
2.1 KiB
Zig
const std = @import("std");
|
|
const print = std.debug.print;
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
const ArrayList = std.ArrayList;
|
|
|
|
|
|
pub const FreqAmpBuffer = struct {
|
|
|
|
const buffer_ticks_default = 44100 * 2;
|
|
|
|
allocator: Allocator,
|
|
buffer_ticks: u32 = buffer_ticks_default,
|
|
current_tick: u32,
|
|
current_index: u32,
|
|
fal_array: [buffer_ticks_default]f64 = std.mem.zeroes([buffer_ticks_default]f64),
|
|
|
|
pub fn init(allocator: Allocator) !FreqAmpBuffer {
|
|
|
|
return .{
|
|
.allocator = allocator,
|
|
.current_tick = 0,
|
|
.current_index = 0,
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *FreqAmpBuffer) void {
|
|
|
|
//Need to redo this
|
|
_ = self;
|
|
|
|
}
|
|
|
|
pub fn increment_tick(self: *FreqAmpBuffer) void {
|
|
|
|
self.current_tick += 1;
|
|
|
|
const cur_index = self.index_by_tick(self.current_tick);
|
|
self.current_index = cur_index;
|
|
|
|
self.fal_array[cur_index] = 0;
|
|
|
|
}
|
|
|
|
pub fn index_by_tick(self: *FreqAmpBuffer, tick: u32) u32 {
|
|
return tick % self.buffer_ticks;
|
|
}
|
|
|
|
pub fn prnt(self: *FreqAmpBuffer) void {
|
|
|
|
print("CURRENT STATE:\n", .{});
|
|
|
|
const prev_index = self.index_by_tick(self.current_tick-%1);
|
|
const cur_index = self.current_index;
|
|
const next_index = self.index_by_tick(self.current_tick+%1);
|
|
|
|
print("{d}: {*}\n", .{self.current_tick-%1, self.fal_array[prev_index]});
|
|
print("{d}\n", .{self.fal_array[prev_index]});
|
|
print("{d}: {*}\n", .{self.current_tick, self.fal_array[cur_index]});
|
|
print("{d}\n", .{self.fal_array[cur_index]});
|
|
print("{d}: {*}\n", .{self.current_tick+%1, self.fal_array[next_index]});
|
|
print("{d}\n", .{self.fal_array[next_index]});
|
|
|
|
}
|
|
|
|
|
|
pub fn add_r_amp(self: *FreqAmpBuffer, r_amp: f64) void {
|
|
|
|
self.fal_array[self.current_index] += r_amp;
|
|
|
|
if (self.fal_array[self.current_index] > 1) {
|
|
self.fal_array[self.current_index] = 1;
|
|
} else if (self.fal_array[self.current_index] < -1) {
|
|
self.fal_array[self.current_index] = -1;
|
|
}
|
|
}
|
|
|
|
pub fn get_current_r_amp(self: *FreqAmpBuffer) f64 {
|
|
|
|
return self.fal_array[self.current_index];
|
|
|
|
}
|
|
|
|
pub fn get_r_amp(self: *FreqAmpBuffer, tick: u32) f64 {
|
|
|
|
const ind: u32 = self.index_by_tick(tick);
|
|
return self.fal_array[ind];
|
|
|
|
}
|
|
|
|
};
|