Exemple #1
0
 /**
  * This method is called by the sound system when a meta event occurs. In this case, when the
  * end-of-track meta event is received, the sequence is restarted if looping is on.
  *
  * @param event
  */
 public void meta(MetaMessage event) {
   if (event.getType() == END_OF_TRACK_MESSAGE) {
     if (sequencer != null && sequencer.isOpen() && loop) {
       sequencer.start();
     }
   }
 }
Exemple #2
0
 /** Creates a new MidiPlayer object. */
 public MidiPlayer() {
   try {
     sequencer = MidiSystem.getSequencer();
     sequencer.open();
     sequencer.addMetaEventListener(this);
   } catch (MidiUnavailableException ex) {
     sequencer = null;
   }
 }
Exemple #3
0
  public void playStop() {

    if (sequence != null) {
      if (playState == PLAYING || playState == PAUSED) {
        sequencer.stop();
        sequencer.close();
        playState = STOPPED;
      }
    }
  }
 /** Configura o estado de pausa. A música pode não parar imediatamente. */
 public void setPaused(boolean paused) {
   if (this.paused != paused && sequencer != null) {
     this.paused = paused;
     if (paused) {
       sequencer.stop();
     } else {
       sequencer.start();
     }
   }
 }
Exemple #5
0
  public void playLoop() {

    if (sequence != null) {
      double playStartTime = (player.getSeekTime() / 100) * (playGetLength());
      sequencer.setMicrosecondPosition((long) playStartTime);
      if (playState != PLAYING) {
        sequencer.start();
        playState = PLAYING;
      }
    }
  }
Exemple #6
0
 /**
  * Plays a sequence, optionally looping. This method returns immediately. The sequence is not
  * played if it is invalid.
  *
  * @param sequence
  * @param loop
  */
 public void play(Sequence sequence, boolean loop) {
   if (sequencer != null && sequence != null && sequencer.isOpen()) {
     try {
       sequencer.setSequence(sequence);
       sequencer.start();
       this.loop = loop;
     } catch (InvalidMidiDataException ex) {
       ex.printStackTrace();
     }
   }
 }
Exemple #7
0
 public void go() {
   try {
     sq.open();
     Sequence seq = new Sequence(Sequence.PPQ, 4);
     Track track = seq.createTrack();
     track.add(addNote(144, 9, 56, 100, 1));
     track.add(addNote(128, 9, 56, 100, 4));
     sq.setSequence(seq);
     sq.setTempoInBPM(tempo);
     sq.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
     sq.start();
   } catch (Exception ex) {
     ex.printStackTrace();
   }
 }
Exemple #8
0
  public void playPause() {

    if (sequence != null) {
      if (playState == PLAYING) {

        sequencer.stop();
        playState = PAUSED;
      }
    }
  }
Exemple #9
0
  public void playResume() {

    if (sequence != null) {
      if (playState == PAUSED) {

        sequencer.start();
        playState = PLAYING;
      }
    }
  }
Exemple #10
0
  /** Gets the default transmitter and receiver, and then links them. */
  private void linkTransmitterToReceiver() {
    try {
      // Set up the sequencer (including its tempo)
      sequencer.open();
      sequencer.setSequence(sequence);
      sequencer.setTempoInBPM(song.getBPM());

      // Get the system's default synthesizer and set that up, too.
      Synthesizer synth = MidiSystem.getSynthesizer();
      synth.open();

      // Get the receiver and transmitter to use and set those up.
      Receiver receiver = synth.getReceiver();
      Transmitter transmitter = sequencer.getTransmitter();
      transmitter.setReceiver(receiver);
    } catch (Exception ex) {
      // Something went wrong.
      ex.printStackTrace();
      System.exit(1);
    }
  }
Exemple #11
0
  public void go() {
    setUpGui();

    try {

      Sequencer sequencer = MidiSystem.getSequencer();
      sequencer.open();
      // make a sequencer and open
      sequencer.addControllerEventListener(m1, new int[] {127});
      Sequence seq = new Sequence(Sequence.PPQ, 4);
      Track track = seq.createTrack();

      int r = 0;
      for (int i = 0; i < 300; i += 4) {

        r = (int) ((Math.random() * 50) + 1);
        track.add(makeEvent(144, 1, r, 100, i));
        track.add(makeEvent(176, 1, 127, 0, i));
        track.add(makeEvent(128, 1, r, 100, i + 2));
      } // end loop

      sequencer.setSequence(seq);
      sequencer.start();
      sequencer.setTempoInBPM(120);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  } // close method
Exemple #12
0
 public void close() {
   System.out.println("midi closing");
   if (synthesizer != null) {
     synthesizer.close();
   }
   if (sequencer != null) {
     sequencer.close();
   }
   sequencer = null;
   synthesizer = null;
   instruments = null;
   channels = null;
 }
Exemple #13
0
  public boolean play() {

    try {
      if (playState != STOPPED) playStop();

      if (sequence == null) return false;
      if (sequencer == null) return false;

      sequencer.open();
      sequencer.setSequence(sequence);

      double playStartTime = (player.getSeekTime() / 100) * (playGetLength());
      sequencer.setMicrosecondPosition((long) playStartTime);

      sequencer.start();
      playState = PLAYING;
      return true;

    } catch (Exception ex) {
      ex.printStackTrace();
      playState = STOPPED;
      return false;
    }
  }
Exemple #14
0
  public boolean open() {

    try {
      if (synthesizer == null) {
        if ((synthesizer = MidiSystem.getSynthesizer()) == null) {
          System.out.println("getSynthesizer() failed!");
          return false;
        }
      }
      synthesizer.open();
      sequencer = MidiSystem.getSequencer();
      sequencer.addMetaEventListener(new ProcessMeta());

      sequence = new Sequence(Sequence.PPQ, 10);
    } catch (Exception ex) {
      System.out.println("midi exception 1 ");
      ex.printStackTrace();
      return false;
    }

    System.out.println("midi opening");

    Soundbank sb = synthesizer.getDefaultSoundbank();
    if (sb != null) {
      instruments = synthesizer.getDefaultSoundbank().getInstruments();
      synthesizer.loadInstrument(instruments[0]);
    }
    MidiChannel midiChannels[] = synthesizer.getChannels();
    numChannels = midiChannels.length;
    channels = new ChannelData[midiChannels.length];
    if (channels.length == 0) return false;
    for (int i = 0; i < channels.length; i++) {
      channels[i] = new ChannelData(midiChannels[i], i);
    }
    cc = channels[0];
    return true;
  }
 /** Fechao o seqüenciador. */
 public void close() {
   if (sequencer != null && sequencer.isOpen()) {
     sequencer.close();
   }
 }
 /** Para o seqüenciador e reinicia sua posição para 0. */
 public void stop() {
   if (sequencer != null && sequencer.isOpen()) {
     sequencer.stop();
     sequencer.setMicrosecondPosition(0);
   }
 }
Exemple #17
0
 public double playGetTime() {
   if (sequence != null) {
     return ((double) sequencer.getMicrosecondPosition()) / 1000.0;
   }
   return 0;
 }
Exemple #18
0
  public void stop() {

    sq.stop();
    sq.close();
  }
Exemple #19
0
 /** Plays back the song. */
 public void play() throws MidiUnavailableException {
   sequencer.start();
 }
Exemple #20
0
  public void playSetSeek(double seekTime) {

    if (sequence != null) {
      sequencer.setMicrosecondPosition((long) (seekTime * 1000.0));
    }
  }