Exemplo n.º 1
0
  public void buildTrackAndStart() {
    int[] trackList = null;

    sequence.deleteTrack(track);
    track = sequence.createTrack();

    for (int i = 0; i < 16; i++) {
      trackList = new int[16];

      int key = instruments[i];

      for (int j = 0; j < 16; j++) {
        JCheckBox jc = (JCheckBox) checkboxList.get(j + (16 * i));
        if (jc.isSelected()) {
          trackList[j] = key;
        } else {
          trackList[j] = 0;
        }
      } // close inner loop

      makeTracks(trackList);
      track.add(makeEvent(176, 1, 127, 0, 16));
    } // close outer

    track.add(makeEvent(192, 9, 1, 0, 15));
    try {
      sequencer.setSequence(sequence);
      sequencer.setLoopCount(sequencer.LOOP_CONTINUOUSLY);
      sequencer.start();
      sequencer.setTempoInBPM(120);
    } catch (Exception e) {
      e.printStackTrace();
    }
  } // close buildTrackAndStart method
Exemplo n.º 2
0
  public void playStop() {

    if (sequence != null) {
      if (playState == PLAYING || playState == PAUSED) {
        sequencer.stop();
        sequencer.close();
        playState = STOPPED;
      }
    }
  }
Exemplo n.º 3
0
  public void playLoop() {

    if (sequence != null) {
      double playStartTime = (player.getSeekTime() / 100) * (playGetLength());
      sequencer.setMicrosecondPosition((long) playStartTime);
      if (playState != PLAYING) {
        sequencer.start();
        playState = PLAYING;
      }
    }
  }
Exemplo n.º 4
0
  public void setUpMidi() {
    try {
      sequencer = MidiSystem.getSequencer();
      sequencer.open();
      sequence = new Sequence(Sequence.PPQ, 4);
      track = sequence.createTrack();
      sequencer.setTempoInBPM(120);

    } catch (Exception e) {
      e.printStackTrace();
    }
  } // close method
Exemplo n.º 5
0
  public void playResume() {

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

        sequencer.start();
        playState = PLAYING;
      }
    }
  }
Exemplo n.º 6
0
  public void playPause() {

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

        sequencer.stop();
        playState = PAUSED;
      }
    }
  }
Exemplo n.º 7
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
Exemplo n.º 8
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;
 }
Exemplo n.º 9
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;
    }
  }
Exemplo n.º 10
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;
  }
Exemplo n.º 11
0
    public void actionPerformed(ActionEvent ev) {

      try {

        // make (and open) a sequencer, make a sequence and track

        Sequencer sequencer = MidiSystem.getSequencer();
        sequencer.open();

        sequencer.addControllerEventListener(myPanel, new int[] {127});
        Sequence seq = new Sequence(Sequence.PPQ, 4);
        Track track = seq.createTrack();

        // now make two midi events (containing a midi message)

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

          int rNum = (int) ((Math.random() * 50) + 1);
          if (rNum < 38) { // so now only do it if num <38 (75% of the time)

            track.add(makeEvent(144, 1, rNum, 100, i));

            track.add(makeEvent(176, 1, 127, 0, i));

            track.add(makeEvent(128, 1, rNum, 100, i + 2));
          }
        } // end loop

        // add the events to the track
        // add the sequence to the sequencer, set timing, and start

        sequencer.setSequence(seq);

        sequencer.start();
        sequencer.setTempoInBPM(220);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    } // close actionperformed
Exemplo n.º 12
0
  public void los() {
    guiErstellen();

    try {

      // einen Sequencer erzeugen (und öffnen),
      // eine Sequence und einen Track erzeugen

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

      // jetzt werden MidiEvents (die eine
      // MidiMessage enthalten) erzeugt

      int r = 0;
      for (int i = 0; i < 60; i += 4) {
        r = (int) ((Math.random() * 50) + 1);
        track.add(eventErzeugen(144, 1, r, 100, i));
        track.add(eventErzeugen(176, 1, 127, 0, i));
        track.add(eventErzeugen(128, 1, r, 100, i + 2));
      } // Ende der for-Schleife

      // Hinzufügen der Events zum Track und der Sequence
      // zum Sequencer, Setzen der Zeiten und Starten

      sequencer.setSequence(seq);
      sequencer.start();
      sequencer.setTempoInBPM(120);

    } catch (Exception ex) {
      ex.printStackTrace();
    }
  } // Methode los schließen
Exemplo n.º 13
0
  public void playSetSeek(double seekTime) {

    if (sequence != null) {
      sequencer.setMicrosecondPosition((long) (seekTime * 1000.0));
    }
  }
Exemplo n.º 14
0
 public double playGetTime() {
   if (sequence != null) {
     return ((double) sequencer.getMicrosecondPosition()) / 1000.0;
   }
   return 0;
 }