Skip to content

Commit 442ed92

Browse files
committed
moved Volume to the output engine, fixed annoying buzzer when switching eqs
1 parent 2711dc8 commit 442ed92

4 files changed

Lines changed: 43 additions & 49 deletions

File tree

native/app/Source/Application.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Application {
194194
let currentDeviceRemoved = list.removed.contains(where: { $0.id == selectedDevice.id })
195195

196196
if (currentDeviceRemoved) {
197-
stopEngines()
197+
removeEngines()
198198
try! AudioDeviceEvents.recreateEventEmitters([.isAliveChanged, .volumeChanged, .nominalSampleRateChanged])
199199
self.setupDriverDeviceEvents()
200200
Utilities.delay(500) {
@@ -245,7 +245,7 @@ class Application {
245245
}
246246

247247
static func selectOutput (device: AudioDevice) {
248-
stopEngines()
248+
stopRemoveEngines()
249249
Utilities.delay(500) {
250250
AudioDevice.currentOutputDevice = device
251251
}
@@ -293,13 +293,12 @@ class Application {
293293
_ = Sources() { sources in
294294
self.sources = sources
295295
effects = Effects()
296-
volume = Volume()
297296
engine = Engine(
298297
sources: sources,
299-
effects: effects,
300-
volume: volume
298+
effects: effects
301299
)
302-
output = Output(device: selectedDevice, engine: engine)
300+
volume = Volume()
301+
output = Output(device: selectedDevice, engine: engine, volume: volume)
303302

304303
selectedDeviceIsAliveListener = AudioDeviceEvents.on(
305304
.isAliveChanged,
@@ -318,7 +317,7 @@ class Application {
318317
onDevice: selectedDevice,
319318
retain: false
320319
) {
321-
stopEngines()
320+
stopRemoveEngines()
322321
Utilities.delay(100) {
323322
// need a delay, because emitter should finish it's work at first
324323
try! AudioDeviceEvents.recreateEventEmitters([.isAliveChanged, .volumeChanged, .nominalSampleRateChanged])
@@ -351,12 +350,18 @@ class Application {
351350
}
352351

353352
static func stopEngines () {
354-
if (output != nil) {
355-
output = nil
356-
}
357-
if (engine != nil) {
358-
engine = nil
359-
}
353+
output?.stop()
354+
engine?.stop()
355+
}
356+
357+
static func removeEngines () {
358+
output = nil
359+
engine = nil
360+
}
361+
362+
static func stopRemoveEngines () {
363+
stopEngines()
364+
removeEngines()
360365
}
361366

362367
private static func setupDataBus () {
@@ -436,7 +441,7 @@ class Application {
436441

437442
static func stopSave () {
438443
stopListeners()
439-
stopEngines()
444+
stopRemoveEngines()
440445
switchBackToLastKnownDevice()
441446
Storage.synchronize()
442447
}

native/app/Source/Audio/Engine.swift

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class Engine {
1717
private var eventListeners: [Any] = []
1818
let sources: Sources
1919
let effects: Effects
20-
let volume: Volume
2120
var attachedEqualizer: Equalizer?
2221

2322
var format: AVAudioFormat!
@@ -28,11 +27,10 @@ class Engine {
2827
// Middleware
2928
var buffer: CircularBuffer<Float>!
3029

31-
init (sources: Sources, effects: Effects, volume: Volume) {
30+
init (sources: Sources, effects: Effects) {
3231
Console.log("Creating Engine")
3332
self.sources = sources
3433
self.effects = effects
35-
self.volume = volume
3634
setupEngine()
3735
setupSink()
3836
setupBuffer()
@@ -58,7 +56,6 @@ class Engine {
5856
private func attach () {
5957
attachSource()
6058
attachEffects()
61-
attachVolume()
6259
}
6360

6461
private func attachSource () {
@@ -89,38 +86,29 @@ class Engine {
8986
attachEqualizer()
9087
}
9188

92-
private func attachVolume () {
93-
volume.attachToEngine(engine: engine)
94-
}
95-
9689
private func chain () {
9790
chainSourceToEffects()
98-
chainEffectsToVolume()
9991
chainEffects()
100-
chainVolumeToSink()
92+
chainEffectsToSink()
10193
setupRenderCallback()
10294
}
10395

10496
private func chainSourceToEffects () {
10597
Console.log("Chaining Source to Volume")
10698
engine.connect(engine.inputNode, to: effects.equalizers.active.eq, format: format)
10799
}
108-
109-
private func chainEffectsToVolume () {
110-
engine.connect(effects.equalizers.active.eq, to: volume.mixer, format: format)
111-
}
112-
100+
113101
private func chainEffects () {
114102
Console.log("Chaining Effects")
115103
}
116104

117-
private func chainVolumeToSink () {
118-
engine.connect(volume.booster, to: engine.mainMixerNode, format: format)
105+
private func chainEffectsToSink () {
106+
engine.connect(effects.equalizers.active.eq, to: engine.mainMixerNode, format: format)
119107
}
120108

121109
private func setupRenderCallback () {
122110
Console.log("Setting up Input Render Callback")
123-
let lastAVUnit = volume.booster as AVAudioUnit
111+
let lastAVUnit = effects.equalizers.active.eq as AVAudioUnit
124112
if let err = checkErr(AudioUnitAddRenderNotify(lastAVUnit.audioUnit,
125113
renderCallback,
126114
UnsafeMutableRawPointer(Unmanaged<Engine>.passUnretained(self).toOpaque()))) {
@@ -166,7 +154,6 @@ class Engine {
166154

167155
private func start () {
168156
engine.prepare()
169-
volume.postSetup()
170157

171158
Console.log("Starting Input Engine")
172159
Console.log(engine)

native/app/Source/Audio/Outputs/Output.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Output {
3535

3636
var device: AudioDevice
3737
var engine: Engine
38+
var volume: Volume
3839
var outputEngine = AVAudioEngine()
3940
var player = AVAudioPlayerNode()
4041
var varispeed = AVAudioUnitVarispeed()
@@ -50,11 +51,12 @@ class Output {
5051
var lowestVarispeedRate: Float
5152
var highestVarispeedRate: Float
5253

53-
init(device: AudioDevice!, engine: Engine!) {
54+
init(device: AudioDevice, engine: Engine, volume: Volume) {
5455
Console.log("Creating Output for Device: " + device.name)
5556
self.device = device
5657
self.engine = engine
57-
58+
self.volume = volume
59+
5860
outputEngine.setOutputDevice(device)
5961

6062
let format = AVAudioFormat.init(
@@ -72,8 +74,11 @@ class Output {
7274

7375
outputEngine.attach(player)
7476
outputEngine.attach(varispeed)
77+
volume.attachToEngine(engine: outputEngine)
78+
7579
outputEngine.connect(player, to: varispeed, format: format)
76-
outputEngine.connect(varispeed, to: outputEngine.mainMixerNode, format: format)
80+
outputEngine.connect(varispeed, to: volume.booster, format: format)
81+
outputEngine.connect(volume.mixer, to: outputEngine.mainMixerNode, format: format)
7782

7883
self.setupCallback()
7984

@@ -109,7 +114,7 @@ class Output {
109114
let output = Unmanaged<Output>.fromOpaque(outputPointer).takeUnretainedValue()
110115

111116
// No input yet, wait
112-
if (output.engine.lastSampleTime == -1) {
117+
if (!output.engine.engine.isRunning || output.engine.lastSampleTime == -1) {
113118
makeBufferSilent(abl)
114119
return noErr
115120
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ class Volume: StoreSubscriber {
2929
let device: AudioDevice! = Application.selectedDevice
3030
let volumeSupported = device.outputVolumeSupported
3131
let balanceSupported = device.outputBalanceSupported
32-
32+
var virtualVolume: Double = 1
3333
if (gain <= 1) {
3434
if (volumeSupported) {
3535
device.setVirtualMasterVolume(Float32(gain), direction: .playback)
36-
booster.globalGain = 0
3736
} else {
38-
booster.globalGain = Float(Utilities.mapValue(value: gain, inMin: 0, inMax: 1, outMin: -96, outMax: 0))
37+
virtualVolume = gain
3938
}
4039

4140
if (balanceSupported) {
@@ -45,13 +44,13 @@ class Volume: StoreSubscriber {
4544
mixer.pan = Float(balance)
4645
}
4746

47+
booster.globalGain = 0
4848
Driver.device!.setVirtualMasterVolume(Float32(gain), direction: .playback)
49-
5049
} else { // gain > 1
5150
if (volumeSupported) {
5251
device.setVirtualMasterVolume(1.0, direction: .playback)
5352
}
54-
booster.globalGain = Float(Utilities.mapValue(value: gain, inMin: 1, inMax: 2, outMin: 0, outMax: 12))
53+
virtualVolume = Utilities.mapValue(value: gain, inMin: 1, inMax: 2, outMin: 0, outMax: 6)
5554

5655
if (balanceSupported) {
5756
device.setVirtualMasterBalance(Float32(Utilities.mapValue(value: balance, inMin: -1, inMax: 1, outMin: 0, outMax: 1)), direction: .playback)
@@ -63,7 +62,8 @@ class Volume: StoreSubscriber {
6362
Driver.device!.setVirtualMasterVolume(1, direction: .playback)
6463
}
6564

66-
65+
mixer.outputVolume = Float(virtualVolume)
66+
6767
if (!volumeSupported) {
6868
Driver.device!.mute = false
6969
device.mute = false
@@ -77,16 +77,13 @@ class Volume: StoreSubscriber {
7777

7878
Application.ignoreNextVolumeEvent = false
7979
Application.ignoreNextDriverMuteEvent = false
80-
81-
Console.log("Boost: \(booster.globalGain) Mix: \(mixer.pan)")
8280
}
8381
}
8482

8583
var muted: Bool = false {
8684
didSet {
8785
if (muted) {
88-
// leftGain = 0
89-
// rightGain = 0
86+
mixer.outputVolume = 0
9087
} else {
9188
(gain = gain)
9289
}
@@ -169,12 +166,12 @@ class Volume: StoreSubscriber {
169166

170167
func attachToEngine (engine: AVAudioEngine) {
171168
self.engine = engine
172-
let format = engine.inputNode.inputFormat(forBus: 0)
169+
let format = engine.outputNode.outputFormat(forBus: 0)
173170

174171
engine.attach(booster)
175172
engine.attach(mixer)
176173

177-
engine.connect(mixer, to: booster, format: format)
174+
engine.connect(booster, to: mixer, format: format)
178175
}
179176

180177
func postSetup () {

0 commit comments

Comments
 (0)