0

I'm trying to write a program to shift the key of a midi file. Basically, I just need to shift every note event by a given amount and live the rest unchanged. I found it easy to use MIKMIDI to read, parse, modify and write back the stream.

Unfortunately, I have a problem that I'm unable to solve. I've a loop in which I select the note events and add/subtract the desired shift value, but when I append the event in the output track I get a message from the MIKMIDI library:

"Warning: attempted to insert a NULL event".

The code I wrote is the following:

        for event in inputTrack.events {
            if event.eventType == .midiNoteMessage {
                var tmpData = event.data
                if (event.data[0] != 9) { // skip percussion channel
                    tmpData[1] = event.data[1] - shift
                    }
                let outEvent = MIKMIDIEvent(timeStamp: event.timeStamp, midiEventType: .midiNoteMessage, data: tmpData)!
                outputSeq.tracks[i].events.append(outEvent)
                }
            else {
                outSeq.tracks[i].events.append(event)
                }
            }

BTW, the code works perfectly (the midi file is plays as expected), it is just that it takes minutes to execute in debugging mode due to the infinite sequence of warning messages printed in the debug screen. Thanks!

Nitin Nain
  • 5,113
  • 1
  • 37
  • 51
FranceD
  • 9
  • 2
  • Latest update: I've changed my source code using the addEvent function provided by the library instead of using the generic Swift 'append'. It now generates less warnings (still someone). The problem now is that it also produces a memory leak. I don't know how it is possible, but it increases rapidly to 1.5 GB while processing a few thousands of midi events. I'm really puzzled... – FranceD Apr 19 '20 at 10:47
  • Any chance you could share your project with me via email? I don't know off the top of my head what would cause this but am happy to take a look. You can find my contact info in the MIKMIDI README (I'm the author and maintainer). – Andrew Madsen May 12 '20 at 04:57
  • Thanks a lot Andrew. Indeed at the moment it would be difficult to do that as I had to replace my MIKMID-based piece of code with the sequencer provided by AVAudioEngine, to exploit the other tools of that framework. However, it was quite troublesome (in the end, I had to write a swift MIDI parser from scratch). It would be great if MIKMIDI could be integrated as an input node of an AVAudioEngine, but I did not find a way to do that. – FranceD May 13 '20 at 17:08

0 Answers0