You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
967 B

import Foundation
/// PCM helpers (16-bit signed little-endian).
///
/// Mirrors Android `audio/PcmKit.kt` — stereo split / mono mix utilities used
/// by the stereo call-translation mode handler.
enum PcmKit {
/// Split an interleaved 16-bit LE stereo buffer into two mono buffers
/// (left, right). Each output has half the input byte length.
static func splitStereo16(_ stereo: Data) -> (Data, Data) {
var left = Data(); left.reserveCapacity(stereo.count / 2)
var right = Data(); right.reserveCapacity(stereo.count / 2)
let count = stereo.count - (stereo.count % 4)
var i = 0
stereo.withUnsafeBytes { (raw: UnsafeRawBufferPointer) in
let p = raw.bindMemory(to: UInt8.self).baseAddress!
while i < count {
// L = bytes[i..i+1], R = bytes[i+2..i+3]
left.append(p[i]); left.append(p[i + 1])
right.append(p[i + 2]); right.append(p[i + 3])
i += 4
}
}
return (left, right)
}
}