2
我想写使用AudioFileWriteBytes()原始音频字节的文件。下面是我在做什么:AudioFileWriteBytes失败,错误代码-40
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);
然后我们的音频数据(存储为花车)复制到将直接写入文件保持缓冲(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到文件工作正常。当然,这听起来像垃圾,因为我写的花车,没有SInt16s,但AudioFileWriteBytes不抱怨。