Distinguish between key press and release of MIDI piano input

不打扰是莪最后的温柔 提交于 2020-03-06 03:15:10

问题


I am about to make a little program for a school project that is supposed to recognize the chords that are being played via a MIDI piano input (that's just one part of it).

At the moment I got so far that for each press and each release of a key on the midi keyboard I get an object of the class ShortMessage.

My question: How do I figure out if the key has been pressed or released? In each case, press and release, the static variable NOTE_OFF does contain the value 128, the variable NOTE_ON the value 144.

I don't get how this is supposed to tell me if the key has been pressed or released. Any idea? Am I missing a fundamental thing?

Thanks in advance.


回答1:


NOTE_ON and NOTE_OFF are just constants; you compare the message's actual command value (getCommand()) with them.

Please note that a note-on message with a velocity (getData2()) of zero must be interpreted as a note-off message.




回答2:


You might like to use the JFugue library for your application.

// You'll need some try/catches around this block. This is traditional Java Midi code.
MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
MidiDevice device = MidiSystem.getMidiDevice(infos[0]); // You'll have to get the right device for your MIDI controller. 

// Here comes the JFugue code
MusicTransmitterToParserListener m = new MusicTransmitterToParserListener(device);
m.addParserListener(new ChordParserListener());

// Choose either this option:
m.startListening();
...do stuff...
m.stopListening();

// Or choose this option:
m.listenForMillis(5000); // Listen for 5000 milliseconds (5 seconds)


public ChordParserListener extends ParserListenerAdapter {
    List<Note> notes = new ArrayList<Note>;

    @Override public void onNotePressed(Note note) {
        notes.add(note);
        // Go through your list and see if you have a chord
    }
    @Override public void onNoteReleased(Note note) {
        // Remove the note from the list, might not be as easy as notes.remove(note)
    }
}

JFugue also has a Chord class that you might find useful -- especially the method that returns a Chord given an array of Notes... it's called Chord.fromNotes(Note[] notes) -- unless that takes away the challenge that you're hoping to solve.




回答3:


I had the same "question" and tried the suggested from Aaron's comment:

Isn't the status field (accessible through the getStatus() method inherited from MidiMessage) the one that may contain NOTE_ON / NOTE_OFF ? I'm pretty sure it is, but can't test it.

It works just fine!. Thank you Poster and Aaron!.

if( sm.getStatus() == sm.NOTE_ON )
{
   piano-key-down.
}


来源:https://stackoverflow.com/questions/37927248/distinguish-between-key-press-and-release-of-midi-piano-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!