AVAudioUnitSampler SF2 SoundBank Load Failure

My use case is to play a MIDI file using a backing SF2 instrument. I have a SF2 file that contains sound samples for just one ethnic instrument. However, I am not able to successfully load that soundbank into the AVAudioUnitSampler instance.


I have been able to successfully load a popular GM soundfont (using the sample code found on the internet). Below is the code snippet that works fine.

_sf2URL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"GeneralUser GS MuseScore v1.442"ofType:@"sf2"]];
[_sampler loadSoundBankInstrumentAtURL:_sf2URL program:6 bankMSB: kAUSampler_DefaultMelodicBankMSB bankLSB:0error:&error];


However, for my sf2 file, the load fails. So, I am pretty confident that the issue is with supplying the right values for the below parameters of the loadSoundankInstrumentAtURL method:


a) Program number
b) Soundbank MSB

c) Soundbank LSB


I tried several combinations, but none worked. I would like to understand a proven approach to get the right values for these parameters. May be to use a soundfont editor and look for some specific values in there?


Appreciate your help.

Answered by in 171934022

If your bank has only one instrument, it seems likely that it is at program 0.


Virtually none of these banks *actually* use the 0x78, 0x79 bank numbers that are exposed via the API -- these are the General MIDI Version 2 bank numbers. The code uses those values to interpret what it finds in the bank file, because there are many different numbering systems out there (and the loading system tries a few possibilities). If yours is really different, you will have to give it the exact MSB and LSB values to get it to load.


There used to be SF2 editors out there which would let you examine the bank numbers, etc. I don't know if they still exist.


You could also repeat your test, using program 0 and then walking through all the bank permutations (127*127, but hey, the computer does all the work).


-DS

Please paste here the text of any errors you see in the system log when you run this command, thanks.


-DS

@SoundOfMind, please find the error message below:

SoundFontDemo[12264:1028008] 18:23:16.475 ERROR: 93: BankEntry::LoadInstrument: Unable to find patch 7 bank 0x79/0

SoundFontDemo[12264:1028008] 18:23:16.477 ERROR: 486: DLS/SF2 bank load failed

SoundFontDemo[12264:1028008] 18:23:16.478 ERROR: AVAudioUnitSampler.mm:163: -[AVAudioUnitSampler loadSoundBankInstrumentAtURL:program:bankMSB:bankLSB:error:]: error -10851


Thanks and I would like to understand how to find the right values for program/bankLSB/bankMSB, given a SF2 file.

It looks like you correctly passed 0x79 for the bank MSB (there is a constant, kAUSampler_DefaultMelodicBankMSB, for this, in the AU headers). BankLSB should be 0 unless you know you have a bank with instrument variations.


Unless your SF2 bank file is a complete GM-compatible bank (i.e., it has an instrument for every program number from 0 to 127), there is the possibility that you are requesting a program that does not exist in a (sparse) bank. Does this call to loadSoundBankInstrumentAtURL:program:bankMSB:bankLSB:error fail on this bank for ALL program numbers? How about other bank files?


-DS

Keeping the BankLSB and BankMSB values constant, I ran the loadSoundBankInstrumentAtURL function in a loop to provide it values 0 to 127. In all the cases, the same error was observed.


So, may be even the bankLSB and MSB values could be wrong.


The SF2 file that I have is certainly a sparse bank, with only one instrument in it.


Would it help if I share the SF2 file itself for you to help suggest the right values for the program-number, bank LSB and bank MSB that will not lead to load-failure? Else, if I get a method to inspect these files and arrive at the right values myself, that'd be great too.


Kindly advise.

Try your code with a valid Sound Font. That will tell you if the problem is your "ethnic sound font" or your code.

Try the Fluid Sound Font which I'm sure is valid.

https://musescore.org/en/handbook/soundfont

Accepted Answer

If your bank has only one instrument, it seems likely that it is at program 0.


Virtually none of these banks *actually* use the 0x78, 0x79 bank numbers that are exposed via the API -- these are the General MIDI Version 2 bank numbers. The code uses those values to interpret what it finds in the bank file, because there are many different numbering systems out there (and the loading system tries a few possibilities). If yours is really different, you will have to give it the exact MSB and LSB values to get it to load.


There used to be SF2 editors out there which would let you examine the bank numbers, etc. I don't know if they still exist.


You could also repeat your test, using program 0 and then walking through all the bank permutations (127*127, but hey, the computer does all the work).


-DS

Thank you much. I had to open the sf2 file in an editor (I used Polyphony) and had to change the 'bank' number from 108 to 128 to get it to work with the Sampler AV Audio Unit. Issue resolved.

I am glad - but if you were to request the instruments from the original file using bank MSB 108 it should work, too.


-DS

I figured that the sampler loads percussive presets from SoundFont bank 128 and melodic presets from SoundFont bank 0.
I have some GM2/XG SoundFonts here, with banks 127, 126, 124, or 8. I did not manage to load presets from any of these banks.

TN2283 says:
"Note: The MSB/LSB for the bank plus a preset number are the same three values used if you were selecting a bank and preset via MIDI."
Furthermore: "If you know the bank contains other variations, you may use those values."

So I have a MIDI file that requests bankMSB 127, no bankLSB, and preset 0. The sampler fails to load the preset but the SF does have a drumkit at bank 127 (I verified this with Polyphone).
TN2283 does not specify how AUSampler maps bankMSB/bankLSB to a SoundFont bank. A SoundFont bank is only one number, and it can even be higher than 127.

Maybe I should write a feedback bug report. Or maybe the AUSampler is only GM2 compatible, which means the TN is wrong.

Edit:
You can select any bank in the SF like this:

Banks 0-127: bankMSB = kAUSamplerDefaultMelodicBankMSB, bankLSB = (SoundFont bank)
Bank 128: bankMSB = kAUSampler
DefaultPercussionBankMSB, bankLSB = kAUSampler_DefaultBankLSB

No idea for banks > 128.
AVAudioUnitSampler SF2 SoundBank Load Failure
 
 
Q