From 04690d9df80ff6775545de662ff33104efd6d948 Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Tue, 9 Sep 2025 12:56:30 +0300 Subject: [PATCH] phase support --- zigsonnum/activity.zig | 9 +++++---- zigsonnum/freqamp.zig | 27 ++++++++++++++------------- zigsonnum/sonnum.zig | 29 +++++++++++++++-------------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig index 701c0c5..d3b7465 100644 --- a/zigsonnum/activity.zig +++ b/zigsonnum/activity.zig @@ -26,11 +26,12 @@ pub const Activity = struct { } pub fn setfreq(self: *Activity) !void { - try self.soundnode.fab.setfreq(self.operands[0], self.operands[1]); + try self.soundnode.fab.setfreq(self.operands[0], self.operands[1], self.operands[2]); } pub fn slide_freq(self: *Activity) !void { + // NEEDS REDO try self.soundnode.fab.reset(); const init_freq: f64 = self.operands[0]; @@ -42,7 +43,7 @@ pub const Activity = struct { const res_freq: f64 = init_freq + @as(f64, @floatCast(freqdiff * @as(f128, @floatFromInt(elapsed)) )); - try self.soundnode.fab.setfreq(res_freq, self.operands[2]); + try self.soundnode.fab.setfreq(res_freq, self.operands[2], 0); if (self.soundnode.fab.current_tick % 1000 == 0) { print("TIMEDIFF {d}\n", .{timediff}); @@ -64,7 +65,7 @@ pub const Activity = struct { current_fal = wired_sn.fab.fal_array[current_index]; for (current_fal.arraylist.items) |fa| { - try self.soundnode.fab.addfreq(fa.freq, fa.r_amp); + try self.soundnode.fab.addfreq(fa.freq, fa.r_amp, fa.phase); } _ = i; @@ -81,7 +82,7 @@ pub const Activity = struct { 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); + try self.soundnode.fab.addfreq(fa.freq, fa.r_amp * attenuation, fa.phase); } _ = i; diff --git a/zigsonnum/freqamp.zig b/zigsonnum/freqamp.zig index f4a57fa..56bd837 100644 --- a/zigsonnum/freqamp.zig +++ b/zigsonnum/freqamp.zig @@ -8,17 +8,18 @@ pub const FreqAmp = struct { freq: f64, r_amp: f64, + phase: f64, - pub fn create(allocator: Allocator, freq: f64, r_amp: f64) !*FreqAmp { + pub fn create(allocator: Allocator, freq: f64, r_amp: f64, phase: f64) !*FreqAmp { const fa = try allocator.create(FreqAmp); - fa.* = .{.freq = freq, .r_amp = r_amp}; + fa.* = .{.freq = freq, .r_amp = r_amp, .phase = phase}; return fa; } pub fn prnt(self: *FreqAmp) void { - print("<{d}::{d}>\n", .{self.freq, self.r_amp}); + print("<{d}::{d}::{d}>\n", .{self.freq, self.r_amp, self.phase}); } }; @@ -45,11 +46,11 @@ pub const FreqAmpList = struct { } - pub fn setfreq(self: *FreqAmpList, freq: f64, r_amp: f64) !void { + pub fn setfreq(self: *FreqAmpList, freq: f64, r_amp: f64, phase: f64) !void { for (self.arraylist.items) |fa| { - if (fa.freq == freq) { + if (fa.freq == freq and fa.phase == phase) { fa.r_amp = r_amp; return; @@ -58,16 +59,16 @@ pub const FreqAmpList = struct { } - const fa = try FreqAmp.create(self.allocator, freq, r_amp); + const fa = try FreqAmp.create(self.allocator, freq, r_amp, phase); try self.arraylist.append(fa); } - pub fn addfreq(self: *FreqAmpList, freq: f64, r_amp: f64) !void { + pub fn addfreq(self: *FreqAmpList, freq: f64, r_amp: f64, phase: f64) !void { for (self.arraylist.items) |fa| { - if (fa.freq == freq) { + if (fa.freq == freq and fa.phase == phase) { fa.r_amp = r_amp; @@ -81,7 +82,7 @@ pub const FreqAmpList = struct { } - const fa = try FreqAmp.create(self.allocator, freq, r_amp); + const fa = try FreqAmp.create(self.allocator, freq, r_amp, phase); try self.arraylist.append(fa); } @@ -213,17 +214,17 @@ pub const FreqAmpBuffer = struct { } - pub fn setfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64) !void { + pub fn setfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64, phase: f64) !void { var current_fal = self.fal_array[self.current_index]; - try current_fal.setfreq(freq, r_amp); + try current_fal.setfreq(freq, r_amp, phase); } - pub fn addfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64) !void { + pub fn addfreq(self: *FreqAmpBuffer, freq: f64, r_amp: f64, phase: f64) !void { var current_fal = self.fal_array[self.current_index]; - try current_fal.addfreq(freq, r_amp); + try current_fal.addfreq(freq, r_amp, phase); } diff --git a/zigsonnum/sonnum.zig b/zigsonnum/sonnum.zig index 60be31c..72f09d9 100644 --- a/zigsonnum/sonnum.zig +++ b/zigsonnum/sonnum.zig @@ -8,11 +8,12 @@ const Endian = std.builtin.Endian; const SoundNode = @import("soundnode.zig").SoundNode; const Activity = @import("activity.zig").Activity; const SoundSettings = @import("settings.zig").SoundSettings; +const utility = @import("utility.zig"); -pub fn singleSineTick(st: *SoundSettings, freq: f64, t: u32) f64 { +pub fn singleSineTick(st: *SoundSettings, freq: f64, t: u32, phase: f64) f64 { const ft: f64 = @floatFromInt(t); - return math.sin(st.sine_multiplier * freq * ft); + return math.sin(st.sine_multiplier * freq * ft - phase * utility.tau); } @@ -46,7 +47,7 @@ pub fn main() !void { .soundnode = &sine, }; - a.operands[0] = 820.0; + a.operands[0] = 440.0; a.operands[1] = 0.5; var sine2: SoundNode = try SoundNode.init(allocator, "sine2"); @@ -54,17 +55,17 @@ pub fn main() !void { var a4 = Activity{ .start_tick = 0, - .end_tick = end_tick-44100, - .opcode = 3, - .soundnode = &sine2, + .end_tick = 0, + .opcode = 1, + .soundnode = &sine, }; - a4.operands[0] = 410.0; - a4.operands[1] = 820.0; - a4.operands[2] = 0.5; + a4.operands[0] = 449.0; + a4.operands[1] = 0.5; + a4.operands[2] = 0.55; try sine.activities.append(&a); - try sine2.activities.append(&a4); + try sine.activities.append(&a4); var a2 = Activity{ .start_tick = 0, @@ -91,8 +92,8 @@ pub fn main() !void { try soundnodes.append(&left); try soundnodes.append(&right); - try left.air_in.append(&sine); - try right.air_in.append(&sine2); + try left.wire_in.append(&sine); + try right.wire_in.append(&sine); var tick: u32 = start_tick; @@ -163,7 +164,7 @@ pub fn main() !void { const current_fal_left = left.fab.fal_array[current_index]; for (current_fal_left.arraylist.items) |fa| { - amp += fa.r_amp * singleSineTick(&settings, fa.freq, tick); + amp += fa.r_amp * singleSineTick(&settings, fa.freq, tick, fa.phase); } sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp))); @@ -174,7 +175,7 @@ pub fn main() !void { const current_fal_right = right.fab.fal_array[current_index]; for (current_fal_right.arraylist.items) |fa| { - amp += fa.r_amp * singleSineTick(&settings, fa.freq, tick); + amp += fa.r_amp * singleSineTick(&settings, fa.freq, tick, fa.phase); } sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp)));