mirror of
https://github.com/HaishinKit/HaishinKit.swift.git
synced 2026-05-07 20:12:28 +00:00
33 lines
1.1 KiB
Swift
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))
|
|
}
|
|
}
|
|
}
|