Exemplo n.º 1
0
  public boolean init(int nbells) {
    fNBells = nbells;
    try {
      fSequencer = MidiSystem.getSequencer();
      fSequencer.addMetaEventListener(fListener);
      fSynthesizer = MidiSystem.getSynthesizer();
      fMidiOut = fSynthesizer.getReceiver();
      fSequencer.getTransmitter().setReceiver(fMidiOut);

      /*
      System.out.println(" Listing available midi devices: ");
      MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();
      for (int i=0; i<info.length; i++)
      {
      	Class c = MidiSystem.getMidiDevice(info[i]).getClass();
      	System.out.println(" MIDI device "+i+": "+info[i]+" is a "+c);
      }
      System.out.println("Using Sequencer "+fSequencer.getClass()+" and receiver "+fMidiOut.getClass());
      */
    } catch (MidiUnavailableException e) {
      System.out.println("Could not obtain MIDI device: " + e);
      return false;
    }
    return true;
  }
Exemplo n.º 2
0
  public void PlayMidiFile(String name, String filename) {
    Sequencer seq = null;
    Transmitter seqTrans = null;
    Synthesizer synth;
    Receiver synthRcvr = null;
    File midiFile = null;

    try {
      seq = MidiSystem.getSequencer();
      seqTrans = seq.getTransmitter();
      synth = MidiSystem.getSynthesizer();
      synthRcvr = synth.getReceiver();
      midiFile = new File(filename);

      if (seq == null) {
        Debug.showMessage("MidiCSD::PlayMidiFile: Sequencer nicht gefunden!");
      } else {
        seq.open();

        seqTrans.setReceiver(synthRcvr);

        Sequence mySeq;
        mySeq = MidiSystem.getSequence(midiFile);

        new Player(name, seq, mySeq, synth, m_playing).start();
      }
    } catch (MidiUnavailableException e) {
      Debug.showException(e, "MidiCSD::PlayMidiFile: MidiUnavailable" + e.getMessage());
    } catch (InvalidMidiDataException e) {
      Debug.showException(e, "MidiCSD::PlayMidiFile: InvalidMidiDataException" + e.getMessage());
    } catch (IOException e) {
      Debug.showException(e, "MidiCSD::PlayMidiFile:IOException (fn:" + filename + ")");
    }
  }
Exemplo n.º 3
0
  public Sequencer(int instrument, int tempo) {
    // Set up initial settings for the sequencer
    this.instrument = instrument;
    this.tempo = tempo;
    Synthesizer synth;
    ticks = 0;
    velocity = 64; // Mid volume

    try {
      // Setup values to create sequencer
      sequence = new Sequence(Sequence.PPQ, 16);
      sequencer = MidiSystem.getSequencer();
      sequencer.open();
      synth = MidiSystem.getSynthesizer();
      synth.open();
      sequencer.getTransmitter().setReceiver(synth.getReceiver());
      sequencer.setTempoInBPM(tempo);
      track = sequence.createTrack();

    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    } catch (MidiUnavailableException e) {
      e.printStackTrace();
    }
  }
 public MidiViewImpl() {
   try {
     this.synth = MidiSystem.getSynthesizer();
     this.receiver = synth.getReceiver();
     this.synth.open();
   } catch (MidiUnavailableException e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 5
0
 public synchronized void init() throws MidiUnavailableException {
   if (unusedChannels != null) return;
   if (synthesizer == null) synthesizer = MidiSystem.getSynthesizer();
   if (!synthesizer.isOpen()) synthesizer.open();
   MidiChannel[] chn = synthesizer.getChannels();
   playTasks = new ArrayList<PlayTask>();
   if (receiver == null) receiver = synthesizer.getReceiver();
   unusedChannels = new ArrayList<Integer>(chn.length);
   for (int i = 0; i < chn.length; i++) if (i != 9) unusedChannels.add(i);
 }
Exemplo n.º 6
0
 /**
  * Construct a MidiView with the specified model and synthesizer
  *
  * @param model the model to display with this view
  * @param synth the synthsizer Midi object
  * @throws NullPointerException if the given model is null
  */
 public MidiView(MusicEditorModel model, Synthesizer synth) {
   Objects.requireNonNull(model);
   this.model = new MusicEditorViewModel(model);
   this.synth = synth;
   Receiver rcvr = null;
   try {
     rcvr = synth.getReceiver();
   } catch (MidiUnavailableException e) {
     System.err.println("unable to access Midi receiver");
   }
   this.receiver = rcvr;
 }
Exemplo n.º 7
0
 /**
  * Constructs a default MidiView by constructing a synthesizer and receiver, and turning the
  * synthesizer open. If a MidiSystem is unavailable, an exception is thrown.
  */
 public MidiView(MusicEditorModel model) {
   Synthesizer synthesizer = null;
   Receiver rcvr = null;
   try {
     synthesizer = MidiSystem.getSynthesizer();
     rcvr = synthesizer.getReceiver();
   } catch (MidiUnavailableException e) {
     e.printStackTrace();
   }
   this.synth = synthesizer;
   try {
     this.synth.open();
   } catch (MidiUnavailableException e) {
     System.err.println("Midi device is unavailable");
   }
   this.receiver = rcvr;
   this.model = new MusicEditorViewModel(model);
 }
Exemplo n.º 8
0
 /**
  * Returns an instance of a Sequencer that uses the provided Synthesizer as its receiver. This is
  * useful when you have made changes to a specific Synthesizer--for example, you've loaded in new
  * patches--that you want the Sequencer to use. You can then pass the Sequencer to the Player
  * constructor.
  *
  * @param synth The Synthesizer to use as the receiver for the returned Sequencer
  * @return a Sequencer with the provided Synthesizer as its receiver
  * @throws MidiUnavailableException
  * @version 4.0
  */
 public static Sequencer getSequencerConnectedToSynthesizer(Synthesizer synth)
     throws MidiUnavailableException {
   Sequencer sequencer = MidiSystem.getSequencer(false); // Get Sequencer
   // which is not
   // connected to
   // new
   // Synthesizer.
   sequencer.open();
   if (!synth.isOpen()) {
     synth.open();
   }
   sequencer.getTransmitter().setReceiver(synth.getReceiver()); // Connect
   // the
   // Synthesizer
   // to
   // our
   // synthesizer
   // instance.
   return sequencer;
 }
Exemplo n.º 9
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);
    }
  }
Exemplo n.º 10
0
 public void run() {
   try {
     if (waitfor != null) waitfor.join();
     Resource res = Loading.waitforint(this.res);
     try {
       seq = MidiSystem.getSequencer(false);
       synth = MidiSystem.getSynthesizer();
       seq.open();
       seq.setSequence(res.layer(Resource.Music.class).seq);
       synth.open();
       seq.getTransmitter().setReceiver(synth.getReceiver());
     } catch (MidiUnavailableException e) {
       return;
     } catch (InvalidMidiDataException e) {
       return;
     } catch (IllegalArgumentException e) {
       /* The soft synthesizer appears to be throwing
        * non-checked exceptions through from the sampled
        * audio system. Ignore them and only them. */
       if (e.getMessage().startsWith("No line matching")) return;
       throw (e);
     }
     seq.addMetaEventListener(
         new MetaEventListener() {
           public void meta(MetaMessage msg) {
             debug("Meta " + msg.getType());
             if (msg.getType() == 47) {
               synchronized (Player.this) {
                 done = true;
                 Player.this.notifyAll();
               }
             }
           }
         });
     do {
       debug("Start loop");
       done = false;
       seq.start();
       synchronized (this) {
         while (!done) this.wait();
       }
       seq.setTickPosition(0);
     } while (loop);
   } catch (InterruptedException e) {
   } finally {
     try {
       debug("Exit player");
       if (seq != null) seq.close();
       try {
         if (synth != null) synth.close();
       } catch (Throwable e2) {
         if (e2 instanceof InterruptedException) {
           /* XXX: There appears to be a bug in Sun's
            * software MIDI implementation that throws back
            * an unchecked InterruptedException here when two
            * interrupts come close together (such as in the
            * case when the current player is first stopped,
            * and then another started immediately afterwards
            * on a new song before the first one has had time
            * to terminate entirely). */
         } else {
           throw (new RuntimeException(e2));
         }
       }
     } finally {
       synchronized (Music.class) {
         if (player == this) player = null;
       }
     }
   }
 }