Skip to content

Commit

Permalink
Check errors after AudioUnit interactions
Browse files Browse the repository at this point in the history
I spent quite a bit of time debugging an issue where
`AudioUnitSetProperty` was returning error `OSStatus`.

Ideally, all of these would be marked as throws, so that call-site can
react appropriatelly to these errors. But I thought this is a good first
step to improve the current situation.
  • Loading branch information
jcavar committed Aug 26, 2024
1 parent 38fff77 commit b743f5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
6 changes: 4 additions & 2 deletions Sources/AudioKit/Internals/Utilities/AudioKitHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ extension Sequence where Iterator.Element: Hashable {
@inline(__always)
internal func AudioUnitGetParameter(_ unit: AudioUnit, param: AudioUnitParameterID) -> AUValue {
var val: AudioUnitParameterValue = 0
AudioUnitGetParameter(unit, param, kAudioUnitScope_Global, 0, &val)
let status = AudioUnitGetParameter(unit, param, kAudioUnitScope_Global, 0, &val)
CheckError(status)
return val
}

@inline(__always)
internal func AudioUnitSetParameter(_ unit: AudioUnit, param: AudioUnitParameterID, to value: AUValue) {
AudioUnitSetParameter(unit, param, kAudioUnitScope_Global, 0, AudioUnitParameterValue(value), 0)
let status = AudioUnitSetParameter(unit, param, kAudioUnitScope_Global, 0, AudioUnitParameterValue(value), 0)
CheckError(status)
}

extension AVAudioNode {
Expand Down
21 changes: 14 additions & 7 deletions Sources/AudioKit/Nodes/Mixing/MatrixMixer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,24 @@ public class MatrixMixer: Node {
// inputs to this mixer.
var inputCount = UInt32(inputs.count)
var outputCount = UInt32(1)
AudioUnitSetProperty(
var status = AudioUnitSetProperty(
unit.audioUnit,
kAudioUnitProperty_ElementCount,
kAudioUnitScope_Input,
0,
&inputCount,
UInt32(MemoryLayout<UInt32>.size)
)
AudioUnitSetProperty(
CheckError(status)
status = AudioUnitSetProperty(
unit.audioUnit,
kAudioUnitProperty_ElementCount,
kAudioUnitScope_Output,
0,
&outputCount,
UInt32(MemoryLayout<UInt32>.size)
)
CheckError(status)
}

private static let masterVolumeElement: AudioUnitElement = 0xFFFFFFFF
Expand All @@ -80,24 +82,26 @@ public class MatrixMixer: Node {
public var masterVolume: Float {
get {
var value: AudioUnitParameterValue = 0
AudioUnitGetParameter(
let status = AudioUnitGetParameter(
unit.audioUnit,
kMatrixMixerParam_Volume,
kAudioUnitScope_Global,
Self.masterVolumeElement,
&value
)
CheckError(status)
return value
}
set {
AudioUnitSetParameter(
let status = AudioUnitSetParameter(
unit.audioUnit,
kMatrixMixerParam_Volume,
kAudioUnitScope_Global,
Self.masterVolumeElement,
newValue,
0
)
CheckError(status)
}
}

Expand All @@ -115,41 +119,44 @@ public class MatrixMixer: Node {
}

public func set(volume: Float, inputChannelIndex: Int) {
AudioUnitSetParameter(
let status = AudioUnitSetParameter(
unit.audioUnit,
kMatrixMixerParam_Volume,
kAudioUnitScope_Input,
UInt32(inputChannelIndex),
volume,
0
)
CheckError(status)
}

/// Set volume of channel
/// To map input channel 0 to output channel 1, use (0, 1) crosspoint
public func set(volume: Float, outputChannelIndex: Int) {
AudioUnitSetParameter(
let status = AudioUnitSetParameter(
unit.audioUnit,
kMatrixMixerParam_Volume,
kAudioUnitScope_Output,
UInt32(outputChannelIndex),
volume,
0
)
CheckError(status)
}

/// Set volume at crosspoint
/// To map input channel 0 to output channel 1, use (0, 1) crosspoint
public func set(volume: Float, atCrosspoints crosspoints: [(Int, Int)]) {
for crosspoint in crosspoints {
AudioUnitSetParameter(
let status = AudioUnitSetParameter(
unit.audioUnit,
kMatrixMixerParam_Volume,
kAudioUnitScope_Global,
(UInt32(crosspoint.0) << 16) | (UInt32(crosspoint.1) & 0x0000FFFF),
volume,
0
)
CheckError(status)
}
}

Expand Down

0 comments on commit b743f5c

Please sign in to comment.