/** * Gets the closest match Note to a given pitchName, from a polylist of NoteSymbols * * @param pitch * @param tonesPL * @return Note an instance of the 'closest' note in list from the pitchName */ public static Note getClosestMatch(int pitch, Polylist tonesPL) { int originalPitch = pitch; // System.out.println("getClosestMatch to " + pitchName + " " + tonesPL); if (!tonesPL.nonEmpty()) { ErrorLog.log(ErrorLog.WARNING, "*** Error: No tones list to match against."); } Polylist list = tonesPL; int[] tones = new int[list.length()]; /* Make an array of pitches of all acceptable tones to match to */ for (int i = 0; list.nonEmpty(); i++) { try { tones[i] = ((NoteSymbol) list.first()).getMIDI() % OCTAVE; list = list.rest(); } catch (Exception ep) { // ep.printStackTrace(); } } /* Search incrementally within the tones list for the 'closest' * acceptable pitchName. This will fit our contour best. */ int stepSearch = 0; int indexMatch; while ((indexMatch = arrayContains((pitch % OCTAVE), tones)) == OUT_OF_BOUNDS) { stepSearch++; if (stepSearch % 2 == 0) { pitch -= stepSearch; } else { pitch += stepSearch; } } Note note = ((NoteSymbol) tonesPL.nth(indexMatch)).toNote(); note.setPitch(pitch); /* if( pitchName != originalPitch ) { System.out.println("closest match to " + new Note(originalPitch).toLeadsheet() + " is " + note.toLeadsheet() + " among " + tonesPL); } */ return note; }
/** * Gets the closest match Note at or above a given pitchName, from a polylist of NoteSymbols * * @param pitch * @param tonesPL * @param upward * @return Note an instance of the 'closest' note in list from the pitchName */ public static Note getClosestMatchDirectional(int pitch, Polylist tonesPL, boolean upward) { // System.out.println("getClosestMatchDirectional to " + pitchName + " " + tonesPL); if (!tonesPL.nonEmpty()) { ErrorLog.log(ErrorLog.WARNING, "*** Error: No tones list to match against."); } Polylist list = tonesPL; int[] tones = new int[tonesPL.length()]; /* Make an array of pitches of all acceptable tones to match to */ for (int i = 0; i < tones.length; i++) { try { tones[i] = ((NoteSymbol) tonesPL.first()).getMIDI() % OCTAVE; tonesPL = tonesPL.rest(); } catch (Exception ep) { // ep.printStackTrace(); } } /* Search incrementally within the tones list for the 'closest' * acceptable pitchName. This will fit our contour best. */ int indexMatch; if (upward) { pitch++; } else { pitch--; } while ((indexMatch = arrayContains((pitch % OCTAVE), tones)) == OUT_OF_BOUNDS) { if (upward) { pitch++; } else { pitch--; } } Note note = ((NoteSymbol) list.nth(indexMatch)).toNote(); note.setPitch(pitch); return note; }