11import Foundation
22import ScreenCaptureKit
33import CoreMedia
4+ import Accelerate
45@preconcurrency import AVFoundation
56
67protocol AudioInputDelegate : AnyObject {
@@ -223,7 +224,7 @@ class AudioInputService: NSObject, SCStreamOutput, SCStreamDelegate, AVCaptureAu
223224 return
224225 }
225226
226- // 3. Extract and Mix Samples
227+ // 3. Extract and Mix Samples using vDSP
227228 let bufferCount = Int ( audioBufferListPtr. baseAddress!. pointee. mNumberBuffers)
228229 let channels = Int ( asbd. mChannelsPerFrame)
229230 let isFloat = ( asbd. mFormatFlags & kAudioFormatFlagIsFloat) != 0
@@ -246,26 +247,26 @@ class AudioInputService: NSObject, SCStreamOutput, SCStreamDelegate, AVCaptureAu
246247 guard let data = buffer. mData else { continue }
247248
248249 if isFloat {
249- // Float32
250+ // Float32 - Use vDSP for vectorized addition
250251 let ptr = data. bindMemory ( to: Float . self, capacity: frameCount)
251- for j in 0 ..< frameCount {
252- floatSamples [ j] += ptr [ j]
253- }
252+ vDSP_vadd ( floatSamples, 1 , ptr, 1 , & floatSamples, 1 , vDSP_Length ( frameCount) )
254253 } else {
255- // Int16
254+ // Int16 - Convert to Float then add using vDSP
256255 let ptr = data. bindMemory ( to: Int16 . self, capacity: frameCount)
257- for j in 0 ..< frameCount {
258- floatSamples [ j] += Float ( ptr [ j] ) / Float( Int16 . max)
259- }
256+ var tempFloats = [ Float] ( repeating: 0 , count: frameCount)
257+ vDSP_vflt16 ( ptr, 1 , & tempFloats, 1 , vDSP_Length ( frameCount) )
258+ // Normalize to -1.0...1.0
259+ var scale = Float ( 1.0 / Float( Int16 . max) )
260+ vDSP_vsmul ( tempFloats, 1 , & scale, & tempFloats, 1 , vDSP_Length ( frameCount) )
261+ // Add to sum
262+ vDSP_vadd ( floatSamples, 1 , tempFloats, 1 , & floatSamples, 1 , vDSP_Length ( frameCount) )
260263 }
261264 }
262265
263- // Average the mix
266+ // Average the mix using vDSP
264267 if buffers. count > 0 {
265- let div = Float ( buffers. count)
266- for j in 0 ..< frameCount {
267- floatSamples [ j] /= div
268- }
268+ var div = Float ( buffers. count)
269+ vDSP_vsdiv ( floatSamples, 1 , & div, & floatSamples, 1 , vDSP_Length ( frameCount) )
269270 }
270271 } // End withUnsafePointer
271272
0 commit comments