Skip to content

Commit 56b6818

Browse files
committed
moved volume related manipulation to a background thread to improve UI responsiveness
1 parent 2629019 commit 56b6818

8 files changed

Lines changed: 54 additions & 23 deletions

File tree

native/app/Source/Application.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,17 @@ class Application {
6161
return appDirectory
6262
}
6363

64+
static private let dispatchActionQueue = DispatchQueue(label: "dispatchActionQueue", qos: .userInitiated)
6465
// Custom dispatch function. Need to execute all dispatches on the main thread
65-
static func dispatchAction(_ action: Action) {
66-
DispatchQueue.main.async {
67-
store.dispatch(action)
66+
static func dispatchAction(_ action: Action, onMainThread: Bool = true) {
67+
if (onMainThread) {
68+
DispatchQueue.main.async {
69+
store.dispatch(action)
70+
}
71+
} else {
72+
dispatchActionQueue.async {
73+
store.dispatch(action)
74+
}
6875
}
6976
}
7077
static let store: Store = Store(reducer: ApplicationStateReducer, state: Storage[.state] ?? ApplicationState(), middleware: [])
@@ -236,7 +243,7 @@ class Application {
236243
setupDriverDeviceEvents()
237244
}
238245

239-
private static var ignoreNextDriverMuteEvent = false
246+
static var ignoreNextDriverMuteEvent = false
240247
private static func setupDriverDeviceEvents () {
241248
AudioDeviceEvents.on(.volumeChanged, onDevice: Driver.device!) {
242249
if ignoreNextVolumeEvent {
@@ -253,7 +260,7 @@ class Application {
253260
if (gain <= 1 && gain != Application.store.state.effects.volume.gain) {
254261
Application.dispatchAction(VolumeAction.setGain(gain, false))
255262
}
256-
263+
257264
}
258265

259266
AudioDeviceEvents.on(.muteChanged, onDevice: Driver.device!) {

native/app/Source/Audio/Volume/Volume.swift

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Volume: StoreSubscriber {
3131

3232
var newLeftGain: Double = gain
3333
var newRightGain: Double = gain
34+
3435
if (gain > 1) {
3536
if (volumeSupported) {
3637
device.setVirtualMasterVolume(1.0, direction: .playback)
@@ -92,9 +93,10 @@ class Volume: StoreSubscriber {
9293
Driver.device!.mute = shouldMute
9394
device.mute = shouldMute
9495

95-
9696
gainChanged.emit(gain)
9797

98+
Application.ignoreNextVolumeEvent = false
99+
Application.ignoreNextDriverMuteEvent = false
98100
}
99101
}
100102

@@ -143,29 +145,43 @@ class Volume: StoreSubscriber {
143145
// MARK: - State
144146
typealias StoreSubscriberStateType = VolumeState
145147

148+
private let changeGainThread = DispatchQueue(label: "change-volume", qos: .userInteractive)
149+
private var latestChangeGainTask: DispatchWorkItem?
150+
private func performOnChangeGainThread (_ code: @escaping () -> Void) {
151+
latestChangeGainTask?.cancel()
152+
latestChangeGainTask = DispatchWorkItem(block: code)
153+
changeGainThread.async(execute: latestChangeGainTask!)
154+
}
155+
146156
func newState(state: VolumeState) {
147157
if (state.balance != balance) {
148-
if (state.transition) {
149-
Transition.perform(from: balance, to: state.balance) { balance in
150-
self.balance = balance
158+
performOnChangeGainThread {
159+
if (state.transition) {
160+
Transition.perform(from: self.balance, to: state.balance) { balance in
161+
self.balance = balance
162+
}
163+
} else {
164+
self.balance = state.balance
151165
}
152-
} else {
153-
balance = state.balance
154166
}
155167
}
156168

157169
if (state.gain != gain) {
158-
if (state.transition) {
159-
Transition.perform(from: gain, to: state.gain) { gain in
160-
self.gain = gain
170+
performOnChangeGainThread {
171+
if (state.transition) {
172+
Transition.perform(from: self.gain, to: state.gain) { gain in
173+
self.gain = gain
174+
}
175+
} else {
176+
self.gain = state.gain
161177
}
162-
} else {
163-
gain = state.gain
164178
}
165179
}
166180

167181
if (state.muted != muted) {
168-
muted = state.muted
182+
performOnChangeGainThread {
183+
self.muted = state.muted
184+
}
169185
}
170186
}
171187

native/app/Source/Audio/Volume/VolumeDataBus.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class VolumeDataBus: DataBus {
3030
throw "Invalid 'gain' value, must be a positive number"
3131
}
3232
let transition = data["transition"] as? Bool ?? false
33+
Application.ignoreNextVolumeEvent = true
34+
Application.ignoreNextDriverMuteEvent = true
3335
Application.dispatchAction(VolumeAction.setGain(gain!, transition))
3436
return "Volume Gain has been set"
3537
}
@@ -44,6 +46,8 @@ class VolumeDataBus: DataBus {
4446
throw "Invalid 'balance' value, must be a floating point number"
4547
}
4648
let transition = data["transition"] as? Bool ?? false
49+
Application.ignoreNextVolumeEvent = true
50+
Application.ignoreNextDriverMuteEvent = true
4751
Application.dispatchAction(VolumeAction.setBalance(balance!, transition))
4852
return "Volume Balance has been set"
4953
}
@@ -57,6 +61,8 @@ class VolumeDataBus: DataBus {
5761
if (muted == nil) {
5862
throw "Invalid 'muted' value, must be a boolean"
5963
}
64+
Application.ignoreNextVolumeEvent = true
65+
Application.ignoreNextDriverMuteEvent = true
6066
Application.dispatchAction(VolumeAction.setMuted(muted!))
6167
return "Volume mute has been set"
6268
}

native/app/Source/Constants.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import AMCoreAudio
1313
struct Constants {
1414

1515
#if DEBUG
16-
// static let UI_ENDPOINT_URL = URL(string: "http://localhost:8080")!
17-
static let UI_ENDPOINT_URL = URL(string: "https://ui-v1.eqmac.app")!
16+
static let UI_ENDPOINT_URL = URL(string: "http://localhost:8080")!
17+
// static let UI_ENDPOINT_URL = URL(string: "https://ui-v1.eqmac.app")!
1818
static let DEBUG = true
1919
#else
2020
static let DEBUG = false

native/app/Source/Helpers/Console.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import Foundation
1111
class Console {
1212
static func log (_ somethings: Any..., fileAbsolutePath: String = #file, line: Int = #line) {
1313
let file = fileAbsolutePath[fileAbsolutePath.range(of: "/app/")!.upperBound...]
14-
print("eqMac (\(file):\(line)) \(somethings.map { ($0 as AnyObject).debugDescription }.joined(separator: " "))")
14+
let dataFormatter = DateFormatter()
15+
dataFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSS"
16+
print("\(dataFormatter.string(from: Date())) eqMac (\(file):\(line)) \(somethings.map { ($0 as AnyObject).debugDescription }.joined(separator: " "))")
1517
}
1618
}
1719

native/eqMac.pkgproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@
573573
<key>USE_HFS+_COMPRESSION</key>
574574
<true/>
575575
<key>VERSION</key>
576-
<string>0.3.4</string>
576+
<string>0.3.7</string>
577577
</dict>
578578
<key>TYPE</key>
579579
<integer>0</integer>

ui/src/app/sections/volume/booster-balance/booster/booster.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class BoosterComponent implements OnInit {
6464
this.getGain()
6565
this.ignoreUpdates = false
6666
}, 500)
67-
return this.boosterService.setGain(gain)
67+
this.boosterService.setGain(gain)
6868
}
6969

7070
async getGain () {

ui/src/app/sections/volume/booster-balance/booster/booster.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class BoosterService extends VolumeService {
1212
}
1313

1414
setGain (gain: number, transition?: boolean) {
15-
return this.request({ method: 'POST', data: { gain, transition } })
15+
this.request({ method: 'POST', data: { gain, transition } })
1616
}
1717

1818
onGainChanged (callback: (gain: number) => void) {

0 commit comments

Comments
 (0)