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