Home > other >  Cannot convert value of type 'UnsafePointer<UnsafeMutablePointer<Float>>?' to
Cannot convert value of type 'UnsafePointer<UnsafeMutablePointer<Float>>?' to

Time:10-18

I'm using a C api in Swift. I need to pass audio for which I'm using floatChannelData from AVAudioPCMBuffer.

let audioFile = try! AVAudioFile(forReading: fileURL as URL)
        let audioFormat = audioFile.processingFormat
        let audioFrameCount = UInt32(audioFile.length)
        let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)

 if whisper_full(ctx, wparams, audioFileBuffer?.floatChannelData , Int32(audioFrameCount)) != 0 {
            print("failed to process audio")
            return
        }

C Header:

WHISPER_API int whisper_full(
            struct whisper_context * ctx,
            struct whisper_full_params params,
            const float * samples,
            int n_samples);

I tried just using UnsafePointer(audioFileBuffer?.floatChannelData) but that gives a different error. I'm a bit confused about how pointers work in Swift.

I've read the Apple UnsafePointer docs but don't feel any wiser. https://developer.apple.com/documentation/swift/unsafepointer

CodePudding user response:

As stated in Apple floatChannelData documentation, floatChannelDatais a pointer to a list of pointers to frames, a frame being a list of frameLength floats (hence the pointer to a pointer to float).

On the other side, whisper_full() function seems to take directly a pointer to the complete list of floats.

I do not know what whisper_full() does, so if it is suitable, you can call it once for each frame, else you will have to perform some operation to put all the frames one after each other in the memory (an operation that can lead to heavy CPU and memory load), and pass the pointer to the first to your C function.

  • Related