pickNotes function
List<Pitch>
pickNotes( - NoteRange range, {
- required int count,
- NoteChoice choice = (only: null, accidentals: Accidentals.none, key: null),
- Random? rng,
})
Implementation
List<Pitch> pickNotes(
NoteRange range, {
required int count,
NoteChoice choice = (only: null, accidentals: Accidentals.none, key: null),
Random? rng,
}) {
final (:only, :accidentals, :key) = choice;
final random = rng ?? Random();
final signature = KeySignature.parse(key);
final letters = [
for (final p in range.naturals)
if (only == null || (only == OnlyOn.lines) == p.onLine) p,
];
if (letters.isEmpty) {
throw ArgumentError('a faixa $range não tem notas para o sorteio');
}
final plain = [
for (final p in letters) Pitch(p.step, signature.alterOf(p.step), p.octave),
];
final altered = _alteredPitches(letters, signature, accidentals);
if (plain.length + altered.length < 2) {
throw ArgumentError('a faixa $range tem uma nota só: não há o que sortear');
}
final result = <Pitch>[];
for (var i = 0; i < count; i++) {
Pitch pick() {
final useAltered = altered.isNotEmpty && random.nextBool();
final pool = useAltered ? altered : plain;
return pool[random.nextInt(pool.length)];
}
var next = pick();
for (
var tries = 0;
result.isNotEmpty && next.midi == result.last.midi && tries < 50;
tries++
) {
next = pick();
}
result.add(next);
}
return result;
}