Ejemplo 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;
  }
Ejemplo 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 + ")");
    }
  }
Ejemplo n.º 3
0
  private void loadSoundBank(File soundbankFile) {

    try {

      synthesizer.close();
      Soundbank sb = MidiSystem.getSoundbank(soundbankFile);
      synthesizer = MidiSystem.getSynthesizer();
      synthesizer.open();

      System.out.println("soundbank added: " + sb);

      if (sb != null) {
        System.out.println("soundbank supported: " + synthesizer.isSoundbankSupported(sb));
        boolean bInstrumentsLoaded = synthesizer.loadAllInstruments(sb);
        System.out.println("Instruments loaded: " + bInstrumentsLoaded);
      }

    } catch (MidiUnavailableException e) {
      e.printStackTrace();
    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 4
0
  public boolean initialize() {

    boolean success = true;

    try {
      sequencer = MidiSystem.getSequencer();
      sequencer.addMetaEventListener(this);

      if (synthesizer == null) {
        if ((synthesizer = MidiSystem.getSynthesizer()) == null) {
          Application.debug("getSynthesizer() failed!");
          return false;
        }

        Soundbank sb = synthesizer.getDefaultSoundbank();
        if (sb != null) {
          instruments = synthesizer.getDefaultSoundbank().getInstruments();
          synthesizer.loadInstrument(instruments[0]);
        }

        channels = synthesizer.getChannels();
      }
    } catch (MidiUnavailableException e) {
      e.printStackTrace();
      return false;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }

    return success;
  }
Ejemplo n.º 5
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();
    }
  }
Ejemplo n.º 6
0
 /**
  * Creates a WAV file based on a MIDI file, using the default sound bank.
  *
  * @param midiFile The MIDI file.
  * @param outputFile An output file.
  * @throws MidiUnavailableException When the synthesizer is not available.
  * @throws InvalidMidiDataException When the MIDI data is invalid.
  * @throws IOException If the WAV file can not be written.
  */
 public void createWavFile(final File midiFile, final File outputFile)
     throws MidiUnavailableException, InvalidMidiDataException, IOException {
   // Create a AdvancedAudioPlayer with this Synthesizer, and get a Sequence
   final Sequence sequence = MidiSystem.getSequence(midiFile);
   final Sequencer sequencer = MidiSystem.getSequencer(false);
   sequencer.getTransmitter().setReceiver(synth.getReceiver());
   createWavFile(sequence, outputFile);
 }
Ejemplo n.º 7
0
 public MidiPlayer() throws MidiUnavailableException {
   this.c();
   this.c = MidiSystem.getReceiver();
   this.sequencer = MidiSystem.getSequencer(false);
   this.sequencer.getTransmitter().setReceiver(this);
   this.sequencer.open();
   this.a(-1L);
 }
Ejemplo n.º 8
0
  /**
   * Saves the MIDI data from a pattern into a file.
   *
   * @param pattern the pattern to save
   * @param file the File to save the pattern to. Should include file extension, such as .mid
   */
  public void saveMidi(Pattern pattern, File file) throws IOException {
    Sequence sequence = getSequence(pattern);

    int[] writers = MidiSystem.getMidiFileTypes(sequence);
    if (writers.length == 0) return;

    MidiSystem.write(sequence, writers[0], file);
  }
Ejemplo n.º 9
0
 /** Closes MIDI resources - be sure to call this after play() has returned. */
 public void close() {
   getSequencer().close();
   try {
     if (MidiSystem.getSynthesizer() != null) {
       MidiSystem.getSynthesizer().close();
     }
   } catch (MidiUnavailableException e) {
     throw new JFugueException(JFugueException.GENERAL_ERROR + e.getMessage());
   }
 }
Ejemplo n.º 10
0
 public MidiToWavRenderer()
     throws MidiUnavailableException, InvalidMidiDataException, IOException {
   try {
     synth = (AudioSynthesizer) MidiSystem.getSynthesizer();
   } catch (ClassCastException e) {
     throw new Error(
         "Please make sure Gervill is included in the classpath: "
             + "it should be de default synth. These are the currently installed synths: "
             + MidiSystem.getMidiDeviceInfo().toString(),
         e);
   }
 }
Ejemplo n.º 11
0
 public Jams() {
   try {
     // From file
     sequence = MidiSystem.getSequence(new File("images/tunes.mid"));
     // Create a sequencer for the sequence
     sequencer = MidiSystem.getSequencer();
     sequencer.open();
     sequencer.setSequence(sequence);
   } catch (IOException e) {
   } catch (MidiUnavailableException e) {
   } catch (InvalidMidiDataException e) {
   }
 }
 public MidiConnector() throws MidiUnavailableException {
   infos = MidiSystem.getMidiDeviceInfo();
   for (int i = 0; i < infos.length; i++) {
     devices.add(MidiSystem.getMidiDevice(infos[i]));
     List<Transmitter> transmitters = devices.get(i).getTransmitters();
     for (int j = 0; j < transmitters.size(); j++) {
       // create a new receiver
       transmitters
           .get(j)
           .setReceiver(new MidiInputReceiver(devices.get(i).getDeviceInfo().toString()));
     }
   }
 }
Ejemplo n.º 13
0
 /** Returns true if at least one MIDI (port) device is correctly installed on the system. */
 public static boolean isMidiInstalled() {
   boolean result = false;
   MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
   for (int i = 0; i < devices.length; i++) {
     try {
       MidiDevice device = MidiSystem.getMidiDevice(devices[i]);
       result = !(device instanceof Sequencer) && !(device instanceof Synthesizer);
     } catch (Exception e1) {
       System.err.println(e1);
     }
     if (result) break;
   }
   return result;
 }
Ejemplo n.º 14
0
    void init_midi(LContext lcontext) {
	if (!midiSynthInitialized) {
	    midiSynthInitialized = true;
	    try {
		midiSynth = MidiSystem.getSynthesizer();
		midiSynth.open();
		if (midiSynth.getDefaultSoundbank() == null) {
		    ((LContext) lcontext).canvas.setMessage
			("Reading sound bank from server. Please wait...");
		    if (lcontext != null) {
			/* empty */
		    }
		    URL url = new URL(((LContext) lcontext).codeBase
				      + "soundbank.gm");
		    Soundbank soundbank = MidiSystem.getSoundbank(url);
		    if (soundbank != null) {
			midiSynth.loadAllInstruments(soundbank);
			((LContext) lcontext).canvas.setMessage("");
		    } else {
			midiSynth.close();
			midiSynth = null;
		    }
		}
	    } catch (MidiUnavailableException midiunavailableexception) {
		midiunavailableexception.printStackTrace();
		midiSynth = null;
	    } catch (MalformedURLException malformedurlexception) {
		malformedurlexception.printStackTrace();
		midiSynth = null;
	    } catch (InvalidMidiDataException invalidmididataexception) {
		invalidmididataexception.printStackTrace();
		midiSynth = null;
	    } catch (IOException ioexception) {
		ioexception.printStackTrace();
		midiSynth = null;
	    } catch (AccessControlException accesscontrolexception) {
		accesscontrolexception.printStackTrace();
		midiSynth = null;
	    }
	    if (midiSynth != null) {
		MidiChannel[] midichannels = midiSynth.getChannels();
		for (int i = 0; i < midichannels.length; i++) {
		    if (midichannels[i] != null)
			midichannels[i].programChange(0);
		}
	    } else
		((LContext) lcontext).canvas.setMessage
		    ("No soundbank; note & drum commands disabled.");
	}
    }
Ejemplo n.º 15
0
 {
   try {
     sq = MidiSystem.getSequencer();
   } catch (MidiUnavailableException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 16
0
 public MIDIReader(String filename) {
   this.filename = filename;
   try {
     this.recebedor = MidiSystem.getReceiver();
     this.sequencia = MidiSystem.getSequence(new File(filename));
     this.tempoProcessor = new MidiUtils.TempoCache(sequencia);
     this.player = MidiSystem.getSequencer(true);
     this.player.setSequence(sequencia);
     this.player.open();
     this.interval = 0.5f;
     this.loadNotes();
     this.duration = this.getRealDuration();
   } catch (Exception ex) {
     Utilidades.alertar(ex.getMessage());
   }
 }
Ejemplo n.º 17
0
 public static void allNotesOff() {
   try {
     allNotesOff(MidiSystem.getSynthesizer());
   } catch (MidiUnavailableException e) {
     throw new JFugueException(JFugueException.GENERAL_ERROR);
   }
 }
Ejemplo n.º 18
0
  public void play(int instrument, int note) {
    try {

      Sequencer player = MidiSystem.getSequencer();
      player.open();
      Sequence seq = new Sequence(Sequence.PPQ, 4);
      Track track = seq.createTrack();

      MidiEvent event = null;
      ShortMessage first = new ShortMessage();
      first.setMessage(192, 1, instrument, 0);
      MidiEvent changeInstrument = new MidiEvent(first, 1);
      track.add(changeInstrument);
      ShortMessage a = new ShortMessage();
      a.setMessage(144, 1, note, 100);
      MidiEvent noteOn = new MidiEvent(a, 1);
      track.add(noteOn);
      ShortMessage b = new ShortMessage();
      b.setMessage(128, 1, note, 100);
      MidiEvent noteOff = new MidiEvent(b, 16);
      track.add(noteOff);
      player.setSequence(seq);
      player.start();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  } // close play
Ejemplo n.º 19
0
  public void los() {
    guiErstellen();

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

      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));
      }

      sequencer.setSequence(seq);
      sequencer.setTempoInBPM(120);
      sequencer.start();
      Thread.sleep(5000);
      sequencer.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Ejemplo n.º 20
0
  /*
   * Uses the sequencer to play a Midi sequence from a .mid file
   * or a .txt file containing a JFugue string.
   */
  public void playMidiFile(String filePath) {

    try {

      if (filePath.equals("")) {
        // launch a file chooser (just for testing)
        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(app.getMainComponent());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
          filePath = fc.getSelectedFile().getAbsolutePath();
        }
      }

      String ext = filePath.substring(filePath.lastIndexOf(".") + 1);
      if (ext.equals("mid")) {
        // Load new sequence from .mid file
        tickPosition = 0;
        sequence = MidiSystem.getSequence(new File(filePath));
        playSequence(sequence, tickPosition);
      } else if (ext.equals("txt")) {
        playJFugueFromFile(new File(filePath));
      } else if (ext.equals("gm")) {
        loadSoundBank(new File(filePath));
      }

    } catch (IOException e) {
      e.printStackTrace();
    } catch (InvalidMidiDataException e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 21
0
  public static MidiDevice getMidiDevice(String name) {

    MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();

    for (Info element : info) {
      if (element.getName().equals(name)) {
        try {
          return MidiSystem.getMidiDevice(element);
        } catch (MidiUnavailableException e) {
          log.error("{}", e);
        }
      }
    }

    return null;
  }
Ejemplo n.º 22
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
Ejemplo n.º 23
0
 public void play() {
   try {
     Sequencer player = MidiSystem.getSequencer();
     System.out.println("Successfully got a sequencer");
   } catch (MidiUnavailableException ex) {
     System.out.println("Bummer");
   }
 }
Ejemplo n.º 24
0
  /**
   * Exports to a file.
   *
   * @param outputFileName The output file name
   * @throws InvalidFileFormatException If the file name doesn't end in .mid or .midi
   */
  public void exportToFile(String outputFileName) throws InvalidFileFormatException {
    // Check for a valid file format.
    if (!outputFileName.endsWith(".mid") && !outputFileName.endsWith(".midi")) {
      String msg = "File names must end in .mid or .midi";
      throw new InvalidFileFormatException(msg);
    }

    // Find a supported file type, and export the file.
    int[] types = MidiSystem.getMidiFileTypes(sequence);
    try {
      File outputFile = new File(outputFileName);
      MidiSystem.write(sequence, types[0], outputFile);
    } catch (IOException ex) {
      ex.printStackTrace();
      System.exit(1);
    }
  }
Ejemplo n.º 25
0
 public MidiPlayer() {
   try {
     sequencer = MidiSystem.getSequencer();
     sequencer.open();
     sequencer.addMetaEventListener(this);
   } catch (MidiUnavailableException ex) {
     sequencer = null;
   }
 }
Ejemplo n.º 26
0
 public MidiViewImpl() {
   try {
     this.synth = MidiSystem.getSynthesizer();
     this.receiver = synth.getReceiver();
     this.synth.open();
   } catch (MidiUnavailableException e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 27
0
 static {
   try {
     Synthesizer synth = MidiSystem.getSynthesizer();
     synth.open();
     channels = synth.getChannels();
   } catch (MidiUnavailableException ex) {
     System.out.println(ex.getMessage());
   }
 }
Ejemplo n.º 28
0
  /** Method to get all available midi ports and add them to the corresponding device list. */
  private void getAvailablePorts() {
    javax.sound.midi.MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
    for (int i = 0; i < infos.length; i++) {
      try {
        javax.sound.midi.MidiDevice theDevice = MidiSystem.getMidiDevice(infos[i]);

        if (theDevice instanceof javax.sound.midi.Sequencer) {
          // Ignore this device as it's a sequencer
        } else if (theDevice.getMaxReceivers() != 0) {
          midiOutDevices.add(theDevice);
        } else if (theDevice.getMaxTransmitters() != 0) {
          midiInputDevices.add(theDevice);
        }
      } catch (MidiUnavailableException e) {
        e.printStackTrace();
      }
    }
  }
Ejemplo n.º 29
0
 public void playMusic() {
   if (judgetone == true) {
     System.out.print("Make Cheerful Song.\r\n");
     try {
       sequence = new Sequence(Sequence.PPQ, 24, 3);
       MelodyMaker.setCheerfulMelody(sequence, length, velocity);
       AccompanimentMaker.makeCheerfulAccompaniment(sequence, length, velocity);
       sequencer = MidiSystem.getSequencer();
       sequencer.open();
       sequencer.setSequence(sequence);
       sequencer.start();
       while (sequencer.isRunning()) Thread.sleep(100);
     } catch (InterruptedException e) {
       e.printStackTrace();
     } catch (MidiUnavailableException e) {
       e.printStackTrace();
     } catch (InvalidMidiDataException e) {
       e.printStackTrace();
     } finally {
       if (sequencer != null && sequencer.isOpen()) sequencer.close();
     }
   } else if (judgetone == false) {
     System.out.print("Make Gloomy Song.\r\n");
     try {
       sequence = new Sequence(Sequence.PPQ, 24, 3);
       MelodyMaker.setGloomyMelody(sequence, length, velocity);
       AccompanimentMaker.makeGloomyAccompaniment(sequence, length, velocity);
       sequencer = MidiSystem.getSequencer();
       sequencer.open();
       sequencer.setSequence(sequence);
       sequencer.start();
       while (sequencer.isRunning()) Thread.sleep(100);
     } catch (InterruptedException e) {
       e.printStackTrace();
     } catch (MidiUnavailableException e) {
       e.printStackTrace();
     } catch (InvalidMidiDataException e) {
       e.printStackTrace();
     } finally {
       if (sequencer != null && sequencer.isOpen()) sequencer.close();
     }
   }
 }
Ejemplo n.º 30
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);
 }