Skip to content

Commit a56607a

Browse files
committed
Merge branch 'feature/max-channes-count'
2 parents 89cc0ee + f4a69b8 commit a56607a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Sources/IO/IOAudioMixerSettings.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public struct IOAudioMixerSettings {
3030
/// Specifies the track settings.
3131
public var tracks: [UInt8: IOAudioMixerTrackSettings] = .init()
3232

33+
/// Specifies the maximum number of channels supported by the system
34+
/// - Description: The maximum number of channels to be used when the number of channels is 0 (not set). More than 2 channels are not supported by the service. It is defined to prevent audio issues since recording does not support more than 2 channels.
35+
public var maximumNumberOfChannels: UInt32 = 2
36+
3337
/// Creates a new instance of a settings.
3438
public init(
3539
sampleRate: Float64 = 0,
@@ -49,7 +53,7 @@ public struct IOAudioMixerSettings {
4953
return nil
5054
}
5155
let sampleRate = min(sampleRate == 0 ? format.sampleRate : sampleRate, Self.maximumSampleRate)
52-
let channelCount = channels == 0 ? format.channelCount : channels
56+
let channelCount = channels == 0 ? min(format.channelCount, maximumNumberOfChannels) : channels
5357
if let channelLayout = AVAudioUtil.makeChannelLayout(channelCount) {
5458
return .init(
5559
commonFormat: Self.commonFormat,

Tests/IO/IOAudioMixerBySingleTrackTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,41 @@ final class IOAudioMixerBySingleTrackTests: XCTestCase {
4747
XCTAssertEqual(mixer.outputFormat?.sampleRate, 48000)
4848
}
4949

50+
func test44100to48000_4ch_2ch() {
51+
let result = Result()
52+
let mixer = IOAudioMixerBySingleTrack()
53+
mixer.delegate = result
54+
mixer.settings = .init(
55+
sampleRate: 44100, channels: 0
56+
)
57+
mixer.append(0, buffer: CMAudioSampleBufferFactory.makeSinWave(48000, numSamples: 1024, channels: 4)!)
58+
XCTAssertEqual(mixer.outputFormat?.channelCount, 2)
59+
XCTAssertEqual(mixer.outputFormat?.sampleRate, 44100)
60+
mixer.settings = .init(
61+
sampleRate: 48000, channels: 0
62+
)
63+
mixer.append(0, buffer: CMAudioSampleBufferFactory.makeSinWave(44100, numSamples: 1024, channels: 4)!)
64+
mixer.append(0, buffer: CMAudioSampleBufferFactory.makeSinWave(44100, numSamples: 1024, channels: 4)!)
65+
XCTAssertEqual(mixer.outputFormat?.channelCount, 2)
66+
XCTAssertEqual(mixer.outputFormat?.sampleRate, 48000)
67+
XCTAssertEqual(result.outputs.count, 2)
68+
}
69+
5070
func test44100to48000_4ch() {
5171
let result = Result()
5272
let mixer = IOAudioMixerBySingleTrack()
5373
mixer.delegate = result
5474
mixer.settings = .init(
5575
sampleRate: 44100, channels: 0
5676
)
77+
mixer.settings.maximumNumberOfChannels = 4
5778
mixer.append(0, buffer: CMAudioSampleBufferFactory.makeSinWave(48000, numSamples: 1024, channels: 4)!)
5879
XCTAssertEqual(mixer.outputFormat?.channelCount, 4)
5980
XCTAssertEqual(mixer.outputFormat?.sampleRate, 44100)
6081
mixer.settings = .init(
6182
sampleRate: 48000, channels: 0
6283
)
84+
mixer.settings.maximumNumberOfChannels = 4
6385
mixer.append(0, buffer: CMAudioSampleBufferFactory.makeSinWave(44100, numSamples: 1024, channels: 4)!)
6486
mixer.append(0, buffer: CMAudioSampleBufferFactory.makeSinWave(44100, numSamples: 1024, channels: 4)!)
6587
XCTAssertEqual(mixer.outputFormat?.channelCount, 4)

0 commit comments

Comments
 (0)