从iPhone上的线性PCM提取振幅数据

从iPhone上的线性PCM提取振幅数据,iphone,ios,core-audio,Iphone,Ios,Core Audio,我很难从存储在audio.caf中的iPhone上的线性PCM中提取振幅数据 我的问题是: 线性PCM将振幅样本存储为16位值。这是正确的吗 振幅如何存储在AudioFileReadPacketData()返回的数据包中?当记录单线性PCM时,每个样本(在一帧中,在一个数据包中)不都只是一个用于SInt16的阵列吗?字节顺序是什么(大端和小端) 线性PCM振幅中的每一步在物理上意味着什么 在iPhone上录制线性PCM时,中心点是0(SInt16)还是32768(UInt16)?物理波形/空气压

我很难从存储在audio.caf中的iPhone上的线性PCM中提取振幅数据

我的问题是:

  • 线性PCM将振幅样本存储为16位值。这是正确的吗
  • 振幅如何存储在AudioFileReadPacketData()返回的数据包中?当记录单线性PCM时,每个样本(在一帧中,在一个数据包中)不都只是一个用于SInt16的阵列吗?字节顺序是什么(大端和小端)
  • 线性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);
    
    感谢您提供的任何见解。这是我的第一个项目使用核心音频,所以请随意撕裂我的方法

    另外,我试图搜索核心音频列表档案,但请求不断给出错误:()

    p.p.S.我已经看过:

    我还阅读了整个核心音频概述和大部分音频会话编程指南,但我的问题仍然存在

  • 如果您要求在您的录制格式中使用16位样本,那么您将获得16位样本。但其他格式确实存在于许多核心音频录制/播放API中,以及可能的caf文件格式中

  • 在mono中,您只需要得到一个有符号的16位整数数组。您可以在一些核心音频录制API中特别要求使用big或little-endian

  • >P>除非你想校准你的特定设备模型的MIC或外部麦克风(并且确保音频处理/ AGC关闭),你可能要考虑音频水平是任意缩放的。另外,响应也随话筒方向性和音频而变化

  • 16位音频采样的中心点通常为0(范围约为-32k到32k)。没有偏见

  • 1) os x/iphone文件读取例程允许您确定示例格式,通常为LPCM的SInt8、SInt16、SInt32、Float32、Float64或连续24位带符号int中的一种

    2) 对于int格式,MIN_for_TYPE表示负相位的最大振幅,max_for_TYPE表示正相位的最大振幅。0等于沉默。浮点格式在[-1…1]之间进行调制,零与浮点一样。当读取、写入、录制或使用特定格式时,endianness将起作用-文件可能需要特定格式,并且您通常希望以本机endianness操作数据。apple音频文件libs中的一些例程允许您传递表示源端的标志,而不是手动转换。CAF有点复杂——它就像一个或多个音频文件的元包装器,支持多种类型

    3) lpcm的振幅表示仅为蛮力线性振幅表示(回放无需转换/解码,且振幅步长相等)

    4) 见#2。这些值与空气压力无关,它们与0 dBFS有关;e、 g.如果将流直接输出到DAC,则int max(或-1/1,如果是浮点)表示单个样本将剪辑的级别

    额外的)它,就像每个ADC和组件链一样,在电压方面,它对输入的处理能力是有限的。此外,采样率定义了可捕获的最高频率(最高为采样率的一半)。adc可以使用固定或可选择的位深度,但在选择另一位深度时,最大输入电压通常不会改变

    您在代码级别犯的一个错误是:您将“Exputffer”作为chars操作,而不是SInt16

    // 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]);
    }