Skip to content

Commit 412495d

Browse files
committed
Midi working
1 parent 96ca163 commit 412495d

File tree

8 files changed

+99
-76
lines changed

8 files changed

+99
-76
lines changed

Cargo.lock

Lines changed: 23 additions & 32 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 = { version = "0.3", features = ["env-filter"] }
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;
@@ -36,8 +37,6 @@ use crate::{
3637
surface::SurfacePlugin,
3738
};
3839

39-
use processing_midi::MidiPlugin;
40-
4140
static IS_INIT: OnceLock<()> = OnceLock::new();
4241

4342
thread_local! {
@@ -281,7 +280,7 @@ fn create_app(config: Config) -> App {
281280
geometry::GeometryPlugin,
282281
LightPlugin,
283282
material::MaterialPlugin,
284-
MidiPlugin,
283+
midi::MidiPlugin,
285284
));
286285
app.add_systems(First, (clear_transient_meshes, activate_cameras))
287286
.add_systems(
@@ -1394,3 +1393,29 @@ pub fn gltf_light(gltf_entity: Entity, index: usize) -> error::Result<Entity> {
13941393
.unwrap()
13951394
})
13961395
}
1396+
1397+
#[cfg(not(target_arch = "wasm32"))]
1398+
pub fn midi_refresh_ports() -> error::Result<()> {
1399+
app_mut(|app| {
1400+
let world = app.world_mut();
1401+
world.run_system_cached(midi::refresh_ports).unwrap()
1402+
})
1403+
}
1404+
1405+
#[cfg(not(target_arch = "wasm32"))]
1406+
pub fn midi_connect(port: usize) -> error::Result<()> {
1407+
app_mut(|app| {
1408+
let world = app.world_mut();
1409+
world.run_system_cached_with(midi::connect, port).unwrap()
1410+
})
1411+
}
1412+
1413+
#[cfg(not(target_arch = "wasm32"))]
1414+
pub fn midi_play_notes(note: u8) -> error::Result<()> {
1415+
app_mut(|app| {
1416+
let world = app.world_mut();
1417+
world
1418+
.run_system_cached_with(midi::play_notes, note)
1419+
.unwrap()
1420+
})
1421+
}
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: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ fn sketch() -> error::Result<()> {
2828
let surface = glfw_ctx.create_surface(width, height, scale_factor)?;
2929
let graphics = graphics_create(surface, width, height, TextureFormat::Rgba16Float)?;
3030

31-
processing_midi::connect(0);
31+
midi_refresh_ports()?;
32+
midi_connect(0)?;
33+
midi_play_notes(60)?;
3234

3335
while glfw_ctx.poll_events() {
3436
graphics_begin_draw(graphics)?;
@@ -45,6 +47,8 @@ fn sketch() -> error::Result<()> {
4547
)?;
4648

4749
graphics_end_draw(graphics)?;
50+
51+
midi_play_notes(60)?;
4852
}
4953
Ok(())
5054
}

0 commit comments

Comments
 (0)