How do you dynamically add new instruments to AUGraph

183 views Asked by At

I am currently making a music app where you can add new instruments as tracks to the music sequnce.

Here's what I have for when a new track has been added:

MusicPlayerStop(_musicPlayer);

status = MusicSequenceNewTrack(sequence, &tracks[tracksCount]);

timeDiff = 32.f/_tempo;

[self setLoopDuration:currentLoopDuration forPattern:tracks[tracksCount]];

AudioComponentDescription samplerNodeDesc;
samplerNodeDesc.componentManufacturer = (OSType)kAudioUnitManufacturer_Apple;
samplerNodeDesc.componentType = (OSType)kAudioUnitType_MusicDevice;
samplerNodeDesc.componentSubType = (OSType)kAudioUnitSubType_Sampler;
samplerNodeDesc.componentFlags = 0;
samplerNodeDesc.componentFlagsMask = 0;


status = AUGraphAddNode(graph, &samplerNodeDesc, &auNodes[tracksCount]);
status = AUGraphNodeInfo(graph, auNodes[tracksCount], 0, &audioUnits[tracksCount]);


status = AUGraphConnectNodeInput(graph, auNodes[tracksCount], 0, filterNode, 0);
status = MusicTrackSetDestNode(tracks[tracksCount], auNodes[tracksCount]);

int trackId = tracksCount;

tracksCount++;

MusicPlayerStart(_musicPlayer);


return trackId;

The following doesn't produce audio at all, what can I Do here please.

Thanks so much !

1

There are 1 answers

0
dave234 On

You need to call AUGraphUpdate(graph, nil); Or if you want the graph to block until it has ben added you supply it with a pointer to a Boolean and the calling thread will wait for the audio thread to add the audio unit.

Boolean updated;
AUGraphUpdate(graph, &updated); //will block until end of next audio cycle

if (updated){
    printf("hooray\n");
}