Get AVAudioEngineGraph programmatically

0

In my app I created an AVAudioEngine and attached AVAudioNodes on it.

  • Is it possible to get the attached nodes programmatically?
  • Is it possible to get the nodes connected to a given AVAudioNode?

When I log the engine's description I can see all the AVAudioEngineGraph, but I cannot find the way to get this information programmatically, can someone give me a solution?

(lldb) po _engine

________ GraphDescription ________
AVAudioEngineGraph 0x7fb04541ac60: initialized = 1, running = 1, number of nodes = 4

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

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

 node 0x60000244cd00 {'aumx' 'mcmx' 'appl'}, 'I'
     inputs = 1
         (bus0, en1) <- (bus0) 0x600002473680, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 1
         (bus0, en1) -> (bus0) 0x600002448c00, {'auou' 'rioc' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x600002473680 {'aumx' 'mcmx' 'appl'}, 'I'
     inputs = 1
         (bus0, en1) <- (bus1) 0x600002462580, {'auou' 'rioc' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 1
         (bus0, en1) -> (bus0) 0x60000244cd00, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x600002462580 {'auou' 'rioc' 'appl'}, 'I'
     outputs = 2
         (bus0, en0) -> (bus0) 0x0, {}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
         (bus1, en1) -> (bus0) 0x600002473680, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 ******** input chain ********

 node 0x600002462580 {'auou' 'rioc' 'appl'}, 'I'
     outputs = 2
         (bus0, en0) -> (bus0) 0x0, {}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
         (bus1, en1) -> (bus0) 0x600002473680, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x600002473680 {'aumx' 'mcmx' 'appl'}, 'I'
     inputs = 1
         (bus0, en1) <- (bus1) 0x600002462580, {'auou' 'rioc' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 1
         (bus0, en1) -> (bus0) 0x60000244cd00, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

 node 0x60000244cd00 {'aumx' 'mcmx' 'appl'}, 'I'
     inputs = 1
         (bus0, en1) <- (bus0) 0x600002473680, {'aumx' 'mcmx' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]
     outputs = 1
         (bus0, en1) -> (bus0) 0x600002448c00, {'auou' 'rioc' 'appl'}, [ 2 ch,  44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved]

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

ios
objective-c
avfoundation
core-audio

1 Answer

0

In order to manage custom initializations I created audioUnit wrappers for each category (or specific) audioUnits I am going to load. These wrappers are provided by a factory and referenced by a business object of my app.

The wrapper I mainly worked on up to now, is the so called AUMIDISynth

componentType = kAudioUnitType_MusicDevice;
componentSubType = kAudioUnitSubType_MIDISynth;
componentManufacturer = kAudioUnitManufacturer_Apple;

which allows to load and play different patches from a soundBank file at the same time.

The subclass wrapper for AUMIDISynth exposes a method called

-(NSError*)loadProgramChange:(uint8_t)programChange channel:(uint8_t)channel;

Other objects (SynthPlayers) observe the notifications sent from the AudioEngine start, and respond to this notification by calling the above method to their own audioUnit wrapper implemented as follows

-(NSError*)loadProgramChange:(uint8_t)programChange channel:(uint8_t)channel{  
    NSError *error;
    if (!self.audioUnit.engine || !self.audioUnit.engine.isRunning) {
        error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:nil];
    }
    uint32_t enabled = 1;
    OSStatus status = AudioUnitSetProperty(
                                           self.audioUnit.audioUnit,
                                           kAUMIDISynthProperty_EnablePreload,
                                           kAudioUnitScope_Global,
                                           0,
                                           &enabled,
                                           sizeof(enabled));
    if (status != noErr) {
        NSLog(@"error %d",status);
        error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:nil];
    }
    uint8_t pcCommand = 0xC0 | channel;
    status = MusicDeviceMIDIEvent(self.audioUnit.audioUnit, pcCommand, programChange, 0, 0);
    if (status != noErr) {
        NSLog(@"error %d",status);
        error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:nil];
    }

    enabled = 0;
    status = AudioUnitSetProperty(
                                  self.audioUnit.audioUnit,
                                  kAUMIDISynthProperty_EnablePreload,
                                  kAudioUnitScope_Global,
                                  0,
                                  &enabled,
                                  sizeof(enabled));
    if (status != noErr) {
        NSLog(@"error %d",status);
        error = [NSError errorWithDomain:NSURLErrorDomain code:-1 userInfo:nil];
    }
    return error;
}
answered on Stack Overflow Oct 27, 2018 by Enrico Cupellini • edited Oct 27, 2018 by Enrico Cupellini

User contributions licensed under CC BY-SA 3.0