示例#1
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);
    }
  }
示例#2
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;
  }