Skip to content

Commit 6befd72

Browse files
committed
python methods
1 parent 399194f commit 6befd72

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
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: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ mod glfw;
1212
mod gltf;
1313
mod graphics;
1414
pub(crate) mod material;
15+
mod midi;
1516

1617
use graphics::{Geometry, Graphics, Image, Light, Topology, get_graphics, get_graphics_mut};
1718
use material::Material;
19+
1820
use pyo3::{
1921
exceptions::PyRuntimeError,
2022
prelude::*,
2123
types::{PyDict, PyTuple},
2224
};
2325
use std::ffi::{CStr, CString};
2426

25-
use gltf::Gltf;
2627
use bevy::log::warn;
28+
use gltf::Gltf;
2729
use std::env;
2830

2931
/// Get a shared ref to the Graphics context, or return Ok(()) if not yet initialized.
@@ -78,6 +80,10 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
7880
m.add_function(wrap_pyfunction!(metallic, m)?)?;
7981
m.add_function(wrap_pyfunction!(emissive, m)?)?;
8082
m.add_function(wrap_pyfunction!(unlit, m)?)?;
83+
m.add_function(wrap_pyfunction!(midi_connect, m)?)?;
84+
m.add_function(wrap_pyfunction!(midi_disconnect, m)?)?;
85+
m.add_function(wrap_pyfunction!(midi_refresh_ports, m)?)?;
86+
m.add_function(wrap_pyfunction!(midi_play_notes, m)?)?;
8187

8288
Ok(())
8389
}
@@ -544,3 +550,24 @@ fn emissive(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult
544550
fn unlit(module: &Bound<'_, PyModule>) -> PyResult<()> {
545551
graphics!(module).unlit()
546552
}
553+
554+
#[pyfunction]
555+
#[pyo3(pass_module)]
556+
fn midi_connect(module: &Bound<'_, PyModule>, port: usize) -> PyResult<()> {
557+
midi::connect(port)
558+
}
559+
#[pyfunction]
560+
#[pyo3(pass_module)]
561+
fn midi_disconnect(module: &Bound<'_, PyModule>) -> PyResult<()> {
562+
midi::disconnect()
563+
}
564+
#[pyfunction]
565+
#[pyo3(pass_module)]
566+
fn midi_refresh_ports(module: &Bound<'_, PyModule>) -> PyResult<()> {
567+
midi::refresh_ports()
568+
}
569+
#[pyfunction]
570+
#[pyo3(pass_module)]
571+
fn midi_play_notes(module: &Bound<'_, PyModule>, note: u8, duration: u64) -> PyResult<()> {
572+
midi::play_notes(note, duration)
573+
}

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
@@ -1409,6 +1409,14 @@ pub fn midi_connect(port: usize) -> error::Result<()> {
14091409
})
14101410
}
14111411

1412+
#[cfg(not(target_arch = "wasm32"))]
1413+
pub fn midi_disconnect() -> error::Result<()> {
1414+
app_mut(|app| {
1415+
let world = app.world_mut();
1416+
world.run_system_cached(midi::disconnect).unwrap()
1417+
})
1418+
}
1419+
14121420
#[cfg(not(target_arch = "wasm32"))]
14131421
pub fn midi_play_notes(note: u8, duration: u64) -> error::Result<()> {
14141422
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)