Вот проверочный код, который ускоряет стерео файл M4A и сохраняет его как файл WAVE. Обработка ошибок минимальна и нет попыток обрабатывать разные схемы каналов, но, надеюсь, это поможет вам начать:
let engine = AVAudioEngine()
// Use AVAudioUnitTimePitch to avoid pitch shifts
let timePitch = AVAudioUnitTimePitch()
let player = AVAudioPlayerNode()
engine.attach(timePitch)
engine.attach(player)
engine.connect(player, to:timePitch, format: nil)
engine.connect(timePitch, to:engine.mainMixerNode, format: nil)
// Speed up the file 1.5x
timePitch.rate = 1.5
// Run the engine in manual rendering mode using chunks of 512 frames
let renderSize: AVAudioFrameCount = 512
let renderFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 44100.0, channels: 2, interleaved: true)!
let renderBuffer = AVAudioPCMBuffer(pcmFormat: renderFormat, frameCapacity: renderSize)!
try! engine.enableManualRenderingMode(.offline, format: renderFormat, maximumFrameCount: renderBuffer.frameCapacity)
try! engine.start()
player.play()
// The render format is also the output format
let output = try! AVAudioFile(forWriting: URL(fileURLWithPath: "/tmp/foo.wav"), settings: renderFormat.settings, commonFormat: renderFormat.commonFormat, interleaved: renderFormat.isInterleaved)
let file = try! AVAudioFile(forReading: URL(fileURLWithPath: "/tmp/test.m4a"))
// Read using a buffer sized to produce `renderSize` frames of output
let readSize = AVAudioFrameCount(Float(renderSize) * timePitch.rate)
let readBuffer = AVAudioPCMBuffer(pcmFormat: file.processingFormat, frameCapacity: readSize)!
// Process the file
while true {
do {
// Processing is finished if all frames have been read
if file.framePosition == file.length {
break
}
try file.read(into: readBuffer)
player.scheduleBuffer(readBuffer, completionHandler: nil)
let result = try engine.renderOffline(renderBuffer.frameCapacity, to: renderBuffer)
// Try to avoid adding silence
let expectedFrames = AVAudioFrameCount(Float(readBuffer.frameLength) / timePitch.rate)
if expectedFrames < renderBuffer.frameLength {
renderBuffer.frameLength = expectedFrames
}
// Write the adjusted-rate audio
try output.write(from: renderBuffer)
if result != .success {
break
}
}
catch {
break
}
}
player.stop()
engine.stop()