Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const playAndScheduleNext = () => {
const next = noteGenerator.next();
if (!next.done) {
playNote(next.value);
const pc = Note.pc(next.value);
const oct = Note.oct(next.value);
if (Math.random() < 0.5) {
const delay = Math.random() < 0.5 ? 0 : Math.random() * 2;
playSecondaryNote(`${pc}${oct + 1}`, delay);
}
Tone.Transport.scheduleOnce(() => {
playAndScheduleNext();
}, `+${3 + Math.random()}`);
} else {
noteGenerator = makeNoteGenerator(swapTwo(notes));
Tone.Transport.scheduleOnce(() => {
playAndScheduleNext();
}, `+${Math.random() + 4}`);
}
};
repeat(() => {
if (Math.random() < 0.9) {
const note = phrase[Math.floor(Math.random() * phrase.length)];
violins.triggerAttack(note);
const pc = Note.pc(note);
if (Math.random() < 0.2) {
cello.triggerAttack(`${pc}${1}`);
}
}
}, (phrase.length * 5) / NOTES_PER_SECOND);
return () => {
const playNote = (instrument, sineSynth, lastNoteMidi) => {
const newNotes = notes.filter(n => Note.midi(n) !== lastNoteMidi);
const note = newNotes[Math.floor(Math.random() * newNotes.length)];
instrument.triggerAttack(note, '+1.5');
const pitchClass = Note.pc(note);
sineSynth.triggerAttackRelease(`${pitchClass}1`, 5, '+1.5');
Tone.Transport.scheduleOnce(() => {
playNote(instrument, sineSynth, Note.midi(note));
}, `+${Math.random() * 10 + 10}`);
};
let primarySampler;
let primaryOctaves;
let secondarySampler;
let secondaryOctaves;
if (Math.random() < 0.5) {
primarySampler = pianoSampler;
primaryOctaves = PIANO_OCTAVES;
secondarySampler = guitarSampler;
secondaryOctaves = GUITAR_OCTAVES;
} else {
primarySampler = guitarSampler;
primaryOctaves = GUITAR_OCTAVES;
secondarySampler = pianoSampler;
secondaryOctaves = PIANO_OCTAVES;
}
const firstPc = Note.pc(pickRandom(notes));
const noteTime = 1 + Math.random();
primarySampler.triggerAttack(
`${firstPc}${pickRandom(primaryOctaves)}`,
`+${noteTime}`
);
if (Math.random() < 0.5) {
secondarySampler.triggerAttack(
`${firstPc}${pickRandom(secondaryOctaves)}`,
`+${noteTime * 3}`
);
}
if (Math.random() < 0.5) {
const otherNoteTime = 3 + Math.random() * 2;
const secondPc = Note.pc(
pickRandom(notes.filter(note => Note.pc(note) !== firstPc))
);
const playNote = (instrument, sineSynth, onTimeoutCreated, lastNoteMidi) => {
const newNotes = notes.filter(n => Note.midi(n) !== lastNoteMidi);
const note = newNotes[Math.floor(Math.random() * newNotes.length)];
instrument.triggerAttack(note, '+1.5');
const pitchClass = Note.pc(note);
sineSynth.triggerAttackRelease(`${pitchClass}1`, 5, '+1.5');
const timeout = setTimeout(() => {
playNote(instrument, sineSynth, onTimeoutCreated, Note.midi(note));
}, Math.random() * 10000 + 10000);
onTimeoutCreated(timeout);
};
export function isPitchClass(note) {
return Note.pc(note) === note;
}
note => Math.random() * 20 * (Note.pc(note) === 'E' ? 3 : 1)
);