AudioFileWriteBytes失败,错误代码为-40

时间:2010-03-21 03:47:14

标签: iphone audio core-audio extaudiofile

我正在尝试使用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);

然后我们将音频数据(存储为浮点数)复制到将直接写入文件的保持缓冲区(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写入文件,一切正常。当然这听起来像垃圾,因为我正在编写浮点数,而不是SInt16,但AudioFileWriteBytes并没有抱怨。

1 个答案:

答案 0 :(得分:2)

关键是明确地将传入数据的字节顺序更改为big endian。我所要做的就是将CFSwapInt16HostToBig包装在我的数据周围以获得:

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