Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
182 lines (160 loc) · 5.1 KB
/
Copy pathlib.rs
File metadata and controls
182 lines (160 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use bevy::prelude::*;
use nannou_midi::{MidiOutput, MidiOutputStream, MidiPort, MidiPortDirection};
use processing_core::app_mut;
use processing_core::error::{self, Result};
pub use nannou_midi::{MidiData, MidiMessage};
pub struct MidiPlugin;
pub const NOTE_ON: u8 = 0b1001_0000;
pub const NOTE_OFF: u8 = 0b1000_0000;
#[derive(Resource, Default)]
struct ActiveOutput(Option<Entity>);
impl Plugin for MidiPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<ActiveOutput>();
app.add_plugins(nannou_midi::MidiPlugin);
}
}
fn sorted_output_ports(world: &mut World) -> Vec<(Entity, String)> {
let mut q = world.query::<(Entity, &Name, &MidiPort)>();
let mut ports: Vec<(Entity, String)> = q
.iter(world)
.filter(|(_, _, p)| p.direction == MidiPortDirection::Output)
.map(|(e, n, _)| (e, n.as_str().to_string()))
.collect();
ports.sort_by(|a, b| a.1.cmp(&b.1));
ports
}
pub fn list_ports(world: &mut World) -> Result<Vec<String>> {
Ok(sorted_output_ports(world)
.into_iter()
.enumerate()
.map(|(i, (_, name))| format!("{i}: {name}"))
.collect())
}
pub fn connect(In(port): In<usize>, world: &mut World) -> Result<()> {
let port_entity = sorted_output_ports(world)
.get(port)
.map(|(e, _)| *e)
.ok_or(error::ProcessingError::MidiPortNotFound(port))?;
let previous = world.resource::<ActiveOutput>().0;
if let Some(e) = previous
&& let Ok(entity) = world.get_entity_mut(e)
{
entity.despawn();
}
let connection = world
.spawn((
Name::new("libprocessing output"),
MidiOutput {
port: Some(port_entity),
},
))
.id();
world.resource_mut::<ActiveOutput>().0 = Some(connection);
Ok(())
}
pub fn disconnect(world: &mut World) -> Result<()> {
let entity = world.resource_mut::<ActiveOutput>().0.take();
if let Some(e) = entity
&& let Ok(entity_mut) = world.get_entity_mut(e)
{
entity_mut.despawn();
}
Ok(())
}
pub fn send_message(In(msg): In<MidiMessage>, world: &mut World) -> Result<()> {
let entity = world
.resource::<ActiveOutput>()
.0
.ok_or(error::ProcessingError::MidiPortNotFound(usize::MAX))?;
if let Some(mut stream) = world.get_mut::<MidiOutputStream>(entity) {
stream.send(msg);
}
Ok(())
}
#[cfg(not(target_arch = "wasm32"))]
pub fn midi_refresh_ports() -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
world
.run_system_cached(nannou_midi::native::enumerate_midi_ports)
.unwrap();
Ok(())
})
}
#[cfg(not(target_arch = "wasm32"))]
pub fn midi_list_ports() -> error::Result<Vec<String>> {
app_mut(|app| {
let world = app.world_mut();
world.run_system_cached(list_ports).unwrap()
})
}
#[cfg(not(target_arch = "wasm32"))]
pub fn midi_connect(port: usize) -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
world.run_system_cached_with(connect, port).unwrap()?;
// Materialize the MidiOutputStream component before the caller sends.
world
.run_system_cached(nannou_midi::native::open_midi_outputs)
.unwrap();
Ok(())
})
}
#[cfg(not(target_arch = "wasm32"))]
pub fn midi_disconnect() -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
world.run_system_cached(disconnect).unwrap()
})
}
#[cfg(not(target_arch = "wasm32"))]
pub fn midi_note_on(note: u8, velocity: u8) -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
world
.run_system_cached_with(send_message, MidiMessage::from([NOTE_ON, note, velocity]))
.unwrap()?;
world
.run_system_cached(nannou_midi::native::send_midi_messages)
.unwrap();
Ok(())
})
}
#[cfg(not(target_arch = "wasm32"))]
pub fn midi_note_off(note: u8) -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
world
.run_system_cached_with(send_message, MidiMessage::from([NOTE_OFF, note, 0]))
.unwrap()?;
world
.run_system_cached(nannou_midi::native::send_midi_messages)
.unwrap();
Ok(())
})
}
#[cfg(not(target_arch = "wasm32"))]
pub fn midi_play_notes(note: u8, duration: u64) -> error::Result<()> {
app_mut(|app| {
let world = app.world_mut();
world
.run_system_cached_with(send_message, MidiMessage::from([NOTE_ON, note, 127]))
.unwrap()?;
world
.run_system_cached(nannou_midi::native::send_midi_messages)
.unwrap();
Ok(())
})?;
std::thread::sleep(std::time::Duration::from_millis(duration));
app_mut(|app| {
let world = app.world_mut();
world
.run_system_cached_with(send_message, MidiMessage::from([NOTE_OFF, note, 127]))
.unwrap()?;
world
.run_system_cached(nannou_midi::native::send_midi_messages)
.unwrap();
Ok(())
})
}