Skip to content

Commit

Permalink
bit depth detection now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentneo committed Mar 28, 2023
1 parent 783b5f1 commit 802f027
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Quality.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@
CODE_SIGN_ENTITLEMENTS = Quality/Quality.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 13;
CURRENT_PROJECT_VERSION = 14;
DEVELOPMENT_ASSET_PATHS = "\"Quality/Preview Content\"";
DEVELOPMENT_TEAM = 3X69W4AQD6;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -376,7 +376,7 @@
CODE_SIGN_ENTITLEMENTS = Quality/Quality.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 13;
CURRENT_PROJECT_VERSION = 14;
DEVELOPMENT_ASSET_PATHS = "\"Quality/Preview Content\"";
DEVELOPMENT_TEAM = 3X69W4AQD6;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down
11 changes: 11 additions & 0 deletions Quality/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let showSampleRateItem = NSMenuItem(title: defaults.statusBarItemTitle, action: #selector(toggleSampleRate(item:)), keyEquivalent: "")
menu.addItem(showSampleRateItem)

let enableBitDepthItem = NSMenuItem(title: "Bit Depth Switching", action: #selector(toggleBitDepthDetection(item:)), keyEquivalent: "")
menu.addItem(enableBitDepthItem)
enableBitDepthItem.state = defaults.userPreferBitDepthDetection ? .on : .off

let selectedDeviceItem = NSMenuItem(title: "Selected Device", action: nil, keyEquivalent: "")
self.devicesMenu = NSMenu()
selectedDeviceItem.submenu = self.devicesMenu
Expand Down Expand Up @@ -164,4 +168,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
item.title = defaults.statusBarItemTitle
}

@objc func toggleBitDepthDetection(item: NSMenuItem) {
Task {
await defaults.setPreferBitDepthDetection(newValue: !defaults.userPreferBitDepthDetection)
item.state = defaults.userPreferBitDepthDetection ? .on : .off
}
}

}
16 changes: 14 additions & 2 deletions Quality/Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@

import Foundation

class Defaults {
class Defaults: ObservableObject {
static let shared = Defaults()
private let kUserPreferIconStatusBarItem = "com.vincent-neo.LosslessSwitcher-Key-UserPreferIconStatusBarItem"
private let kSelectedDeviceUID = "com.vincent-neo.LosslessSwitcher-Key-SelectedDeviceUID"
private let kUserPreferBitDepthDetection = "com.vincent-neo.LosslessSwitcher-Key-BitDepthDetection"

private init() {
UserDefaults.standard.register(defaults: [
kUserPreferIconStatusBarItem : true
kUserPreferIconStatusBarItem : true,
kUserPreferBitDepthDetection : false
])

self.userPreferBitDepthDetection = UserDefaults.standard.bool(forKey: kUserPreferBitDepthDetection)
}

var userPreferIconStatusBarItem: Bool {
Expand All @@ -35,6 +39,14 @@ class Defaults {
UserDefaults.standard.set(newValue, forKey: kSelectedDeviceUID)
}
}

@Published var userPreferBitDepthDetection: Bool


@MainActor func setPreferBitDepthDetection(newValue: Bool) {
UserDefaults.standard.set(newValue, forKey: kUserPreferBitDepthDetection)
self.userPreferBitDepthDetection = newValue
}

var statusBarItemTitle: String {
let title = self.userPreferIconStatusBarItem ? "Show Sample Rate" : "Show Icon"
Expand Down
22 changes: 18 additions & 4 deletions Quality/OutputDevices.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class OutputDevices: ObservableObject {
@Published var outputDevices = [AudioDevice]()
@Published var currentSampleRate: Float64?

private var enableBitDepthDetection = Defaults.shared.userPreferBitDepthDetection
private var enableBitDepthDetectionCancellable: AnyCancellable?

private let coreAudio = SimplyCoreAudio()

private var changesCancellable: AnyCancellable?
Expand Down Expand Up @@ -53,12 +56,18 @@ class OutputDevices: ObservableObject {
self.getDeviceSampleRate()
})

enableBitDepthDetectionCancellable = Defaults.shared.$userPreferBitDepthDetection.sink(receiveValue: { newValue in
self.enableBitDepthDetection = newValue
})


}

deinit {
changesCancellable?.cancel()
defaultChangesCancellable?.cancel()
timerCancellable?.cancel()
enableBitDepthDetectionCancellable?.cancel()
//timer.upstream.connect().cancel()
}

Expand Down Expand Up @@ -118,10 +127,15 @@ class OutputDevices: ObservableObject {
let musicLogs = try Console.getRecentEntries(type: .music)
let coreAudioLogs = try Console.getRecentEntries(type: .coreAudio)
let coreMediaLogs = try Console.getRecentEntries(type: .coreMedia)
allStats.append(contentsOf: CMPlayerParser.parseMusicConsoleLogs(musicLogs))
//allStats.append(contentsOf: CMPlayerParser.parseCoreAudioConsoleLogs(coreAudioLogs))
allStats.append(contentsOf: CMPlayerParser.parseCoreMediaConsoleLogs(coreMediaLogs))

allStats.append(contentsOf: CMPlayerParser.parseMusicConsoleLogs(musicLogs))
if enableBitDepthDetection {
allStats.append(contentsOf: CMPlayerParser.parseCoreAudioConsoleLogs(coreAudioLogs))
}
else {
allStats.append(contentsOf: CMPlayerParser.parseCoreMediaConsoleLogs(coreMediaLogs))
}

allStats.sort(by: {$0.priority > $1.priority})
print("[getAllStats] \(allStats)")
}
Expand All @@ -140,7 +154,7 @@ class OutputDevices: ObservableObject {
let sampleRate = Float64(first.sampleRate)
let bitDepth = Int32(first.bitDepth)

if self.currentTrack == self.previousTrack/*, let prevSampleRate = currentSampleRate, prevSampleRate > sampleRate */ {
if self.currentTrack == self.previousTrack, let prevSampleRate = currentSampleRate, prevSampleRate > sampleRate {
print("same track, prev sample rate is higher")
return
}
Expand Down

0 comments on commit 802f027

Please sign in to comment.