func createPcmBuffer(frames:UInt32,frequency:Double,amplitude:Double)->AVAudioPCMBuffer{
let buffer = AVAudioPCMBuffer.init(pcmFormat: AVAudioFormat.init(settings:[
AVFormatIDKey:kAudioFormatLinearPCM,
AVSampleRateKey:44100,
AVNumberOfChannelsKey:1,
AVLinearPCMBitDepthKey:16,
AVLinearPCMIsFloatKey:false
])!, frameCapacity: frames*44100)!
buffer.frameLength = buffer.frameCapacity
let length = buffer.frameCapacity
let data = buffer.int16ChannelData!
let p = data.pointee
for i in 0..<Int(length) {
let v = Int16(32767*sin(2*Double.pi*frequency*i/44100)*amplitude) //这里的32767是Math.pow(2,16)-1得来的
// v = (v<<8) + ((v>>8)&0xFF) 经过我测试不需要高低位互换
p[i] = v
}
return buffer;
}
原文:https://www.cnblogs.com/RoysPhoneBlog/p/9483287.html