1

I'm starting to dig into asynchronous code with Swift and Mac OS. I want to add a sequencer into my app, so I need a loop pretty much like SpriteKit's game loop runs, without the visual library.

What I want to do is a virtual timeline that I can run, pause, stop and use it to schedule actions that will send network messages. I need that to be as accurate as possible, probably running on a high priority thread or using high priority timers.

As long as I get a high precision run loop running on a different thread as the GUI, I'll be good to build the rest. I'm just looking for the best possible way to achieve that in Swift. Any library available to do that so I don't have to do the basic plumbing?

Thanks!

Edit: This loop has to run no matter what even if the app goes in the background.

The plan is to eventually use it as an OSC sequencer to control a sample accurate audio engine (right now I can get around 14000 OSC messages in about 4 seconds, and it will get bigger). It will process the timing of teiggers and actions processing parameters over time (no audio involved in that loop, at least not directly) and possibly generating/syncing to SMPTE timecode to sync with video and audio playback servers. It is important that the timing stays as precise as possible as it could be used (would be) in live events.

So using a high precision timer for accuracy? Or running a GCD thread? I haven't touched async coding in OS X yet, so I just want to get started in the right direction.

BadgerBadger
  • 696
  • 3
  • 12
  • Is there some reason you don't just use a GCD queue? You can queue, pause, stop, add scheduled timers, etc., on that queue. I'm unclearly why you'd add runloop overhead. – Rob Jul 25 '17 at 00:05
  • Also, you say it needs to be as "accurate as possible". It's overkill for most scenarios, but if you really need high precision timer, consider Apple [Technical Note TN2169](https://developer.apple.com/library/content/technotes/tn2169/_index.html). You might want to qualify what sort of accuracy you really need. – Rob Jul 25 '17 at 00:07
  • @Rob : as I said, I'm exploring different avenues. I haven't used GCD before as I'm fairly new to OS X programming. But I have the feeling that for my purpose, it's something that will eventually need to synchronize with a sample accurate audio engine and probably video servers and timecode tracks for live events uses. Accuracy is everything in my "real field of work" and so is real time. I just want to start the basic building blocks properly. Most of my actions running on the timeline will have to process values (not audio, only parameters) in real time. GUI is only for control. – BadgerBadger Jul 25 '17 at 03:16
  • Adding some details in the question – BadgerBadger Jul 25 '17 at 04:44

1 Answers1

0

I think you want some kind of event handling setup.

A crude version of which would look like…

struct MyApp {
    static var stillRunning: Bool = true
    
    public static func main() {
        Task {
            let output: String
            do {
                output = try await MyAsyncAction()
            } catch {
                output = error.localizedDescription
            }
            
            print("\(output)")
            stillRunning = false
        }
        
        repeat {
            sleep(1)
        } while stillRunning
    }
}

MyApp.main()
bshirley
  • 8,217
  • 1
  • 37
  • 43