如何从MIDI序列中获取Note On/Off信息? [英] How to get Note On/Off messages from a MIDI sequence?

查看:178
本文介绍了如何从MIDI序列中获取Note On/Off信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以MIDI播放顺序获得音符开/关事件的通知,以在基于屏幕的(钢琴)键盘上显示音符.

I am hoping to get notifications of note on/off events in a playing MIDI sequence to show the notes on a screen based (piano) keyboard.

下面的代码在播放MIDI文件时添加了MetaEventListenerControllerEventListener,但是在曲目的开头和结尾仅显示了一些消息.

The code below adds a MetaEventListener and a ControllerEventListener when playing a MIDI file, but only shows a few messages at the start and end of the track.

我们如何在&记下MIDI事件?

How can we listen for note on & note off MIDI events?

import java.io.File;
import javax.sound.midi.*;
import javax.swing.JOptionPane;

class PlayMidi {

    public static void main(String[] args) throws Exception {
        /* This MIDI file can be found at..
        https://drive.google.com/open?id=0B5B9wDXIGw9lR2dGX005anJsT2M&authuser=0
        */
        File path = new File("I:\\projects\\EverLove.mid");

        Sequence sequence = MidiSystem.getSequence(path);
        Sequencer sequencer = MidiSystem.getSequencer();

        sequencer.open();

        MetaEventListener mel = new MetaEventListener() {

            @Override
            public void meta(MetaMessage meta) {
                final int type = meta.getType();
                System.out.println("MEL - type: " + type);
            }
        };
        sequencer.addMetaEventListener(mel);

        int[] types = new int[128];
        for (int ii = 0; ii < 128; ii++) {
            types[ii] = ii;
        }
        ControllerEventListener cel = new ControllerEventListener() {

            @Override
            public void controlChange(ShortMessage event) {
                int command = event.getCommand();
                if (command == ShortMessage.NOTE_ON) {
                    System.out.println("CEL - note on!");
                } else if (command == ShortMessage.NOTE_OFF) {
                    System.out.println("CEL - note off!");
                } else {
                    System.out.println("CEL - unknown: " + command);
                }
            }
        };
        int[] listeningTo = sequencer.addControllerEventListener(cel, types);
        for (int ii : listeningTo) {
            System.out.println("Listening To: " + ii);
        }

        sequencer.setSequence(sequence);
        sequencer.start();
        JOptionPane.showMessageDialog(null, "Exit this dialog to end");
        sequencer.stop();
        sequencer.close();
    }
}

推荐答案

我将观察是否有比我的两个建议中的任何一个更好的答案,这显然不理想.

I'll be watching to see if there is a better answer than either of the two suggestions that I have, which are clearly less than ideal.

  1. 编辑midi文件以包括与现有键开/关匹配的元事件
  2. 编写您自己的事件系统

我自己对MIDI并没有做很多事情.我只是偶尔导入MIDI乐谱并剥离大部分信息,然后将其转换为与为满足自己的音频需求而编写的事件系统配合使用(触发我编写的FM合成器).

I don't do a lot yet with MIDI, myself. I only occasionally import MIDI scores and strip out most of the info, converting it to use with an event-system I wrote for my own audio needs (triggering an FM synthesizer I wrote).

这篇关于如何从MIDI序列中获取Note On/Off信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆