1

我没有运气在我的 IOS 应用程序中加载声音字体文件 (.SF2)。我最初尝试使用来自 Tech note TN2283 的 Apple 代码

- (OSStatus) loadFromDLSOrSoundFont: (NSURL *)bankURL withPatch: (int)presetNumber {

OSStatus result = noErr;

// fill out a instrument data structure
AUSamplerInstrumentData instdata;
instdata.bankURL  = (CFURLRef) bankURL;
instdata.instrumentType = kInstrumentType_DLSPreset;
instdata.bankMSB  = kAUSampler_DefaultMelodicBankMSB;
instdata.bankLSB  = kAUSampler_DefaultBankLSB;
instdata.presetID = (UInt8) presetNumber;

// set the kAUSamplerProperty_LoadPresetFromBank property
result = AudioUnitSetProperty(self.mySamplerUnit,
                              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);

return result; }

但是编译器抱怨说“结构 AUSamplerInstrumentData”中没有名为“bankURL”的成员,这是真的,该结构不包含“bankURL”成员?

然后我遇到了以下代码,我相信是 Apple

- (OSStatus)loadSoundFont:(NSURL *)bankURL withPatch:(int)presetNumber
{
    OSStatus result = noErr;

    // fill out a bank preset data structure
    AUSamplerBankPresetData bpdata;
    bpdata.bankURL  = (__bridge CFURLRef) bankURL;
    bpdata.bankMSB  = kAUSampler_DefaultMelodicBankMSB;
    bpdata.bankLSB  = kAUSampler_DefaultBankLSB;
    bpdata.presetID = (UInt8) presetNumber;

    // set the kAUSamplerProperty_LoadPresetFromBank property
    result = AudioUnitSetProperty(self.samplerUnit,
                                  kAUSamplerProperty_LoadPresetFromBank,
                                  kAudioUnitScope_Global,
                                  0,
                                  &bpdata,
                                  sizeof(bpdata));

    // check for errors
    NSCAssert (result == noErr,
               @"Unable to set the preset property on the Sampler. Error code:%d '%.4s'",
               (int) result,
               (const char *)&result);

    return result;
}

这一切看起来都是正确的,但是当我尝试使用这种方法加载声音字体时,如下所示

NSURL *SFURL = [[NSBundle mainBundle] URLForResource:@"YAMAHA DX7Piano" withExtension:@"SF2"];
[self loadSoundFont:url withPatch:0];

它抛出错误“无法在采样器上设置预设属性..”这确实让我认为我指定补丁编号的方式存在一些错误,例如提供不存在的补丁编号。但我最终发现我提供的 NSURL 是空的,所以我尝试按如下方式指定 url:

NSString *resources = [[NSBundle mainBundle] resourcePath];
NSURL *SFURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",resources,@"YAMAHA DX7Piano.SF2"]];

这让我更近了一步。我想我现在正在为我的应用程序包中的声音字体文件提供一个有效的 url。但它仍然无法正常工作。我的编译器现在告诉我

错误:[0x19a824310] 486:DLS/SF2 bank 加载失败

有一块拼图不见了,我看不到什么。??

4

1 回答 1

1

好吧,我找到了解决方案。声音字体未加载。它没有加载,因为它没有正确添加到应用程序包中。我已将其拖入资源中,但随后必须将其添加到项目“构建阶段”中的“复制捆绑资源”中。

于 2015-07-21T14:07:17.850 回答