Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: update to latest Zig version #34

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: Setup Zig
run: |
sudo apt install xz-utils
sudo sh -c 'wget -c https://pkg.machengine.org/zig/zig-linux-x86_64-0.12.0-dev.1092+68ed78775.tar.xz -O - | tar -xJ --strip-components=1 -C /usr/local/bin'
sudo sh -c 'wget -c https://pkg.machengine.org/zig/zig-linux-x86_64-0.12.0-dev.2063+804cee3b9.tar.xz -O - | tar -xJ --strip-components=1 -C /usr/local/bin'
- name: x86_64-linux -> aarch64-macos
run: zig build -Dtarget=aarch64-macos
- name: build
Expand All @@ -26,10 +26,10 @@ jobs:
- name: Setup Zig
run: |
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri "https://pkg.machengine.org/zig/zig-windows-x86_64-0.12.0-dev.1092+68ed78775.zip" -OutFile "C:\zig.zip"
Invoke-WebRequest -Uri "https://pkg.machengine.org/zig/zig-windows-x86_64-0.12.0-dev.2063+804cee3b9.zip" -OutFile "C:\zig.zip"
cd C:\
7z x zig.zip
Add-Content $env:GITHUB_PATH "C:\zig-windows-x86_64-0.12.0-dev.1092+68ed78775\"
Add-Content $env:GITHUB_PATH "C:\zig-windows-x86_64-0.12.0-dev.2063+804cee3b9\"
- name: build
run: zig build
- name: test
Expand All @@ -42,7 +42,7 @@ jobs:
- name: Setup Zig
run: |
brew install xz
sudo sh -c 'wget -c https://pkg.machengine.org/zig/zig-macos-x86_64-0.12.0-dev.1092+68ed78775.tar.xz -O - | tar -xJ --strip-components=1 -C /usr/local/bin'
sudo sh -c 'wget -c https://pkg.machengine.org/zig/zig-macos-x86_64-0.12.0-dev.2063+804cee3b9.tar.xz -O - | tar -xJ --strip-components=1 -C /usr/local/bin'
- name: build
run: zig build
- name: test
Expand Down
45 changes: 31 additions & 14 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,32 @@ const sysaudio = @import("mach_sysaudio");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const module = b.addModule("mach-opus", .{ .source_file = .{ .path = "src/lib.zig" } });

const sysaudio_dep = b.dependency("mach_sysaudio", .{
.target = target,
.optimize = optimize,
});
const opusfile_dep = b.dependency("opusfile", .{
.target = target,
.optimize = optimize,
});
const opusenc_dep = b.dependency("opusenc", .{
.target = target,
.optimize = optimize,
});

const module = b.addModule("mach-opus", .{
.root_source_file = .{ .path = "src/lib.zig" },
});
module.linkLibrary(opusfile_dep.artifact("opusfile"));
module.linkLibrary(opusenc_dep.artifact("opusenc"));

const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/lib.zig" },
.target = target,
.optimize = optimize,
});
link(b, main_tests);
addPaths(main_tests);
b.installArtifact(main_tests);

const test_step = b.step("test", "Run library tests");
Expand All @@ -24,13 +42,9 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
example.addModule("mach-opus", module);
example.addModule("mach-sysaudio", b.dependency("mach_sysaudio", .{
.target = target,
.optimize = optimize,
}).module("mach-sysaudio"));
link(b, example);
sysaudio.link(b, example);
example.root_module.addImport("mach-opus", module);
example.root_module.addImport("mach-sysaudio", sysaudio_dep.module("mach-sysaudio"));
addPaths(example);
b.installArtifact(example);

const example_run_cmd = b.addRunArtifact(example);
Expand All @@ -40,9 +54,12 @@ pub fn build(b: *std.Build) void {
example_run_step.dependOn(&example_run_cmd.step);
}

pub fn link(b: *std.Build, step: *std.build.CompileStep) void {
step.linkLibrary(b.dependency("opusfile", .{
.target = step.target,
.optimize = step.optimize,
}).artifact("opusfile"));
pub fn addPaths(step: *std.Build.Step.Compile) void {
sysaudio.addPaths(step);
}

pub fn link(b: *std.Build, step: *std.Build.Step.Compile) void {
_ = b;
_ = step;
@panic("link(b, step) has been deprecated, use addPaths(step) instead");
}
16 changes: 10 additions & 6 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
},
.dependencies = .{
.opusfile = .{
.url = "https://pkg.machengine.org/opusfile/234b8f39f232a61a6c901f62044ca97590bfd725.tar.gz",
.hash = "12203293a3503683ee57c0f348899ab1f6d2c6aa7a84e8bbfd4df46a9c8faa7ba305",
.url = "https://pkg.machengine.org/opusfile/e24d748b96d6b2a3164d4b8373346fd42d113c1b.tar.gz",
.hash = "1220a87381d9328511d0d8d7d0aefc65f4df769700cabc27462d4e212ea53a6f30d4",
},
.opusenc = .{
.url = "https://pkg.machengine.org/opusenc/b855a67fd2a2171cae506579795eb3817c8c3925.tar.gz",
.hash = "12205b82e9375d940b78e8c7df0b84bd1015c9a8c5b2a6b6ff2c21454a2763ef7aa6",
},
.mach_sysaudio = .{
.url = "https://pkg.machengine.org/mach-sysaudio/f987d55fefbb7b7ef5c742ad2e64db68a5a2110d.tar.gz",
.hash = "1220539dbe43f4651bac0495175d5567661c8b9c323fb4fbea9aa1de67a2a7b26566",
.url = "https://pkg.machengine.org/mach-sysaudio/8e4d73150af291d63a5390b157552ce0bbcd4a1d.tar.gz",
.hash = "12206c2ca0378bbc54721553e8167c037325e95e2791379fbe0e245a34334679dc90",
},
.linux_audio_headers = .{
.url = "https://pkg.machengine.org/linux-audio-headers/6b182ff9d51009ca903f3bac63777fbfa654e5f5.tar.gz",
.hash = "122052c4415f4d5be98e3324083939af644d5f8eddd2e6a1a544596645eda8cf411a",
.url = "https://pkg.machengine.org/linux-audio-headers/302c41dc4ab5fa292c3062e9e0d26c7f62652740.tar.gz",
.hash = "1220dc84528a0e298467bec65ab158426306961f08e7555eab9eef684ef126893689",
},
},
}
33 changes: 24 additions & 9 deletions examples/play.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ pub fn main() !void {
return error.InvalidDevice;
}

// encode the decoded file
const encoded_file = try std.fs.cwd().createFile("/tmp/time.opus", .{});
var comments = try Opus.Comments.init();
defer comments.deinit();
try Opus.encodeStream(
std.io.StreamSource{ .file = encoded_file },
comments,
file_decoded.sample_rate,
file_decoded.channels,
.mono_stereo,
file_decoded.samples,
);

player = try ctx.createPlayer(device, writeCallback, .{});
defer player.deinit();
try player.start();
Expand All @@ -36,7 +49,7 @@ pub fn main() !void {
var iter = std.mem.split(u8, line, ":");
const cmd = std.mem.trimRight(u8, iter.first(), &std.ascii.whitespace);
if (std.mem.eql(u8, cmd, "vol")) {
var vol = try std.fmt.parseFloat(f32, std.mem.trim(u8, iter.next().?, &std.ascii.whitespace));
const vol = try std.fmt.parseFloat(f32, std.mem.trim(u8, iter.next().?, &std.ascii.whitespace));
try player.setVolume(vol);
} else if (std.mem.eql(u8, cmd, "pause")) {
try player.pause();
Expand All @@ -53,12 +66,14 @@ pub fn main() !void {
}

var i: usize = 0;
fn writeCallback(_: ?*anyopaque, frames: usize) void {
for (0..frames) |fi| {
if (i >= file_decoded.samples.len) i = 0;
for (0..file_decoded.channels) |ch| {
player.write(player.channels()[ch], fi, file_decoded.samples[i]);
i += 1;
}
}
fn writeCallback(_: ?*anyopaque, output: []u8) void {
if (i >= file_decoded.samples.len) i = 0;
const to_write = @min(output.len / player.format().size(), file_decoded.samples.len - i);
sysaudio.convertTo(
f32,
file_decoded.samples[i..][0..to_write],
player.format(),
output[0 .. to_write * player.format().size()],
);
i += to_write;
}
Loading
Loading