/**
   * Refills the input buffer.
   *
   * @return <code>false</code>, iff there was new input.
   * @exception java.io.IOException if any I/O-Error occurs
   */
  private boolean zzRefill() throws java.io.IOException {

    /* first: make room (if you can) */
    if (zzStartRead > 0) {
      System.arraycopy(zzBuffer, zzStartRead, zzBuffer, 0, zzEndRead - zzStartRead);

      /* translate stored positions */
      zzEndRead -= zzStartRead;
      zzCurrentPos -= zzStartRead;
      zzMarkedPos -= zzStartRead;
      zzPushbackPos -= zzStartRead;
      zzStartRead = 0;
    }

    /* is the buffer big enough? */
    if (zzCurrentPos >= zzBuffer.length) {
      /* if not: blow it up */
      char newBuffer[] = new char[zzCurrentPos * 2];
      System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length);
      zzBuffer = newBuffer;
    }

    /* finally: fill the buffer with new input */
    int numRead = zzReader.read(zzBuffer, zzEndRead, zzBuffer.length - zzEndRead);

    if (numRead < 0) {
      return true;
    } else {
      zzEndRead += numRead;
      return false;
    }
  }
Esempio n. 2
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);
   }
 }
Esempio 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);
  }
Esempio 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);
    }
  }
Esempio 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();
  }
Esempio 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);
    }
  }