iOS Swift malloc错误

时间:2017-01-05 14:46:59

标签: ios swift audio malloc avaudiofile

我想在iOS上实时录制音频,分析原始音频数据并保存部分记录数据。我正在使用以下代码记录数据:https://gist.github.com/hotpaw2/ba815fc23b5d642705f2b1dedfaf0107

现在,我的数据保存在Float数组中,我想将其保存到音频文件中。我尝试用这段代码做的:

let fileMgr = FileManager.default
    let dirPaths = fileMgr.urls(for: .documentDirectory, in: .userDomainMask)
    let recordSettings = [AVEncoderAudioQualityKey: AVAudioQuality.min.rawValue,
                      AVEncoderBitRateKey: 16,
                      AVNumberOfChannelsKey: 2,
                      AVSampleRateKey: 44100] as [String: Any]
    let soundFileUrl = dirPaths[0].appendingPathComponent("recording-" + getDate() + ".pcm")


    do {
        let audioFile = try AVAudioFile(forWriting: soundFileUrl, settings: recordSettings)
        let format = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 44100, channels: 2, interleaved: true)

        let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 3000)

        for i in 0..<circBuffer.count {
            audioFileBuffer.int16ChannelData?.pointee[i] = Int16(circBuffer[i])
        }

        try audioFile.write(from: audioFileBuffer)

    }

在最后一行,我收到一条错误消息:

错误:&gt; avae&gt; AVAudioFile.mm:306: - [AVAudioFile writeFromBuffer:error:]:error -50 amplitudeDemo(79264,0x70000f7cb000)malloc: *对象0x7fc5b9057e00的错误:释放对象的校验和不正确 - 对象可能在被释放后被修改。 * 在malloc_error_break中设置断点以进行调试

我已经搜索了很多其他问题,但我找不到任何帮助我的东西。

2 个答案:

答案 0 :(得分:1)

在此行的代码中:

let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: 3000)

您声明一个容量为3000帧* 2个频道的AVAudioPCMBuffer,这意味着您audioFileBuffer的已分配缓冲区可以包含6000个样本。如果通道数据的索引超出此限制,则代码会破坏堆中的附近区域,从而导致对象可能已修改错误。

因此,您的circBuffer.count可能超出此限制。您需要为AVAudioPCMBuffer分配足够的缓冲区大小。

    do {
        //### You need to specify common format
        let audioFile = try AVAudioFile(forWriting: soundFileUrl, settings: recordSettings, commonFormat: .pcmFormatInt16, interleaved: true)

        let channels = 2
        let format = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 44100, channels: AVAudioChannelCount(channels), interleaved: true)
        let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(circBuffer.count / channels)) //<-allocate enough frames
        //### `stride` removed as it seems useless...
        let int16ChannelData = audioFileBuffer.int16ChannelData! //<-cannot be nil for the `format` above

        //When interleaved, channel data of AVAudioPCMBuffer is not as described in its doc:
        //  https://developer.apple.com/reference/avfoundation/avaudiopcmbuffer/1386212-floatchanneldata .
        //  The following code is modified to work with actual AVAudioPCMBuffer.
        //Assuming `circBuffer` as
        //  Interleaved, 2 channel, Float32
        //  Each sample is normalized to [-1.0, 1.0] (as usual floating point audio format)
        for i in 0..<circBuffer.count {
            int16ChannelData[0][i] = Int16(circBuffer[i] * Float(Int16.max))
        }
        //You need to update `frameLength` of the `AVAudioPCMBuffer`.
        audioFileBuffer.frameLength = AVAudioFrameCount(circBuffer.count / channels)

        try audioFile.write(from: audioFileBuffer)

    } catch {
        print("Error", error)
    }

添加了一些注释作为注释,请在尝试此代码之前检查它们。

<强>更新 抱歉,为了显示未经测试的代码,修改了两件事:

  • 在实例化commonFormat:
  • 时,您需要指定AVAudioFile
  • int16ChannelData(和其他通道数据)不会返回预期的指针,如交错时的文档中所述,修改数据填充循环以适应实际行为。

请尝试。

答案 1 :(得分:1)

您似乎已经frameCapacity任意选择了3000

将其设置为实际样本数:

let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(circBuffer.count))