gpt4 book ai didi

ios - 如何在 Objective C 中编写以下代码?

转载 作者:行者123 更新时间:2023-11-30 11:43:21 25 4
gpt4 key购买 nike

我将下面的代码从 swift 转换为 Objective C:

func toPCMBuffer(data: NSData) -> AVAudioPCMBuffer {
let audioFormat = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false) // given NSData audio format
let PCMBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: UInt32(data.length) / audioFormat.streamDescription.pointee.mBytesPerFrame)
PCMBuffer.frameLength = PCMBuffer.frameCapacity
let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: Int(PCMBuffer.format.channelCount))
data.getBytes(UnsafeMutableRawPointer(channels[0]) , length: data.length)
return PCMBuffer
}

我转换为 objective-c :

-(AVAudioPCMBuffer*)toPCMBuffer: (NSData*)data {

AVAudioFormat * audioFormat = [[AVAudioFormat alloc]initWithCommonFormat:AVAudioPCMFormatFloat32 sampleRate:8000 channels:1 interleaved:false];
AVAudioPCMBuffer* PCMBuffer = [[AVAudioPCMBuffer alloc]initWithPCMFormat:audioFormat frameCapacity:data.length/audioFormat.streamDescription->mBytesPerFrame];
PCMBuffer.frameLength = PCMBuffer.frameCapacity;
float *channels = malloc(PCMBuffer.format.channelCount * sizeof(float)); // remember to free eventually
memcpy(channels, PCMBuffer.floatChannelData, PCMBuffer.format.channelCount * sizeof(float));
[data getBytes:channels length:data.length];
return PCMBuffer;

}

但我不确定是否更正行代码:

AVAudioPCMBuffer* PCMBuffer = [[AVAudioPCMBuffer alloc]initWithPCMFormat:audioFormat frameCapacity:data.length/audioFormat.streamDescription->mBytesPerFrame];

这是我不知道如何将 UInt32(data.length) 写入 objective-c 的代码

和:

如何将UnsafeMutableRawPointer(channels[0])转换为objc?

[data getBytes:channels length:data.length];

最佳答案

原始代码的作用是将参数data复制到PCMBuffer.floatChannelData中。

您的代码所做的是创建一个新的字节缓冲区,然后将该空缓冲区复制到 PCMBuffer 并将参数数据复制到缓冲区中。

我相信它应该类似于:

PCMBuffer.frameLength = PCMBuffer.frameCapacity;
[data getBytes:PCMBuffer.floatChannelData length:data.length];
return PCMBuffer;

关于ios - 如何在 Objective C 中编写以下代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49093885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com