AudioKit / DunneAudioKit

Sampler and Synth Instruments as well as Chorus, Flanger and Stereo Delay Effects for AudioKit
MIT License
45 stars 29 forks source link

Ramping mastervolume for Synth and Sampler #10

Closed andreiantonescu closed 2 years ago

andreiantonescu commented 2 years ago

I have a sequencer based on sampled sounds. The samples are around 1-2 secs long, but the sequencer can play them faster, meaning it stops the sample before it gets to the end.

When it does so, there’s a “click/tick” sound and I’m trying to prevent this by ramping the volume down before actually stopping the node. However, the ramp doesn't seem to have an effect.

I've managed to reproduce this with the Synth as well, for simplicity.

To Reproduce See example code here:

    let akEngine = AudioEngine()
    let osc = Synth()

    override func viewDidLoad() {
        super.viewDidLoad()

        akEngine.output = osc
        do {
            try akEngine.start()
        } catch {
            print("Couldn't start AudioEngine.")
        }

        osc.play(noteNumber: 50, velocity: 100)
        osc.$masterVolume.ramp(to: 0, duration: 1)
        sleep(2)
        osc.stop()
    }

Expected behavior I would have expected the note to stop playing before the sleep call finishes, thus ending via the ramp, not abruptly via the stop call.

Details (please complete the following information):

Type: Simulator iPhone 13 Pro OS: iOS 15.5 AudioKit Version 5.4.2 DunneAudioKit Version 5.4.1

verySimpleRampTest2.zip Sample project attached.

andreiantonescu commented 2 years ago

For anyone affected by this, I managed to circumvent it by passing the node through a Fader and then ramping the Fader's gain, like this:

        fader = Fader(osc)
        akEngine.output = fader

        do {
            try akEngine.start()
        } catch {
            print("Couldn't start AudioEngine.")
        }

        osc.play(noteNumber: 30, velocity: 100)
        fader?.$leftGain.ramp(to: 0, duration: 1)
        fader?.$rightGain.ramp(to: 0, duration: 1)

        sleep(2)
        osc.stop()