This commit is contained in:
GHOSCHT 2024-12-23 22:02:43 +01:00
parent 66b6396103
commit ca8ee979ea
Signed by: ghoscht
GPG key ID: 2C2C1C62A5388E82
3 changed files with 27 additions and 20 deletions

View file

@ -3,7 +3,7 @@ use shaku::{self, Interface};
pub type MidiConnectionCallback = Box<dyn Fn(&[u8]) + Send + 'static>;
#[cfg_attr(test, mockall::automock)]
pub trait MidiService<MP: MidiPort>: Interface {
pub trait MidiClient<MP: MidiPort>: Interface {
fn ports(&self) -> Vec<MP>;
fn connect(&self, port: MP, port_name: &str, callback: MidiConnectionCallback) -> ();
}

View file

@ -1,31 +1,38 @@
use midir::{MidiInput, MidiInputPort};
use midir::{MidiInput, MidiInputPort, Ignore};
use shaku::{self, Component};
use super::midi_service::{MidiConnectionCallback, MidiPort, MidiService};
use super::midi_client::{MidiClient, MidiConnectionCallback, MidiPort};
use std::time;
fn create_input(client_name: &str) -> MidiInput {
MidiInput::new(client_name).expect("Creating MIDI device failed")
let mut input = MidiInput::new(client_name).expect("Creating MIDI device failed");
input.ignore(Ignore::None);
return input;
}
#[derive(Component)]
#[shaku(interface = MidiService<MidirPort>)]
pub struct MidirService {
#[shaku(interface = MidiClient<MidirPortImpl>)]
pub struct MidirClientImpl {
client_name: String,
}
impl MidiService<MidirPort> for MidirService {
fn ports(&self) -> Vec<MidirPort> {
impl MidiClient<MidirPortImpl> for MidirClientImpl {
fn ports(&self) -> Vec<MidirPortImpl> {
let midi_in = create_input(&self.client_name);
midi_in
.ports()
.iter()
.map(|p| MidirPort::new(p.clone(), self.client_name.clone())) // Cast to Box<dyn MidiPort>
.map(|p| MidirPortImpl::new(p.clone(), self.client_name.clone())) // Cast to Box<dyn MidiPort>
.collect()
}
fn connect(&self, port: MidirPort, port_name: &str, callback: MidiConnectionCallback) -> () {
fn connect(
&self,
port: MidirPortImpl,
port_name: &str,
callback: MidiConnectionCallback,
) -> () {
let midi_in = MidiInput::new(&self.client_name).expect("Creating MIDI device failed");
let connection = midi_in.connect(
let _connection = midi_in.connect(
port.get_port(),
port_name,
move |_, message, _| callback(message),
@ -37,21 +44,21 @@ impl MidiService<MidirPort> for MidirService {
}
}
pub struct MidirPort {
pub struct MidirPortImpl {
port: MidiInputPort,
client_name: String,
}
impl MidiPort for MidirPort {
impl MidiPort for MidirPortImpl {
fn name(&self) -> String {
let midi_in = create_input(&self.client_name);
midi_in.port_name(&self.port).unwrap()
}
}
impl MidirPort {
fn new(port: MidiInputPort, client_name: String) -> MidirPort {
MidirPort { port, client_name }
impl MidirPortImpl {
fn new(port: MidiInputPort, client_name: String) -> MidirPortImpl {
MidirPortImpl { port, client_name }
}
fn get_port(&self) -> &MidiInputPort {
&self.port

View file

@ -1,5 +1,5 @@
pub mod midi;
mod services {
pub mod midi_client;
pub mod midir_client_impl;
pub mod midi_service;
pub mod midir_service;
}
pub mod midi_service_impl;