private int parseNoteElement(String s, int index, StaccatoParserContext parserContext) { boolean repeat = false; NoteContext noteContext = new NoteContext(); do { // Begin the voyage of creating a note by populating the NoteContext index = parseNoteElement(s, index, noteContext, parserContext); if (noteContext.isChord) { Chord chord = noteContext.createChord(parserContext); parserContext.getParser().fireChordParsed(chord); } else { Note note = noteContext.createNote(parserContext); parserContext.getParser().fireNoteParsed(note); } // // If the note is a chord, fire all the note events for each note in the chord // if (noteContext.isChord) { // Note[] notes = new Chord(note, noteContext.intervals).getNotes(); // for (int i=1; i < notes.length; i++) { // NoteContext context2 = noteContext.createChordNoteContext(); // context2.noteNumber = notes[i].getValue(); // logger.info("Next note in chord is "+context2.noteNumber); // parserContext.fireNoteParsed(context2.createNote(parserContext)); // } // } // Determine if there is another note to parse, and set up the next NoteContext repeat = noteContext.thereIsAnother; noteContext = noteContext.createNextNoteContext(); } while (repeat); return index; }
public static void populateContext(StaccatoParserContext context) { for (int i = 0; i < Note.PERCUSSION_NAMES.length; i++) { context.getDictionary().put(Note.PERCUSSION_NAMES[i], (byte) (i + 35)); } // Also give a hand to Chord! for (String key : Chord.chordMap.keySet()) { context.getDictionary().put(key, Chord.chordMap.get(key)); } }
/** This method does a variety of calculations to get the actual value of the note. */ private void computeNoteValue(NoteContext noteContext, StaccatoParserContext parserContext) { // Don't compute note value for a rest if (noteContext.isRest) { return; } // Adjust for Key Signature if (parserContext.getKey() != null) { int keySig = KeyProviderFactory.getKeyProvider().convertKeyToByte(parserContext.getKey()); if ((keySig != 0) && (!noteContext.isNatural)) { if ((keySig <= -1) && (noteContext.noteNumber == 11)) noteContext.noteNumber = 10; if ((keySig <= -2) && (noteContext.noteNumber == 4)) noteContext.noteNumber = 3; if ((keySig <= -3) && (noteContext.noteNumber == 9)) noteContext.noteNumber = 8; if ((keySig <= -4) && (noteContext.noteNumber == 2)) noteContext.noteNumber = 1; if ((keySig <= -5) && (noteContext.noteNumber == 7)) noteContext.noteNumber = 6; if ((keySig <= -6) && (noteContext.noteNumber == 0)) { noteContext.noteNumber = 11; noteContext.octaveNumber--; } if ((keySig <= -7) && (noteContext.noteNumber == 5)) noteContext.noteNumber = 4; if ((keySig >= +1) && (noteContext.noteNumber == 5)) noteContext.noteNumber = 6; if ((keySig >= +2) && (noteContext.noteNumber == 0)) noteContext.noteNumber = 1; if ((keySig >= +3) && (noteContext.noteNumber == 7)) noteContext.noteNumber = 8; if ((keySig >= +4) && (noteContext.noteNumber == 2)) noteContext.noteNumber = 3; if ((keySig >= +5) && (noteContext.noteNumber == 9)) noteContext.noteNumber = 10; if ((keySig >= +6) && (noteContext.noteNumber == 4)) noteContext.noteNumber = 5; if ((keySig >= +7) && (noteContext.noteNumber == 11)) { noteContext.noteNumber = 0; noteContext.octaveNumber++; } logger.info( "After adjusting for Key Signature, noteNumber=" + noteContext.noteNumber + " octave=" + noteContext.octaveNumber); } } // Compute the actual note number, based on octave and note if (!noteContext.isNumericNote) { int intNoteNumber = ((noteContext.octaveNumber) * 12) + noteContext.noteNumber + noteContext.internalInterval; if (intNoteNumber > 127) { throw new ParserException( StaccatoMessages.CALCULATED_NOTE_OUT_OF_RANGE, Integer.toString(intNoteNumber)); } noteContext.noteNumber = (byte) intNoteNumber; logger.info("Computed note number: " + noteContext.noteNumber); } }
/** * Creates a Note based on the settings in this NoteContext * * @return Note */ public Note createNote(StaccatoParserContext parserContext) { try { if (noteValueAsString != null) { noteNumber = (Byte) parserContext.getDictionary().get(noteValueAsString); } } catch (NullPointerException e) { throw new RuntimeException( "JFugue NoteSubparser: Could not find '" + noteValueAsString + "' in dictionary."); } try { if (durationValueAsString != null) { decimalDuration = (Byte) parserContext.getDictionary().get(durationValueAsString); } } catch (NullPointerException e) { throw new RuntimeException( "JFugue NoteSubparser: Could not find '" + durationValueAsString + "' in dictionary."); } Note note = new Note(noteNumber); note.setOctaveExplicitlySet(isOctaveExplicitlySet); if (isDurationExplicitlySet) { note.setDuration(decimalDuration); } note.setOriginalString(originalString); note.setRest(isRest); if (hasNoteOnVelocity) { if (noteOnVelocityValueAsString != null) { noteOnVelocity = (Byte) parserContext.getDictionary().get(noteOnVelocityValueAsString); } note.setOnVelocity(noteOnVelocity); } if (hasNoteOffVelocity) { if (noteOffVelocityValueAsString != null) { noteOffVelocity = (Byte) parserContext.getDictionary().get(noteOffVelocityValueAsString); } note.setOffVelocity(noteOffVelocity); } note.setEndOfTie(isEndOfTie); note.setStartOfTie(isStartOfTie); note.setFirstNote(isFirstNote); note.setHarmonicNote(isHarmonicNote); note.setMelodicNote(isMelodicNote); return note; }
/** * Creates a Note based on the settings in this NoteContext * * @return Note */ public Chord createChord(StaccatoParserContext parserContext) { if (noteValueAsString != null) { noteNumber = (Byte) parserContext.getDictionary().get(noteValueAsString); } if (durationValueAsString != null) { decimalDuration = (Byte) parserContext.getDictionary().get(durationValueAsString); } Note rootNote = createNote(parserContext); if (isChord) { Chord chord = new Chord(rootNote, intervals); if (!(inversionBassNote == null)) { chord.setBassNote(inversionBassNote); } else if (inversionCount > 0) { chord.setInversion(inversionCount); } return chord; } return null; }