示例#1
0
  /**
   * Import a song into the MidiExporter to play or export.
   *
   * @param song The Song to convert to MIDI
   */
  public void importSong(Song song) {
    // Fetch important data from the song.
    // Meaning, metadata and the song itself.
    Beat[] beats = song.getBeatArray();
    this.song = song;

    // Create a track for each voice.
    int numVoices = song.getNumVoices();
    for (int voiceCount = 0; voiceCount < numVoices; voiceCount++) {
      sequence.createTrack();
    }

    // Iterate through each beat, adding each note to the corresponding
    // track.
    Track[] tracks = sequence.getTracks();
    for (int beat = 0; beat < beats.length; beat++) {
      Note[] firstHalf = beats[beat].getNotesFirstHalf();
      Note[] secondHalf = beats[beat].getNotesSecondHalf();
      // Iterate through each note in the beat, adding it to the
      // corresponding track.
      for (int note = 0; note < firstHalf.length; note++) {
        if (firstHalf[note] == secondHalf[note]) {
          createNote(firstHalf[note], 2 * beat, 2, tracks[note]);
        } else {
          createNote(firstHalf[note], 2 * beat, 1, tracks[note]);
          createNote(secondHalf[note], 2 * beat + 1, 1, tracks[note]);
        } // if/else
      } // for
    } // for

    try {
      setUpSequencer();
    } catch (MidiUnavailableException ex) {
      System.out.println("Unable to set up sequencer");
      // do nothing
    }
  }