Ejemplo n.º 1
0
 /**
  * Constructor. It breaks a Song into its MidiEvent components, and adds them together in order to
  * create a MIDI file.
  *
  * @param song The song to export
  */
 public MidiExporter() {
   // Create the Sequence that will contain everything
   try {
     sequence = new Sequence(Sequence.PPQ, 2);
   } catch (InvalidMidiDataException ex) {
     ex.printStackTrace();
     System.exit(1);
   }
 }
Ejemplo n.º 2
0
  public static void main(String[] args) throws Exception {
    String portName = null;
    String portVersion = null;
    String portVendor = null;
    String portDesc = null;
    String ymzFilename = null;

    boolean invalid = true;
    LinkedList<String> list = new LinkedList<>(Arrays.asList(args));
    argCheck:
    while (!list.isEmpty()) {
      String arg = list.removeFirst();
      switch (arg) {
        case "--midi-name":
          if (list.isEmpty()) {
            break argCheck;
          }
          portName = list.removeFirst();
          break;
        case "--midi-version":
          if (list.isEmpty()) {
            break argCheck;
          }
          portVersion = list.removeFirst();
          break;
        case "--midi-vendor":
          if (list.isEmpty()) {
            break argCheck;
          }
          portVendor = list.removeFirst();
          break;
        case "--midi-desc":
          if (list.isEmpty()) {
            break argCheck;
          }
          portDesc = list.removeFirst();
          break;
        default:
          ymzFilename = arg;
          invalid = false;
          break argCheck;
      }
    }

    if (invalid || !(list.isEmpty())) {
      usage();
      System.exit(1);
    }

    File input = new File(ymzFilename);
    YMZPlayer player = new YMZPlayer(input);
    player.play(portName, portVersion, portVendor, portDesc);
  }
Ejemplo n.º 3
0
  /**
   * Create the MidiEvent for a note, given the data.
   *
   * @param command The command value for the ShortMessage
   * @param note The MIDI value for the note to be played
   * @param eventTime When this event should occur
   * @param velocity The velocity of this note
   * @return The MidiEvent for the note
   */
  private MidiEvent createNoteEvent(int command, int note, int eventTime, int velocity) {
    // Create the message and set its parameters to the ones given.
    ShortMessage message = new ShortMessage();
    try {
      message.setMessage(command, 0, note, velocity);
    } catch (InvalidMidiDataException ex) {
      // Something went wrong.
      ex.printStackTrace();
      System.exit(1);
    }

    // Create the MidiEvent and return it.
    return new MidiEvent(message, eventTime);
  }
Ejemplo n.º 4
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.º 5
0
  /**
   * Sets up the sequencer with a given sequence.
   *
   * @param sequenceToUse
   * @throws MidiUnavailableException
   */
  private void setUpSequencer() throws MidiUnavailableException {
    // First, get the system's default sequencer.
    try {
      sequencer = MidiSystem.getSequencer();
    } catch (MidiUnavailableException ex) {
      // Something went wrong.
      ex.printStackTrace();
      System.exit(1);
    }

    // If there is none, throw an exception.
    if (sequencer == null) {
      String msg = "Cannot find a sequencer";
      throw new MidiUnavailableException(msg);
    }

    // Set up the transmitter and receiver of the synth to play the song.
    linkTransmitterToReceiver();
  }
Ejemplo n.º 6
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);
    }
  }
Ejemplo n.º 7
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();

      Thread.sleep(2000);
      player.close();
      System.exit(0);

    } catch (Exception ex) {
      System.out.println(ex.getMessage());
      ex.printStackTrace();
    }
  } // close play