压缩(AAC)音频大于PCM

时间:2019-02-22 23:21:16

标签: swift aac

最终,我正在尝试将音频从一台设备传输到另一台设备。 PCM / WAV太大。我正在尝试将PCM音频字节流转换为MPEG4AAC。但是,我的压缩音频实际上比原始音频流(即PCM)大。即使我将比特率降低到32000,压缩后的音频也只会占原始大小的70%。我的代码没有错误执行,并且压缩的音频确实通过AudioQueue在接收设备上播放。如果有人可以提供帮助,我将不胜感激。

let inDescriptionRef = CMSampleBufferGetFormatDescription(sampleBuffer)
        if inDescriptionRef != nil {
            var inDescription = CMAudioFormatDescriptionGetStreamBasicDescription(inDescriptionRef!)!.pointee
            var outDescriptionACC = AudioStreamBasicDescription()
            outDescriptionACC.mSampleRate = (inDescription.mSampleRate)
            outDescriptionACC.mFormatID = kAudioFormatMPEG4AAC//encoding mp3 not supported
            outDescriptionACC.mChannelsPerFrame = (inDescription.mChannelsPerFrame)
            outDescriptionACC.mFormatFlags = 2//kMP4Audio_AAC_LC_ObjectType

            //create these objects once so we don't keep creating them
            if mConverterToAAC == nil {
                mInFormat = AVAudioFormat(streamDescription: &inDescription)!
                mOutFormat = AVAudioFormat(streamDescription: &outDescriptionACC)!
                mConverterToAAC = AVAudioConverter(from: mInFormat, to: mOutFormat)
                mConverterToAAC.bitRate = 92000
            }


            //convert the pcm data to PCMBuffer
            let inBuffer = toPCMBuffer(pcmData)

            //out buffer to hold compressed results
            let outBuffer = AVAudioCompressedBuffer(format: mOutFormat, packetCapacity: 8, maximumPacketSize: mConverterToAAC.maximumOutputPacketSize)

            //closure to get data to convert
            let newBufferAvailable = true
            let inputBlock : AVAudioConverterInputBlock = { inNumPackets, outStatus in
                if newBufferAvailable {
                    outStatus.pointee = AVAudioConverterInputStatus.haveData
                    return inBuffer
                }
                else{
                    outStatus.pointee = AVAudioConverterInputStatus.noDataNow
                    return nil
                }
            }

            //convert PCM to Compressed AAC
            var errorPtr : NSError?
            let convertStatus = mConverterToAAC.convert(to: outBuffer, error: &errorPtr, withInputFrom: inputBlock)
            //print("ConverterStatusToACC=\(convertStatus), Name=\(Utilities().AudioConverterOutputStatusToStr(convertStatus))")
            if errorPtr != nil {
                print("ConverterStatusToACC Error Message=\(errorPtr!)")
            }
            else{
                let accData = Data(bytes: outBuffer.data, count: Int(outBuffer.byteLength))

                pcmDataTotalCount += pcmData.count
                accDataTotalCount += accData.count
                pcmDataCount += pcmData.count
                accDataCount += accData.count
                let timeLapsed = util.getCurrentMillis() - mCurrentTime
                if timeLapsed > 1000 {
                    print("PCM Data=\(pcmDataCount), Compressed Data=\(accDataCount), TimeLapsed=\(timeLapsed)")
                    pcmDataCount = 0
                    accDataCount = 0;
                    mCurrentTime = util.getCurrentMillis()

                    let pcmDataTotalCountSizeWithUnit = ByteCountFormatter.string(fromByteCount: Int64(pcmDataTotalCount), countStyle: .file)
                    let accDataTotalCountSizeWithUnit = ByteCountFormatter.string(fromByteCount: Int64(accDataTotalCount), countStyle: .file)
                    let seconds = (util.getCurrentMillis() - mStartTime) / 1000
                    //print("Total PCM Data=\(pcmDataTotalCountSizeWithUnit), Total Compressed Data=\(accDataTotalCountSizeWithUnit), Total Time=\(seconds)")
                }

                if self.connection.connected {
                    self.connection.sendAudio(pcmData)
                }
            }
        }
        else{
            print("inDescriptionRef is nil")
        }

0 个答案:

没有答案