【问题标题】:How to correctly update AUSampler's loadInstrument property?如何正确更新 AUSampler 的 loadInstrument 属性?
【发布时间】:2015-09-12 11:23:58
【问题描述】:

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

那么更新此属性的正确方法是什么?

【问题讨论】:

    标签: ios objective-c core-audio audiounit


    【解决方案1】:

    打电话

    AudioUnitReset(_midiPlayer.instrumentUnit, kAudioUnitScope_Global, 0);
    

    刚刚调用 AudioUnitSetProperty 函数似乎解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-31
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2021-06-18
      • 1970-01-01
      相关资源
      最近更新 更多