2016-09-19 75 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