AudioKit 5 - Mixer only plays 1 AmplitudeEnvelope at a time

Viewed 100

I am using AudioKit 5 (develop) with Xcode 12 and iOS 14.

I start by creating an array of 28 Oscillators and an array of 28 AmplitudeEnvelopes.

I initialize the 28 AmplitudeEnvelopes with the corresponding Oscillator, and start all of the oscillators, but do not start the AmplitudeEnvelopes.

I connect every AmplitudeEnvelope to a Mixer and then assign the AudioEngine's output to be that Mixer.

When the user touches 1 of the 28 buttons corresponding to one of the AmplitudeEnvelopes, I start that envelope and I get the correct sound coming from the speakers (attack and release is correct).

However, when the user lifts his/her/their finger from the button, I access the array of AmplitudeEnvelopes and stop the corresponding envelope.

However, if I press and hold one button, and then I start to press another, the first AmplitudeEnvelope cuts out automatically.

Why?

In the touch gesture recognizer, I print out "START" and "STOP" whenever the gesture begins and ends, respectively.

I find that starting one envelope does not stop another, so I don't know what the problem is.

    @objc func handletouch(gesture: UILongPressGestureRecognizer) {
        if gesture.state == .began {
            print("START")
            self.soft.impactOccurred()
            let theindex = (gesture.view as! BUTTON).theindex
            let thefrequency = (gesture.view as! BUTTON).midinumber.midiNoteToFrequency()
            let theoscillator = self.oscillators[theindex]
            theoscillator.frequency = thefrequency
            let theenvelope = self.envelopes[theindex]
            theenvelope.start()
        }
        if gesture.state == .ended {
            print("STOP")
            let theindex = (gesture.view as! BUTTON).theindex
            let theenvelope = self.envelopes[theindex]
            theenvelope.stop()
        }
    }
1 Answers

It turns out that I was able to solve the problem.

I created an array of oscillators with the Array<Oscillator> = Array(repeating, count) and found that all of the oscillators would be given the same frequency that I assigned to any of them.

So, in reality, more than one AmplitudeEnvelope was being played at the same time, but they were playing with the same frequency.

I just initialized an empty array of Oscillators and manually created them in a simple for loop. This solved the problem.

Related