iphone - 使用 Core Audio 从 PCM 原始数据获取电平值

标签 iphone c core-audio

我正在尝试使用核心音频从 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 输入文件 = 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 位整数。 ka-boom。

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

你根本不需要你的缓冲区变量。 (为此)

关于iphone - 使用 Core Audio 从 PCM 原始数据获取电平值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4057298/

相关文章:

c - Linux 内核 write() 和 read() 函数

c++ - sizeof - 将指针转换为数组

ios - 试图找出 Core Audio 的 NBandEQ

css - 如何在 GtkTreeView 中交替亮/暗行?

objective-c - 如何在没有时间延迟的情况下从一种音乐序列更改为另一种音乐序列

audio - 如何创建AIFF文件

ios - 将 UIViewcontroller 的 View 添加到 UIWindow 不显示

iphone - 向上滑动UIView

iphone - 如何获取 iOS 的代码覆盖率数据?

iOS UILabel 大小问题