Skip to content

Instantly share code, notes, and snippets.

View rgcottrell's full-sized avatar

Robert Cottrell rgcottrell

View GitHub Profile
@rgcottrell
rgcottrell / gist:325ac087585ff6eeb8cb50e2729fd1ab
Created May 3, 2016 03:48
Transcode a movie to a different framerate.
// This sketches a script that will sample images from an AVAsset at a
// new framerate. This might be used to transcode a movie or create an
// animated GIF.
import AppKit
import AVFoundation
import CoreMedia
let FPS = 12 // The target framerate for the new movie.
let frameDuration = CMTimeMake(1, Int32(FPS))
@rgcottrell
rgcottrell / gist:5b876d9c5eea4c9e411c
Created September 21, 2014 17:38
An FM Synthesizer in Swift using AVAudioEngine
import AVFoundation
import Foundation
// The maximum number of audio buffers in flight. Setting to two allows one
// buffer to be played while the next is being written.
private let kInFlightAudioBuffers: Int = 2
// The number of audio samples per buffer. A lower value reduces latency for
// changes but requires more processing but increases the risk of being unable
// to fill the buffers in time. A setting of 1024 represents about 23ms of
@rgcottrell
rgcottrell / gist:c42e62d1f09e711f54b5
Created June 21, 2014 15:01
Unwrapping Multiple Swift Optionals
// NOTE: @schwa figured this out, but I'm claiming some of the credit for asking the question.
// See https://gist.github.com/schwa/ecd5f8c154e60fcb0f58 for the original solution.
// Playground - noun: a place where people can play
import Foundation
// Implementation (repeat as needed for number of parameters).
func unwrap<T1, T2>(p1: T1?, p2: T2?) -> (T1, T2)? {
@rgcottrell
rgcottrell / gist:fdc7dd7cf5bc3ed61010
Last active May 24, 2023 21:57
Thread safe shared instance "singletons" in Swift.
class Singleton {
class var sharedInstance: Singleton {
struct Static {
static var token: dispatch_once_t = 0
static var instance: Singleton!
}
dispatch_once(&Static.token) {
Static.instance = Singleton()
}
return Static.instance
@rgcottrell
rgcottrell / gist:5478939
Created April 28, 2013 23:59
Hello world in C++11 with threads and lambdas.
#include <iostream>
#include <thread>
int main(int argc, const char * argv[])
{
std::thread t([]{
std::cout << "Hello, Lambda!" << std::endl;
});
t.join();