Skip to content

Commit 0754b62

Browse files
committed
rust-jack quickstart code
0 parents  commit 0754b62

File tree

8 files changed

+274
-0
lines changed

8 files changed

+274
-0
lines changed

.envrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
use flake

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/target
2+
.direnv
3+
.vscode

Cargo.lock

+111
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "step-sequencer"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
jack = "0.13.0"

flake.lock

+46
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
description = "A Nix-flake-based Rust development environment";
3+
4+
inputs = {
5+
nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1.*.tar.gz";
6+
rust-overlay = {
7+
url = "github:oxalica/rust-overlay";
8+
inputs.nixpkgs.follows = "nixpkgs";
9+
};
10+
};
11+
12+
outputs = { self, nixpkgs, rust-overlay }:
13+
let
14+
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
15+
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
16+
pkgs = import nixpkgs {
17+
inherit system;
18+
overlays = [ rust-overlay.overlays.default self.overlays.default ];
19+
};
20+
});
21+
in
22+
{
23+
overlays.default = final: prev: {
24+
rustToolchain =
25+
let
26+
rust = prev.rust-bin;
27+
in
28+
if builtins.pathExists ./rust-toolchain.toml then
29+
rust.fromRustupToolchainFile ./rust-toolchain.toml
30+
else if builtins.pathExists ./rust-toolchain then
31+
rust.fromRustupToolchainFile ./rust-toolchain
32+
else
33+
rust.stable.latest.default.override {
34+
extensions = [ "rust-src" "rustfmt" ];
35+
};
36+
};
37+
38+
devShells = forEachSupportedSystem ({ pkgs }: {
39+
default = pkgs.mkShell {
40+
packages = with pkgs; [
41+
rustToolchain
42+
openssl
43+
libjack2
44+
pkg-config
45+
cargo-deny
46+
cargo-edit
47+
cargo-watch
48+
rust-analyzer
49+
];
50+
51+
env = {
52+
# Required by rust-analyzer
53+
RUST_SRC_PATH = "${pkgs.rustToolchain}/lib/rustlib/src/rust/library";
54+
};
55+
};
56+
});
57+
};
58+
}

rust-toolchain.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[toolchain]
2+
channel = "stable"
3+

src/main.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::io;
2+
3+
fn main() {
4+
// 1. Create client
5+
let (client, _status) =
6+
jack::Client::new("rust_jack_simple", jack::ClientOptions::default()).unwrap();
7+
8+
// 2. Register ports. They will be used in a callback that will be
9+
// called when new data is available.
10+
let in_a: jack::Port<jack::AudioIn> = client
11+
.register_port("二狗_左声道", jack::AudioIn::default())
12+
.unwrap();
13+
let in_b: jack::Port<jack::AudioIn> = client
14+
.register_port("二狗_右声道", jack::AudioIn::default())
15+
.unwrap();
16+
let mut out_a: jack::Port<jack::AudioOut> = client
17+
.register_port("大马猴_左声道", jack::AudioOut::default())
18+
.unwrap();
19+
let mut out_b: jack::Port<jack::AudioOut> = client
20+
.register_port("大马猴_右声道", jack::AudioOut::default())
21+
.unwrap();
22+
let process_callback = move |_: &jack::Client, ps: &jack::ProcessScope| -> jack::Control {
23+
let out_a_p = out_a.as_mut_slice(ps);
24+
let out_b_p = out_b.as_mut_slice(ps);
25+
let in_a_p = in_a.as_slice(ps);
26+
let in_b_p = in_b.as_slice(ps);
27+
out_a_p.clone_from_slice(in_a_p);
28+
out_b_p.clone_from_slice(in_b_p);
29+
jack::Control::Continue
30+
};
31+
let process = jack::contrib::ClosureProcessHandler::new(process_callback);
32+
33+
// 3. Activate the client, which starts the processing.
34+
let active_client = client.activate_async((), process).unwrap();
35+
36+
// 4. Wait for user input to quit
37+
println!("Press enter/return to quit...");
38+
let mut user_input = String::new();
39+
io::stdin().read_line(&mut user_input).ok();
40+
41+
// 5. Not needed as the async client will cease processing on `drop`.
42+
if let Err(err) = active_client.deactivate() {
43+
eprintln!("JACK exited with error: {err}");
44+
}
45+
}

0 commit comments

Comments
 (0)