2015-02-11 56 views
2

我一直對音樂節拍器應用在Java和我無意中發現的併發症。考慮下面這是取自代碼:https://community.oracle.com/thread/2135272。這裏的實現與我爲獲得多個節奏所做的相似。我有這個唯一的問題是,它使事情有點亂,我希望也許有人可以優選通過使用一個序提供另一種方式,以具有多節奏。如何實現使用一個序多個節奏在java中

import java.util.*; 
import javax.swing.*; 
import javax.sound.midi.*; 

public class MetronomeTestApplet extends JApplet 
{ 
    public void init() 
    { 
      MetronomeTest metTest = new MetronomeTest(); 
      metTest.play(); 
    } 
} 

class MetronomeTest 
{ 
    Sequencer sequencer1=null; 
    Sequencer sequencer2=null; 

    Sequence sequence1=null; 
    Sequence sequence2=null; 

    Track track1=null; 
    Track track2=null; 

    MidiEvent newEvent=null; 

    int volume = 90;  
    int tempo  = 100; 

    public MetronomeTest() 
    {  
      try 
      {  // SEQUENCER 1 
       sequencer1 = MidiSystem.getSequencer(); 
       sequencer1.open(); 
       sequence1 = new Sequence(Sequence.PPQ, 16); 
       track1 = sequence1.createTrack(); 

       ShortMessage instrumentMsg = new ShortMessage(); 
       instrumentMsg.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 115, 0);   // 115 == woodblock 
       track1.add(new MidiEvent(instrumentMsg, 0)); 

       // SEQUENCER 2 
       sequencer2 = MidiSystem.getSequencer(); 
       sequencer2.open(); 
       sequence2 = new Sequence(Sequence.PPQ, 16); 
       track2 = sequence2.createTrack(); 

       instrumentMsg = new ShortMessage(); 
       instrumentMsg.setMessage(ShortMessage.PROGRAM_CHANGE, 0, 117, 0);   // 117 == melodic tom 
       track2.add(new MidiEvent(instrumentMsg, 0)); 
      } 
      catch (Exception e) { e.printStackTrace(); } 
    } 

    public void destroy() 
    { 
      sequencer1.stop(); 
      sequencer1.close(); 

      sequencer2.stop(); 
      sequencer2.close(); 
    } 

    public void play() 
    { 
      try 
      {    
       // LOAD SEQUENCE 1    
       for (int i=0; i<4; i++) 
       { 
        ShortMessage noteOnMsg = new ShortMessage(); 
        noteOnMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, volume); 
        track1.add(new MidiEvent(noteOnMsg, 0 + 16*i)); 

        ShortMessage noteOffMsg = new ShortMessage(); 
        noteOffMsg.setMessage(ShortMessage.NOTE_OFF, 0, 60, volume); 
        track1.add(new MidiEvent(noteOffMsg, 16 + 16*i)); 
       } 

       // LOAD SEQUENCE 2    
       for (int i=0; i<8; i++) 
       { 
        ShortMessage noteOnMsg = new ShortMessage(); 
        noteOnMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, volume); 
        track2.add(new MidiEvent(noteOnMsg, 0 + 8*i)); 

        ShortMessage noteOffMsg = new ShortMessage(); 
        noteOffMsg.setMessage(ShortMessage.NOTE_OFF, 0, 60, volume); 
        track2.add(new MidiEvent(noteOffMsg, 8 + 8*i)); 
       } 

       // LOAD AND START SEQUENCERS 
       sequencer1.setSequence(sequence1); 
       sequencer1.setTempoInBPM(tempo); 
       sequencer1.setLoopCount(Sequencer.LOOP_CONTINUOUSLY); 

       sequencer2.setSequence(sequence2); 
       sequencer2.setTempoInBPM(tempo); 
       sequencer2.setLoopCount(Sequencer.LOOP_CONTINUOUSLY); 

       sequencer1.start(); 
       sequencer2.start(); 
      } 
      catch (Exception e) { e.printStackTrace(); } 
    } 
} 
+0

爲什麼你還要說你需要多個節奏當兩個您的序列是一樣的速度運行? – jaket 2015-02-11 17:27:54

+0

感謝您的注意。我試圖運行不同的分辨率和不同的速度 – 2015-02-11 18:11:18

回答

1

documentation說:

序列可包含封裝標準的MIDI節奏變化的消息事件。當音序器處理這種事件時,它會改變播放速度以反映所指示的速度。

相關問題