Changing instrument with AVMIDIControlChangeEvent bankSelect

I've been trying to use AVMIDIControlChangeEvent with a bankSelect message type to change the instrument the sequencer uses on a AVMusicTrack with no luck.

I started with the Apple AVAEMixerSample, converting the initial setup/loading and portions dealing with the sequencer to Swift. I got that working and playing the "bluesyRiff" and then modified it to play individual notes. So my createAndSetupSequencer looked like

func createAndSetupSequencer() {
        sequencer = AVAudioSequencer(audioEngine: engine)
//        guard let midiFileURL = Bundle.main.url(forResource: "bluesyRiff", withExtension: "mid") else {
//            print (" failed guard trying to get URL for bluesyRiff")
//            return
//        }
        let track = sequencer.createAndAppendTrack()
        var currTime = 1.0
       for i: UInt32 in 0...8 {
            let newNoteEvent = AVMIDINoteEvent(channel: 0, key: 60+i, velocity: 64, duration: 2.0)
            track.addEvent(newNoteEvent, at: AVMusicTimeStamp(currTime))
            currTime += 2.0
        }


The notes played, so then I also replaced the gs_instruments sound bank with GeneralUser GS MuseScore v1.442 first by trying

        guard let soundBankURL = Bundle.main.url(forResource: "GeneralUser GS MuseScore v1.442", withExtension: "sf2") else {
return}

do {
            try sampler.loadSoundBankInstrument(at: soundBankURL, program: 0x001C, bankMSB: 0x79, bankLSB: 0x08)
} catch{....
}

This appears to work, the instrument (8 which is "Funk Guitar") plays. If I change to bankLSB: 0x00 I get the "Palm Muted guitar". So I know that the soundfont has these instruments

Stuff goes off the rails when I try to change the instruments in createAndSetupSequencer. Putting

let programChange = AVMIDIProgramChangeEvent(channel: 0, programNumber: 0x001C)
        let bankChange = AVMIDIControlChangeEvent(channel: 0, messageType: AVMIDIControlChangeEvent.MessageType.bankSelect,  value: 0x00)
        track.addEvent(programChange, at: AVMusicTimeStamp(1.0))
        track.addEvent(bankChange, at: AVMusicTimeStamp(1.0))

just before my add note loop doesn't produce any change. Loading bankLSB 8 (Funk) in sampler.loadSoundBankInstrument and trying to change with bankSelect 0 (Palm muted) in createAndSetupSequencer results in instrument 8 (Funk) playing not Palm Muted.

Loading bankLSB 0 (Palm muted) and trying to change with bankSelect 8 (Funk) doesn't work, 0 (Palm muted) plays

I also tried sampler.loadInstrument(at: soundBankURL) and then I always get the first instrument in the sound font file (piano)no matter what values I put in my programChange/bankChange

I've also changed the time in the track.addEvent to be 0, 1.0, 3.0 etc to no success

The sampler.loadSoundBankInstrument specifies two UInt8 parameters, bankMSB and BankLSB while the AVMIDIControlChangeEvent bankSelect value is UInt32 suggesting it might be some combination of bankMSB and BankLSB. But the documentation makes no mention of what this should look like. I tried various combinations of 0x7908, 0X0879 etc to no avail

I will also point out that I am able to successfully execute other control change events

For example adding

        if i == 1 {
                let portamentoOnEvent = AVMIDIControlChangeEvent(channel: 0, messageType: AVMIDIControlChangeEvent.MessageType.portamento, value: 0xFF)
               track.addEvent(portamentoOnEvent, at: AVMusicTimeStamp(currTime))
                let portamentoRateEvent = AVMIDIControlChangeEvent(channel: 0, messageType: AVMIDIControlChangeEvent.MessageType.portamentoTime, value: 64)
               track.addEvent(portamentoRateEvent, at: AVMusicTimeStamp(currTime))

           }

does produce a change in the sound. (As an aside, a definition of what portamento time is, other than "the rate of portamento" would be welcome. is it notes/seconds? freq/minute? beats/hour?)

I was able to get the instrument to change in a different program using MusicPlayer and a series of MusicTrackNewMIDIChannelEvent on a track but these operate on a MusicTrack not the AVMusicTrack which the sequencer uses.

Has anyone been successful in switching instruments through an AVMIDIControlChangeEvent or have any feedback on how to do this?

Changing instrument with AVMIDIControlChangeEvent bankSelect
 
 
Q