Skip to content

Commit

Permalink
Enabling multiple output formats to be used at the same time (AudioKi…
Browse files Browse the repository at this point in the history
…t#2872)

* Expose output format to be able to have multiple multiple formats

* Fix hound reported issue

* Fix failing test by lazy reading of Settings.audioFormat
  • Loading branch information
Truba authored Nov 23, 2023
1 parent 6d22aea commit 16e7025
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
14 changes: 10 additions & 4 deletions Sources/AudioKit/Internals/Engine/AudioEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public class AudioEngine {
/// Main mixer at the end of the signal chain
public private(set) var mainMixerNode: Mixer?

/// Output format to be used when making connections to the output
public var outputAudioFormat: AVAudioFormat?
private var outputFormat: AVAudioFormat { outputAudioFormat ?? Settings.audioFormat }

/// Input node mixer
public class InputNode: Mixer {
var isNotConnected = true
Expand Down Expand Up @@ -122,7 +126,8 @@ public class AudioEngine {

// has the sample rate changed?
if let currentSampleRate = mainMixerNode?.avAudioNode.outputFormat(forBus: 0).sampleRate,
currentSampleRate != Settings.sampleRate
let currentChannelCount = mainMixerNode?.avAudioNode.outputFormat(forBus: 0).channelCount,
(currentSampleRate != outputFormat.sampleRate || currentChannelCount != outputFormat.channelCount)
{
Log("Sample Rate has changed, creating new mainMixerNode at", Settings.sampleRate)
removeEngineMixer()
Expand All @@ -139,15 +144,16 @@ public class AudioEngine {
}

// simulate the AVAudioEngine.mainMixerNode, but create it ourselves to ensure the
// correct sample rate is used from Settings.audioFormat
// correct sample rate is used from outputFormat (default: Settings.audioFormat)
private func createEngineMixer() {
guard mainMixerNode == nil else { return }

let mixer = Mixer(name: "AudioKit Engine Mixer")
mixer.outputFormat = outputFormat
avEngine.attach(mixer.avAudioNode)
avEngine.connect(mixer.avAudioNode,
to: avEngine.outputNode,
format: Settings.audioFormat)
format: outputFormat)

mainMixerNode = mixer
}
Expand Down Expand Up @@ -201,7 +207,7 @@ public class AudioEngine {
do {
avEngine.reset()
try avEngine.enableManualRenderingMode(.offline,
format: Settings.audioFormat,
format: outputFormat,
maximumFrameCount: maximumFrameCount)
try start()
} catch let err {
Expand Down
3 changes: 3 additions & 0 deletions Sources/AudioKit/Nodes/Mixing/MatrixMixer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class MatrixMixer: Node {
public var connections: [Node] { inputs }
public var avAudioNode: AVAudioNode { unit }

/// Output format to be used when making connections from this node
public var outputFormat = Settings.audioFormat

public let unit = instantiate(
componentDescription:
AudioComponentDescription(
Expand Down
3 changes: 3 additions & 0 deletions Sources/AudioKit/Nodes/Mixing/Mixer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class Mixer: Node, NamedNode {
/// Underlying AVAudioNode
public var avAudioNode: AVAudioNode

/// Output format to be used when making connections from this node
public var outputFormat = Settings.audioFormat

/// Name of the node
open var name = "(unset)"

Expand Down
14 changes: 7 additions & 7 deletions Sources/AudioKit/Nodes/Mixing/Mixer3D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public class Mixer3D: Mixer {

fileprivate let mixerAU = AVAudioMixerNode()

public var outputFormat: AVAudioFormat {
guard let monoFormat = AVAudioFormat(
override init(volume: AUValue = 1.0, name: String? = nil) {
super.init(volume: volume, name: name)

outputFormat = AVAudioFormat(
standardFormatWithSampleRate: Settings.audioFormat.sampleRate,
channels: 1) else {
return Settings.audioFormat
}
return monoFormat
}
channels: 1
) ?? Settings.audioFormat
}

// MARK: - 3D Mixing Properties

Expand Down

0 comments on commit 16e7025

Please sign in to comment.