{sysaudio,examples}: get sysaudio running on linux, separate audio configuration and descriptor (#518)
* Get sysaudio working on linux * Separate audio configuration and descriptor * Config/Descriptor -> Options/Properties - Rename sysaudio DeviceConfig and DeviceDescriptor to Device.Options and Device.Properties - example: Convert buffer before passing to renderWithType * make Device.start() idempotent
This commit is contained in:
parent
f807c85232
commit
0e71daf504
4 changed files with 193 additions and 98 deletions
|
|
@ -1,6 +1,7 @@
|
|||
const std = @import("std");
|
||||
const Mode = @import("main.zig").Mode;
|
||||
const DeviceDescriptor = @import("main.zig").DeviceDescriptor;
|
||||
const DeviceOptions = @import("main.zig").DeviceOptions;
|
||||
const DeviceProperties = @import("main.zig").DeviceProperties;
|
||||
const js = @import("sysjs");
|
||||
|
||||
const Audio = @This();
|
||||
|
|
@ -11,11 +12,14 @@ else
|
|||
*const fn (device: *Device, user_data: ?*anyopaque, buffer: []u8) void;
|
||||
|
||||
pub const Device = struct {
|
||||
descriptor: DeviceDescriptor,
|
||||
properties: DeviceProperties,
|
||||
|
||||
// Internal fields.
|
||||
context: js.Object,
|
||||
|
||||
pub const Options = DeviceOptions;
|
||||
pub const Properties = DeviceProperties;
|
||||
|
||||
pub fn deinit(device: *Device, allocator: std.mem.Allocator) void {
|
||||
device.context.deinit();
|
||||
allocator.destroy(device);
|
||||
|
|
@ -41,7 +45,7 @@ pub const DeviceIterator = struct {
|
|||
ctx: *Audio,
|
||||
mode: Mode,
|
||||
|
||||
pub fn next(_: DeviceIterator) IteratorError!?DeviceDescriptor {
|
||||
pub fn next(_: DeviceIterator) IteratorError!?DeviceProperties {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
|
@ -67,18 +71,18 @@ pub fn deinit(audio: Audio) void {
|
|||
audio.context_constructor.deinit();
|
||||
}
|
||||
|
||||
// TODO)sysaudio): implement waitEvents for WebAudio, will a WASM process terminate without this?
|
||||
// TODO(sysaudio): implement waitEvents for WebAudio, will a WASM process terminate without this?
|
||||
pub fn waitEvents(_: Audio) void {}
|
||||
|
||||
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, config: DeviceDescriptor) Error!*Device {
|
||||
// NOTE: WebAudio only supports F32 audio format, so config.format is unused
|
||||
const mode = config.mode orelse .output;
|
||||
const channels = config.channels orelse default_channel_count;
|
||||
const sample_rate = config.sample_rate orelse default_sample_rate;
|
||||
pub fn requestDevice(audio: Audio, allocator: std.mem.Allocator, options: DeviceOptions) 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;
|
||||
const sample_rate = options.sample_rate orelse default_sample_rate;
|
||||
|
||||
const context_options = js.createMap();
|
||||
defer context_options.deinit();
|
||||
|
|
@ -113,15 +117,16 @@ pub fn requestDevice(audio: Audio, allocator: std.mem.Allocator, config: DeviceD
|
|||
_ = node.call("connect", &.{destination.toValue()});
|
||||
}
|
||||
|
||||
// TODO(sysaudio): introduce a descriptor type that has non-optional fields.
|
||||
var descriptor = config;
|
||||
descriptor.mode = descriptor.mode orelse .output;
|
||||
descriptor.channels = descriptor.channels orelse default_channel_count;
|
||||
descriptor.sample_rate = descriptor.sample_rate orelse default_sample_rate;
|
||||
var properties = DeviceProperties {
|
||||
.format = .F32,
|
||||
.mode = options.mode orelse .output,
|
||||
.channels = options.channels orelse default_channel_count,
|
||||
.sample_rate = options.sample_rate orelse default_sample_rate,
|
||||
};
|
||||
|
||||
const device = try allocator.create(Device);
|
||||
device.* = .{
|
||||
.descriptor = descriptor,
|
||||
.properties = properties,
|
||||
.context = context,
|
||||
};
|
||||
return device;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue