Recording
On this page
A recording captures a live run while you play it. The frames are written as they render and the sound as it plays, straight into a movie file in real time.
Export is the other way to make a video, and the two serve different purposes. An export re-renders the sketch on a fixed clock, so the file reproduces exactly, and nothing you do while it renders can reach it. A recording keeps the run that is happening right now, including the mouse, the parameters, and the sound. So you can tune a sketch live and record the take, or play a live-coding set and keep the set.
From a sketch
override func keyPressed() {
guard key == "r" else { return }
if isRecording { // recordingElapsed is how long it has been running
stopRecording()
} else {
startRecording()
}
}startRecording() begins a take and stopRecording() finishes the file. With no destination, the movie is written to ~/Movies/Ollin/ and named after the sketch and the time, for example Orbit 2026-08-17 at 21.14.05. On a phone or a tablet it goes to the photo library instead (see On a phone or a tablet). To choose the destination, pass a path: startRecording(. The path's extension picks the container, which is .mov unless you ask for .mp4.
stopRecording takes a closure that runs once the take is kept. It is handed where the take went: .photos, or .file(path) with the path it was written to. It is handed nil when nothing could be kept, and also at once when nothing was recording.
stopRecording { kept in
if case .file(let path)? = kept { print("saved to \(path)") }
}Each frame is stamped with the wall clock. A frame that took longer to render therefore lasts longer in the file, and time is not stretched. The working example is Examples/.
One frame to the clipboard
copyFrame()copyFrame() puts the frame on the clipboard as a PNG, ready to paste into a message, a document, or an image editor. It takes the place of whatever the clipboard held. The frame is the next one the window shows, at the canvas's own size whatever the window's. It is read through the same frame grab a recording uses, so the copy costs one more pass on that frame and no second render. The host's own chrome, such as the ground grid, stays out of it.
override func keyPressed() {
if key == "c" { copyFrame() }
}A sketch that has stopped looping draws one frame for the copy, as redraw() would, so what it copies is what its draw() makes now. A still sketch that rolls new numbers in every draw() copies a new roll. Building the picture in setup(), or seeding the numbers inside draw(), keeps the copy the picture on screen. In an export there is no window and no clipboard, and the call does nothing. To keep the frame as a file instead, saveFrame() writes it, and --export renders one offline.
One frame as a picture
saveFrame()saveFrame() keeps the next frame shown as a picture. With no destination it goes to ~/Pictures/Ollin/ on a Mac, named after the sketch and the time, and to the photo library on a phone or a tablet. A path picks the format by its extension: saveFrame( writes a JPEG, .heic writes HEIC, .tiff writes TIFF, and .png or no extension writes PNG. Any other extension is refused, and sessionRecorder? says why. Two frames saved in the same second get two names.
override func keyPressed() {
if key == "s" { saveFrame() }
}It reads the frame a recording reads, at the same size, so it can be taken in the middle of a take, and it costs one more pass on that frame. Like stopRecording, it takes a closure handed where the picture went, or nil when it could not be kept. A stopped sketch draws one frame for it, as it does for copyFrame(). In an export there is no window, and the call keeps nothing.
Sound
By default, the recording listens to the sketch. It finds the instruments and players the sketch holds (Synth, AudioPlayer, Tone), the same way the offline exporters find a soundtrack. Then it mixes what they play into the file's audio track. There is nothing to wire and no permission to grant. An instrument made during the run joins the mix when it appears.
The audio: parameter chooses what the recording hears:
startRecording(audio: .sketch) // the sketch's own sound (the default)
startRecording(audio: .microphone) // the room, through the default input
startRecording(audio: .sketchAndMicrophone) // both, mixed
startRecording(audio: .none) // picture onlyThe microphone modes ask for permission the first time you use them. Sound and picture share one clock. A device can still drop buffers, for example when you switch headphones during a take. Then the recording leaves silence in the gap instead of shifting everything after it. So sound and picture stay together for the length of a set.
Two sounds do not reach a recording yet: a VideoPlayer's soundtrack, and anything another app is playing. For a set played over external music, record the room through the microphone.
In the live hosts
Both hosts start and stop a recording with ⌘⇧R in the Sketch menu. In OllinLiveCoding, a red chip on the stage shows the elapsed time of the take, and a toast names the file when it is saved.
A recording survives an evaluation. The take stays with the runner across the swap and picks up the new sketch's instruments, so the movie plays straight through the reload. A live-coding set is one performance, however many times the code changed, so the recording is built to run through it.
OllinLive also takes a --record flag, which records a run from its first frame:
swift run OllinLive MySketches/Loop.swift --record
ollin Loop.swift --recordOn a phone or a tablet
A sketch running on a phone or a tablet shows a small control at the foot of its screen. The camera button saves the frame as a picture, and the red button records a video with the sketch's sound. While a take runs, the red button stops it and shows its length. Both go to Photos, where the person holding the phone shares them the way they share anything else. Nothing is posted anywhere by the sketch.
The control keeps the canvas, so it is never in its own picture. A take started from the sketch's code shows on the control too, and can be stopped there. A piece whose screen should carry nothing of the framework's turns the control off and keeps the calls:
override var showsCaptureControls: Bool { false }Photos asks once. The first save asks the person to let the app add to the library. Only the right to add is asked for, so the sketch can never read the library. An app needs the line NSPhotoLibraryAddUsageDescription in its property list to ask. Both ollin phone and the iOS app the project generator writes put it there.
A refused capture is kept, not lost. When Photos is not allowed, the video or picture is kept in the app's own files instead. The Files app shows them under the app's name. The control shows the sentence that says so, which is also sessionRecorder?. A capture the sketch names a path for skips Photos: a path that is not absolute is read from the app's own files.
A full-screen canvas records at the screen's pixels. A canvas set to .resizable is measured in points, a third of a phone's pixels each way. So on a phone or a tablet its frames are recorded at the screen's own size, the picture on the glass pixel for pixel. Turning the device changes that size, which ends the take. A canvas of a set size records at that size, as on the Mac.
The room's sound needs one more line. A take with audio: .microphone asks for the microphone too, and the app needs NSMicrophoneUsageDescription to ask. ollin phone adds it when the sketch's text names the microphone. In an app of your own, add it to the property list. Without it, the take goes on without the room's sound, and lastError says why. The control's take hears what the sketch's recorder hears, so attach one that hears the room for the control to keep it:
override func setup() {
extend(SessionRecorder(audio: .sketchAndMicrophone))
}Endings
A take ends cleanly wherever the run ends. Stopping writes the file and prints its path. Quitting the host finishes the take first. Control-C in the terminal, or a kill, finishes it too, because a file left half-written is not a complete movie.
A take cannot survive a change in canvas size, because every frame in a movie is the same size. When the canvas changes size, the recording finishes the file it was writing and reports that it did. Start a new take on the new canvas.
The typed layer
The bare calls forward to SessionRecorder, a SketchExtension that you can also hold yourself:
let recorder = SessionRecorder(audio: .sketch, codec: .hevc)
override func setup() {
extend(recorder)
}The full surface is recorder.start(to:), stop(completion:), stopAndWait(), saveFrame(, isRecording, elapsed, url, and lastError. stop() returns at once and the file is finished in the background, and a new take may start before that finish is done: each take is its own file, and the one finishing is left alone. The completion is handed a SessionRecorder, .photos or .file(path). When a take or a still did not go as asked, lastError says why in a sentence worth drawing, until the next one goes well. That covers a file that could not be finished, Photos refused, a take that went without the room's sound, and a take that could not begin. codec takes the same VideoCodec choices as an export, and the default is h264. A see-through canvas (background(.clear)) records with its alpha through .proRes4444 or .hevcWithAlpha, the two codecs that carry one. A running sketch's recorder is reachable as sessionRecorder.
What it costs, and what it cannot do yet
A take reads the frame the window shows, once the GPU has finished it. Recording therefore costs the sketch almost nothing, just one tone-map pass and one copy per frame. The render loop never waits for them. The picture is the live one, at the canvas size, and the movie takes the size of its first frame. A window larger than the canvas is scaled down into the take. A window smaller than the canvas draws the frame at the canvas size while the take runs, and shows it scaled. That means the window never decides how sharp the movie is. The 3D ground grid is host chrome, so it leaves the window while a take runs.
A take is not an export, and the difference shows in the picture. Temporal anti-aliasing settles over several frames, and reflections accumulate, as they do on screen. In an export, --export-video resolves every frame on its own. A frame the encoder cannot take in time is dropped rather than waited for, and the stop line counts every drop. The window never waits for the recording either. The movie is opened away from the window's thread, and a writer that falls behind keeps at most 128 MB of frames waiting, about ten at a phone's full screen, because each waiting frame holds a copy of the whole picture. A sketch with colorOutput set to .extended cannot record live yet, so HDR stays with the offline exporters. A wide-gamut sketch does record live, and its file is tagged as P3. A standard one is tagged as Rec. 709.