Proper way to disconnect nodes in AudioKit
Asked Answered
P

1

6

The app I'm developing requires dynamically adding/removing/rearranging components in the sound chain.

So far, I have mostly been using the .disconnectOutput() method on most components, then reconnecting everything. This works most of the time, but occasionally it seems that a node is connected at multiple points in the sound chain, and I also get crashes if the node is connected to AudioKit.output.

AudioKit provides a number of public methods such as .detach(), .disconnectInput(), .disconnect() and I'm not really clear on what is cleanest or safest way to modify the sound chain. What is the best way to do this?

Also, is there some way to keep track of which nodes are connected to which?

Petty answered 18/2, 2018 at 1:9 Comment(2)
CAShow for the underlying AUGraph could be of help. There is a way to get the graph from Audiokit, but i'm not right in front of Xcode developer.apple.com/documentation/audiotoolbox/1475988-cashowMelody
do you have an answer to you question? detach or disconnect?Lanate
D
5

Use the detach() method on an AKNode to remove it from the chain.

The disconnect() and disconnect(nodes: ) methods of AKNode are deprecated. Use AKNode.detach() and AudioKit.detach(nodes: ) instead.

I agree this terminology is very unclear and not explained in the existing documentation. I am still struggling with the lifecycle and runtime chain dynamics as I learn the API, so I can't convey best practices. In general, you don't want to break your object graph. I'm using AKMixer objects and then dynamically attaching child nodes using the .connect(input:bus:) and .disconnectInput(bus:) methods and internal tracking of the associated busses, but I am still running into crashes with this approach :(

Apple's parent AVAudioEngine documentation page provides a couple rules of thumb for dynamic chaining practices: https://developer.apple.com/documentation/avfoundation/avaudioengine

Dag answered 13/6, 2018 at 21:26 Comment(1)
I've been having a signal chain horror show with AudioKit, as well... In the WWDC15 talk on "What's new in Core Audio" they state clearly that nodes must be attached first, then connected after, though I don't know if it follows that they must be disconnected before they are detached. I would assume, however, that if you detach, then they are really "gone" from the engine entirely and need to be re-attached before being reconnected. But if you have any more recent insights, I'd love to hear what you're found.Indigence

© 2022 - 2024 — McMap. All rights reserved.