From 69cd15291c0f715813f2826cf00b1a2cccdb041c Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Sat, 6 Sep 2025 22:33:57 +0300 Subject: [PATCH] test writing to wav (failed) --- zigsonnum/activity.zig | 15 +++++ zigsonnum/sonnum.zig | 121 ++++++++++++++++++++++++++++++++++++++++ zigsonnum/soundnode.zig | 11 +++- 3 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 zigsonnum/activity.zig create mode 100644 zigsonnum/sonnum.zig diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig new file mode 100644 index 0000000..6391747 --- /dev/null +++ b/zigsonnum/activity.zig @@ -0,0 +1,15 @@ +const SoundNode = @import("soundnode.zig").SoundNode; + +pub const Activity = struct { + + start_tick: u64, + end_tick: u64, + opcode: u16, + soundnode: *SoundNode, + operands: ?[10]f16, + + pub fn do(self: *Activity) !void { + try self.soundnode.freqmap.put(440, 0.8); + } + +}; \ No newline at end of file diff --git a/zigsonnum/sonnum.zig b/zigsonnum/sonnum.zig new file mode 100644 index 0000000..5e1dbae --- /dev/null +++ b/zigsonnum/sonnum.zig @@ -0,0 +1,121 @@ +const std = @import("std"); +const math = std.math; +const print = std.debug.print; + +const ArrayList = std.ArrayList; +const Endian = std.builtin.Endian; + +const SoundNode = @import("soundnode.zig").SoundNode; +const Activity = @import("activity.zig").Activity; +const SoundSettings = @import("settings.zig").SoundSettings; + + +pub fn singleSineTick(st: *SoundSettings, freq: u16, t: u64) f64 { + const ft: f64 = @floatFromInt(t); + const fr: f64 = @floatFromInt(freq); + return math.sin(st.sine_multiplier * fr * ft); + //return math.sin(st.sine_multiplier * @as(f64, freq) * ft); +} + + +pub fn main() !void { + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); + + var settings: SoundSettings = SoundSettings{}; + + const start_tick: u64 = 0; + const end_tick: u64 = 44100 * 2; + + var sn1: SoundNode = try SoundNode.init(allocator, "sine"); + defer sn1.deinit(); + + var a = Activity{ + .start_tick = 0, + .end_tick = 0, + .opcode = 1, + .soundnode = &sn1, + .operands = undefined, + }; + + try sn1.activities.append(&a); + + var soundnodes = ArrayList(*SoundNode).init(allocator); + defer soundnodes.deinit(); + + try soundnodes.append(&sn1); + + var tick: u64 = start_tick; + + //STARTING WAV + const stdout_file = std.io.getStdOut().writer(); + var bw = std.io.bufferedWriter(stdout_file); + const stdout = bw.writer(); + + //WRITING WAV HEADER + try stdout.writeAll("RIFF"); + + try stdout.writeInt( + u64, + 36 + (end_tick * 2 * settings.sample_width), + Endian.little, + ); + + try stdout.writeAll("WAVE"); + try stdout.writeAll("fmt "); + + try stdout.writeInt(u32, 16, Endian.little); + try stdout.writeInt(u16, 1, Endian.little); + try stdout.writeInt(u16, 2, Endian.little); + try stdout.writeInt(u32, settings.sample_rate, Endian.little); + + const block_align: u16 = @intCast(2 * settings.sample_width); + + try stdout.writeInt(u32, @as(u32, block_align) * settings.sample_rate, Endian.little); + try stdout.writeInt(u16, block_align, Endian.little); + try stdout.writeInt(u16, settings.bit_depth, Endian.little); + + try stdout.writeAll("data"); + try stdout.writeInt( + u64, + end_tick * 2 * settings.sample_width, + Endian.little, + ); + + var sample: i24 = 0.0; + + while (tick < end_tick) { + + for (soundnodes.items, 0..) |soundnode, i| { + + for (soundnode.activities.items, 0..) |activity, j| { + + try activity.do(); + _ = j; + + } + + var amp: f64 = 0; + var it = soundnode.freqmap.iterator(); + + while (it.next()) |entry| { + + amp += entry.value_ptr.* * singleSineTick(&settings, @as(u16, entry.key_ptr.*), tick); + } + + sample = @intFromFloat(amp * @as(f64, @floatFromInt(settings.max_amp))); + + try stdout.writeInt(i24, sample, Endian.little); + try stdout.writeInt(i24, sample, Endian.little); + + _ = i; + } + + tick += 1; + + } + + try bw.flush(); + +} \ No newline at end of file diff --git a/zigsonnum/soundnode.zig b/zigsonnum/soundnode.zig index d980ed0..ea14030 100644 --- a/zigsonnum/soundnode.zig +++ b/zigsonnum/soundnode.zig @@ -8,6 +8,7 @@ const AutoHashMap = std.AutoHashMap; const Allocator = std.mem.Allocator; const Pnt = @import("point.zig").Pnt; +const Activity = @import("activity.zig").Activity; const SoundSettings = @import("settings.zig").SoundSettings; const utility = @import("utility.zig"); @@ -23,20 +24,23 @@ pub const SoundNode = struct { air_in: ArrayList(*SoundNode), wire_in: ArrayList(*SoundNode), + activities: ArrayList(*Activity), - freqmap: AutoHashMap(u64, f16), + freqmap: AutoHashMap(u16, f16), pub fn init(allocator: Allocator, name: []const u8) !SoundNode { const air_in = ArrayList(*SoundNode).init(allocator); const wire_in = ArrayList(*SoundNode).init(allocator); - const freqmap = AutoHashMap(u64, f16).init(allocator); + const activities = ArrayList(*Activity).init(allocator); + const freqmap = AutoHashMap(u16, f16).init(allocator); //REDO THIS TO INCORPORATE TICKS return .{ .allocator = allocator, .name = name, .air_in = air_in, .wire_in = wire_in, + .activities = activities, .freqmap = freqmap, }; @@ -46,6 +50,7 @@ pub const SoundNode = struct { self.air_in.deinit(); self.wire_in.deinit(); + self.activities.deinit(); self.freqmap.deinit(); } @@ -57,7 +62,7 @@ pub fn singleSineTick(st: *SoundSettings, freq: f16, t: u64) f64 { return math.sin(st.sine_multiplier * @as(f64, freq) * ft); } -pub fn main() !void { +pub fn nmain() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator();