我很难从iPhone上存储在audio.caf中的线性PCM中提取振幅数据。
我的问题是:
-
线性PCM将振幅样本存储为16位值。这是正确的吗?
-
音频文件readpacketdata()返回的数据包中如何存储振幅?当记录单线性PCM时,每个样本(在一帧中,在一个数据包中)是否只是用于SINT16的一个数组?字节顺序是什么(big-endian和little-endian)?
-
线性PCM振幅的每一步在物理上意味着什么?
-
在iPhone上记录线性PCM时,中心点是0(sint16)还是32768(uint16)?在物理波形/气压中,最大-最小值意味着什么?
还有一个额外的问题:有没有iPhone麦克风无法测量的声音/气压波?
我的代码如下:
// get the audio file proxy object for the audio
AudioFileID fileID;
AudioFileOpenURL((CFURLRef)audioURL, kAudioFileReadPermission, kAudioFileCAFType, &fileID);
// get the number of packets of audio data contained in the file
UInt64 totalPacketCount = [self packetCountForAudioFile:fileID];
// get the size of each packet for this audio file
UInt32 maxPacketSizeInBytes = [self packetSizeForAudioFile:fileID];
// setup to extract the audio data
Boolean inUseCache = false;
UInt32 numberOfPacketsToRead = 4410; // 0.1 seconds of data
UInt32 ioNumPackets = numberOfPacketsToRead;
UInt32 ioNumBytes = maxPacketSizeInBytes * ioNumPackets;
char *outBuffer = malloc(ioNumBytes);
memset(outBuffer, 0, ioNumBytes);
SInt16 signedMinAmplitude = -32768;
SInt16 signedCenterpoint = 0;
SInt16 signedMaxAmplitude = 32767;
SInt16 minAmplitude = signedMaxAmplitude;
SInt16 maxAmplitude = signedMinAmplitude;
// process each and every packet
for (UInt64 packetIndex = 0; packetIndex < totalPacketCount; packetIndex = packetIndex + ioNumPackets)
{
// reset the number of packets to get
ioNumPackets = numberOfPacketsToRead;
AudioFileReadPacketData(fileID, inUseCache, &ioNumBytes, NULL, packetIndex, &ioNumPackets, outBuffer);
for (UInt32 batchPacketIndex = 0; batchPacketIndex < ioNumPackets; batchPacketIndex++)
{
SInt16 packetData = outBuffer[batchPacketIndex * maxPacketSizeInBytes];
SInt16 absoluteValue = abs(packetData);
if (absoluteValue < minAmplitude) { minAmplitude = absoluteValue; }
if (absoluteValue > maxAmplitude) { maxAmplitude = absoluteValue; }
}
}
NSLog(@"minAmplitude: %hi", minAmplitude);
NSLog(@"maxAmplitude: %hi", maxAmplitude);
有了这个代码,我几乎总能得到最小值0和最大值128!那就不行了
对我有感觉。
我正在使用录音机录制音频,如下所示:
// specify mono, 44.1 kHz, Linear PCM with Max Quality as recording format
NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMax], AVEncoderAudioQualityKey,
nil];
// store the sound file in the app doc folder as calibration.caf
NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSURL *audioFileURL = [NSURL fileURLWithPath:[documentsDir stringByAppendingPathComponent: @"audio.caf"]];
// create the audio recorder
NSError *createAudioRecorderError = nil;
AVAudioRecorder *newAudioRecorder = [[AVAudioRecorder alloc] initWithURL:audioFileURL settings:recordSettings error:&createAudioRecorderError];
[recordSettings release];
if (newAudioRecorder)
{
// record the audio
self.recorder = newAudioRecorder;
[newAudioRecorder release];
self.recorder.delegate = self;
[self.recorder prepareToRecord];
[self.recorder record];
}
else
{
NSLog(@"%@", [createAudioRecorderError localizedDescription]);
}
感谢您提供的任何见解。这是我第一个使用核心音频的项目,所以请随意撕开我的方法!
P.S.我试图搜索核心音频列表存档,但请求不断给出错误:(
http://search.lists.apple.com/?q=linear+pcm+amplitude&cmd=Search%21&ul=coreaudio-api
)
P.P.S.我看过:
http://en.wikipedia.org/wiki/Sound_pressure
http://en.wikipedia.org/wiki/Linear_PCM
http://wiki.multimedia.cx/index.php?title=PCM
Get the amplitude at a given time within a sound file?
http://music.columbia.edu/pipermail/music-dsp/2002-April/048341.html
我还阅读了核心音频概述和大多数音频会话编程指南的全部内容,但我的问题仍然存在。