|
1 | 1 | use bevy::prelude::*; |
2 | 2 | use bevy_midi::prelude::*; |
3 | 3 |
|
| 4 | +use processing_core::error::Result; |
| 5 | + |
4 | 6 | pub struct MidiPlugin; |
5 | 7 |
|
6 | 8 | impl Plugin for MidiPlugin { |
7 | 9 | fn build(&self, app: &mut App) { |
| 10 | + // TODO: Update `bevy_midi` to treat connections as entities |
| 11 | + // in order to support hot-plugging |
8 | 12 | app.insert_resource(MidiOutputSettings { |
9 | | - port_name: "output", |
10 | | - }) |
11 | | - .add_plugins(MidiOutputPlugin); |
| 13 | + port_name: "libprocessing 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()); |
12 | 23 | } |
| 24 | + Ok(()) |
13 | 25 | } |
14 | 26 |
|
15 | | -pub fn connect(_port: usize) { |
16 | | - // we need to work with the ECS |
17 | | - // do we pass a MidiCommand to Bevy? |
| 27 | +pub fn disconnect(output: Res<MidiOutput>) -> Result<()> { |
| 28 | + output.disconnect(); |
| 29 | + Ok(()) |
18 | 30 | } |
19 | 31 |
|
20 | | -pub fn disconnect() {} |
21 | | -pub fn refresh_ports() {} |
| 32 | +pub fn refresh_ports(output: Res<MidiOutput>) -> Result<()> { |
| 33 | + output.refresh_ports(); |
| 34 | + Ok(()) |
| 35 | +} |
| 36 | + |
| 37 | +pub fn play_notes(In((note, duration)): In<(u8, u64)>, output: Res<MidiOutput>) -> Result<()> { |
| 38 | + output.send([0b1001_0000, note, 127].into()); // Note on, channel 1, max velocity |
| 39 | + |
| 40 | + std::thread::sleep(std::time::Duration::from_millis(duration)); |
22 | 41 |
|
23 | | -pub fn play_notes() {} |
| 42 | + output.send([0b1000_0000, note, 127].into()); // Note on, channel 1, max velocity |
| 43 | + |
| 44 | + Ok(()) |
| 45 | +} |
0 commit comments