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>; pub type MidiConnectionCallback = Box<dyn Fn(&[u8]) + Send + 'static>;
#[cfg_attr(test, mockall::automock)] #[cfg_attr(test, mockall::automock)]
pub trait MidiService<MP: MidiPort>: Interface { pub trait MidiClient<MP: MidiPort>: Interface {
fn ports(&self) -> Vec<MP>; fn ports(&self) -> Vec<MP>;
fn connect(&self, port: MP, port_name: &str, callback: MidiConnectionCallback) -> (); 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 shaku::{self, Component};
use super::midi_service::{MidiConnectionCallback, MidiPort, MidiService}; use super::midi_client::{MidiClient, MidiConnectionCallback, MidiPort};
use std::time; use std::time;
fn create_input(client_name: &str) -> MidiInput { 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)] #[derive(Component)]
#[shaku(interface = MidiService<MidirPort>)] #[shaku(interface = MidiClient<MidirPortImpl>)]
pub struct MidirService { pub struct MidirClientImpl {
client_name: String, client_name: String,
} }
impl MidiService<MidirPort> for MidirService { impl MidiClient<MidirPortImpl> for MidirClientImpl {
fn ports(&self) -> Vec<MidirPort> { fn ports(&self) -> Vec<MidirPortImpl> {
let midi_in = create_input(&self.client_name); let midi_in = create_input(&self.client_name);
midi_in midi_in
.ports() .ports()
.iter() .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() .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 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.get_port(),
port_name, port_name,
move |_, message, _| callback(message), move |_, message, _| callback(message),
@ -37,21 +44,21 @@ impl MidiService<MidirPort> for MidirService {
} }
} }
pub struct MidirPort { pub struct MidirPortImpl {
port: MidiInputPort, port: MidiInputPort,
client_name: String, client_name: String,
} }
impl MidiPort for MidirPort { impl MidiPort for MidirPortImpl {
fn name(&self) -> String { fn name(&self) -> String {
let midi_in = create_input(&self.client_name); let midi_in = create_input(&self.client_name);
midi_in.port_name(&self.port).unwrap() midi_in.port_name(&self.port).unwrap()
} }
} }
impl MidirPort { impl MidirPortImpl {
fn new(port: MidiInputPort, client_name: String) -> MidirPort { fn new(port: MidiInputPort, client_name: String) -> MidirPortImpl {
MidirPort { port, client_name } MidirPortImpl { port, client_name }
} }
fn get_port(&self) -> &MidiInputPort { fn get_port(&self) -> &MidiInputPort {
&self.port &self.port

View file

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