private void shiftNote(NotePos note, List<NotePos> mergedNotePositions, int index) { NotePos prevOrNextNote = mergedNotePositions.get(index); if (prevOrNextNote.getVoice() != note.getVoice()) { int prevOrNextPosition = prevOrNextNote.getPosition(); int notePosition = note.getPosition(); int length = Math.abs(notePosition - prevOrNextPosition); if (length > MIN_LENGTH && (length % 2) == 0) { // divisable by 2 note.setPosition(notePosition + (length / 2)); System.out.println("shift"); } else { note.setPosition(prevOrNextPosition); } } }
private boolean containsNoteAtPosition(List<NotePos> notePositions, int notePosition) { for (NotePos notePos : notePositions) { if (notePos.getPosition() == notePosition) { return true; } } return false; }
/** * Perform the mutation operation * * @param probability Mutation probability * @param solution The solution to mutate * @throws JMException */ public void doMutation(double probability, Solution solution) throws JMException { if (PseudoRandom.randDouble() < probability) { List<Partition> partitions = ((MusicVariableAtonal) solution.getDecisionVariables()[0]).getPartitions(); int s = partitions.size() - 1; int melodyIndex = PseudoRandom.randInt(0, s); Partition partition = partitions.get(melodyIndex); List<NotePos> notePositions = partition.getNotes(); int startPartition = partition.getPosition(); int endPartition = startPartition + partition.getLength(); if (!notePositions.isEmpty() && notePositions.size() > 1) { int position = PseudoRandom.randInt(0, notePositions.size() - 1); NotePos note = notePositions.get(position); int index = notePositions.indexOf(note); if (random.nextBoolean()) { if (index >= 1) { NotePos prevNote = notePositions.get(index - 1); if (prevNote.getVoice() != note.getVoice()) { // int prevPosition = prevNote.getPosition(); int notePosition = note.getPosition(); if (!containsNoteAtPosition(notePositions, notePosition - MIN_LENGTH) && startPartition <= notePosition - MIN_LENGTH) { note.setPosition(notePosition - MIN_LENGTH); System.out.println("Shifted"); } } } } else { if (index < notePositions.size() - 1) { NotePos nextNote = notePositions.get(index + 1); if (nextNote.getVoice() != note.getVoice()) { // int nextPosition = nextNote.getPosition(); int notePosition = note.getPosition(); if (!containsNoteAtPosition(notePositions, notePosition + MIN_LENGTH) && notePosition + MIN_LENGTH <= endPartition) { note.setPosition(notePosition + MIN_LENGTH); System.out.println("Shifted"); } } } } } } }