4

I've been searching all over to find some sample code of how to use Apple's MusicSequence framework or anything else available. I'm an Objective C programmer, so I find the MusicSequence documentation (and also WWDC videos) rather thin and TBH, without some sample code, unusable from where I stand.

I am able to send MIDI messages using PGMidi but what I can't find is how to create events and sequence/loop them in real time, allowing the possibility to change the pattern while it's playing - classic drum machine / step sequencer style. I found some references to CADisplayLink for accurate timing, but that doesn't help either (although it's being called 60 times/second - every 0.016667 sec, a BPM of 135 would need calls every 0.44444 which is not divisible by the 0.016667 frame).

I've been looking at libpd also and while it's possible to create a sequencer directly in pd, the recommendation is to use iOS' timing.

Everything else I found on GitHub is more than two years old and usually comes with many compiler errors.

kanstraktar
  • 5,357
  • 2
  • 21
  • 29
  • 1
    You can tell CoreMIDI when to send future MIDI events. Use the `timeStamp` field in a `MIDIPacket` in a `MIDIPacketList`. (If you specify a zero timestamp, it means "send immediately", which is probably what you're doing now.) – Kurt Revis Nov 30 '14 at 20:53
  • So in order to make a loop/sequence using the timestamp, what should I do? The way I see it, is that if I could get notified by a delegate prior to executing the current event, I could send it the next one for execution at a later time. But I can't find anything like this. – kanstraktar Dec 01 '14 at 21:40
  • Set a timer to run periodically on a regular interval. (You could continue to use the CADisplayLink if you like.) When the timer fires, give CoreMIDI the whole next interval's worth of timestamped events all at once. It will take care of playing them at the right time. The length of the interval is up to you -- it's a tradeoff between processing overhead and latency in reacting to the user. – Kurt Revis Dec 02 '14 at 07:27
  • OK, thanks, I will try this with an NSTimer. From what I've read in other threads, NSTimer is not too reliable for this kind of precision and the problem with CADisplayLink is that you can't really set a custom time, rather it has to be a multiple of 0.016667 sec. which doesn't work for all the possible cases. – kanstraktar Dec 02 '14 at 16:28
  • The timer would not run when the app is in background. I wonder if there are any other means of getting called while the app is in background. Already thought of implementing a Sequencer within a pseudo audio unit because audio unit render callbacks are called when background mode ‘audio’ is enabled. – bio Mar 05 '18 at 07:59

0 Answers0