With just a button press, I want to play a sequence of notes using a PolySynth
and a Sequence
. When the button is pressed repeatedly, I want the currently playing notes to stop and restart.
The challenge: No matter what method I use, I can't seem to completely stop/silence the previously played notes in order to start the sequence again when the button is clicked. This could be due to the envelope's decay/sustain settings.
My Synthesizer Setup:
import { PolySynth } from 'tone'
const synth = new PolySynth(Synth, {
oscillator: {
type: 'sine4',
volume: -6,
},
envelope: {
attack: 0.01,
decay: 0.5,
sustain: 0.1,
release: 1,
},
}).toDestination()
synth.maxPolyphony = 4 // unsure if this is necessary
My Note Sequence:
import { Sequence } from 'tone'
// Play two notes individually followed by playing them together
const notes = [
{ note: 'C4', duration: '8n' },
{ note: 'G4', duration: '8n' },
{ note: ['C4', 'G4'], duration: '4n' }
]
// The sequence that plays the notes one after another
const sequence = new Sequence({
subdivision: '8n',
loop: false,
events: notes,
callback: (time, note) => synth.triggerAttackRelease(note.note, note.duration, time),
})
This is how it's triggered using an event handler:
import { start, Transport } from 'tone'
// Event handler attached to a button's onClick
function onButtonClicked() {
// Begin playback
start()
// Attempting to stop any current sound
Transport.cancel()
Transport.stop()
// Restart playback
Transport.start()
sequence.start()
}
Is there a way to completely silence all sound before starting a new sequence?