Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone AudioFileWriteBytes失败,错误代码为-40_Iphone_Audio_Core Audio_Extaudiofile - Fatal编程技术网

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

Iphone AudioFileWriteBytes失败,错误代码为-40,iphone,audio,core-audio,extaudiofile,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 b

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

这个错误是什么-40?顺便说一句,如果我直接从ringBuffer写入文件,一切都很好。当然,这听起来像垃圾,因为我在写浮动,而不是SInt16s,但AudioFileWriteBytes并不抱怨

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

float audioVal = rb->data[(i + rb->lastReadIndex) & buffLen];
holdingBuffer[i] = CFSwapInt16HostToBig((SInt16) audioVal );
    status = AudioFileWriteBytes(audioFileID, NO, 0, &numBytesToWrite, &holdingBuffer);     
    rb->lastReadIndex = lastFreshSample;

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

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