Boddlnagg / midir

Cross-platform realtime MIDI processing in Rust.
MIT License
620 stars 76 forks source link

When i press and release 2 piano keys at the same time, the callback sends the wrong message. #120

Closed staurosKaragiannis closed 1 year ago

staurosKaragiannis commented 2 years ago

Introduction

Hello, i have an issue that i think might be a bug with either midir or maybe my electric piano (im not sure if this is possible).

First of all, i own an 88-key electric piano, i connected it to my Windows 10 computer and i wrote a program that does something with the key that i press on the piano, so this code will calculate a number ranging from 0 to 87 (0 is the key with the lowest pitch, and 87 is the highest). here's the code:

let mut switch = false;
let _conn_in = midi_in.connect(in_port, "midir-read-input", move |_, message, _| {
        if message.len() > 1 {
            let piano_key = (message[1] as f64) - 21.0; 
            if piano_key >= 0.0 {
                if switch {
                    println!("Key: {} --- Message: {:?}", piano_key, message);
                }
                switch = !switch;
            }
        }
    }, () )?;

What is the issue exactly?

Lets say that i press the key 1 and key 2 at the same time, the program prints these two keys like this:

Key: 1 --- Message: [144, 22, 81] Key: 2 --- Message: [144, 23 (Don't get confused, it should be 23 because i didn't subtract ), 81]

That works fine like it should, BUT when i release both of key 1 and key 2 at the same time, it prints:

Key: 1 --- Message: [144, 22, 81] Key: 1 --- Message: [144, 22, 81]

it prints key 1 two times as if i released it 2 times at the same time.. which makes no sense! THIS is what we should expect:

Key: 1 --- Message: [144, 22, 81] Key: 2 --- Message: [144, 23, 81]

Boddlnagg commented 1 year ago

Was this issue resolved for you? I'm sorry I never answered, somehow this got lost. In case the issue persists, it would be important to know whether you're running with the normal Windows backend or with the WinRT backend (winrt feature). I have never observed a similar issue on my Windows computer, so it's probably hard to find the cause unless somebody can reproduce it.