Files
HaishinKit.swift/HaishinKit/Sources/Extension/AVAudioCompressedBuffer+Extension.swift
2025-07-13 12:43:53 +09:00

33 lines
1.1 KiB
Swift

import AVFoundation
import Foundation
extension AVAudioCompressedBuffer {
@discardableResult
@inline(__always)
final func copy(_ buffer: AVAudioBuffer) -> Bool {
guard let buffer = buffer as? AVAudioCompressedBuffer else {
return false
}
if let packetDescriptions = buffer.packetDescriptions {
self.packetDescriptions?.pointee = packetDescriptions.pointee
}
packetCount = buffer.packetCount
byteLength = buffer.byteLength
data.copyMemory(from: buffer.data, byteCount: Int(buffer.byteLength))
return true
}
package func encode(to data: inout Data) {
guard let config = AudioSpecificConfig(formatDescription: format.formatDescription) else {
return
}
config.encode(to: &data, length: Int(byteLength))
data.withUnsafeMutableBytes {
guard let baseAddress = $0.baseAddress else {
return
}
memcpy(baseAddress.advanced(by: AudioSpecificConfig.adtsHeaderSize), self.data, Int(self.byteLength))
}
}
}