-
-
Notifications
You must be signed in to change notification settings - Fork 620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multichannel audio mixer #1386
Merged
shogo4405
merged 6 commits into
shogo4405:main
from
stream-labs:feature/basic-audio-mixer
Apr 6, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a475e05
Add multichannel audio mixer
levs42 7f837a7
Fix ring buffer initialization for default settings
levs42 c8b84ec
Use bus arguments for AudioNode connect method
levs42 44e506f
Rename createTrack to makeTrack
levs42 5451160
Make default track optional, fix app audio only screen sharing audio
levs42 372aac5
Remove CustomStringConvertible from AudioStreamBasicDescription
levs42 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import AudioUnit | ||
|
||
extension AudioNode: CustomStringConvertible { | ||
public var description: String { | ||
var description: [String] = [] | ||
|
||
for scope in BusScope.allCases { | ||
guard let busCount = try? busCount(scope: scope) else { | ||
description.append("failed to get \(scope.rawValue) bus count") | ||
continue | ||
} | ||
guard busCount > 0 else { | ||
continue | ||
} | ||
var busDescription: [String] = [] | ||
for busIndex in 0..<busCount { | ||
guard let asbd = try? format(bus: busIndex, scope: scope) else { | ||
busDescription.append("failed to get \(scope.rawValue) bus format for bus \(busIndex)") | ||
continue | ||
} | ||
if let mixerNode = self as? MixerNode, let volume = try? mixerNode.volume(bus: busIndex, of: scope) { | ||
if scope != .input || scope == .input && (try? mixerNode.isEnabled(bus: busIndex, scope: scope)) ?? false { | ||
busDescription.append("bus: \(busIndex), volume: \(volume), format: \(asbd.verboseDescription)") | ||
} | ||
} else { | ||
busDescription.append("bus: \(busIndex), format: \(asbd.verboseDescription)") | ||
} | ||
} | ||
|
||
description.append("\(scope.rawValue) \(busDescription.count)/\(busCount)") | ||
description.append(busDescription.joined(separator: "; ")) | ||
} | ||
|
||
let parametersList = (try? parameters) ?? [] | ||
if !parametersList.isEmpty { | ||
description.append("parameters: ") | ||
for parameter in parametersList { | ||
description.append("\(parameter)") | ||
} | ||
} | ||
|
||
return "AudioNode(\(description.joined(separator: "; ")))" | ||
} | ||
|
||
private var parameters: [AudioUnitParameter] { | ||
get throws { | ||
var result = [AudioUnitParameter]() | ||
var status: OSStatus = noErr | ||
|
||
var parameterListSize: UInt32 = 0 | ||
AudioUnitGetPropertyInfo(audioUnit, | ||
kAudioUnitProperty_ParameterList, | ||
kAudioUnitScope_Global, | ||
0, | ||
¶meterListSize, | ||
nil) | ||
|
||
let numberOfParameters = Int(parameterListSize) / MemoryLayout<AudioUnitParameterID>.size | ||
let parameterIds = UnsafeMutablePointer<AudioUnitParameterID>.allocate(capacity: numberOfParameters) | ||
defer { parameterIds.deallocate() } | ||
|
||
if numberOfParameters > 0 { | ||
status = AudioUnitGetProperty(audioUnit, | ||
kAudioUnitProperty_ParameterList, | ||
kAudioUnitScope_Global, | ||
0, | ||
parameterIds, | ||
¶meterListSize) | ||
guard status == noErr else { | ||
throw AudioNodeError.unableToRetrieveValue(status) | ||
} | ||
} | ||
|
||
var info = AudioUnitParameterInfo() | ||
var infoSize = UInt32(MemoryLayout<AudioUnitParameterInfo>.size) | ||
|
||
for i in 0..<numberOfParameters { | ||
let id = parameterIds[i] | ||
status = AudioUnitGetProperty(audioUnit, | ||
kAudioUnitProperty_ParameterInfo, | ||
kAudioUnitScope_Global, | ||
id, | ||
&info, | ||
&infoSize) | ||
guard status == noErr else { | ||
throw AudioNodeError.unableToRetrieveValue(status) | ||
} | ||
result.append(AudioUnitParameter(info, id: id)) | ||
} | ||
|
||
return result | ||
} | ||
} | ||
} | ||
|
||
private struct AudioUnitParameter: CustomStringConvertible { | ||
var id: Int | ||
var name: String = "" | ||
var minValue: Float | ||
var maxValue: Float | ||
var defaultValue: Float | ||
var unit: AudioUnitParameterUnit | ||
|
||
init(_ info: AudioUnitParameterInfo, id: AudioUnitParameterID) { | ||
self.id = Int(id) | ||
if let cfName = info.cfNameString?.takeUnretainedValue() { | ||
name = String(cfName) | ||
} | ||
minValue = info.minValue | ||
maxValue = info.maxValue | ||
defaultValue = info.defaultValue | ||
unit = info.unit | ||
} | ||
|
||
var description: String { | ||
return "\(name), id: \(id), min: \(minValue), max: \(maxValue), default: \(defaultValue), unit: \(unit) \(unitName)" | ||
} | ||
|
||
var unitName: String { | ||
switch unit { | ||
// swiftlint:disable switch_case_on_newline | ||
case .generic: return "generic" | ||
case .indexed: return "indexed" | ||
case .boolean: return "boolean" | ||
case .percent: return "percent" | ||
case .seconds: return "seconds" | ||
case .sampleFrames: return "sampleFrames" | ||
case .phase: return "phase" | ||
case .rate: return "rate" | ||
case .hertz: return "hertz" | ||
case .cents: return "cents" | ||
case .relativeSemiTones: return "relativeSemiTones" | ||
case .midiNoteNumber: return "midiNoteNumber" | ||
case .midiController: return "midiController" | ||
case .decibels: return "decibels" | ||
case .linearGain: return "linearGain" | ||
case .degrees: return "degrees" | ||
case .equalPowerCrossfade: return "equalPowerCrossfade" | ||
case .mixerFaderCurve1: return "mixerFaderCurve1" | ||
case .pan: return "pan" | ||
case .meters: return "meters" | ||
case .absoluteCents: return "absoluteCents" | ||
case .octaves: return "octaves" | ||
case .BPM: return "BPM" | ||
case .beats: return "beats" | ||
case .milliseconds: return "milliseconds" | ||
case .ratio: return "ratio" | ||
case .customUnit: return "customUnit" | ||
case .midi2Controller: return "midi2Controller" | ||
default: return "unknown_\(unit)" | ||
// swiftlint:enable switch_case_on_newline | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Q]
It's convenient for debugging, but I prefer to avoid having it in the library to prevent changing the default behavior. Will moving it to an example still not work as expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AudioNode
is a new class so I implemented a custom description. I can rename it to something else. Is it more about reimplementing a custom description forAudioStreamBasicDescription
? It's very handy to printmFormatFlags
as a readable flags. For example,instead of just
mFormatFlags: 12
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding custom classes in HK, that's fine. However, please keep the Extension folder for Cocoa frame-only extensions. Please move it to the bottom of the definition file.
As for AudioStreamBasicDescription, I haven't adopted it because the default behavior changes. I acknowledge that it's convenient for debugging, but we often avoid it in application-level development due to the numerous problems it can cause.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, I'll move it tomorrow. Can I keep
AudioStreamBasicDescription
extension while removingdescription
override and rename it to something likeverboseDescription
for debug purpose?