iosswiftaudiokitenvelope

AudioKit 5 Amplitude Envelope no sound


I'm new here and new with music apps (and programming in general).

I'm trying to build a synth app using AudioKit 5 for my final project. I made an oscillator and tried to add Amplitude Envelope but no sound coming out. (If I put the oscillator in the output - there is a sound.)

I saw this question in the internet several times in different types but without any solution.

Does anyone know what is the problem? And if not - do you have other solution for envelope?

The code:

import AudioKit
import SoundpipeAudioKit
import UIKit

class TryingViewController: UIViewController {
    
    var osc = Oscillator(waveform: Table(.sine), frequency: 440, amplitude: 0.8)
    var engine = AudioEngine()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    @IBAction func onBtn(_ sender: UIButton) {
        
        let envelope = AmplitudeEnvelope(osc)
        envelope.attackDuration = 0.1
        envelope.decayDuration = 0.01
        envelope.sustainLevel = 0.5
        envelope.releaseDuration = 0.3
        
        osc.start()
        do {
            try engine.start()
        } catch {
            print(error, "Field")
        }
        engine.output = envelope
        
        envelope.start()
    }
    
    @IBAction func offBtn(_ sender: UIButton) {
        
        osc.stop()
    }
    
}

Edit:

I add this code and it works now, thanks to Aurelius Prochazk!

var isGateOpend = false 
.
.
. 
if isGateOpend{ 
    envelope.closeGate() 
    isGateOpend = false 
   } else { 
    envelope.openGate() 
    isGateOpend = true 
   }

I still have a click, but I will open another question about it if I won't figure it out.

Thanks in advance!


Solution

  • The AmplitudeEnvelope is a "Gated" Node meaning that it responds to openGate and closeGate which should be used instead of start stop since those are at the node level rather than control level.