sysaudio: fix compile for web

This commit is contained in:
Louis Pearson 2022-09-09 23:14:24 -06:00 committed by Stephen Gutekanst
parent b72c2c978f
commit 70f4514c66
5 changed files with 59 additions and 52 deletions

View file

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const mach = @import("mach"); const mach = @import("mach");
const gpu = @import("gpu");
const sysaudio = mach.sysaudio; const sysaudio = mach.sysaudio;
const js = mach.sysjs; const js = mach.sysjs;
const builtin = @import("builtin"); 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(); engine.swap_chain.?.present();
back_buffer_view.release(); back_buffer_view.release();
}
} }
// A simple tone engine. // A simple tone engine.

View file

@ -27,38 +27,6 @@ pub const Format = enum {
F32, 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(); const Audio = @This();
backend: Backend, backend: Backend,

View file

@ -1,7 +1,5 @@
const std = @import("std"); const std = @import("std");
const Mode = @import("main.zig").Mode; 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 Format = @import("main.zig").Format;
const c = @import("soundio").c; const c = @import("soundio").c;
const Aim = @import("soundio").Aim; const Aim = @import("soundio").Aim;
@ -35,8 +33,25 @@ pub const Device = struct {
planar_buffer: [512000]u8 = undefined, planar_buffer: [512000]u8 = undefined,
started: bool = false, started: bool = false,
pub const Options = DeviceOptions; pub const Options = struct {
pub const Properties = DeviceProperties; 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 { pub fn deinit(self: *Device, allocator: std.mem.Allocator) void {
switch (self.handle) { switch (self.handle) {
@ -176,14 +191,14 @@ pub const DeviceIterator = struct {
device_len: u16, device_len: u16,
index: u16, index: u16,
pub fn next(self: *DeviceIterator) IteratorError!?DeviceOptions { pub fn next(self: *DeviceIterator) IteratorError!?Device.Options {
if (self.index < self.device_len) { if (self.index < self.device_len) {
const device_desc = switch (self.mode) { const device_desc = switch (self.mode) {
.input => self.ctx.handle.getInputDevice(self.index) orelse return null, .input => self.ctx.handle.getInputDevice(self.index) orelse return null,
.output => self.ctx.handle.getOutputDevice(self.index) orelse return null, .output => self.ctx.handle.getOutputDevice(self.index) orelse return null,
}; };
self.index += 1; self.index += 1;
return DeviceOptions{ return Device.Options{
.mode = switch (@intToEnum(Aim, device_desc.handle.aim)) { .mode = switch (@intToEnum(Aim, device_desc.handle.aim)) {
.input => .input, .input => .input,
.output => .output, .output => .output,
@ -251,7 +266,7 @@ pub fn waitEvents(self: Audio) void {
self.handle.waitEvents(); 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; var sio_device: SoundIoDevice = undefined;
if (options.id) |id| { 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); // const name = std.mem.sliceTo(name_ptr, 0);
var properties = DeviceProperties{ var properties = Device.Properties{
.is_raw = options.is_raw orelse false, .is_raw = options.is_raw orelse false,
.format = format, .format = format,
.mode = options.mode, .mode = options.mode,

View file

@ -1,7 +1,6 @@
const std = @import("std"); const std = @import("std");
const Mode = @import("main.zig").Mode; const Mode = @import("main.zig").Mode;
const DeviceOptions = @import("main.zig").DeviceOptions; const Format = @import("main.zig").Format;
const DeviceProperties = @import("main.zig").DeviceProperties;
const js = @import("sysjs"); const js = @import("sysjs");
const Audio = @This(); const Audio = @This();
@ -12,13 +11,30 @@ else
*const fn (device: *Device, user_data: ?*anyopaque, buffer: []u8) void; *const fn (device: *Device, user_data: ?*anyopaque, buffer: []u8) void;
pub const Device = struct { pub const Device = struct {
properties: DeviceProperties, properties: Properties,
// Internal fields. // Internal fields.
context: js.Object, context: js.Object,
pub const Options = DeviceOptions; pub const Options = struct {
pub const Properties = DeviceProperties; 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 { pub fn deinit(device: *Device, allocator: std.mem.Allocator) void {
device.context.deinit(); device.context.deinit();
@ -45,7 +61,7 @@ pub const DeviceIterator = struct {
ctx: *Audio, ctx: *Audio,
mode: Mode, mode: Mode,
pub fn next(_: DeviceIterator) IteratorError!?DeviceProperties { pub fn next(_: DeviceIterator) IteratorError!?Device.Properties {
return null; return null;
} }
}; };
@ -78,7 +94,7 @@ const default_channel_count = 2;
const default_sample_rate = 48000; const default_sample_rate = 48000;
const default_buffer_size_per_channel = 1024; // 21.33ms 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 // NOTE: WebAudio only supports F32 audio format, so options.format is unused
const mode = options.mode; const mode = options.mode;
const channels = options.channels orelse default_channel_count; 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()}); _ = 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, .format = .F32,
.mode = options.mode orelse .output, .mode = options.mode,
.is_raw = false,
.channels = options.channels orelse default_channel_count, .channels = options.channels orelse default_channel_count,
.sample_rate = options.sample_rate orelse default_sample_rate, .sample_rate = options.sample_rate orelse default_sample_rate,
}; };

View file

@ -3,6 +3,7 @@ const app_pkg = @import("app");
const Core = @import("../Core.zig"); const Core = @import("../Core.zig");
const structs = @import("../structs.zig"); const structs = @import("../structs.zig");
const enums = @import("../enums.zig"); const enums = @import("../enums.zig");
const gpu = @import("gpu");
const js = struct { const js = struct {
extern fn machCanvasInit(selector_id: *u8) CanvasId; extern fn machCanvasInit(selector_id: *u8) CanvasId;