Created
October 18, 2014 20:24
-
-
Save mikeash/2e2d482058013c837a43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let engine = AVAudioEngine() | |
func setup() { | |
let inputNode = engine.inputNode | |
let outputNode = engine.outputNode | |
NSLog("all: %@", AudioDevice.All().map{$0.name}) | |
NSLog("inputs: %@", AudioDevice.All().filter{$0.isInput}.map{$0.name}) | |
NSLog("outputs: %@", AudioDevice.All().filter{$0.isOutput}.map{$0.name}) | |
let inputDeviceName = "Logitech USB Headset"//"Soundflower (2ch)" | |
let outputDeviceName = "USB Sound Device"//"USB Sound Device" | |
let devices = AudioDevice.All() | |
let inputDevices = devices.filter{$0.isInput} | |
let outputDevices = devices.filter{$0.isOutput} | |
let inputDevice = inputDevices.filter{$0.name == inputDeviceName}.first! | |
let outputDevice = outputDevices.filter{$0.name == outputDeviceName}.first! | |
func setCurrentDevice(node: AVAudioIONode, device: AudioDevice) { | |
var id = device.id | |
check(AudioUnitSetProperty( | |
inputNode.audioUnit, | |
AudioUnitPropertyID(kAudioOutputUnitProperty_CurrentDevice), | |
AudioUnitScope(kAudioUnitScope_Global), | |
AudioUnitElement(0), | |
&id, | |
UInt32(sizeofValue(device.id)))) | |
} | |
setCurrentDevice(inputNode, inputDevice) | |
setCurrentDevice(outputNode, outputDevice) | |
let mixer = AVAudioMixerNode() | |
mixer.renderingAlgorithm = .StereoPassThrough | |
engine.attachNode(mixer) | |
let format = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 6) | |
engine.connect(inputNode, to: mixer, format: format) | |
engine.connect(mixer, to: outputNode, format: format) | |
let (success, error) = errorWrap{ self.engine.startAndReturnError($0) } | |
println(success, error) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment