AVAudioMixerNode gets automatically disconnected from destination node when calling AVAudioEngine.prepare()

421 views Asked by At

I am writing an audio sequencer application using Swift and Mac OS desktop. I user AVAudioMixerNode objects for generic routing and pass-through nodes (nodes where the audio just goes from point A to point B). The pass through nodes provide temporary "pit stops" in the audio chain where I can apply volume changes.

I have found that if you connect an AVAudioMixerNode to the AVAudioEngine's main mixer node, then call prepare, the mixer node gets automatically disconnected. There is no error or warning message. I have been ripping my hair out for four days trying to figure out why some nodes in my signal path were not connecting. Now I have written a simple Playground that proves definitively that this is the case.

import Cocoa
import AVFoundation


let engine = AVAudioEngine()
let mixer1 = AVAudioMixerNode()
engine.attach(mixer1)
let format = engine.mainMixerNode.outputFormat(forBus: 0)
engine.connect(mixer1, to: engine.mainMixerNode, format: format)

var connections = engine.outputConnectionPoints(for: mixer1, outputBus: 0)
print("Number of connections = \(connections.count)")
engine.prepare()
connections = engine.outputConnectionPoints(for: mixer1, outputBus: 0)
print("Number of connections = \(connections.count)")
1

There are 1 answers

6
sbooth On

The node is still present in the graph as evidenced by print but is in a pending state:

let engine = AVAudioEngine()
let mixer1 = AVAudioMixerNode()
engine.attach(mixer1)
engine.connect(mixer1, to: engine.mainMixerNode, format: nil)
print("\(engine)")
engine.prepare()
print("\(engine)")
________ GraphDescription ________
AVAudioEngineGraph 0x10050bc70: initialized = 0, running = 0, number of nodes = 3

     ******** output chain ********

     node 0x600002c50f00 {'auou' 'ahal' 'appl'}, 'U'
         inputs = 1
             (bus0, en1) <- (bus0) 0x600002c51000, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

     node 0x600002c51000 {'aumx' 'mcmx' 'appl'}, 'U'
         inputs = 1
             (bus0, en1) <- (bus0) 0x600002c03b80, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
         outputs = 1
             (bus0, en1) -> (bus0) 0x600002c50f00, {'auou' 'ahal' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

     node 0x600002c03b80 {'aumx' 'mcmx' 'appl'}, 'U'
         outputs = 1
             (bus0, en1) -> (bus0) 0x600002c51000, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
______________________________________



________ GraphDescription ________
AVAudioEngineGraph 0x10050bc70: initialized = 1, running = 0, number of nodes = 3

     ******** output chain ********

     node 0x600002c50f00 {'auou' 'ahal' 'appl'}, 'I'
         inputs = 1
             (bus0, en1) <- (bus0) 0x600002c51000, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  48000 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

     node 0x600002c51000 {'aumx' 'mcmx' 'appl'}, 'I'
         outputs = 1
             (bus0, en1) -> (bus0) 0x600002c50f00, {'auou' 'ahal' 'appl'}, [ 2 ch,  48000 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

     ******** pending connections - output ********

     (bus0) 0x600002c03b80, {'aumx' 'mcmx' 'appl'} -> (bus0) 0x600002c51000, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
______________________________________

I suspect this is because there is no attached input or generator node although I haven't had a chance to experiment further.