0

Using the AudioKit Cookbook as a reference, I am trying to build a simple application to test AudioKit features before building a larger application.

There are 2 buttons, 2 oscillators, 2 amplitude envelopes for each oscillator, 2 faders for each envelope, and 1 mixer.

Using the "prepare_audio" function, I instantiate the objects, assign oscillator frequencies, assign envelope parameters, create the mixer, set the engine output, and start the engine.

Inside the two gesture recognizer handler functions, I open and close the gates of the amplitude envelopes when the gesture begins and ends, respectively.

When I run the application and press the buttons for the first time, there is an unwanted pause in the application, then the sound starts.

However, the release duration is programmed to be 1, but it is possible to hear the sound for much longer, say, 4 seconds.

Any help would be greatly appreciated - either on the extended release, or the unwanted pause. Thanks.

class ViewController {

    var engine = AudioEngine()
    var oscillator1: Oscillator?
    var oscillator2: Oscillator?
    var envelope1: AmplitudeEnvelope?
    var envelope2: AmplitudeEnvelope?
    var fader1: Fader?
    var fader2: Fader?
    var mixer: Mixer?

    var button = UIView()
    var button2 = UIView()

    ...
}
func prepare_audio {
        self.oscillator1 = Oscillator()
        self.oscillator1?.frequency = 440
        self.oscillator1?.start()
        
        self.oscillator2 = Oscillator()
        self.oscillator2?.frequency = 550
        self.oscillator2?.start()
        
        self.envelope1 = AmplitudeEnvelope(self.oscillator1!)
        self.envelope1?.attackDuration = 0.25
        self.envelope1?.decayDuration = 0
        self.envelope1?.sustainLevel = 1
        self.envelope1?.releaseDuration = 1
        
        self.envelope2 = AmplitudeEnvelope(self.oscillator2!)
        self.envelope2?.attackDuration = 0.25
        self.envelope2?.decayDuration = 0
        self.envelope2?.sustainLevel = 1
        self.envelope2?.releaseDuration = 1
        
        self.fader1 = Fader(self.envelope1!)
        self.fader2 = Fader(self.envelope2!)
        
        let faders = [self.fader1!, self.fader2!]
        self.mixer = Mixer(faders)
        
        self.engine.output = self.mixer
        try? self.engine.start()

}
@objc func handlebutton1(gesture: UIGestureRecognizer) {
        if gesture.state == .began {
            self.envelope1?.openGate()
        }
        if gesture.state == .ended {
            self.envelope1?.closeGate()
        }
}

@objc func handlebutton2(gesture: UIGestureRecognizer) {
        if gesture.state == .began {
            self.envelope2?.openGate()
        }
        if gesture.state == .ended {
            self.envelope2?.closeGate()
        }
}

UPDATE: I was able to eliminate the unwanted pause by immediately opening and closing the envelope gates after I started the audio engine. See below:

...
        try? self.engine.start()
        
        self.envelope1?.openGate()
        self.envelope2?.openGate()

        self.envelope1?.closeGate()
        self.envelope2?.closeGate()

0 Answers0