我正在创建一个音乐应用程序,您可以在其中添加新乐器到现有音序器。这些乐器是存储在阵列中的独立AudioUnitSamplers,因此当加载新乐器和音轨时,它将加载该音轨将要使用的声音。
这是我目前如何向AUGraph添加曲目。
// Return count as an id for the new track
- (int) addMusicTrack: (MusicTrack)musicTrack withNode:(AUNode)newSamplerNode withAudioUnit:(AudioUnit)newSamplerUnit {
tracksCount++;
int trackId = tracksCount;
MusicPlayerStop(_musicPlayer);
status = MusicSequenceNewTrack(sequence, &tracks[trackId]);
MusicSequenceSetAUGraph(sequence, graph);
if(status){
printf("Error adding main track: %d", (int)status);
status = 0;
}
timeDiff = 32.f/_tempo;
[self setLoopDuration:DEFAULT_LOOP_DURATION forTrack:trackId];
currentOctaveNumber = 59;
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, &auSampleNodes[trackId]);
AudioComponentDescription splitterNodeDesc;
splitterNodeDesc.componentManufacturer = (OSType)kAudioUnitManufacturer_Apple;
splitterNodeDesc.componentType = (OSType)kAudioUnitType_FormatConverter;
splitterNodeDesc.componentSubType = (OSType)kAudioUnitSubType_Splitter;
splitterNodeDesc.componentFlags = 0;
splitterNodeDesc.componentFlagsMask = 0;
status = AUGraphAddNode(graph, &splitterNodeDesc, &splitterNodes[trackId]);
// node info
status = AUGraphNodeInfo(graph, auSampleNodes[trackId], 0, &auSampleUnits[trackId]);
status = AUGraphNodeInfo(graph, splitterNodes[trackId], 0, &splitterUnits[trackId]);
// connecting nodes samplernode ==> Splitter ==> Mixer
status = AUGraphConnectNodeInput(graph, auSampleNodes[trackId], 0, splitterNodes[trackId], 0);
status = AUGraphConnectNodeInput(graph, splitterNodes[trackId], 0, mixerNode, trackId);
status = MusicTrackSetDestNode(tracks[trackId], auSampleNodes[trackId]);
status = AUGraphUpdate(graph, NULL);
if (status) {printf("Error updating graph: %ld\n", status);}
MusicPlayerStart(_musicPlayer);
return trackId;
}
这就是我为该曲目加载声音的方式:
[self samplerUnit:auSampleUnits[trackId] loadFromDLSOrSoundFont:url withPatch:patchNumber];
soundfont正在加载:
- (OSStatus)samplerUnit:(AudioUnit)sampler loadFromDLSOrSoundFont:(NSURL *)bankURL withPatch:(int)presetNumber{
OSStatus result = noErr;
// Fill out the sampler instrument data structure
AUSamplerInstrumentData insdata;
insdata.fileURL = (__bridge CFURLRef) bankURL;
insdata.bankMSB = kAUSampler_DefaultMelodicBankMSB;
insdata.bankLSB = (UInt8)0;
insdata.presetID = (UInt8) presetNumber;
insdata.instrumentType = kInstrumentType_DLSPreset; // DLS and SF2 are the same enum values
// Load the instrument
result = AudioUnitSetProperty(sampler,
kAUSamplerProperty_LoadInstrument,
kAudioUnitScope_Global,
0,
&insdata,
sizeof(insdata));
NSCAssert (result == noErr,
@"Unable to set the preset property on the Sampler. Error code: %d",
(int) result);
return result;
}
目前,为最近添加的曲目加载了合适的声音,但旧曲目的声音现在是默认的AudioUnitSampler正弦波声音。
我一直试图解决这个问题大约一天但是还没有找到解决方法,那里有任何解决方案吗?
干杯!!!
答案 0 :(得分:1)
您是否将trackCount与MusicSequenceGetTrackCount
进行了对比?
查看MusicSequenceSetAUGraph
的文档。
"默认情况下,序列中的所有音乐曲目都定向到图表中作为乐器单位的第一个节点(类型为DLSMusicDevice"
你想让分离器做什么?它现在做得不多。