我在Swift中使用了一个C api。我需要传递使用来自AVAudioPCMBuffer的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头:
WHISPER_API int whisper_full(
struct whisper_context * ctx,
struct whisper_full_params params,
const float * samples,
int n_samples);我试过使用UnsafePointer(audioFileBuffer?.floatChannelData),但这带来了一个不同的错误。我有点搞不懂斯威夫特的指针是怎么工作的。
我读过苹果的UnsafePointer文档,但没有感觉更聪明。https://developer.apple.com/documentation/swift/unsafepointer
发布于 2022-10-17 05:05:38
正如苹果floatChannelData文档中所述,floatChannelData是指向指向帧的指针列表的指针,而帧是frameLength的列表(因此指向指向浮点的指针)。
另一方面,whisper_full()函数似乎直接接受一个指向完整浮动列表的指针。
我不知道whisper_full()是做什么的,所以如果它合适的话,您可以对每个帧调用一次,否则您将不得不执行一些操作,将所有的帧一个接一个地放在内存中(一种可能导致沉重的CPU和内存负载的操作),并将指针传递到第一个指向您的C函数。
https://stackoverflow.com/questions/74091304
复制相似问题