gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:03:31 25 4
gpt4 key购买 nike

我正在尝试使用核心音频从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com