3

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?

Amirca
  • 143
  • 1
  • 8
  • 1
    have you solved this? – Rhenz Jul 29 '19 at 05:59
  • not yet! there is not any resource for that! – Amirca May 15 '20 at 05:06
  • I am also playing around with this, but when I subclass SPTCoreAudioController and set my custom controller with [self.player startWithClientId:auth.clientID audioController:self.customAudioController allowCaching:YES error:&error], the overriden function is never called. Could that be because the old Spotify SDK is deprecated? – Nusatad Apr 07 '21 at 12:40

0 Answers0