ios – Why does my AVMIDIPlayer code not play vocal tracks from a MIDI (.mid) file?

0
1
ios – Why does my AVMIDIPlayer code not play vocal tracks from a MIDI (.mid) file?


If I play a MIDI file (in .mid format) utilizing AVMIDIPlayer, it simply performs instrument tracks, not vocal tracks.

Right here is how I’m taking part in the MIDI file:

func playMIDIAndSyncCursor() {
    guard let midiURL = Bundle.fundamental.url(forResource: midiFileName, withExtension: "mid"),
          let soundFontURL = Bundle.fundamental.url(forResource: soundFontName, withExtension: soundFontExt) else {
        print("❌ Lacking MIDI or DLS file")
        return
    }
    
    extractTrackNames(from: midiURL)
    inspectMIDIFile(midiURL)
    
    do {
        let information = attempt Knowledge(contentsOf: midiURL)
        midiPlayer = attempt AVMIDIPlayer(information: information, soundBankURL: soundFontURL)
        midiPlayer?.prepareToPlay()
        
        playbackStartTime = CACurrentMediaTime()
        Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { _ in
            self.updateCursor()
        }
        
        print("🎡 Beginning playback")
        midiPlayer?.play {
            print("βœ… Playback completed")
            self.displayLink?.invalidate()
        }
    } catch {
        print("❌ MIDI error: (error)")
    }

I scanned the .mid file, and I can see that there are 8 tracks:

func inspectMIDIFile(_ url: URL) {

    var sequence: MusicSequence?
    NewMusicSequence(&sequence) // allocate
            
    guard let seq = sequence else {
        print("Didn't create MusicSequence")
        return
    }
    
    // Load the MIDI file into the sequence
    let standing = MusicSequenceFileLoad(seq, url as CFURL, .midiType, MusicSequenceLoadFlags())
    if standing != noErr {
        print("Error loading MIDI file: (standing)")
        return
    }
            
    var trackCount: UInt32 = 0
    MusicSequenceGetTrackCount(seq, &trackCount)
    print("MIDI has (trackCount) tracks")
}

It prints:

MIDI has 8 tracks

For displaying the musical sheet (rating), I’m utilizing OpenSheetMusicDisplay in a WKWebView and loading the .musicxml/.mxl information. I can see vocal components within the rating sheet as properly, as proven within the under picture:

enter image description here

Vocal components:

  1. Soprano
  2. Alto
  3. Tenor
  4. Bass

Instrument components:

  1. Violino 1
  2. Violino 2
  3. Viola
  4. Contrabasso e Organo

Nevertheless, if I add a .mxl file for a similar association (not a .mid file) on MuseScore, it performs the vocal components as properly.

I downloaded .mid and .mxl information from:
https://www.cpdl.org/wiki/index.php/Ave_verum_corpus,KV_618(Wolfgang_Amadeus_Mozart)

So am I lacking / misunderstanding one thing with my code above? Do I want a .musicxml or .mid file for audio playback, or one thing?

Be aware: I’m not speaking about playback of lyrics (in human voice). I do know I should use solely an .mp3 file for that.

LEAVE A REPLY

Please enter your comment!
Please enter your name here