Jas999
Jas999

Reputation: 111

How to load Sound Font

I'm having no luck loading a Sound Font file (.SF2) in my IOS app. I initially tried using Apple's code from Tech note TN2283

- (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; }

But the compiler complains that 'No member named 'bankURL' in struct AUSamplerInstrumentData' which is true, the struct does not contain a 'bankURL' member??

I then came across the following code, by Apple I believe

- (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;
}

This all looks correct but when I attempt to load a sound font using this method such as follows

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

it throws the error "Unable to set the preset property on the Sampler.." This did lead me to think there was some error in how I specified the patch number, such as supplying a non-existent patch number. But I did eventually discover that the NSURL I was supplying was null, so I tried specifying the url as follows:

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

This has got me a step closer. I think I am now supplying a valid url to the sound font file in my app bundle. But it still is not working. My compiler now tells me

ERROR: [0x19a824310] 486: DLS/SF2 bank load failed

There is a piece of the puzzle missing and I can't see what.??

Upvotes: 1

Views: 1016

Answers (1)

Jas999
Jas999

Reputation: 111

Well I found the solution. The Sound font wasn't loading. It wasn't loadoing because it was not added to the app bundle correctly. I had dragged it into resources but had to then add it to 'Copy bundle resources' in the project 'Build Phases'.

Upvotes: 1

Related Questions