/** * Transforms the Voices of the Piece into Pitches which are then subscribed according to timing * to an internal SequencePlayer, then plays that SequencePlayer to play the Piece out loud * * <p>Catches SequencePlayer Errors internally to avoid them being part of the ABCMusic's * responsibility * * <p>Note: because tempo must be converted to quarter notes, tempos that are not divisible by 4 * will have rounding errors that propagate into the music itself, its fairly negligible, but it * exists. * * <p>Note: Assumes that 1/16 of the default note length is the shortest note that would be * desired to play, any note shorter will play for 1/16 of a default note duration */ public void PlayMusic() { try { // Right now it converts bpm to be based on the number of 1/4 given how many L*Q notes exist // with the quantized time being int quartersPerMin = (getTempo() * getNoteLength()[0] * 4) / (getNoteLength()[1]); int ticksPerQuarter = (getNoteLength()[1] * 16) / (getNoteLength()[0] * 4); player = new SequencePlayer(quartersPerMin, ticksPerQuarter); // Figure out what the Key means, and keep it as a map to edit notes as they are added keySignature = getKeySignature(); // Initialize the accidentals as none, then use that as what gets edited through a meter resetKeyAccidental(); for (Iterator<Voice> i = VoicesList.iterator(); i.hasNext(); ) { int startTick = 0; for (Iterator<Bars> j = i.next().BarsList.iterator(); j.hasNext(); ) { for (Iterator<Meters> k = j.next().MetersList.iterator(); k.hasNext(); ) { for (Iterator<NoteToken> l = k.next().getElts().iterator(); l.hasNext(); ) { NoteToken workingNote = l.next(); if (workingNote.getType() == Tokens.Type.CHORD) { int noteTicks = 0; for (int m = 0; m < workingNote.getElts().length; m++) { Pitch newNote = constructNote(workingNote.getElts()[m]); // Length of a note in terms of quarters double noteLength = ((double) workingNote.getElts()[m].getLength()[0] / (double) workingNote.getElts()[m].getLength()[1]) * ((double) (getNoteLength()[0] * 4) / ((double) getNoteLength()[1])); // Convert the quarters to valid ticks noteTicks = (int) (noteLength * ticksPerQuarter); player.addNote(newNote.toMidiNote(), startTick, noteTicks); } startTick += noteTicks; } else if (workingNote.getType() == Tokens.Type.REST) { // Don't need to make rests // Length of a note in terms of quarters double noteLength = ((double) workingNote.getLength()[0] / (double) workingNote.getLength()[1]) * ((double) (getNoteLength()[0] * 4) / ((double) getNoteLength()[1])); // Convert the quarters to valid ticks // Add ticks for the rests skipping time int noteTicks = (int) (noteLength * ticksPerQuarter); startTick += noteTicks; } else { Pitch newNote = constructNote(workingNote); // Length of a note in terms of quarters double noteLength = ((double) workingNote.getLength()[0] / (double) workingNote.getLength()[1]) * ((double) (getNoteLength()[0] * 4) / ((double) getNoteLength()[1])); // Convert the quarters to valid ticks int noteTicks = (int) (noteLength * ticksPerQuarter); player.addNote(newNote.toMidiNote(), startTick, noteTicks); startTick += noteTicks; } } // Reset Accidentals at the end of the Meter resetKeyAccidental(); } } } // Play the finished SequencePlayer with all Voice there-in player.play(); // Catch undesirable errors } catch (MidiUnavailableException e) { e.printStackTrace(); } catch (InvalidMidiDataException e) { e.printStackTrace(); } }
// Transforms a Song in to language that can be fed to the MIDI Sequencer public void play() throws MidiUnavailableException, InvalidMidiDataException { // LCM calculations that ultimately give us how many ticks per beat we should have Fraction lcmCalc = defaultNoteLength; int lcm = 0; for (String voiceName : this.musicForVoiceName.keySet()) { for (Music m : this.musicForVoiceName.get(voiceName)) { lcmCalc = new Fraction( 1, Fraction.LCM(m.getDuration().getDenominator(), lcmCalc.getDenominator())); } if (lcmCalc.getDenominator() > lcm) { lcm = lcmCalc.getDenominator(); } } LyricListener listener = new LyricListener() { public void processLyricEvent(String text) { System.out.println(text); } }; // Initializes a new SequencePlayer that will make our notes audible SequencePlayer seqPlayer = new SequencePlayer(this.beatsPerMinute, lcm, listener); int startTick = 0; int duration; // Considers every Voice in our Song for (String voiceName : this.musicForVoiceName.keySet()) { String lyricsPrefix = ""; if (!voiceName.equals("THE_DEFAULT_VOICE")) lyricsPrefix = voiceName; startTick = 0; // Considers every Music element in our Voice for (Music m : this.musicForVoiceName.get(voiceName)) { duration = (int) (m.getDuration().toDouble() * defaultNoteLength.toDouble() / tempoBeat.toDouble() * lcm); if (m instanceof Note) { Note mNote = (Note) m; Pitch pitch = new Pitch(mNote.getNote().toString().charAt(0)) .transpose(mNote.getAccidental().getSemitoneOffset() + 12 * mNote.getOctave()); if (mNote.getSyllable() != null && !mNote.getSyllable().equals("")) { seqPlayer.addLyricEvent(lyricsPrefix + mNote.getSyllable(), startTick); } seqPlayer.addNote(pitch.toMidiNote(), startTick, duration); startTick += duration; } else if (m instanceof Chord) { Chord mChord = (Chord) m; if (mChord.getSyllable() != null && !mChord.getSyllable().equals("")) { seqPlayer.addLyricEvent(lyricsPrefix + mChord.getSyllable(), startTick); } for (Note n : mChord.getNotes()) { Pitch pitch = new Pitch(n.getNote().toString().charAt(0)) .transpose(n.getAccidental().getSemitoneOffset() + 12 * n.getOctave()); seqPlayer.addNote(pitch.toMidiNote(), startTick, duration); } startTick += duration; } else if (m instanceof Tuplet) { Tuplet mTuplet = (Tuplet) m; int tupleNoteDur = duration; tupleNoteDur = getTupleNoteDur(mTuplet.getType(), tupleNoteDur); for (Music tupletElem : mTuplet.getNotes()) { if (tupletElem instanceof Note) { Pitch pitch1 = null; Note n = (Note) tupletElem; pitch1 = new Pitch(n.getNote().toString().charAt(0)) .transpose(n.getAccidental().getSemitoneOffset() + 12 * n.getOctave()); if (n.getSyllable() != null && !n.getSyllable().equals("")) { seqPlayer.addLyricEvent(lyricsPrefix + n.getSyllable(), startTick); } seqPlayer.addNote(pitch1.toMidiNote(), startTick, tupleNoteDur); startTick += tupleNoteDur; } else if (tupletElem instanceof Chord) { Chord c = (Chord) tupletElem; if (c.getSyllable() != null && !c.getSyllable().equals("")) { seqPlayer.addLyricEvent(lyricsPrefix + c.getSyllable(), startTick); } for (Note note : c.getNotes()) { Pitch pitch2 = null; pitch2 = new Pitch(note.getNote().toString().charAt(0)) .transpose( note.getAccidental().getSemitoneOffset() + 12 * note.getOctave()); seqPlayer.addNote(pitch2.toMidiNote(), startTick, tupleNoteDur); } startTick += tupleNoteDur; } else if (tupletElem instanceof Rest) { startTick += tupleNoteDur; } else { throw new RuntimeException( "You cannot build a tuple out of anything but a Note, Chord, or Rest"); } } } else if (m instanceof Rest) { startTick += duration; } } } seqPlayer.play(); }