ios - 如何正确更新AUSampler的loadInstrument属性?

标签 ios objective-c core-audio audiounit

我有一个由两个节点组成的 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/

相关文章:

ios - 使用不同数据从 ViewController 到自身的动画/Segue - Swift

iphone - 如何确保我的 iOS 7 布局在 iOS 6 上看起来相同?

iphone - 使用磁航向获取地理方向

ios - [[NSNotificationCenter defaultCenter] addObserver:选择器:@selector(mymethod)名称:对象:];在iOS 9中不再被调用

ios - 更改 Swift 中的导航栏按钮图标

ios - 重新加载 UITableView 除了它的一部分

objective-c - 是否可以以编程方式绑定(bind)两个关键路径,在 Cocoa 中设置连续更新?

objective-c - 如何使用 AVAssetReader 添加到 AudioBufferList?

ios - 异常 com.apple.coreaudio.avfaudio 原因 : error -50

c++ - 通过 M-Audio ProFire 610 输出声音