/** * Turns a NoteToken into a Pitch by equating their representations and then enforcing the Key of * the Piece and the Accidentals encountered in the given meter * * @param workingNote, the NoteToken from which to make a Pitch * @return A Pitch that represents the given input NoteToken */ private Pitch constructNote(NoteToken workingNote) { Pitch workingPitch; int octave = 0; // Add the note initally if ('A' <= workingNote.getNote() && workingNote.getNote() <= 'G') { workingPitch = new Pitch(workingNote.getNote()); } else { octave++; workingPitch = new Pitch(Character.toUpperCase(workingNote.getNote())).transpose(Pitch.OCTAVE); } // Transpose by the octaves octave += workingNote.getOctave(); workingPitch = workingPitch.octaveTranspose(workingNote.getOctave()); // Collect accidentals, use them to modify the accidental list switch (workingNote.getNote()) { case 'A': case 'a': if (workingNote.getAccidental() == Integer.MIN_VALUE) { keyAccidental.put("A" + octave, 0); } else if (workingNote.getAccidental() == 0) { keyAccidental.put("A" + octave, -1 * keySignature.get('A')); } else { keyAccidental.put("A" + octave, -1 * keySignature.get('A') + workingNote.getAccidental()); } break; case 'B': case 'b': if (workingNote.getAccidental() == Integer.MIN_VALUE) { keyAccidental.put("B" + octave, 0); } else if (workingNote.getAccidental() == 0) { keyAccidental.put("B" + octave, -1 * keySignature.get('B')); } else { keyAccidental.put("B" + octave, -1 * keySignature.get('B') + workingNote.getAccidental()); } break; case 'C': case 'c': if (workingNote.getAccidental() == Integer.MIN_VALUE) { keyAccidental.put("C" + octave, 0); } else if (workingNote.getAccidental() == 0) { keyAccidental.put("C" + octave, -1 * keySignature.get('C')); } else { keyAccidental.put("C" + octave, -1 * keySignature.get('C') + workingNote.getAccidental()); } break; case 'D': case 'd': if (workingNote.getAccidental() == Integer.MIN_VALUE) { keyAccidental.put("D" + octave, 0); } else if (workingNote.getAccidental() == 0) { keyAccidental.put("D" + octave, -1 * keySignature.get('D')); } else { keyAccidental.put("D" + octave, -1 * keySignature.get('D') + workingNote.getAccidental()); } break; case 'E': case 'e': if (workingNote.getAccidental() == Integer.MIN_VALUE) { keyAccidental.put("E" + octave, 0); } else if (workingNote.getAccidental() == 0) { keyAccidental.put("E" + octave, -1 * keySignature.get('E')); } else { keyAccidental.put("E" + octave, -1 * keySignature.get('E') + workingNote.getAccidental()); } break; case 'F': case 'f': if (workingNote.getAccidental() == Integer.MIN_VALUE) { keyAccidental.put("F" + octave, 0); } else if (workingNote.getAccidental() == 0) { keyAccidental.put("F" + octave, -1 * keySignature.get('F')); } else { keyAccidental.put("F" + octave, -1 * keySignature.get('F') + workingNote.getAccidental()); } break; case 'G': case 'g': if (workingNote.getAccidental() == Integer.MIN_VALUE) { keyAccidental.put("G" + octave, 0); } else if (workingNote.getAccidental() == 0) { keyAccidental.put("G" + octave, -1 * keySignature.get('G')); } else { keyAccidental.put("G" + octave, -1 * keySignature.get('G') + workingNote.getAccidental()); } break; } // Now modify by the accidental and key signature in tandem switch (workingNote.getNote()) { case 'A': case 'a': workingPitch = workingPitch.accidentalTranspose(keyAccidental.get("A" + octave)); workingPitch = workingPitch.accidentalTranspose(keySignature.get('A')); break; case 'B': case 'b': workingPitch = workingPitch.accidentalTranspose(keyAccidental.get("B" + octave)); workingPitch = workingPitch.accidentalTranspose(keySignature.get('B')); break; case 'C': case 'c': workingPitch = workingPitch.accidentalTranspose(keyAccidental.get("C" + octave)); workingPitch = workingPitch.accidentalTranspose(keySignature.get('C')); break; case 'D': case 'd': workingPitch = workingPitch.accidentalTranspose(keyAccidental.get("D" + octave)); workingPitch = workingPitch.accidentalTranspose(keySignature.get('D')); break; case 'E': case 'e': workingPitch = workingPitch.accidentalTranspose(keyAccidental.get("E" + octave)); workingPitch = workingPitch.accidentalTranspose(keySignature.get('E')); break; case 'F': case 'f': workingPitch = workingPitch.accidentalTranspose(keyAccidental.get("F" + octave)); workingPitch = workingPitch.accidentalTranspose(keySignature.get('F')); break; case 'G': case 'g': workingPitch = workingPitch.accidentalTranspose(keyAccidental.get("G" + octave)); workingPitch = workingPitch.accidentalTranspose(keySignature.get('G')); break; } return workingPitch; }
/** * 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(); } }
/** * The terminal recursion method that compiles the list of NoteTokens that make up a given meter * according to the ABC Grammar specifications * * <p>Note: Currently only enforces that a meter not have timing greater than specified in the * ABCHeader but does not care about truncated Meters, apparently those are okay in music * * <p>Note: Assumes that meters can be of any length if desired, even if those lengths violate the * M field this is due to the availability of truncated starts and ends, or switches to time mid * music * * @return An ADT Meters that contains a list of all the NoteTokens that exist in the meter it * begins from */ private Meters parseMeter() { next = lexer.GetNextTokenType(); Meters currentMeter = new Meters(); // Create variables to enforce meter lengths double noteLength; double meterCount = 0; double meterLength = ((double) ThePiece.getMeterSum()[0]) / ((double) ThePiece.getMeterSum()[1]); // Until the given meter is terminated while (next != Tokens.Type.BARLINE && next != Tokens.Type.REPEAT && next != Tokens.Type.END_OF_LINE && next != Tokens.Type.COMMENT && meterCount < meterLength) { NoteToken nextToken = lexer.getNextNoteToken(); switch (next) { case NOTE: case REST: // Construct the note or rest length and add it to the meter length noteLength = (((double) (nextToken.getLength()[0] * ThePiece.getNoteLength()[0])) / ((double) (nextToken.getLength()[1] * (double) ThePiece.getNoteLength()[1]))); meterCount += noteLength; // As long as the meter is not overloaded, add the note or rest to the Meters List if (meterCount <= meterLength) { currentMeter.addElts(nextToken); next = lexer.GetNextTokenType(); } else { throw new IllegalArgumentException( "Overloaded Meter with count greater than permitted"); } next = lexer.GetNextTokenType(); break; case TUPLET: // Calculate the Tuplet length as a product of the type of Tuplet and its inner note // lengths // this is dependent on all Tuplet notes having the same length noteLength = (((double) (nextToken.getElts()[0].getLength()[0] * ThePiece.getNoteLength()[0])) / ((double) (nextToken.getElts()[0].getLength()[1] * (double) ThePiece.getNoteLength()[1]))); noteLength = noteLength * nextToken.getLength()[0]; meterCount += noteLength; // Add each note in the Tuplet as long as the whole tuplet would fit in the meter if (meterCount <= meterLength) { for (int i = 0; i < nextToken.getElts().length; i++) { currentMeter.addElts( nextToken.getElts()[i].changeLength( nextToken.getLength()[0], nextToken.getLength()[1])); } next = lexer.GetNextTokenType(); } else { throw new IllegalArgumentException( "Overloaded Meter with a tuplet, is it counted wrong?"); } next = lexer.GetNextTokenType(); break; case CHORD: // A Chord is multiple notes played at once, so its length is simply the length of one of // the elements // This relies on the Chord having all notes of the same length noteLength = (((double) (nextToken.getLength()[0] * ThePiece.getNoteLength()[0])) / ((double) (nextToken.getLength()[1] * (double) ThePiece.getNoteLength()[1]))); meterCount += noteLength; // If the Chord fits, add its construct to the Meters List if (meterCount <= meterLength) { currentMeter.addElts(nextToken); next = lexer.GetNextTokenType(); } else { throw new IllegalArgumentException( "Overloaded Meter with a tuplet, is it counted wrong?"); } next = lexer.GetNextTokenType(); break; default: throw new IllegalArgumentException( "Somehow made a note token, but it was not a NOTE, TUPLET, CHORD or REST"); } } // To check measure lengths vs meter, apparently this no longer matters, kept for legacy /* System.out.println(meterCount+" vs "+meterLength); if(meterCount != meterLength && meterCount + .000001 < meterLength){ throw new IllegalArgumentException("Got a meter with the wrong timing, bad stuff mang"); } */ // Return the finished meter return currentMeter; }