0

I am trying to make a button that plays an oscillator that has ADSR and if the button is pressed while the oscillator is already playing, I want it to override the already playing oscillator and replay the oscillator from the start.

This code achieves my goal, but the "sleep" is way too long; it needs to be instantaneous.

@IBAction func play(_ sender: UIButton) {
    envelope.stop()
    oscillator.stop()
    sleep(1)
    envelope.start()
    oscillator.start()
}
c3n3
  • 25
  • 5
  • 3
    FYI - **Never** use `sleep` on the main thread. – rmaddy Jan 02 '17 at 04:10
  • I know sleep is not a good solution, it just shows what my goal is. – c3n3 Jan 02 '17 at 14:02
  • Why do you need to start and stop the oscillator? If you're putting an amplitude envelope on it, you're not hearing the first cycles anyway. – Aurelius Prochazka Jan 20 '17 at 09:13
  • I need to stop and start it because when I try to play the oscillator again, since it has already been started, the code must be stopped before it will replay the oscillator. – c3n3 Feb 15 '17 at 15:54

0 Answers0