Phrase[] population = new Phrase[populationSize];
for (int i = 0; i < populationSize; i++) {
population[i] = seed.copy();
Note target;
int targetBeat;
int climax = 0;
// Set target
if (isClimaxAccepted(seed, beatsPerBar)) {
climax = findClimax(seed);
target = new Note(TONIC, (double) beatsPerBar);
targetBeat = 7 * beatsPerBar;
} else {
int lowestPitch = Note.MAX_PITCH;
for (int j = 0; j < seed.size(); j++) {
int currentPitch = seed.getNote(j).getPitch();
if (currentPitch != Note.REST && currentPitch < lowestPitch) {
lowestPitch = currentPitch;
}
}
target = generateClimax(lowestPitch);
climax = target.getPitch();
targetBeat = 4 * beatsPerBar;
}
/* Find the absolute minimum pitch which is the lower of an octave
* below the starting note or a fifth below middle C (53),.
*/
int lowerlimit = -1;
for (int j = 0; j < seed.size(); j++) {
int pitch = seed.getNote(j).getPitch();
if (pitch != Note.REST) {
lowerlimit = pitch - 12;
break;
}
}
if (lowerlimit < 53) {
lowerlimit = 53;
}
// Extend to target
extend(population[i], target, targetBeat, beatRhythmArray,
intervalArray, climax, beatsPerBar, lowerlimit);
addAppropriateTarget(population[i], target);
// population[i].addNote(target);
// If the melody isn't complete, extend to final note
if (population[i].getEndTime() != 8 * beatsPerBar) {
target = new Note(TONIC, (double) beatsPerBar);
targetBeat = 7 * beatsPerBar;
extend(population[i], target, targetBeat,
beatRhythmArray, intervalArray, climax,
beatsPerBar, lowerlimit);
/* Check last note */
int noteIndex = population[i].size() - 1;
int previousPitch = population[i].getNote(noteIndex).getPitch();
while (previousPitch == Note.REST) {
previousPitch = population[i].getNote(--noteIndex).getPitch();
}
/* Find tonic closest to previous pitch */
int targetPitch = target.getPitch();
if (previousPitch < targetPitch) {
if (targetPitch - previousPitch > 6) {
target.setPitch(targetPitch - 12);
}
} else if (previousPitch > targetPitch) {
if (previousPitch - targetPitch > 6) {
target.setPitch(targetPitch + 12);
}
}
population[i].addNote(target);
}