AVAudioMixerNode хорош для преобразований sampleRate, но для широких изменений формата, таких как Int16 на Float, вам, вероятно, лучше преобразовать себя. Для повышения производительности я предлагаю использовать vDSP Accelerate.
import Cocoa
import AVFoundation
import Accelerate
import PlaygroundSupport
let bufferSize = 512
let bufferByteSize = MemoryLayout<Float>.size * bufferSize
var pcmInt16Data: [Int16] = []
var pcmFloatData = [Float](repeating: 0.0, count: bufferSize) // allocate once and reuse
// one buffer of noise as an example
for _ in 0..<bufferSize {
let value = Int16.random(in: Int16.min...Int16.max)
pcmInt16Data.append(value)
}
let engine = AVAudioEngine()
let player = AVAudioPlayerNode()
let audioFormat = AVAudioFormat(standardFormatWithSampleRate: 48_000.0, channels: 1)!
let buffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: UInt32(bufferSize))
let mixer = engine.mainMixerNode
engine.attach(player)
engine.connect(player, to: mixer, format: audioFormat)
engine.prepare()
do {
try engine.start()
} catch {
print("Error info: \(error)")
}
player.play()
if let buffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: UInt32(bufferSize)) {
let monoChannel = buffer.floatChannelData![0]
// Int16 ranges from -32768 to 32767 -- we want to convert and scale these to Float values between -1.0 and 1.0
var scale = Float(Int16.max) + 1.0
vDSP_vflt16(pcmInt16Data, 1, &pcmFloatData, 1, vDSP_Length(bufferSize)) // Int16 to Float
vDSP_vsdiv(pcmFloatData, 1, &scale, &pcmFloatData, 1, vDSP_Length(bufferSize)) // divide by scale
memcpy(monoChannel, pcmFloatData, bufferByteSize)
buffer.frameLength = UInt32(bufferSize)
player.scheduleBuffer(buffer, completionHandler: nil) // load more buffers in the completionHandler
}
PlaygroundPage.current.needsIndefiniteExecution = true
Если вместо этого вы хотите воспроизвести AVAudioFile, используйте AVAudioPlayerNode.scheduleFile () и .scheduleSegment , а не пытаться читать данные Int16 напрямую из WAV / AIFF. Вам нужно обратить внимание на параметр AVAudioFile.processingFormat и использовать его для формата соединения проигрывателя с микшером.
import Cocoa
import PlaygroundSupport
import AVFoundation
let engine = AVAudioEngine()
let player = AVAudioPlayerNode()
let playEntireFile = true
func playLocalFile() {
// file needs to be in ~/Documents/Shared Playground Data
let localURL = playgroundSharedDataDirectory.appendingPathComponent("MyAwesomeMixtape6.aiff")
guard let audioFile = try? AVAudioFile(forReading: localURL) else { return }
let audioFormat = audioFile.processingFormat
let mixer = engine.mainMixerNode
engine.attach(player)
engine.connect(player, to: mixer, format: audioFormat)
engine.prepare()
do {
try engine.start()
} catch {
print("Error info: \(error)")
}
player.play()
if playEntireFile {
player.scheduleFile(audioFile, at: nil, completionHandler: nil)
} else { // play segment
let startTimeSeconds = 5.0
let durationSeconds = 2.0
let sampleRate = audioFormat.sampleRate
let startFramePostion = startTimeSeconds * sampleRate
let durationFrameCount = durationSeconds * sampleRate
player.scheduleSegment(audioFile, startingFrame: AVAudioFramePosition(startFramePostion), frameCount: AVAudioFrameCount(durationFrameCount), at: nil, completionHandler: nil)
}
}
playLocalFile()
PlaygroundPage.current.needsIndefiniteExecution = true
Для удаленных файлов , попробуйте AVPlayer.
import Cocoa
import AVFoundation
import PlaygroundSupport
var player: AVPlayer?
func playRemoteFile() {
guard let remoteURL = URL(string: "https://ondemand.npr.org/anon.npr-mp3/npr/me/2020/03/20200312_me_singapore_wins_praise_for_its_covid-19_strategy_the_us_does_not.mp3"
) else { return }
player = AVPlayer(url: remoteURL)
player?.play()
}
playRemoteFile()
PlaygroundPage.current.needsIndefiniteExecution = true