@@ -196,4 +196,95 @@ struct AudioMixerSuite {
196196 #expect( samples [ 1024 ] == 0 )
197197 #expect( samples [ 2047 ] == 0 )
198198 }
199+
200+ @Test
201+ func threeMonoInputsMonoOutput( ) async throws {
202+ let mixer = AudioMixer ( outputSampleRate: 48000 , outputChannels: 1 , outputSamplesPerBuffer: 1024 )
203+ let inputId1 = UUID ( )
204+ let inputId2 = UUID ( )
205+ let inputId3 = UUID ( )
206+ let format = try #require( AVAudioFormat ( standardFormatWithSampleRate: 48000 , channels: 1 ) )
207+ mixer. add ( inputId: inputId1, format: format)
208+ mixer. add ( inputId: inputId2, format: format)
209+ mixer. add ( inputId: inputId3, format: format)
210+ #expect( mixer. numberOfInputs ( ) == 3 )
211+ let inputBuffer1 = try #require( AVAudioPCMBuffer ( pcmFormat: format, frameCapacity: 1024 ) )
212+ inputBuffer1. frameLength = 1024
213+ var samples = try #require( inputBuffer1. floatChannelData? . pointee)
214+ samples [ 0 ] = 10
215+ mixer. append ( inputId: inputId1, buffer: inputBuffer1)
216+ let inputBuffer2 = try #require( AVAudioPCMBuffer ( pcmFormat: format, frameCapacity: 1024 ) )
217+ inputBuffer2. frameLength = 1024
218+ samples = try #require( inputBuffer2. floatChannelData? . pointee)
219+ samples [ 0 ] = 20
220+ mixer. append ( inputId: inputId2, buffer: inputBuffer2)
221+ let inputBuffer3 = try #require( AVAudioPCMBuffer ( pcmFormat: format, frameCapacity: 1024 ) )
222+ inputBuffer3. frameLength = 1024
223+ samples = try #require( inputBuffer3. floatChannelData? . pointee)
224+ samples [ 0 ] = 30
225+ mixer. append ( inputId: inputId3, buffer: inputBuffer3)
226+ try ? await sleep ( milliSeconds: processDelayMs)
227+ let outputBuffer = mixer. process ( )
228+ #expect( outputBuffer? . format. sampleRate == 48000 )
229+ #expect( outputBuffer? . format. channelCount == 1 )
230+ #expect( outputBuffer? . frameLength == 1024 )
231+ samples = try #require( outputBuffer? . floatChannelData? . pointee)
232+ #expect( samples [ 0 ] == 60 / sqrt( 2 ) )
233+ #expect( samples [ 500 ] == 0 )
234+ #expect( samples [ 1023 ] == 0 )
235+ mixer. remove ( inputId: inputId1)
236+ #expect( mixer. numberOfInputs ( ) == 2 )
237+ mixer. remove ( inputId: inputId2)
238+ #expect( mixer. numberOfInputs ( ) == 1 )
239+ mixer. remove ( inputId: inputId3)
240+ #expect( mixer. numberOfInputs ( ) == 0 )
241+ }
242+
243+ @Test
244+ func addAndRemoveInputsDynamically( ) async throws {
245+ let mixer = AudioMixer ( outputSampleRate: 48000 , outputChannels: 1 , outputSamplesPerBuffer: 1024 )
246+ let inputId1 = UUID ( )
247+ let inputId2 = UUID ( )
248+ let format = try #require( AVAudioFormat ( standardFormatWithSampleRate: 48000 , channels: 1 ) )
249+ mixer. add ( inputId: inputId1, format: format)
250+ #expect( mixer. numberOfInputs ( ) == 1 )
251+ let inputBuffer1 = try #require( AVAudioPCMBuffer ( pcmFormat: format, frameCapacity: 1024 ) )
252+ inputBuffer1. frameLength = 1024
253+ var samples = try #require( inputBuffer1. floatChannelData? . pointee)
254+ samples [ 0 ] = 50
255+ mixer. append ( inputId: inputId1, buffer: inputBuffer1)
256+ try ? await sleep ( milliSeconds: processDelayMs)
257+ var outputBuffer = mixer. process ( )
258+ samples = try #require( outputBuffer? . floatChannelData? . pointee)
259+ #expect( samples [ 0 ] == 50 / sqrt( 2 ) )
260+ mixer. add ( inputId: inputId2, format: format)
261+ #expect( mixer. numberOfInputs ( ) == 2 )
262+ let inputBuffer1b = try #require( AVAudioPCMBuffer ( pcmFormat: format, frameCapacity: 1024 ) )
263+ inputBuffer1b. frameLength = 1024
264+ samples = try #require( inputBuffer1b. floatChannelData? . pointee)
265+ samples [ 0 ] = 30
266+ mixer. append ( inputId: inputId1, buffer: inputBuffer1b)
267+ let inputBuffer2 = try #require( AVAudioPCMBuffer ( pcmFormat: format, frameCapacity: 1024 ) )
268+ inputBuffer2. frameLength = 1024
269+ samples = try #require( inputBuffer2. floatChannelData? . pointee)
270+ samples [ 0 ] = 20
271+ mixer. append ( inputId: inputId2, buffer: inputBuffer2)
272+ try ? await sleep ( milliSeconds: processDelayMs)
273+ outputBuffer = mixer. process ( )
274+ samples = try #require( outputBuffer? . floatChannelData? . pointee)
275+ #expect( samples [ 0 ] == 50 / sqrt( 2 ) )
276+ mixer. remove ( inputId: inputId1)
277+ #expect( mixer. numberOfInputs ( ) == 1 )
278+ let inputBuffer2b = try #require( AVAudioPCMBuffer ( pcmFormat: format, frameCapacity: 1024 ) )
279+ inputBuffer2b. frameLength = 1024
280+ samples = try #require( inputBuffer2b. floatChannelData? . pointee)
281+ samples [ 0 ] = 100
282+ mixer. append ( inputId: inputId2, buffer: inputBuffer2b)
283+ try ? await sleep ( milliSeconds: processDelayMs)
284+ outputBuffer = mixer. process ( )
285+ samples = try #require( outputBuffer? . floatChannelData? . pointee)
286+ #expect( samples [ 0 ] == 100 / sqrt( 2 ) )
287+ mixer. remove ( inputId: inputId2)
288+ #expect( mixer. numberOfInputs ( ) == 0 )
289+ }
199290}
0 commit comments