Last active
October 2, 2018 17:01
-
-
Save Ivorforce/1ff52d4c9e878e6f6193dcf8a09464ca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// VisualizerWindowController.swift | |
// TenTunes | |
// | |
// Created by Lukas Tenbrink on 07.09.18. | |
// Copyright © 2018 ivorius. All rights reserved. | |
// | |
import Cocoa | |
import AudioKitUI | |
class VisualizerWindowController: NSWindowController { | |
@IBOutlet var _visualizerView: VisualizerView! | |
var fft: AKFFTTap? | |
var inputDevice: AKDevice? { | |
didSet { | |
inputNode = nil | |
updateAudioNode() | |
} | |
} | |
var inputNode: AKNode? { | |
didSet { | |
if fft != nil { | |
// According to AKFFTTap class reference, it will always be on tap 0 | |
oldValue?.avAudioNode.removeTap(onBus: 0) | |
} | |
fft = inputNode ?=> AKFFTTap.init | |
} | |
} | |
@IBOutlet var _settingsButton: NSButton! | |
@IBOutlet var _settingsSheet: NSPanel! | |
@IBOutlet var _audioSourceSelector: NSPopUpButton! | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
_settingsButton.alphaValue = 0.35 | |
AudioKit.addObserver(self, forKeyPath: #keyPath(AudioKit.inputDevices), options: [.new, .initial], context: nil) | |
} | |
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | |
if keyPath == #keyPath(AudioKit.inputDevices) { | |
_audioSourceSelector.removeAllItems() | |
_audioSourceSelector.addItem(withTitle: "Ten Tunes") | |
for input in AudioKit.inputDevices ?? [] { | |
_audioSourceSelector.addItem(withTitle: input.name) | |
_audioSourceSelector.itemArray.last!.representedObject = input | |
} | |
} | |
} | |
@IBAction func showSettings(_ sender: Any) { | |
window?.beginSheet(_settingsSheet) | |
} | |
@IBAction func selectedAudioSource(_ sender: Any) { | |
inputDevice = (sender as! NSPopUpButton).selectedItem?.representedObject as? AKDevice | |
} | |
func updateAudioNode() { | |
let visible = window?.occlusionState.contains(.visible) ?? false | |
if !visible { | |
inputNode = nil | |
} | |
else if inputNode == nil { | |
guard let device = inputDevice else { | |
inputNode = ViewController.shared.player.mixer | |
return | |
} | |
do { | |
try AudioKit.setInputDevice(device) | |
} | |
catch { | |
print("Error setting input device: \(error)") | |
return | |
} | |
let microphoneNode = AKMicrophone() | |
do { | |
try microphoneNode.setDevice(device) | |
} | |
catch { | |
print("Failed setting node input device: \(error)") | |
return | |
} | |
microphoneNode.start() | |
microphoneNode.volume = 3 | |
print("Switched Node: \(microphoneNode), started: \(microphoneNode.isStarted)") | |
inputNode = microphoneNode | |
try! AudioKit.start() | |
} | |
} | |
} | |
extension VisualizerWindowController : NSWindowDelegate { | |
func windowDidChangeOcclusionState(_ notification: Notification) { | |
updateAudioNode() | |
} | |
} | |
extension VisualizerWindowController : VisualizerViewDelegate { | |
func visualizerViewFFTData(_ view: VisualizerView) -> [Double]? { | |
print(fft?.fftData) // Constantly prints [0, 0 ...] | |
return fft?.fftData | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment