Skip to content

Commit 48783f9

Browse files
committed
python methods
1 parent a879582 commit 48783f9

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

crates/processing_pyo3/examples/midi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ def draw():
1818

1919
# pick a random note value, and duration value for that note
2020
# then send the midi command
21-
note = random.randit(57,68)
22-
note_duration = random.randit(25, 250)
21+
note = random.randint(57,68)
22+
note_duration = random.randint(25, 250)
2323
midi_play_notes(note, note_duration)
2424

2525
# TODO: this should happen implicitly on module load somehow

crates/processing_pyo3/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ mod glfw;
1212
mod gltf;
1313
mod graphics;
1414
pub(crate) mod material;
15+
mod midi;
1516
pub(crate) mod shader;
1617
#[cfg(feature = "webcam")]
1718
mod webcam;
1819

1920
use graphics::{Geometry, Graphics, Image, Light, Topology, get_graphics, get_graphics_mut};
2021
use material::Material;
22+
2123
use pyo3::{
2224
exceptions::PyRuntimeError,
2325
prelude::*,
@@ -94,6 +96,10 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
9496
m.add_function(wrap_pyfunction!(metallic, m)?)?;
9597
m.add_function(wrap_pyfunction!(emissive, m)?)?;
9698
m.add_function(wrap_pyfunction!(unlit, m)?)?;
99+
m.add_function(wrap_pyfunction!(midi_connect, m)?)?;
100+
m.add_function(wrap_pyfunction!(midi_disconnect, m)?)?;
101+
m.add_function(wrap_pyfunction!(midi_refresh_ports, m)?)?;
102+
m.add_function(wrap_pyfunction!(midi_play_notes, m)?)?;
97103

98104
#[cfg(feature = "webcam")]
99105
{
@@ -589,3 +595,24 @@ fn create_webcam(
589595
) -> PyResult<webcam::Webcam> {
590596
webcam::Webcam::new(width, height, framerate)
591597
}
598+
599+
#[pyfunction]
600+
#[pyo3(pass_module)]
601+
fn midi_connect(module: &Bound<'_, PyModule>, port: usize) -> PyResult<()> {
602+
midi::connect(port)
603+
}
604+
#[pyfunction]
605+
#[pyo3(pass_module)]
606+
fn midi_disconnect(module: &Bound<'_, PyModule>) -> PyResult<()> {
607+
midi::disconnect()
608+
}
609+
#[pyfunction]
610+
#[pyo3(pass_module)]
611+
fn midi_refresh_ports(module: &Bound<'_, PyModule>) -> PyResult<()> {
612+
midi::refresh_ports()
613+
}
614+
#[pyfunction]
615+
#[pyo3(pass_module)]
616+
fn midi_play_notes(module: &Bound<'_, PyModule>, note: u8, duration: u64) -> PyResult<()> {
617+
midi::play_notes(note, duration)
618+
}

crates/processing_pyo3/src/midi.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use processing::prelude::*;
2+
use pyo3::{exceptions::PyRuntimeError, prelude::*};
3+
4+
pub fn connect(port: usize) -> PyResult<()> {
5+
midi_connect(port).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
6+
}
7+
pub fn disconnect() -> PyResult<()> {
8+
midi_disconnect().map_err(|e| PyRuntimeError::new_err(format!("{e}")))
9+
}
10+
pub fn refresh_ports() -> PyResult<()> {
11+
midi_refresh_ports().map_err(|e| PyRuntimeError::new_err(format!("{e}")))
12+
}
13+
pub fn play_notes(note: u8, duration: u64) -> PyResult<()> {
14+
midi_play_notes(note, duration).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
15+
}

crates/processing_render/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,14 @@ pub fn midi_connect(port: usize) -> error::Result<()> {
12801280
})
12811281
}
12821282

1283+
#[cfg(not(target_arch = "wasm32"))]
1284+
pub fn midi_disconnect() -> error::Result<()> {
1285+
app_mut(|app| {
1286+
let world = app.world_mut();
1287+
world.run_system_cached(midi::disconnect).unwrap()
1288+
})
1289+
}
1290+
12831291
#[cfg(not(target_arch = "wasm32"))]
12841292
pub fn midi_play_notes(note: u8, duration: u64) -> error::Result<()> {
12851293
app_mut(|app| {

crates/processing_render/src/midi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Plugin for MidiPlugin {
1010
// TODO: Update `bevy_midi` to treat connections as entities
1111
// in order to support hot-plugging
1212
app.insert_resource(MidiOutputSettings {
13-
port_name: "output",
13+
port_name: "libprocessing output",
1414
});
1515

1616
app.add_plugins(MidiOutputPlugin);

0 commit comments

Comments
 (0)