2

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!

David
  • 41
  • 7

1 Answers1

2

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.

Aurelius Prochazka
  • 4,510
  • 2
  • 11
  • 34
  • First - thanks! Can you please explain more and give a code example? And you mean like "envelope.open"? I'm really new. – David Jun 23 '21 at 11:02
  • Ok, I used this code and it is work but I'm still getting a digital noise (tic) when I close it. is it because of the computer or I need to add something? ` var isGateOpend = false . . . if isGateOpend{ envelope.closeGate() isGateOpend = false } else { envelope.openGate() isGateOpend = true } ` – David Jun 23 '21 at 13:08
  • 1
    With a release value of 0.3 yo should not hear a click when running envelope.closeGate(). Are you also stopping the oscillator as it looks like you are in offBtn code originally? – Aurelius Prochazka Jun 24 '21 at 17:05
  • Even with a release value of 0.7 I still have a click, and I don't have oscillator.stop() anymore. Do you have any idea why I still have a click? The attack works good – David Jun 25 '21 at 15:03
  • 1
    FWIW I pasted in your code and I don't hear a click on my system. – Aurelius Prochazka Jun 28 '21 at 05:31
  • Solved, Thanks! When I wrote it correctly with mvc.. – David Jul 19 '21 at 11:11