refactored, working core
This commit is contained in:
parent
d33bd771f8
commit
866b9e66c1
5 changed files with 158 additions and 144 deletions
|
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
const print = std.debug.print;
|
||||
const SoundNode = @import("soundnode.zig").SoundNode;
|
||||
const FreqAmpList = @import("freqamp.zig").FreqAmpList;
|
||||
|
||||
pub const Activity = struct {
|
||||
|
||||
|
|
@ -8,7 +9,7 @@ pub const Activity = struct {
|
|||
end_tick: u32,
|
||||
opcode: u16,
|
||||
soundnode: *SoundNode,
|
||||
operands: [10]f64 = std.mem.zeroes([10]f64),
|
||||
operands: [6]f64 = std.mem.zeroes([6]f64),
|
||||
|
||||
pub fn do(self: *Activity) !void {
|
||||
switch (self.opcode) {
|
||||
|
|
@ -21,47 +22,66 @@ pub const Activity = struct {
|
|||
}
|
||||
|
||||
pub fn reset(self: *Activity) !void {
|
||||
try self.soundnode.freqamp.reset();
|
||||
try self.soundnode.fab.reset();
|
||||
}
|
||||
|
||||
pub fn setfreq(self: *Activity) !void {
|
||||
try self.soundnode.freqamp.setfreq(self.operands[0], self.operands[1]);
|
||||
try self.soundnode.fab.setfreq(self.operands[0], self.operands[1]);
|
||||
}
|
||||
|
||||
pub fn slide_freq(self: *Activity) !void {
|
||||
|
||||
try self.soundnode.freqamp.reset();
|
||||
try self.soundnode.fab.reset();
|
||||
|
||||
const init_freq: f64 = self.operands[0];
|
||||
const final_freq: f64 = self.operands[1];
|
||||
|
||||
const timediff: u32 = self.end_tick - self.start_tick;
|
||||
const elapsed: u32 = self.soundnode.freqamp.current_tick - self.start_tick;
|
||||
const elapsed: u32 = self.soundnode.fab.current_tick - self.start_tick;
|
||||
const freqdiff: f128 = ((@as(f128, final_freq - init_freq))) / @as(f128, @floatFromInt(timediff));
|
||||
|
||||
const res_freq: f64 = init_freq + @as(f64, @floatCast(freqdiff * @as(f128, @floatFromInt(elapsed)) ));
|
||||
|
||||
try self.soundnode.freqamp.setfreq(res_freq, self.operands[2]);
|
||||
try self.soundnode.fab.setfreq(res_freq, self.operands[2]);
|
||||
|
||||
if (self.soundnode.freqamp.current_tick % 44100 == 0) {
|
||||
if (self.soundnode.fab.current_tick % 1000 == 0) {
|
||||
print("TIMEDIFF {d}\n", .{timediff});
|
||||
print("FREQDIFF {d}\n", .{@as(f64, @floatCast(freqdiff))});
|
||||
print("RESFREQ {d}\n", .{res_freq});
|
||||
self.soundnode.freqamp.prnt();
|
||||
print("RESFREQ {d}\n", .{res_freq});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn relay(self: *Activity) !void {
|
||||
|
||||
try self.soundnode.freqamp.reset();
|
||||
try self.soundnode.fab.reset();
|
||||
const current_tick: u32 = self.soundnode.fab.current_tick;
|
||||
const current_index: u32 = self.soundnode.fab.current_index;
|
||||
|
||||
var current_fal: *FreqAmpList = undefined;
|
||||
|
||||
for (self.soundnode.wire_in.items, 0..) |wired_sn, i| {
|
||||
|
||||
const current_index: u32 = self.soundnode.freqamp.current_index();
|
||||
const clist = wired_sn.freqamp.freqamps[current_index];
|
||||
current_fal = wired_sn.fab.fal_array[current_index];
|
||||
|
||||
for (clist.lst.items) |freqamp| {
|
||||
try self.soundnode.freqamp.addfreq(freqamp.freq, freqamp.r_amp);
|
||||
for (current_fal.arraylist.items) |fa| {
|
||||
try self.soundnode.fab.addfreq(fa.freq, fa.r_amp);
|
||||
}
|
||||
|
||||
_ = i;
|
||||
|
||||
}
|
||||
|
||||
for (self.soundnode.air_in.items, 0..) |aired_sn, i| {
|
||||
|
||||
const dist: f32 = self.soundnode.distance(aired_sn);
|
||||
const sample_tick: u32 = @intFromFloat(@floor(@as(f32, @floatFromInt(current_tick)) - @floor(128.571428 * dist)));
|
||||
|
||||
const sample_index = aired_sn.fab.index_by_tick(sample_tick);
|
||||
const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0)));
|
||||
current_fal = aired_sn.fab.fal_array[sample_index];
|
||||
|
||||
for (current_fal.arraylist.items) |fa| {
|
||||
try self.soundnode.fab.addfreq(fa.freq, fa.r_amp * attenuation);
|
||||
}
|
||||
|
||||
_ = i;
|
||||
|
|
|
|||
|
|
@ -27,38 +27,79 @@ pub const FreqAmpList = struct {
|
|||
|
||||
allocator: Allocator,
|
||||
refcount: u64,
|
||||
lst: ArrayList(*FreqAmp),
|
||||
arraylist: ArrayList(*FreqAmp),
|
||||
|
||||
|
||||
pub fn create(allocator: Allocator) !*FreqAmpList {
|
||||
|
||||
const lst = ArrayList(*FreqAmp).init(allocator);
|
||||
const arraylist = ArrayList(*FreqAmp).init(allocator);
|
||||
const fal = try allocator.create(FreqAmpList);
|
||||
|
||||
fal.* = .{
|
||||
.allocator = allocator,
|
||||
.refcount = 0,
|
||||
.lst = lst,
|
||||
.arraylist = arraylist,
|
||||
};
|
||||
|
||||
return fal;
|
||||
|
||||
}
|
||||
|
||||
pub fn setfreq(self: *FreqAmpList, freq: f64, r_amp: f64) !void {
|
||||
|
||||
for (self.arraylist.items) |fa| {
|
||||
|
||||
if (fa.freq == freq) {
|
||||
|
||||
fa.r_amp = r_amp;
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const fa = try FreqAmp.create(self.allocator, freq, r_amp);
|
||||
try self.arraylist.append(fa);
|
||||
|
||||
}
|
||||
|
||||
pub fn addfreq(self: *FreqAmpList, freq: f64, r_amp: f64) !void {
|
||||
|
||||
for (self.arraylist.items) |fa| {
|
||||
|
||||
if (fa.freq == freq) {
|
||||
|
||||
fa.r_amp = r_amp;
|
||||
|
||||
if (fa.r_amp > 1) {
|
||||
fa.r_amp = 1;
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const fa = try FreqAmp.create(self.allocator, freq, r_amp);
|
||||
try self.arraylist.append(fa);
|
||||
|
||||
}
|
||||
|
||||
pub fn deinit(self: *FreqAmpList) void {
|
||||
|
||||
for (self.lst.items) |freqamp| {
|
||||
for (self.arraylist.items) |freqamp| {
|
||||
self.allocator.destroy(freqamp);
|
||||
}
|
||||
|
||||
self.lst.deinit();
|
||||
self.arraylist.deinit();
|
||||
}
|
||||
|
||||
pub fn prnt(self: *FreqAmpList) void {
|
||||
|
||||
print("<Freqamp List (referenced {d} times)>\n", .{self.refcount});
|
||||
|
||||
for (self.lst.items) |freqamp| {
|
||||
for (self.arraylist.items) |freqamp| {
|
||||
freqamp.prnt();
|
||||
}
|
||||
|
||||
|
|
@ -69,52 +110,56 @@ pub const FreqAmpList = struct {
|
|||
|
||||
pub const FreqAmpBuffer = struct {
|
||||
|
||||
const buffer_ticks_default = 44100;
|
||||
const buffer_ticks_default = 44100 * 2;
|
||||
|
||||
allocator: Allocator,
|
||||
buffer_ticks: u32 = buffer_ticks_default,
|
||||
current_tick: u32,
|
||||
freqamps: [buffer_ticks_default]*FreqAmpList,
|
||||
current_index: u32,
|
||||
fal_array: [buffer_ticks_default]*FreqAmpList,
|
||||
|
||||
pub fn init(allocator: Allocator) !FreqAmpBuffer {
|
||||
|
||||
const current_tick = 0;
|
||||
|
||||
|
||||
//Creating an empty filler FreaAmpList
|
||||
var initial_list = try FreqAmpList.create(allocator);
|
||||
|
||||
initial_list.refcount = buffer_ticks_default;
|
||||
initial_list.prnt();
|
||||
|
||||
var freqamps: [buffer_ticks_default]*FreqAmpList = undefined;
|
||||
|
||||
@memset(&freqamps, initial_list);
|
||||
|
||||
//Initializing the array with empry FreaAmpLists
|
||||
var fal_array: [buffer_ticks_default]*FreqAmpList = undefined;
|
||||
@memset(&fal_array, initial_list);
|
||||
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.current_tick = current_tick,
|
||||
.freqamps = freqamps,
|
||||
.current_tick = 0,
|
||||
.current_index = 0,
|
||||
.fal_array = fal_array,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *FreqAmpBuffer) void {
|
||||
|
||||
//Need to redo this
|
||||
_ = self;
|
||||
|
||||
}
|
||||
|
||||
pub fn increment_tick(self: *FreqAmpBuffer) void {
|
||||
|
||||
const prev_index = self.index_by_tick(self.current_tick);
|
||||
const prev_index = self.current_index;
|
||||
|
||||
self.current_tick += 1;
|
||||
|
||||
const cur_index = self.index_by_tick(self.current_tick);
|
||||
self.current_index = cur_index;
|
||||
|
||||
self.freqamps[cur_index].refcount -= 1;
|
||||
self.freqamps[prev_index].refcount += 1;
|
||||
self.fal_array[cur_index].refcount -= 1;
|
||||
self.fal_array[prev_index].refcount += 1;
|
||||
|
||||
if (self.freqamps[cur_index].refcount == 0) {
|
||||
defer self.freqamps[cur_index].deinit();
|
||||
if (self.fal_array[cur_index].refcount == 0) {
|
||||
defer self.fal_array[cur_index].deinit();
|
||||
}
|
||||
|
||||
self.freqamps[cur_index] = self.freqamps[prev_index];
|
||||
self.fal_array[cur_index] = self.fal_array[prev_index];
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -122,14 +167,10 @@ pub const FreqAmpBuffer = struct {
|
|||
return tick % self.buffer_ticks;
|
||||
}
|
||||
|
||||
pub fn current_index(self: *FreqAmpBuffer) u32 {
|
||||
return self.index_by_tick(self.current_tick);
|
||||
}
|
||||
|
||||
pub fn current_list(self: *FreqAmpBuffer) *FreqAmpList {
|
||||
|
||||
const cur_index = self.index_by_tick(self.current_tick);
|
||||
return self.freqamps[cur_index];
|
||||
return self.fal_array[cur_index];
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -138,25 +179,25 @@ pub const FreqAmpBuffer = struct {
|
|||
print("CURRENT STATE:\n", .{});
|
||||
|
||||
const prev_index = self.index_by_tick(self.current_tick-%1);
|
||||
const cur_index = self.index_by_tick(self.current_tick);
|
||||
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.freqamps[prev_index]});
|
||||
self.freqamps[prev_index].prnt();
|
||||
print("{d}: {*}\n", .{self.current_tick, self.freqamps[cur_index]});
|
||||
self.freqamps[cur_index].prnt();
|
||||
print("{d}: {*}\n", .{self.current_tick+%1, self.freqamps[next_index]});
|
||||
self.freqamps[next_index].prnt();
|
||||
print("{d}: {*}\n", .{self.current_tick-%1, self.fal_array[prev_index]});
|
||||
self.fal_array[prev_index].prnt();
|
||||
print("{d}: {*}\n", .{self.current_tick, self.fal_array[cur_index]});
|
||||
self.fal_array[cur_index].prnt();
|
||||
print("{d}: {*}\n", .{self.current_tick+%1, self.fal_array[next_index]});
|
||||
self.fal_array[next_index].prnt();
|
||||
|
||||
}
|
||||
|
||||
pub fn reset(self: *FreqAmpBuffer) !void {
|
||||
|
||||
const prev_index = self.index_by_tick(self.current_tick -% 1);
|
||||
const cur_index = self.index_by_tick(self.current_tick);
|
||||
const cur_index = self.current_index;
|
||||
|
||||
if (self.freqamps[cur_index] == self.freqamps[prev_index]) {
|
||||
var clist = self.freqamps[cur_index];
|
||||
if (self.fal_array[cur_index] == self.fal_array[prev_index]) {
|
||||
var clist = self.fal_array[cur_index];
|
||||
|
||||
clist.refcount -= 1;
|
||||
|
||||
|
|
@ -167,89 +208,29 @@ pub const FreqAmpBuffer = struct {
|
|||
var new_list = try FreqAmpList.create(self.allocator);
|
||||
|
||||
new_list.refcount += 1;
|
||||
self.freqamps[cur_index] = new_list;
|
||||
self.fal_array[cur_index] = new_list;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn setfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64) !void {
|
||||
|
||||
const cur_index = self.index_by_tick(self.current_tick);
|
||||
var clist = self.freqamps[cur_index];
|
||||
|
||||
// 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);
|
||||
|
||||
try clist.lst.append(fa);
|
||||
var current_fal = self.fal_array[self.current_index];
|
||||
try current_fal.setfreq(freq, r_amp);
|
||||
|
||||
}
|
||||
|
||||
pub fn addfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64) !void {
|
||||
|
||||
const cur_index = self.index_by_tick(self.current_tick);
|
||||
var clist = self.freqamps[cur_index];
|
||||
|
||||
// If this freq already is set, sum the r_amps
|
||||
|
||||
for (clist.lst.items) |freqamp| {
|
||||
|
||||
if (freqamp.freq == freq) {
|
||||
|
||||
freqamp.r_amp += r_amp;
|
||||
|
||||
if (freqamp.r_amp > 1) {
|
||||
freqamp.r_amp = 1;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Otherwise, append new freqamp
|
||||
|
||||
const fa = try FreqAmp.create(self.allocator, freq, r_amp);
|
||||
|
||||
try clist.lst.append(fa);
|
||||
var current_fal = self.fal_array[self.current_index];
|
||||
try current_fal.addfreq(freq, r_amp);
|
||||
|
||||
}
|
||||
|
||||
pub fn get_fal(self: *FreqAmpBuffer, tick: u32) *FreqAmpList {
|
||||
|
||||
return self.fal_array[self.index_by_tick(tick)];
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
pub fn nmain() !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);
|
||||
|
||||
try fab.reset();
|
||||
fab.prnt();
|
||||
|
||||
fab.increment_tick();
|
||||
print("\nINCREMENT TICK\n\n", .{});
|
||||
|
||||
fab.prnt();
|
||||
|
||||
}
|
||||
|
|
@ -15,7 +15,7 @@ pub const Pnt = struct {
|
|||
|
||||
};
|
||||
|
||||
pub fn distanceBetweenPoints(pt1: *const Pnt, pt2: *const Pnt) f32 {
|
||||
pub fn distanceBetweenPoints(pt1: Pnt, pt2: Pnt) f32 {
|
||||
|
||||
if ((pt1.x == pt2.x) and (pt1.y == pt2.y) and (pt1.z == pt2.z)) {
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ pub fn main() !void {
|
|||
var settings: SoundSettings = SoundSettings{};
|
||||
|
||||
const start_tick: u32 = 0;
|
||||
const end_tick: u32 = 44100 * 3;
|
||||
const end_tick: u32 = 44100 * 10;
|
||||
|
||||
var left: SoundNode = try SoundNode.init(allocator, "left_sink");
|
||||
defer left.deinit();
|
||||
|
|
@ -46,7 +46,7 @@ pub fn main() !void {
|
|||
.soundnode = &sine,
|
||||
};
|
||||
|
||||
a.operands[0] = 410.0;
|
||||
a.operands[0] = 820.0;
|
||||
a.operands[1] = 0.5;
|
||||
|
||||
var sine2: SoundNode = try SoundNode.init(allocator, "sine2");
|
||||
|
|
@ -54,7 +54,7 @@ pub fn main() !void {
|
|||
|
||||
var a4 = Activity{
|
||||
.start_tick = 0,
|
||||
.end_tick = end_tick,
|
||||
.end_tick = end_tick-44100,
|
||||
.opcode = 3,
|
||||
.soundnode = &sine2,
|
||||
};
|
||||
|
|
@ -91,8 +91,8 @@ pub fn main() !void {
|
|||
try soundnodes.append(&left);
|
||||
try soundnodes.append(&right);
|
||||
|
||||
try left.wire_in.append(&sine);
|
||||
try right.wire_in.append(&sine2);
|
||||
try left.air_in.append(&sine);
|
||||
try right.air_in.append(&sine2);
|
||||
|
||||
var tick: u32 = start_tick;
|
||||
|
||||
|
|
@ -152,18 +152,18 @@ pub fn main() !void {
|
|||
}
|
||||
|
||||
|
||||
soundnode.freqamp.increment_tick();
|
||||
soundnode.fab.increment_tick();
|
||||
_ = i;
|
||||
}
|
||||
|
||||
var amp: f64 = 0;
|
||||
|
||||
const current_index = left.freqamp.current_index();
|
||||
const current_index = left.fab.current_index;
|
||||
|
||||
const clist_left = left.freqamp.freqamps[current_index];
|
||||
const current_fal_left = left.fab.fal_array[current_index];
|
||||
|
||||
for (clist_left.lst.items) |freqamp| {
|
||||
amp += freqamp.r_amp * singleSineTick(&settings, freqamp.freq, tick);
|
||||
for (current_fal_left.arraylist.items) |fa| {
|
||||
amp += fa.r_amp * singleSineTick(&settings, fa.freq, tick);
|
||||
}
|
||||
|
||||
sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp)));
|
||||
|
|
@ -171,10 +171,10 @@ pub fn main() !void {
|
|||
|
||||
amp = 0;
|
||||
|
||||
const clist_right = right.freqamp.freqamps[current_index];
|
||||
const current_fal_right = right.fab.fal_array[current_index];
|
||||
|
||||
for (clist_right.lst.items) |freqamp| {
|
||||
amp += freqamp.r_amp * singleSineTick(&settings, freqamp.freq, tick);
|
||||
for (current_fal_right.arraylist.items) |fa| {
|
||||
amp += fa.r_amp * singleSineTick(&settings, fa.freq, tick);
|
||||
}
|
||||
|
||||
sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp)));
|
||||
|
|
|
|||
|
|
@ -7,7 +7,8 @@ const ArrayList = std.ArrayList;
|
|||
const AutoHashMap = std.AutoHashMap;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const Pnt = @import("point.zig").Pnt;
|
||||
const pnt = @import("point.zig");
|
||||
const Pnt = pnt.Pnt;
|
||||
const Activity = @import("activity.zig").Activity;
|
||||
const SoundSettings = @import("settings.zig").SoundSettings;
|
||||
const FreqAmpBuffer = @import("freqamp.zig").FreqAmpBuffer;
|
||||
|
|
@ -27,14 +28,14 @@ pub const SoundNode = struct {
|
|||
wire_in: ArrayList(*SoundNode),
|
||||
activities: ArrayList(*Activity),
|
||||
|
||||
freqamp: FreqAmpBuffer,
|
||||
fab: FreqAmpBuffer,
|
||||
|
||||
pub fn init(allocator: Allocator, name: []const u8) !SoundNode {
|
||||
|
||||
const air_in = ArrayList(*SoundNode).init(allocator);
|
||||
const wire_in = ArrayList(*SoundNode).init(allocator);
|
||||
const activities = ArrayList(*Activity).init(allocator);
|
||||
const freqamp = try FreqAmpBuffer.init(allocator);
|
||||
const fab = try FreqAmpBuffer.init(allocator);
|
||||
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
|
|
@ -42,7 +43,7 @@ pub const SoundNode = struct {
|
|||
.air_in = air_in,
|
||||
.wire_in = wire_in,
|
||||
.activities = activities,
|
||||
.freqamp = freqamp,
|
||||
.fab = fab,
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -52,8 +53,20 @@ pub const SoundNode = struct {
|
|||
self.air_in.deinit();
|
||||
self.wire_in.deinit();
|
||||
self.activities.deinit();
|
||||
self.freqamp.deinit();
|
||||
self.fab.deinit();
|
||||
|
||||
}
|
||||
|
||||
pub fn current_tick(self: *SoundNode) u32 {
|
||||
|
||||
return self.fab.current_tick;
|
||||
|
||||
}
|
||||
|
||||
pub fn distance(self: *SoundNode, other: *SoundNode) f32 {
|
||||
|
||||
return pnt.distanceBetweenPoints(self.location, other.location);
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue