Skip to content

Commit e8a75ba

Browse files
committed
wip
1 parent b604d36 commit e8a75ba

File tree

8 files changed

+186
-165
lines changed

8 files changed

+186
-165
lines changed

Cargo.lock

Lines changed: 111 additions & 121 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ bevy = { git = "https://github.com/bevyengine/bevy", branch = "main" }
2525
processing = { path = "." }
2626
processing_pyo3 = { path = "crates/processing_pyo3" }
2727
processing_render = { path = "crates/processing_render" }
28-
processing_midi = { path = "crates/processing_midi" }
2928

3029
[dependencies]
3130
bevy = { workspace = true }
3231
processing_render = { workspace = true }
33-
processing_midi = { workspace = true }
3432

3533
[dev-dependencies]
3634
glfw = "0.60.0"

crates/processing_midi/Cargo.toml

Lines changed: 0 additions & 12 deletions
This file was deleted.

crates/processing_midi/src/lib.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

crates/processing_render/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tracing = "0.1"
2020
tracing-subscriber = "0.3"
2121
half = "2.7"
2222
crossbeam-channel = "0.5"
23-
processing_midi = { workspace = true }
23+
bevy_midi = { git = "https://github.com/BlackPhlox/bevy_midi", branch = "latest" }
2424

2525
[target.'cfg(target_os = "macos")'.dependencies]
2626
objc2 = { version = "0.6", default-features = false }

crates/processing_render/src/lib.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod graphics;
66
pub mod image;
77
pub mod light;
88
pub mod material;
9+
pub mod midi;
910
pub mod render;
1011
pub mod sketch;
1112
mod surface;
@@ -34,8 +35,6 @@ use crate::{
3435
surface::SurfacePlugin,
3536
};
3637

37-
use processing_midi::MidiPlugin;
38-
3938
static IS_INIT: OnceLock<()> = OnceLock::new();
4039

4140
thread_local! {
@@ -270,7 +269,7 @@ fn create_app(config: Config) -> App {
270269
geometry::GeometryPlugin,
271270
LightPlugin,
272271
material::MaterialPlugin,
273-
MidiPlugin,
272+
midi::MidiPlugin,
274273
));
275274
app.add_systems(First, (clear_transient_meshes, activate_cameras))
276275
.add_systems(
@@ -1342,3 +1341,29 @@ pub fn gltf_light(gltf_entity: Entity, index: usize) -> error::Result<Entity> {
13421341
.unwrap()
13431342
})
13441343
}
1344+
1345+
#[cfg(not(target_arch = "wasm32"))]
1346+
pub fn midi_refresh_ports() -> error::Result<()> {
1347+
app_mut(|app| {
1348+
let world = app.world_mut();
1349+
world.run_system_cached(midi::refresh_ports).unwrap()
1350+
})
1351+
}
1352+
1353+
#[cfg(not(target_arch = "wasm32"))]
1354+
pub fn midi_connect(port: usize) -> error::Result<()> {
1355+
app_mut(|app| {
1356+
let world = app.world_mut();
1357+
world.run_system_cached_with(midi::connect, port).unwrap()
1358+
})
1359+
}
1360+
1361+
#[cfg(not(target_arch = "wasm32"))]
1362+
pub fn midi_play_notes(note: u8) -> error::Result<()> {
1363+
app_mut(|app| {
1364+
let world = app.world_mut();
1365+
world
1366+
.run_system_cached_with(midi::play_notes, note)
1367+
.unwrap()
1368+
})
1369+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::error::Result;
2+
use bevy::prelude::*;
3+
4+
use bevy_midi::prelude::*;
5+
6+
pub struct MidiPlugin;
7+
8+
impl Plugin for MidiPlugin {
9+
fn build(&self, app: &mut App) {
10+
// TODO: Update `bevy_midi` to treat connections as entities
11+
// in order to support hot-plugging
12+
app.insert_resource(MidiOutputSettings {
13+
port_name: "output",
14+
});
15+
16+
app.add_plugins(MidiOutputPlugin);
17+
}
18+
}
19+
20+
pub fn connect(In(port): In<usize>, output: Res<MidiOutput>) -> Result<()> {
21+
if let Some((_, port)) = output.ports().get(port) {
22+
output.connect(port.clone());
23+
}
24+
Ok(())
25+
}
26+
27+
pub fn disconnect() -> Result<()> {
28+
Ok(())
29+
}
30+
31+
pub fn refresh_ports(output: Res<MidiOutput>) -> Result<()> {
32+
output.refresh_ports();
33+
Ok(())
34+
}
35+
36+
pub fn play_notes(In(note): In<u8>, output: Res<MidiOutput>) -> Result<()> {
37+
output.send([0b1001_0000, note, 127].into()); // Note on, channel 1, max velocity
38+
39+
output.send([0b1000_0000, note, 127].into()); // Note on, channel 1, max velocity
40+
41+
Ok(())
42+
}

examples/midi.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ fn sketch() -> error::Result<()> {
2828
let surface = glfw_ctx.create_surface(width, height, scale_factor)?;
2929
let graphics = graphics_create(surface, width, height)?;
3030

31-
processing_midi::connect(0);
31+
midi_refresh_ports()?;
32+
midi_connect(0)?;
3233

3334
while glfw_ctx.poll_events() {
3435
graphics_begin_draw(graphics)?;
@@ -45,6 +46,8 @@ fn sketch() -> error::Result<()> {
4546
)?;
4647

4748
graphics_end_draw(graphics)?;
49+
50+
midi_play_notes(60)?;
4851
}
4952
Ok(())
5053
}

0 commit comments

Comments
 (0)