Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

midir not seeing events as they happen #129

Open
worikgh opened this issue Mar 12, 2023 · 1 comment
Open

midir not seeing events as they happen #129

worikgh opened this issue Mar 12, 2023 · 1 comment

Comments

@worikgh
Copy link

worikgh commented Mar 12, 2023

I have a small midir based programme to read events from a MIDI pedal.

I compare the performance of this to aseqdump, which is working.

When I run this midir programme, and press the pedal buttons, I see nothing. Then I shut the programme down, and restart it, and I see the events. None more are detected.

Using aseqdump I see the events as they occur.

The little midir programme. I cannot see anything I have done incorrectly.

use midir::MidiIO;
use midir::MidiInput;
use std::env::args;
use std::error::Error;
use std::io::stdin;
use std::iter::zip;

fn main() {
    let port_name_in: String = args().nth(1).unwrap();
    run(port_name_in).unwrap();
}

fn run(name_of_port_in: String) -> Result<(), Box<dyn Error>> {
    handle_input(name_of_port_in.as_str()).unwrap();

    let mut input = String::new();
    println!("Running....");
    stdin().read_line(&mut input)?; // wait for next enter key press                                                                                                     
    println!("Closing connection");
    Ok(())
}

fn find_port<T: MidiIO>(name: &str, midi_io: &T) -> Result<T::Port, Box<dyn Error>> {
    let ports = midi_io.ports();
    let port_names: Vec<String> = ports
        .iter()
        .map(|p| midi_io.port_name(p).unwrap())
        .collect();
    let mut names_ports = zip(ports, port_names);
    let port_name = names_ports
        .find(|x| &x.1 == name)
        .expect(format!("{} not found", name).as_str());
    Ok(port_name.0)
}


fn handle_input(port_name: &str) -> Result<(), Box<dyn Error>> {
    let midi_input = MidiInput::new("120Proof-midi-pedal-driver")?;
    let input_port = match find_port(port_name, &midi_input) {
        Ok(p) => p,
        Err(err) => panic!("Failed to bet port {port_name}: {err}"),
    };
    let _ = midi_input.connect(
        &input_port,
        "midir-read-input",
        move |stamp, message, _| {
            println!("{}: {:?} (len = {})", stamp, message, message.len());
        },
        (),
    )?;
    Ok(())
}
@worikgh
Copy link
Author

worikgh commented Mar 12, 2023

Running on a Raspberry Pi with a 32-bit OS (PatchboxOs)

I used cargo add to install midir it gave me version 0.9.1

rustc 1.67.1 (d5a82bbd2 2023-02-07)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant