-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ itertools = "0.12" | |
|
||
# MIDI stuff | ||
midly = "0.5" | ||
midir = "0.9" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod codec; | ||
mod transport; | ||
|
||
use std::collections::HashMap; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use std::io::{ErrorKind, Write}; | ||
|
||
use midir::{MidiOutput, MidiOutputConnection, MidiOutputPort}; | ||
|
||
pub(super) fn get_default_port(out: &MidiOutput) -> Option<MidiOutputPort> { | ||
let ports = out.ports(); | ||
if ports.is_empty() { | ||
return None; | ||
} | ||
|
||
if ports.len() == 1 { | ||
ports.into_iter().next() | ||
} else { | ||
let mut without_midi_through = ports.iter().filter(|p| { | ||
out.port_name(p) | ||
.map_or(false, |name| !name.contains("Midi Through")) | ||
}); | ||
|
||
without_midi_through | ||
.next() | ||
.cloned() | ||
.or_else(|| ports.into_iter().next()) | ||
} | ||
} | ||
|
||
type AnyError = Box<dyn std::error::Error>; | ||
|
||
pub fn get_default_connection() -> Result<Connection, AnyError> { | ||
let out = MidiOutput::new("musik output checker")?; | ||
let port = get_default_port(&out).ok_or("Not found any MIDI output device")?; | ||
|
||
let conn = out.connect(&port, "playing through musik")?; | ||
Ok(Connection { inner: conn }) | ||
} | ||
|
||
pub struct Connection { | ||
inner: MidiOutputConnection, | ||
} | ||
|
||
impl Write for Connection { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
let size = buf.len(); | ||
self.inner | ||
.send(buf) | ||
.map_err(|err| std::io::Error::new(ErrorKind::InvalidData, err))?; | ||
Ok(size) | ||
} | ||
|
||
fn flush(&mut self) -> std::io::Result<()> { | ||
Ok(()) | ||
} | ||
} |