gpt4 book ai didi

iphone - AudioFileWriteBytes 失败,错误代码为 -40

转载 作者:行者123 更新时间:2023-12-03 21:12:09 25 4
gpt4 key购买 nike

我正在尝试使用 AudioFileWriteBytes() 将原始音频字节写入文件。这就是我正在做的事情:

void writeSingleChannelRingBufferDataToFileAsSInt16(AudioFileID audioFileID, AudioConverterRef audioConverter, ringBuffer *rb, SInt16 *holdingBuffer) {
// First, figure out which bits of audio we'll be
// writing to file from the ring buffer

UInt32 lastFreshSample = rb->lastWrittenIndex;
OSStatus status;
int numSamplesToWrite;
UInt32 numBytesToWrite;


if (lastFreshSample < rb->lastReadIndex) {
numSamplesToWrite = kNumPointsInWave + lastFreshSample - rb->lastReadIndex - 1;
}
else {
numSamplesToWrite = lastFreshSample - rb->lastReadIndex;
}
numBytesToWrite = numSamplesToWrite*sizeof(SInt16);

然后我们将音频数据(存储为 float )复制到保存缓冲区 (SInt16),该缓冲区将直接写入文件。复制看起来很时髦,因为它来自环形缓冲区。

    UInt32 buffLen = rb->sizeOfBuffer - 1;
for (int i=0; i < numSamplesToWrite; ++i) {
holdingBuffer[i] = rb->data[(i + rb->lastReadIndex) & buffLen];
}

好吧,现在我们实际上尝试将 SInt16 缓冲区“holdingBuffer”中的音频写入音频文件。 NSLog 将输出错误 -40,但也声称它正在写入字节。没有数据写入文件。

    status = AudioFileWriteBytes(audioFileID, NO, 0, &numBytesToWrite, &holdingBuffer);     
rb->lastReadIndex = lastFreshSample;

NSLog(@"Error = %d, wrote %d bytes", status, numBytesToWrite);

return;

这个错误-40是什么?顺便说一句,如果我直接从 RingBuffer 写入文件,一切都会正常。当然,这听起来像垃圾,因为我写的是 float ,而不是 SInt16,但 AudioFileWriteBytes 不会提示。

最佳答案

关键是显式地将传入数据的字节序更改为大字节序。我所要做的就是将 CFswapInt16HostToBig 包裹在我的数据周围以获得:

float audioVal = rb->data[(i + rb->lastReadIndex) & buffLen];
holdingBuffer[i] = CFSwapInt16HostToBig((SInt16) audioVal );

关于iphone - AudioFileWriteBytes 失败,错误代码为 -40,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2485842/

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