【发布时间】:2010-10-30 04:49:27
【问题描述】:
我正在尝试使用核心音频从 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]);
}
}
不知道我做错了什么,我认为这与读取字节数组有关。抱歉,代码太长了...
【问题讨论】:
-
我发现了这个:stackoverflow.com/questions/3833356/… 但我仍然看不出我做错了什么。
-
我还应该补充一点,track.location 是一个带有 pcm caf 文件的 NSURL,BUFFER_SIZE = ((4096 * 4) * 8) 应该是 32K。
标签: iphone c core-audio