// small offset of all chords from the start of the measure
// misaligning it slightly with the main part
double measureOffset = Chance.test(5) ? 0.15 : 0;
for (Phrase phrase : mainPart.getPhraseArray()) {
Phrase accompanimentPhrase = new Phrase();
accompanimentPhrase.setTitle("Accompaniment phrase");
Scale currentScale = ((ExtendedPhrase) phrase).getScale();
// get copies of the static ones, so that we can shuffle them without affecting the original
List<Chord> scaleChords = new ArrayList<Chord>(ChordUtils.chords.get(currentScale));
List<Chord> scaleSeventhChords = new ArrayList<Chord>(ChordUtils.seventhChords.get(currentScale));
List<Chord> scaleOtherChords = new ArrayList<Chord>(ChordUtils.otherChords.get(currentScale));
Note[] notes = phrase.getNoteArray();
List<ToneType> firstToneTypes = new ArrayList<>();
boolean interMeasureChord = false;
double measureChordLength = 0;
boolean canHaveInterMeasureChords = ctx.getMetre()[0] > 3 && ctx.getMetre()[0] % 4 == 0;
Chord chord = null;
for (int i = 0; i < notes.length; i++) {
// shuffle every time, so that we don't always get the same chord for a given note
Collections.shuffle(scaleChords, random);
Collections.shuffle(scaleSeventhChords, random);
Collections.shuffle(scaleOtherChords, random);
Note currentNote = notes[i];
if (currentNote.getRhythmValue() == 0) {
continue; // rhythm value is 0 for the first notes of a (main-part) chord. So progress to the next
}
// inter-measure chords only for even-numbered, compound metres
if (canHaveInterMeasureChords && currentMeasureSize == 0) {
interMeasureChord = Chance.test(18);
}
double chordLength = interMeasureChord ? normalizedMeasureSize / 2 : normalizedMeasureSize;
boolean isHalfMeasure = currentMeasureSize == normalizedMeasureSize / 2;
if (currentNote.getPitch() == 0) {
logger.warn("Pitch is 0 in main part.");
continue;
}
if (!currentNote.isRest() && (currentMeasureSize == 0 || (interMeasureChord && isHalfMeasure))) {
boolean preferStable = ToneResolver.needsContrastingChord(firstToneTypes, ToneGroups.UNSTABLE);
boolean preferUnstable = ToneResolver.needsContrastingChord(firstToneTypes, ToneGroups.STABLE);
Chord previous = chord;
chord = ChordUtils.getChord(ctx, currentNote.getPitch(), previous, scaleChords, scaleSeventhChords, scaleOtherChords, preferStable, preferUnstable);
if (chord != null && Chance.test(90)) {
if (Chance.test(20)) { // change the special note type
if (Chance.test(60)) { // to a new value
specialNoteType = SpecialNoteType.values()[random.nextInt(SpecialNoteType.values().length)];
} else { // reset
specialNoteType = null;
}
}
firstToneTypes.add(chord.getFirstToneType());
int[] chordPitches = chord.getPitches();
logger.debug(Arrays.toString(chordPitches) + " : " + currentNote.getPitch());
postProcessPitches(accompanimentPart, chordPitches);
int dynamics = InstrumentGroups.getInstrumentSpecificDynamics(65 + random.nextInt(20), accompanimentPart.getInstrument());
// in some cases repeat the chord
if (Chance.test(82)) {
if (Chance.test(75)) { //full chord
addChord(chordPitches, chordLength, dynamics, accompanimentPhrase,
specialNoteType, preferChordNotesOffset, measureOffset);
} else { //partial chord + rest
// make the filling rest between 1/16 and 1/4
double restLength = 0.125 * Math.pow(2, random.nextInt(4));
if (restLength >= chordLength) {
restLength = chordLength / 2;
}
addChord(chordPitches, chordLength - restLength, dynamics,
accompanimentPhrase, specialNoteType, preferChordNotesOffset,
measureOffset);
accompanimentPhrase.addRest(new Rest(restLength));
}
} else {
addChord(chordPitches, chordLength / 2, dynamics, accompanimentPhrase,
specialNoteType, preferChordNotesOffset, measureOffset);
addChord(chordPitches, chordLength / 2, dynamics, accompanimentPhrase,
specialNoteType, preferChordNotesOffset, measureOffset);
}
} else {
accompanimentPhrase.addRest(new Rest(chordLength));
}
measureChordLength += chordLength;
}
if (currentNote.isRest() && (currentMeasureSize == 0 || (interMeasureChord && isHalfMeasure))) {
accompanimentPhrase.addRest(new Rest(chordLength));
measureChordLength += chordLength;
}
currentMeasureSize += currentNote.getRhythmValue();
if (currentMeasureSize >= normalizedMeasureSize) {
// when there's a long note and so no inter-measure chord is possible, fill the measure with a rest
if (measureChordLength != currentMeasureSize) {
double fillingSize = normalizedMeasureSize - measureChordLength;
accompanimentPhrase.addRest(new Rest(fillingSize));
}
currentMeasureSize = 0;
measureChordLength = 0;
}
}