我有一个由两个节点组成的 AUGraph:AUSampler 单元和输出单元。 我正在将示例从 sf2 文件加载到 AUSampler。我有多个 sf2 文件,想在运行时在它们之间切换。
目前我有以下代码来设置 sf2 文件:
- (void) loadSoundFontWithName:(NSString *) name {
CheckError (AUGraphStop(_midiPlayer.graph), "couldn't stop graph");
OSStatus result = noErr;
// fill out a instrument data structure
AUSamplerInstrumentData instdata;
NSString *presetPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat: @"Sampler Files/%@", name]
ofType:@"sf2"];
const char* presetPathC = [presetPath cStringUsingEncoding:NSUTF8StringEncoding];
NSLog (@"presetPathC: %s", presetPathC);
CFURLRef presetURL = CFURLCreateFromFileSystemRepresentation(
kCFAllocatorDefault,
presetPathC,
[presetPath length],
NO);
instdata.fileURL = presetURL;
instdata.instrumentType = kInstrumentType_DLSPreset;
instdata.bankMSB = kAUSampler_DefaultMelodicBankMSB;
instdata.bankLSB = kAUSampler_DefaultBankLSB;
instdata.presetID = (UInt8) 0;
// set the kAUSamplerProperty_LoadPresetFromBank property
result = AudioUnitSetProperty(_midiPlayer.instrumentUnit,
kAUSamplerProperty_LoadInstrument,
kAudioUnitScope_Global,
0,
&instdata,
sizeof(instdata));
// check for errors
NSCAssert (result == noErr,
@"Unable to set the preset property on the Sampler. Error code:%d '%.4s'",
(int) result,
(const char *)&result);
//===============
CheckError (AUGraphStart(_midiPlayer.graph), "couldn't start graph");
}
仅当切换到另一个文件时没有任何声音播放时它才起作用。如果在播放图形时设置该属性,则会在 DLSSample::GetMoreFrames(unsigned long long, unsigned long long, void*) () 处崩溃并出现 EXC_BAD_ACCESS
那么更新此属性的正确方法是什么?
最佳答案
调用
AudioUnitReset(_midiPlayer.instrumentUnit, kAudioUnitScope_Global, 0);
调用 AudioUnitSetProperty 函数后似乎解决了我的问题。
关于ios - 如何正确更新AUSampler的loadInstrument属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32538234/