MIDI轨道不改变音高或乐器

时间:2016-09-19 06:00:55

标签: java audio midi

我是java编程的新手,我试图绕过javax.sound API(特别是midi序列发生器)并且遇到一些公认的基本问题。根据ShortMessage类的文档,其中一个重载的setmessage方法采用int命令,int channel,int data1,int data2。我理解前两个论点,但我不完全确定最后两个的选项是什么。我试图从中学到的这本书说它是有意义的音高和速度但是当我改变这些音符时,扬声器音调的音高或音量都没有变化。这是我的源代码。

import javax.sound.midi.*;

public class BeastBoxStarter {

    public static void main(String args[]) {
        BeastBoxStarter playWithThis = new BeastBoxStarter();
        playWithThis.play();
    }

    public void play(){
        try {
            Sequencer player = MidiSystem.getSequencer();
            try{
                Sequence seq = new Sequence(Sequence.PPQ, 4);
                Track track = seq.createTrack(); //initialize a track

                ShortMessage one = new ShortMessage(); //initialize a new ShortMessage
                one.setMessage(ShortMessage.NOTE_ON, 1, 127, 1); //set the message
                MidiEvent NoteOn = new MidiEvent(one, 1); //add a midi method to turn on the note
                track.add(NoteOn); //add the midi to the sequence track

                ShortMessage two = new ShortMessage(); //initialize a new ShortMessage
                one.setMessage(ShortMessage.NOTE_OFF, 1, 127, 1); //set the message
                MidiEvent NoteOff = new MidiEvent(two, 16); //add a midi method to turn on the note
                track.add(NoteOff); //add the midi to the track

                player.setSequence(seq); //add the sequence to the sequencer

                player.open();
                player.start(); //play the sequence with the sequencer
            }
            catch(InvalidMidiDataException iex){
                iex.printStackTrace();
            }

        }
        catch (MidiUnavailableException mex) {
            mex.printStackTrace();
        }
    }


}

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

data1 / data2值是MIDI消息的数据字节(如果有的话)。

假设您知道如何格式化MIDI信息。 请参阅official specificationsummary table

对于Note On消息,data1是音符编号,data2是速度(=音量)。 对于注释关闭消息,data1是注释编号,data2是速度(通常被忽略)。

相关问题