-
Notifications
You must be signed in to change notification settings - Fork 7
/
choice_demo.rs
53 lines (50 loc) · 1.87 KB
/
choice_demo.rs
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
use std::sync::{Arc, Mutex};
use crossbeam_queue::SegQueue;
use crossbeam_utils::atomic::AtomicCell;
use midi_fundsp::{
io::{
choose_midi_device, console_choice_from, start_input_thread, start_output_thread, Speaker,
SynthMsg,
},
sound_builders::ProgramTable,
sounds::options,
};
use midir::MidiInput;
fn main() -> anyhow::Result<()> {
let reset = Arc::new(AtomicCell::new(false));
let mut quit = false;
while !quit {
let mut midi_in = MidiInput::new("midir reading input")?;
let in_port = choose_midi_device(&mut midi_in)?;
let midi_msgs = Arc::new(SegQueue::new());
while reset.load() {}
start_input_thread(midi_msgs.clone(), midi_in, in_port, reset.clone());
let program_table = Arc::new(Mutex::new(options()));
start_output_thread::<10>(midi_msgs.clone(), program_table.clone());
run_chooser(midi_msgs, program_table.clone(), reset.clone(), &mut quit);
}
Ok(())
}
fn run_chooser(
midi_msgs: Arc<SegQueue<SynthMsg>>,
program_table: Arc<Mutex<ProgramTable>>,
reset: Arc<AtomicCell<bool>>,
quit: &mut bool,
) {
let main_menu = vec!["Pick New Synthesizer Sound", "Pick New MIDI Device", "Quit"];
while !*quit && !reset.load() {
println!("Play notes at will. When ready for a change, select one of the following:");
match console_choice_from("Choice", &main_menu, |s| *s) {
0 => {
let program = {
let program_table = program_table.lock().unwrap();
console_choice_from("Change synth to", &program_table, |opt| opt.0.as_str())
};
midi_msgs.push(SynthMsg::program_change(program as u8, Speaker::Both));
}
1 => reset.store(true),
2 => *quit = true,
_ => panic!("This should never happen."),
}
}
}