Play MIDI Notes in Swift’s Playgrounds

Sounds good to me!

Image for post
Image for post
Photo by Park Troopers on Unsplash

Prerequisites:

Terminology

Motivation

MIDI Audio — In a Playground

The Playground setup

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

The MIDI steps

var sequence : MusicSequence? = nilvar musicSequenceStatus: OSStatus = NewMusicSequence(&sequence)var track : MusicTrack? = nilvar musicTrack = MusicSequenceNewTrack(sequence!, &track)var time = MusicTimeStamp(1.0)

Drooooopppp

var note = MIDINoteMessage(channel: 0,
note: 85,
velocity: 64,
releaseVelocity: 0,
duration: 5.0)
musicTrack = MusicTrackNewMIDINoteEvent(track, time, &note)
var musicPlayer : MusicPlayer? = nilvar player = NewMusicPlayer(&musicPlayer)player = MusicPlayerSetSequence(musicPlayer!, sequence)player = MusicPlayerStart(musicPlayer!)

The MIDI steps: multiple notes

var notes: [UInt8] = [71,69,62,72,71,69,67]

The Singleton Story

Conclusion:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store