2

我正在尝试使用 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 写入文件,一切正常。当然这听起来像垃圾,因为我在写浮点数,而不是 SInt16s,但 AudioFileWriteBytes 不会抱怨。

4

1 回答 1

2

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

float audioVal = rb->data[(i + rb->lastReadIndex) & buffLen];
holdingBuffer[i] = CFSwapInt16HostToBig((SInt16) audioVal );
于 2010-03-23T01:42:32.027 回答