<commit_before>//
//  AppDelegate.swift
//  Demo
//
//  Created by Ruben on 7/7/15.
//  Copyright © 2015 9Labs. All rights reserved.
//

import Cocoa
import AMCoreAudio

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    // Instantiate our audio hardware object
    private let audioHardware = AMAudioHardware.sharedInstance

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Enable audio hardware events
        audioHardware.enableDeviceMonitoring()

        // Subscribe to events
        AMNotificationCenter.defaultCenter.subscribe(self, eventType: AMAudioHardwareEvent.self, dispatchQueue: DispatchQueue.main)
        AMNotificationCenter.defaultCenter.subscribe(self, eventType: AMAudioDeviceEvent.self, dispatchQueue: DispatchQueue.main)
        AMNotificationCenter.defaultCenter.subscribe(self, eventType: AMAudioStreamEvent.self, dispatchQueue: DispatchQueue.main)

        print("+ All known devices: \(AMAudioDevice.allDevices())")

        if let defaultOutputDevice = AMAudioDevice.defaultOutputDevice() {
            print("\n+ Default output device is '\(defaultOutputDevice.deviceName())':")
            printAudioDevice(defaultOutputDevice)
        }

        if let defaultInputDevice = AMAudioDevice.defaultInputDevice() {
            print("\n+ Default input device is '\(defaultInputDevice.deviceName())':")
            printAudioDevice(defaultInputDevice)
        }

        if let defaultSystemOutputDevice = AMAudioDevice.defaultSystemOutputDevice() {
            print("\n+ Default system output device is '\(defaultSystemOutputDevice.deviceName())':")
            printAudioDevice(defaultSystemOutputDevice)
        }
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Disable audio hardware events
        audioHardware.disableDeviceMonitoring()

        AMNotificationCenter.defaultCenter.unsubscribe(self, eventType: AMAudioHardwareEvent.self)
        AMNotificationCenter.defaultCenter.unsubscribe(self, eventType: AMAudioDeviceEvent.self)
        AMNotificationCenter.defaultCenter.unsubscribe(self, eventType: AMAudioStreamEvent.self)
    }

    private func printAudioDevice(_ audioDevice: AMAudioDevice) {
        let deviceID = audioDevice.deviceID
        print("|- ID: \(deviceID)")

        if let deviceUID = audioDevice.deviceUID() {
            print("|- UID: \(deviceUID)")
        }

        if let modelUID = audioDevice.deviceModelUID() {
            print("|- Model UID: \(modelUID)")
        }

        if let manufacturer = audioDevice.deviceManufacturer() {
            print("|- Manufacturer: \(manufacturer)")
        }

        if let transportType = audioDevice.transportType() {
            print("|- Transport type: \(transportType)")
        }

        if let configurationApplication = audioDevice.deviceConfigurationApplication() {
            print("|- Configuration application: \(configurationApplication)")
        }

        let deviceIsHidden = audioDevice.deviceIsHidden()
        print("|- Is hidden? \(deviceIsHidden)")

        let playbackDirection = Direction.Playback
        print("|- Preferred stereo channels for \(playbackDirection): \(audioDevice.preferredStereoChannelsForDirection(playbackDirection))")

        let recordingDirection = Direction.Recording
        print("|- Preferred stereo channels for \(recordingDirection): \(audioDevice.preferredStereoChannelsForDirection(recordingDirection))")

        if let nominalSampleRates = audioDevice.nominalSampleRates() {
            print("|- Nominal sample rates: \(nominalSampleRates)")
        }

        if let nominalSampleRate = audioDevice.nominalSampleRate() {
            print("|- Nominal sample rate: \(nominalSampleRate)")
        }

        if let actualSampleRate = audioDevice.actualSampleRate() {
            print("|- Actual sample rate: \(actualSampleRate)")
        }

        if let playbackClockSources = audioDevice.clockSourcesForChannel(0, andDirection: playbackDirection) {
            print("|- Available clock sources for \(playbackDirection): \(playbackClockSources)")
        }

        if let playbackClockSource = audioDevice.clockSourceForChannel(0, andDirection: playbackDirection) {
            print("|- Active clock source for \(playbackDirection): \(playbackClockSource)")
        }

        if let recordingClockSources = audioDevice.clockSourcesForChannel(0, andDirection: recordingDirection) {
            print("|- Available clock sources for \(recordingDirection): \(recordingClockSources)")
        }

        if let recordingClockSource = audioDevice.clockSourceForChannel(0, andDirection: recordingDirection) {
            print("|- Active clock source for \(recordingDirection): \(recordingClockSource)")
        }

        if let playbackLatency = audioDevice.deviceLatencyFramesForDirection(playbackDirection) {
            print("|- \(playbackDirection) latency: \(playbackLatency) frames")
        }

        if let recordingLatency = audioDevice.deviceLatencyFramesForDirection(recordingDirection) {
            print("|- \(recordingDirection) latency: \(recordingLatency) frames")
        }

        if let playbackSafetyOffset = audioDevice.deviceSafetyOffsetFramesForDirection(playbackDirection) {
            print("|- \(playbackDirection) safety offset: \(playbackSafetyOffset) frames")
        }

        if let recordingSafetyOffset = audioDevice.deviceSafetyOffsetFramesForDirection(recordingDirection) {
            print("|- \(recordingDirection) safety offset: \(recordingSafetyOffset) frames")
        }

        let canSetMasterPlaybackVolume = audioDevice.canSetMasterVolumeForDirection(playbackDirection)
        print("|- Can set master \(playbackDirection) volume? \(canSetMasterPlaybackVolume)")

        let canSetMasterRecordingVolume = audioDevice.canSetMasterVolumeForDirection(recordingDirection)
        print("|- Can set master \(recordingDirection) volume? \(canSetMasterRecordingVolume)")

        if let masterPlaybackVolume = audioDevice.masterVolumeInDecibelsForDirection(playbackDirection) {
            print("|- \(playbackDirection) master volume: \(masterPlaybackVolume)dbFS")

            if let masterPlaybackVolumeInfo = audioDevice.volumeInfoForChannel(UInt32(0), andDirection: playbackDirection) {
                print("|  - Volume info: \(masterPlaybackVolumeInfo)")
            }
        }

        if let masterRecordingVolume = audioDevice.masterVolumeInDecibelsForDirection(recordingDirection) {
            print("|- \(recordingDirection) master volume: \(masterRecordingVolume)dbFS")

            if let masterRecordingVolumeInfo = audioDevice.volumeInfoForChannel(UInt32(0), andDirection: recordingDirection) {
                print("|  - Volume info: \(masterRecordingVolumeInfo)")
            }
        }

        let playbackChannels = audioDevice.channelsForDirection(playbackDirection)

        if playbackChannels > 0 {
            print("|- \(playbackDirection) channel count is \(playbackChannels)")

            for channel in 0...playbackChannels {
                if let volume = audioDevice.volumeInDecibelsForChannel(channel, andDirection: playbackDirection) {
                    let nameForChannel = audioDevice.nameForChannel(channel, andDirection: playbackDirection) ?? "<No named>"
                    print("|  - Channel \(channel) (\(nameForChannel)) volume is \(volume)dBFS")

                    if let volumeInfo = audioDevice.volumeInfoForChannel(channel, andDirection: playbackDirection) {
                        print("|    - Volume info: \(volumeInfo)")
                    }
                }
            }
        }

        let recordingChannels = audioDevice.channelsForDirection(recordingDirection)

        if recordingChannels > 0 {
            print("|- \(recordingDirection) channel count is \(recordingChannels)")

            for channel in 0...recordingChannels {
                if let volume = audioDevice.volumeInDecibelsForChannel(channel, andDirection: recordingDirection) {
                    let nameForChannel = audioDevice.nameForChannel(channel, andDirection: recordingDirection) ?? "<No named>"
                    print("|  - Channel \(channel) (\(nameForChannel)) volume is \(volume)dBFS")

                    if let volumeInfo = audioDevice.volumeInfoForChannel(channel, andDirection: recordingDirection) {
                        print("|    - Volume info: \(volumeInfo)")
                    }
                }
            }
        }

        if let streamForPlayback = audioDevice.streamsForDirection(.Playback) {
            print("|- Streams for playback: \(streamForPlayback)")

            streamForPlayback.forEach({ (stream) in
                print("|- Stream (playback): \(stream.streamID)")

                if let owningDevice = stream.owningDevice {
                    print("|- Owned by device: \(owningDevice)")
                }

                if let physicalFormats = stream.availablePhysicalFormats {
                    print("|- Available physical formats for playback (\(stream.streamID)): \(physicalFormats)")
                }

                if let virtualFormats = stream.availableVirtualFormats {
                    print("|- Available virtual formats for playback (\(stream.streamID)): \(virtualFormats)")
                }

                if let filteredPhysicalFormats = stream.availablePhysicalFormatsMatchingCurrentNominalSampleRate(false) {
                    print("|- Available physical formats for playback (filtered by sample rate) (\(stream.streamID)): \(filteredPhysicalFormats)")
                }

                if let filteredVirtualFormats = stream.availableVirtualFormatsMatchingCurrentNominalSampleRate(false) {
                    print("|- Available physical formats for playback (filtered by sample rate) (\(stream.streamID)): \(filteredVirtualFormats)")
                }
<commit_msg>Misc demo cleanup.<commit_after>//
//  AppDelegate.swift
//  Demo
//
//  Created by Ruben on 7/7/15.
//  Copyright © 2015 9Labs. All rights reserved.
//

import Cocoa
import AMCoreAudio

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    // Instantiate our audio hardware object
    private let audioHardware = AMAudioHardware.sharedInstance

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Enable audio hardware events
        audioHardware.enableDeviceMonitoring()

        // Subscribe to events
        AMNotificationCenter.defaultCenter.subscribe(self, eventType: AMAudioHardwareEvent.self, dispatchQueue: DispatchQueue.main)
        AMNotificationCenter.defaultCenter.subscribe(self, eventType: AMAudioDeviceEvent.self, dispatchQueue: DispatchQueue.main)
        AMNotificationCenter.defaultCenter.subscribe(self, eventType: AMAudioStreamEvent.self, dispatchQueue: DispatchQueue.main)

        print("+ All known devices: \(AMAudioDevice.allDevices())")

        if let defaultOutputDevice = AMAudioDevice.defaultOutputDevice() {
            print("\n+ Default output device is '\(defaultOutputDevice.deviceName())':")
            printAudioDevice(defaultOutputDevice)
        }

        if let defaultInputDevice = AMAudioDevice.defaultInputDevice() {
            print("\n+ Default input device is '\(defaultInputDevice.deviceName())':")
            printAudioDevice(defaultInputDevice)
        }

        if let defaultSystemOutputDevice = AMAudioDevice.defaultSystemOutputDevice() {
            print("\n+ Default system output device is '\(defaultSystemOutputDevice.deviceName())':")
            printAudioDevice(defaultSystemOutputDevice)
        }
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Disable audio hardware events
        audioHardware.disableDeviceMonitoring()

        AMNotificationCenter.defaultCenter.unsubscribe(self, eventType: AMAudioHardwareEvent.self)
        AMNotificationCenter.defaultCenter.unsubscribe(self, eventType: AMAudioDeviceEvent.self)
        AMNotificationCenter.defaultCenter.unsubscribe(self, eventType: AMAudioStreamEvent.self)
    }

    private func printAudioDevice(_ audioDevice: AMAudioDevice) {
        let deviceID = audioDevice.deviceID
        print("|- ID: \(deviceID)")

        if let deviceUID = audioDevice.deviceUID() {
            print("|- UID: \(deviceUID)")
        }

        if let modelUID = audioDevice.deviceModelUID() {
            print("|- Model UID: \(modelUID)")
        }

        if let manufacturer = audioDevice.deviceManufacturer() {
            print("|- Manufacturer: \(manufacturer)")
        }

        if let transportType = audioDevice.transportType() {
            print("|- Transport type: \(transportType)")
        }

        if let configurationApplication = audioDevice.deviceConfigurationApplication() {
            print("|- Configuration application: \(configurationApplication)")
        }

        let deviceIsHidden = audioDevice.deviceIsHidden()
        print("|- Is hidden? \(deviceIsHidden)")

        print("|- Preferred stereo channels for \(Direction.Playback): \(audioDevice.preferredStereoChannelsForDirection(.Playback))")

        print("|- Preferred stereo channels for \(Direction.Recording): \(audioDevice.preferredStereoChannelsForDirection(.Recording))")

        if let nominalSampleRates = audioDevice.nominalSampleRates() {
            print("|- Nominal sample rates: \(nominalSampleRates)")
        }

        if let nominalSampleRate = audioDevice.nominalSampleRate() {
            print("|- Nominal sample rate: \(nominalSampleRate)")
        }

        if let actualSampleRate = audioDevice.actualSampleRate() {
            print("|- Actual sample rate: \(actualSampleRate)")
        }

        if let playbackClockSources = audioDevice.clockSourcesForChannel(0, andDirection: .Playback) {
            print("|- Available clock sources for \(Direction.Playback): \(playbackClockSources)")
        }

        if let playbackClockSource = audioDevice.clockSourceForChannel(0, andDirection: .Playback) {
            print("|- Active clock source for \(Direction.Playback): \(playbackClockSource)")
        }

        if let recordingClockSources = audioDevice.clockSourcesForChannel(0, andDirection: .Recording) {
            print("|- Available clock sources for \(Direction.Recording): \(recordingClockSources)")
        }

        if let recordingClockSource = audioDevice.clockSourceForChannel(0, andDirection: .Recording) {
            print("|- Active clock source for \(Direction.Recording): \(recordingClockSource)")
        }

        if let playbackLatency = audioDevice.deviceLatencyFramesForDirection(.Playback) {
            print("|- \(Direction.Playback) latency: \(playbackLatency) frames")
        }

        if let recordingLatency = audioDevice.deviceLatencyFramesForDirection(.Recording) {
            print("|- \(Direction.Recording) latency: \(recordingLatency) frames")
        }

        if let playbackSafetyOffset = audioDevice.deviceSafetyOffsetFramesForDirection(.Playback) {
            print("|- \(Direction.Playback) safety offset: \(playbackSafetyOffset) frames")
        }

        if let recordingSafetyOffset = audioDevice.deviceSafetyOffsetFramesForDirection(.Recording) {
            print("|- \(Direction.Recording) safety offset: \(recordingSafetyOffset) frames")
        }

        let canSetMasterPlaybackVolume = audioDevice.canSetMasterVolumeForDirection(.Playback)
        print("|- Can set master \(Direction.Playback) volume? \(canSetMasterPlaybackVolume)")

        let canSetMasterRecordingVolume = audioDevice.canSetMasterVolumeForDirection(.Playback)
        print("|- Can set master \(Direction.Recording) volume? \(canSetMasterRecordingVolume)")

        if let masterPlaybackVolume = audioDevice.masterVolumeInDecibelsForDirection(.Playback) {
            print("|- \(Direction.Playback) master volume: \(masterPlaybackVolume)dbFS")

            if let masterPlaybackVolumeInfo = audioDevice.volumeInfoForChannel(UInt32(0), andDirection: .Playback) {
                print("|  - Volume info: \(masterPlaybackVolumeInfo)")
            }
        }

        if let masterRecordingVolume = audioDevice.masterVolumeInDecibelsForDirection(.Recording) {
            print("|- \(Direction.Recording) master volume: \(masterRecordingVolume)dbFS")

            if let masterRecordingVolumeInfo = audioDevice.volumeInfoForChannel(UInt32(0), andDirection: .Recording) {
                print("|  - Volume info: \(masterRecordingVolumeInfo)")
            }
        }

        let playbackChannels = audioDevice.channelsForDirection(.Playback)

        if playbackChannels > 0 {
            print("|- \(Direction.Playback) channel count is \(playbackChannels)")

            for channel in 0...playbackChannels {
                let nameForChannel = audioDevice.nameForChannel(channel, andDirection: .Playback) ?? (channel == 0 ? "Master" : "\(channel)")
                print("|  - Channel \(channel) (\(nameForChannel))")

                if let volume = audioDevice.volumeInDecibelsForChannel(channel, andDirection: .Playback) {
                    print("|    +- Volume: \(volume)")
                }
                if let volumeInfo = audioDevice.volumeInfoForChannel(channel, andDirection: .Playback) {
                    print("|    +- Volume info: \(volumeInfo)")
                }
            }
        }

        let recordingChannels = audioDevice.channelsForDirection(.Recording)

        if recordingChannels > 0 {
            print("|- \(Direction.Recording) channel count is \(recordingChannels)")

            for channel in 0...recordingChannels {
                let nameForChannel = audioDevice.nameForChannel(channel, andDirection: .Playback) ?? (channel == 0 ? "Master" : "\(channel)")

                print("|  - Channel \(channel) (\(nameForChannel))")

                if let volume = audioDevice.volumeInDecibelsForChannel(channel, andDirection: .Recording) {
                    print("|    +- Volume: \(volume)")
                }
                if let volumeInfo = audioDevice.volumeInfoForChannel(channel, andDirection: .Recording) {
                    print("|    +- Volume info: \(volumeInfo)")
                }
            }
        }

        if let streamForPlayback = audioDevice.streamsForDirection(.Playback) {
            print("|- Streams for playback: \(streamForPlayback)")

            streamForPlayback.forEach({ (stream) in
                print("|- Stream (playback): \(stream.streamID)")

                if let owningDevice = stream.owningDevice {
                    print("|- Owned by device: \(owningDevice)")
                }

                if let physicalFormats = stream.availablePhysicalFormats {
                    print("|- Available physical formats for playback (\(stream.streamID)): \(physicalFormats)")
                }

                if let virtualFormats = stream.availableVirtualFormats {
                    print("|- Available virtual formats for playback (\(stream.streamID)): \(virtualFormats)")
                }

                if let filteredPhysicalFormats = stream.availablePhysicalFormatsMatchingCurrentNominalSampleRate(false) {
                    print("|- Available physical formats for playback (filtered by sample rate) (\(stream.streamID)): \(filteredPhysicalFormats)")
                }

                if let filteredVirtualFormats = stream.availableVirtualFormatsMatchingCurrentNominalSampleRate(false) {
                    print("|- Available physical formats for playback (filtered by sample rate) (\(stream.streamID)): \(filteredVirtualFormats)")
                }

{'fraction_non_alphanumeric': 0.09986826104580462, 'fraction_numerical': 0.0024321037697608433, 'mean_word_length': 2.7558515699333967, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6306131', 'n_tokens_mistral': 5100, 'n_tokens_neox': 4731, 'n_words': 1092}