我正在尝试使用核心音频从PCM音频文件中提取电平数据。我已经尽我所能将原始数据放入字节数组(UInt8),但它是16位PCM数据,我在读取数据时遇到了麻烦。输入来自iPhone麦克风,我将其设置为:

[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];

[recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSetting setValue:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];


显然是16位然后,我尝试仅打印出一些值,以查看它们是否适合以下调试目的,并且看起来不合理(很多0)。

ExtAudioFileRef inputFile = NULL;
    ExtAudioFileOpenURL(track.location,&inputFile);

AudioStreamBasicDescription inputFileFormat;
UInt32 dataSize = (UInt32)sizeof(inputFileFormat);
ExtAudioFileGetProperty(inputFile, kExtAudioFileProperty_FileDataFormat, &dataSize, &inputFileFormat);

UInt8 *buffer = malloc(BUFFER_SIZE);
AudioBufferList bufferList;
bufferList.mNumberBuffers = 1;
bufferList.mBuffers[0].mNumberChannels = 1;
bufferList.mBuffers[0].mData = buffer; //pointer to buffer of audio data
bufferList.mBuffers[0].mDataByteSize = BUFFER_SIZE; //number of bytes in the buffer

while(true) {

    UInt32 frameCount = (bufferList.mBuffers[0].mDataByteSize / inputFileFormat.mBytesPerFrame);

    // Read a chunk of input
    OSStatus status = ExtAudioFileRead(inputFile, &frameCount, &bufferList);

    // If no frames were returned, conversion is finished
    if(0 == frameCount)
        break;

    NSLog(@"---");

    int16_t *bufferl = &buffer;
    for(int i=0;i<100;i++){
        //const int16_t *bufferl = bufferl[i];
        NSLog(@"%d",bufferl[i]);
    }

}


不知道我在做什么错,我认为这与读取字节数组有关。抱歉,很长的代码...

最佳答案

您分配了一个无符号的8位整数的缓冲区,然后将其地址转换为一个无符号的16位整数。轰。

您要在for循环中执行的操作是将bufferList.mBuffers [0] .mData强制转换为SInt16 *,然后对其进行迭代以打印出您的值。

您根本不需要缓冲区var。 (为了这)

09-11 19:24
查看更多