[英]iOS Swift malloc error
我想在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)
}
在最后一行,我得到一个错误,上面写着:
错误:> avae> AVAudioFile.mm:306: - [AVAudioFile writeFromBuffer:error:]:error -50 amplitudeDemo(79264,0x70000f7cb000)malloc: *对象0x7fc5b9057e00的错误:释放对象的校验和不正确 - 对象可能在被释放后被修改。 *在malloc_error_break中设置断点以进行调试
我已经搜索了很多其他问题,但我找不到任何帮助我的东西。
在此行的代码中:
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
(和其他通道数据)不返回预期指针,如文档中所述,修改数据填充循环以适应实际行为。 请试试。
你似乎已经将frameCapacity
任意选择为3000
。
将其设置为实际样本计数:
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: AVAudioFrameCount(circBuffer.count))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.