2016-09-19 71 views
-1

我对Java编程还很陌生,我试图围绕javax.sound API(特别是MIDI序列)封闭我的头,并且遇到了一些公认的基本问题。根据ShortMessage类的文档,重载的setmessage方法之一使用int命令,int channel,int data1,int data2。我理解前两个论点,但我不完全确定最后两个选项的选项。我试图从中学习的书说,音速和速度是有道理的,但是当我改变这些整数时,从音箱中发出的音符的音高或音量都不会改变。以下是我的源代码。MIDI音轨不改变音高或乐器

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(); 
     } 
    } 


} 

感谢您的帮助!

回答

2

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

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

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

+0

这就是我认为只是不知道在哪里可以找到任何文件说这一点。但是我的问题仍然存在:当我改变数据字节1时,音符音调根本没有改变 – bailey2092

+0

你到底在改变什么? –

+0

0到127之间的任何数字。它不会更改音高,无论它更改为 – bailey2092