1 package com.jamesbryangraves.bob; 2 3 import javax.sound.midi.InvalidMidiDataException; 4 import javax.sound.midi.MidiUnavailableException; 5 6 import org.apache.log4j.Logger; 7 8 import com.jamesbryangraves.bob.midi.BobMidiSequencer; 9 import com.jamesbryangraves.bob.music.Composer; 10 11 public class BobApplication implements Runnable { 12 13 static Logger log = Logger.getLogger(BobApplication.class); 14 15 private Composer composer; 16 private BobMidiSequencer sequencer; 17 18 /*** 19 * creates a Sequence for the Sequencer 20 * adds a track to the sequence for every MusicEventGenerator in Composer 21 * opens the Sequencer 22 * 23 * @throws MidiUnavailableException 24 * @throws InvalidMidiDataException 25 */ 26 public void init() throws MidiUnavailableException, InvalidMidiDataException { 27 sequencer.open(); 28 composer.open(); 29 } 30 31 /*** 32 * starts the composer 33 * starts the sequencer 34 */ 35 public void run() { 36 37 composer.start(); 38 39 new Thread() { 40 public void run() { 41 sequencer.start(); 42 } 43 }.start(); 44 45 while(sequencer.isOpen()) { 46 if(composer.isOpen() && composer.getTick() - sequencer.getTickPosition() > 10000) { 47 log.debug("Composer is taking a break."); 48 composer.close(); 49 } else if(!composer.isOpen() && composer.getTick() - sequencer.getTickPosition() < 1000) { 50 log.debug("Composer is composing again."); 51 composer.open(); 52 } 53 } 54 } 55 56 public Composer getComposer() { 57 return composer; 58 } 59 public void setComposer(Composer composer) { 60 this.composer = composer; 61 } 62 63 public BobMidiSequencer getSequencer() { 64 return sequencer; 65 } 66 67 public void setSequencer(BobMidiSequencer sequencer) { 68 this.sequencer = sequencer; 69 } 70 71 }