diff --git a/examples/sysaudio/main.zig b/examples/sysaudio/main.zig index af3fedef..0551b31a 100644 --- a/examples/sysaudio/main.zig +++ b/examples/sysaudio/main.zig @@ -1,5 +1,6 @@ const std = @import("std"); const mach = @import("mach"); +const gpu = @import("gpu"); const sysaudio = mach.sysaudio; const js = mach.sysjs; const builtin = @import("builtin"); @@ -48,10 +49,12 @@ pub fn update(app: *App, engine: *mach.Core) !void { } } - const back_buffer_view = engine.swap_chain.?.getCurrentTextureView(); + if (builtin.cpu.arch != .wasm32) { + const back_buffer_view = engine.swap_chain.?.getCurrentTextureView(); - engine.swap_chain.?.present(); - back_buffer_view.release(); + engine.swap_chain.?.present(); + back_buffer_view.release(); + } } // A simple tone engine. diff --git a/libs/sysaudio/src/main.zig b/libs/sysaudio/src/main.zig index 85578500..c18cef73 100644 --- a/libs/sysaudio/src/main.zig +++ b/libs/sysaudio/src/main.zig @@ -27,38 +27,6 @@ pub const Format = enum { F32, }; -pub const DeviceOptions = struct { - mode: Mode = .output, - format: ?Format = null, - is_raw: ?bool = null, - channels: ?u8 = null, - sample_rate: ?u32 = null, - id: ?[:0]const u8 = null, - name: ?[]const u8 = null, -}; - -pub const DeviceProperties = struct { - mode: Mode, - format: Format, - is_raw: bool, - channels: u8, - sample_rate: u32, - id: [:0]const u8, - name: []const u8, - - pub fn intoConfig(properties: DeviceProperties) DeviceOptions { - return .{ - .mode = properties.mode, - .format = properties.format, - .is_raw = properties.is_raw, - .channels = properties.channels, - .sample_rate = properties.sample_rate, - .id = properties.id, - .name = properties.name, - }; - } -}; - const Audio = @This(); backend: Backend, diff --git a/libs/sysaudio/src/soundio.zig b/libs/sysaudio/src/soundio.zig index 2b917e5d..e2d96a26 100644 --- a/libs/sysaudio/src/soundio.zig +++ b/libs/sysaudio/src/soundio.zig @@ -1,7 +1,5 @@ const std = @import("std"); const Mode = @import("main.zig").Mode; -const DeviceOptions = @import("main.zig").DeviceOptions; -const DeviceProperties = @import("main.zig").DeviceProperties; const Format = @import("main.zig").Format; const c = @import("soundio").c; const Aim = @import("soundio").Aim; @@ -35,8 +33,25 @@ pub const Device = struct { planar_buffer: [512000]u8 = undefined, started: bool = false, - pub const Options = DeviceOptions; - pub const Properties = DeviceProperties; + pub const Options = struct { + mode: Mode = .output, + format: ?Format = null, + is_raw: ?bool = null, + channels: ?u8 = null, + sample_rate: ?u32 = null, + id: ?[:0]const u8 = null, + name: ?[]const u8 = null, + }; + + pub const Properties = struct { + mode: Mode, + format: Format, + is_raw: bool, + channels: u8, + sample_rate: u32, + id: [:0]const u8, + name: []const u8, + }; pub fn deinit(self: *Device, allocator: std.mem.Allocator) void { switch (self.handle) { @@ -176,14 +191,14 @@ pub const DeviceIterator = struct { device_len: u16, index: u16, - pub fn next(self: *DeviceIterator) IteratorError!?DeviceOptions { + pub fn next(self: *DeviceIterator) IteratorError!?Device.Options { if (self.index < self.device_len) { const device_desc = switch (self.mode) { .input => self.ctx.handle.getInputDevice(self.index) orelse return null, .output => self.ctx.handle.getOutputDevice(self.index) orelse return null, }; self.index += 1; - return DeviceOptions{ + return Device.Options{ .mode = switch (@intToEnum(Aim, device_desc.handle.aim)) { .input => .input, .output => .output, @@ -251,7 +266,7 @@ pub fn waitEvents(self: Audio) void { self.handle.waitEvents(); } -pub fn requestDevice(self: Audio, allocator: std.mem.Allocator, options: DeviceOptions) Error!*Device { +pub fn requestDevice(self: Audio, allocator: std.mem.Allocator, options: Device.Options) Error!*Device { var sio_device: SoundIoDevice = undefined; if (options.id) |id| { @@ -324,7 +339,7 @@ pub fn requestDevice(self: Audio, allocator: std.mem.Allocator, options: DeviceO // }; // const name = std.mem.sliceTo(name_ptr, 0); - var properties = DeviceProperties{ + var properties = Device.Properties{ .is_raw = options.is_raw orelse false, .format = format, .mode = options.mode, diff --git a/libs/sysaudio/src/webaudio.zig b/libs/sysaudio/src/webaudio.zig index 4ab6e553..3fe7dc3d 100644 --- a/libs/sysaudio/src/webaudio.zig +++ b/libs/sysaudio/src/webaudio.zig @@ -1,7 +1,6 @@ const std = @import("std"); const Mode = @import("main.zig").Mode; -const DeviceOptions = @import("main.zig").DeviceOptions; -const DeviceProperties = @import("main.zig").DeviceProperties; +const Format = @import("main.zig").Format; const js = @import("sysjs"); const Audio = @This(); @@ -12,13 +11,30 @@ else *const fn (device: *Device, user_data: ?*anyopaque, buffer: []u8) void; pub const Device = struct { - properties: DeviceProperties, + properties: Properties, // Internal fields. context: js.Object, - pub const Options = DeviceOptions; - pub const Properties = DeviceProperties; + pub const Options = struct { + mode: Mode = .output, + format: ?Format = null, + is_raw: ?bool = null, + channels: ?u8 = null, + sample_rate: ?u32 = null, + id: ?[:0]const u8 = null, + name: ?[]const u8 = null, + }; + + pub const Properties = struct { + mode: Mode, + format: Format, + is_raw: bool, + channels: u8, + sample_rate: u32, + id: [:0]const u8, + name: []const u8, + }; pub fn deinit(device: *Device, allocator: std.mem.Allocator) void { device.context.deinit(); @@ -45,7 +61,7 @@ pub const DeviceIterator = struct { ctx: *Audio, mode: Mode, - pub fn next(_: DeviceIterator) IteratorError!?DeviceProperties { + pub fn next(_: DeviceIterator) IteratorError!?Device.Properties { return null; } }; @@ -78,7 +94,7 @@ const default_channel_count = 2; const default_sample_rate = 48000; const default_buffer_size_per_channel = 1024; // 21.33ms -pub fn requestDevice(audio: Audio, allocator: std.mem.Allocator, options: DeviceOptions) Error!*Device { +pub fn requestDevice(audio: Audio, allocator: std.mem.Allocator, options: Device.Options) Error!*Device { // NOTE: WebAudio only supports F32 audio format, so options.format is unused const mode = options.mode; const channels = options.channels orelse default_channel_count; @@ -117,9 +133,13 @@ pub fn requestDevice(audio: Audio, allocator: std.mem.Allocator, options: Device _ = node.call("connect", &.{destination.toValue()}); } - var properties = DeviceProperties{ + // TODO(sysaudio): Figure out ID/name or make optional again + var properties = Device.Properties{ + .id = "0", + .name = "WebAudio", .format = .F32, - .mode = options.mode orelse .output, + .mode = options.mode, + .is_raw = false, .channels = options.channels orelse default_channel_count, .sample_rate = options.sample_rate orelse default_sample_rate, }; diff --git a/src/platform/wasm.zig b/src/platform/wasm.zig index b829b749..4d0d6bcb 100644 --- a/src/platform/wasm.zig +++ b/src/platform/wasm.zig @@ -3,6 +3,7 @@ const app_pkg = @import("app"); const Core = @import("../Core.zig"); const structs = @import("../structs.zig"); const enums = @import("../enums.zig"); +const gpu = @import("gpu"); const js = struct { extern fn machCanvasInit(selector_id: *u8) CanvasId;