summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: b6e39f29c34e9c8a1e6a6adf48354936e7ed09e8 (plain)
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
extern crate cpal;

use std::io::Write;

//use cpal::traits::{HostTrait, DeviceTrait, StreamTrait};

mod midi_input;

fn main() {
	let mut device_mgr = midi_input::DeviceManager::new().expect("Couldn't create device manager");
	device_mgr.set_quiet(true);
	let devices = device_mgr.list().expect("couldn't list MIDI devices");
	for (index, device) in (&devices).into_iter().enumerate() {
		print!("{:3} | ", index + 1);
		let mut first = true;
		for line in device.name.lines() {
			if !first {
				print!("    | ");
			}
			println!("{}", line);
			first = false;
		}
		println!("    -----------------");
	}
	print!("Select a device (default {}): ", devices.default + 1);
	if std::io::stdout().flush().is_err() {
		//who cares
	}
	
	let device_id;
	{
		let mut buf = String::new();
		std::io::stdin()
			.read_line(&mut buf)
			.expect("error reading stdin");
		let s = buf.trim();
		if s.is_empty() {
			device_id = &devices[devices.default].id;
		} else {
			match s.parse::<usize>() {
				Ok(idx) if idx >= 1 && idx <= devices.len() => {
					device_id = &devices[idx - 1].id;
				}
				_ => {
					eprintln!("Bad device ID: {}", s);
					return;
				}
			}
		}
	}
	let mut device = device_mgr.open(device_id)
			.expect("error opening MIDI device");

	while device.is_connected() {
		let maybe_event = device.read_event();
		if let Some(event) = maybe_event {
			println!("{:?}", event);
		} else {
			std::thread::sleep(std::time::Duration::from_millis(10));
		}
		if let Some(err) = device.get_error() {
			eprintln!("Error: {}", err);
			device.clear_error();
		}
	}
	/*
	let host = cpal::default_host();
	let device = host.default_output_device().expect("no output device available");
	let mut supported_configs_range = device.supported_output_configs()
		.expect("error while querying configs");
	let config = supported_configs_range.next()
		.expect("no supported config?!")
		.with_max_sample_rate()
		.into();
	let stream = device.build_output_stream(
		&config,
		move |data: &mut [i16], _: &cpal::OutputCallbackInfo| {
			for sample in data.iter_mut() {
				*sample = 0;
			}
		},
		move |err| {
			println!("audio stream error: {}", err);
		},
	).expect("couldn't build output stream");
	stream.play().expect("couldn't play stream");

	loop {
		std::thread::sleep(std::time::Duration::from_millis(100));
	}
	*/
}