区分按键和释放MIDI钢琴输入

时间:2016-06-20 16:15:25

标签: java release midi piano

我即将为一个学校项目制作一个小程序,该项目应该识别通过MIDI钢琴输入播放的和弦(这只是其中的一部分)。

目前我到目前为止每次按下并且midi键盘上的每个按键释放时,我都会得到类ShortMessage的对象。

我的问题:如何确定按键是否被按下或释放? 在每种情况下,按下和释放,静态变量NOTE_OFF确实包含值128,变量NOTE_ON包含值144。

我不明白这是怎么告诉我键是否被按下或释放。任何的想法?我错过了一个基本的东西吗?

提前致谢。

3 个答案:

答案 0 :(得分:1)

NOTE_ONNOTE_OFF只是常量;您将消息的实际命令值(getCommand())与它们进行比较。

请注意,速度(getData2())为零的音符开启消息必须被解释为音符关闭消息。

答案 1 :(得分:0)

您可能希望将JFugue库用于您的应用程序。

// 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还有一个你可能会觉得有用的Chord类 - 尤其是在给定一系列Notes的情况下返回Chord的方法...它被称为Chord.fromNotes(Note[] notes) - 除非它带走了挑战你希望解决的问题。

答案 2 :(得分:0)

我也有同样的“问题”,并尝试了亚伦评论中的建议:

不是状态字段(可通过getStatus()方法访问 从MidiMessage继承)可能包含NOTE_ON / NOTE_OFF?我很确定,但是无法测试。

效果很好!谢谢Poster和Aaron!。

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