I am trying to perform some magic on Spotify's audio stream based on this. I have subclassed SPTCoreAudioController
.
It seems Spotify pointer, which is passed into the overridden function, points to a 16-bit integer. I have tried to create AVAudioPCMBuffer
based on audioFrames and audioDescription and pass it playerNode. The player node which is the node in Audio Engine works properly if I use an audio file.
override func attempt(toDeliverAudioFrames audioFrames: UnsafeRawPointer!, ofCount frameCount: Int, streamDescription audioDescription: AudioStreamBasicDescription) -> Int {
let ptr = audioFrames.bindMemory(to: Int16.self, capacity: frameCount)
let framePtr = UnsafeBufferPointer(start: ptr, count: frameCount)
let frames = Array(framePtr)
var newAudioDescription = audioDescription
let audioFormat = AVAudioFormat(streamDescription: &newAudioDescription)!
let audioPCMBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: AVAudioFrameCount(frameCount))!
audioPCMBuffer.frameLength = audioPCMBuffer.frameCapacity
let channelCount = Int(audioDescription.mChannelsPerFrame)
if let int16ChannelData = audioPCMBuffer.int16ChannelData {
for channel in 0..<channelCount {
for sampleIndex in 0..<frameLength {
int16ChannelData[channel][Int(sampleIndex)] = frames[Int(sampleIndex)]
}
}
}
didReceive(pcmBuffer: audioPCMBuffer)
return super.attempt(toDeliverAudioFrames: audioFrames, ofCount: frameCount, streamDescription: audioDescription)
}
func didReceive(pcmBuffer: AVAudioPCMBuffer) {
playerNode.scheduleBuffer(pcmBuffer) {
}
}
I get AURemoteIO::IOThread (19): EXC_BAD_ACCESS (code=1, address=0x92e370f25cc0) the error which I think the data is moved before I can copy it to the pcm buffer.
I was wondering if someone knows what is the proper way of using attemptToDeliverAudioFrames:ofCount:streamDescription: function?