欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

iOS 音频处理, AVAudioEngine

程序员文章站 2022-03-27 21:47:09
音频处理的格式有 ASBD , AudioStreamBasicDescription和 ASPD, AudioStreamPacketDescription有 ASPD ,就是压缩音频,例如 mp3let isCompressed = packetDescriptionsOrNil != nil没有 ASPD ,就使用 ASBD, 就是非压缩音频,例如 wav压缩音频,里面的数据不均匀,每个包含有的帧数据不等func ParserPacketCallback(_ context: Unsaf...

音频处理的格式有 ASBD , AudioStreamBasicDescription

和 ASPD, AudioStreamPacketDescription

有 ASPD ,就是压缩音频,例如 mp3

let isCompressed = packetDescriptionsOrNil != nil

没有 ASPD ,就使用 ASBD, 就是非压缩音频,例如 wav

压缩音频,里面的数据不均匀,每个包含有的帧数据不等

func ParserPacketCallback(_ context: UnsafeMutableRawPointer, _ byteCount: UInt32, _ packetCount: UInt32, _ data: UnsafeRawPointer, _ packetDescriptions: Optional<UnsafeMutablePointer<AudioStreamPacketDescription>>) {
    let parser = Unmanaged<Parser>.fromOpaque(context).takeUnretainedValue()
    let packetDescriptionsOrNil: UnsafeMutablePointer<AudioStreamPacketDescription>? = packetDescriptions
    let isCompressed = packetDescriptionsOrNil != nil
    os_log("%@ - %d [bytes: %i, packets: %i, compressed: %@]", log: Parser.loggerPacketCallback, type: .debug, #function, #line, byteCount, packetCount, "\(isCompressed)")
    
    /// At this point we should definitely have a data format
    guard let dataFormat = parser.dataFormat else {
        return
    }
    
    /// Iterate through the packets and store the data appropriately
    if isCompressed {
        guard let packetDesp = packetDescriptions else {
            return
        }
        for i in 0 ..< Int(packetCount) {
            let packetDescription = packetDesp[i]
            let packetStart = Int(packetDescription.mStartOffset)
            let packetSize = Int(packetDescription.mDataByteSize)
            let packetData = Data(bytes: data.advanced(by: packetStart), count: packetSize)
            parser.packets.append((packetData, packetDescription))
        }
    } else {
        let format = dataFormat.streamDescription.pointee
        let bytesPerPacket = Int(format.mBytesPerPacket)
        for i in 0 ..< Int(packetCount) {
            let packetStart = i * bytesPerPacket
            let packetSize = bytesPerPacket
            let packetData = Data(bytes: data.advanced(by: packetStart), count: packetSize)
            parser.packets.append((packetData, nil))
        }
    }
}

本文地址:https://blog.csdn.net/dengjiangszhan/article/details/109612365

相关标签: 666 ios