简体   繁体   English

如何在iPhone上将音频转换为整数数组

[英]How to convert audio to integer array on iPhone

I am working on a sound analyzer program and I am looking to turn audio data into an array of integers (or float) based on its amplitude each frame, it is required for the algorithm that I am using. 我正在开发一个声音分析器程序,我希望根据每帧的振幅将音频数据转换为整数(或浮点数)数组,这是我使用的算法所必需的。 I have the app at recording sound into .caf. 我有将声音录制到.caf中的应用程序。

Any help would be great, as there's very little out there for this topic. 任何帮助都将非常有用,因为该主题的帮助很少。

Have a look at ExtAudioFile. 看看ExtAudioFile。 Here is a rough example of how to use it to read integer samples: 这是一个如何使用它读取整数样本的粗略示例:

-(void)setupStreamReader
        {
        CAStreamBasicDescription dstFormat, srcFormat;
        UInt32 sizeZ = sizeof(srcFormat);
        SInt64 sizeN = 0;
        UInt32 sizeofN = sizeof(sizeN);
        ExtAudioFileGetProperty(file, kExtAudioFileProperty_FileDataFormat, &sizeZ, &srcFormat);
        ExtAudioFileGetProperty(file, kExtAudioFileProperty_FileLengthFrames, &sizeofN, &sizeN);
        size = sizeN*4;

        dstFormat.mSampleRate = 44100; // set sample rate
        dstFormat.mFormatID = kAudioFormatLinearPCM;
        dstFormat.mChannelsPerFrame = 2;
        dstFormat.mBitsPerChannel = 16;
        dstFormat.mBytesPerPacket = dstFormat.mBytesPerFrame = 2 * dstFormat.mChannelsPerFrame;
        dstFormat.mFramesPerPacket = 1;
        dstFormat.mFormatFlags = kLinearPCMFormatFlagIsPacked | kLinearPCMFormatFlagIsSignedInteger; // little-endian

        sizeZ = sizeof(dstFormat);
        ExtAudioFileSetProperty(file, kExtAudioFileProperty_ClientDataFormat, sizeZ, &dstFormat);
        }

And then this to get the actual short integer array 然后获得实际的短整数数组

-(void)getBytes:(short*)buffer range:(NSRange)range
{
SInt64 off = range.location/4;
ExtAudioFileSeek(file, off);

AudioBufferList fillBufList;
fillBufList.mNumberBuffers = 1;
fillBufList.mBuffers[0].mNumberChannels = 2;
fillBufList.mBuffers[0].mDataByteSize = range.length;
fillBufList.mBuffers[0].mData = buffer;
UInt32 frames = range.length/2;
ExtAudioFileRead(file, &frames, &fillBufList);
}

file is an ExtAudioFileRef declared in the header. 文件是在标头中声明的ExtAudioFileRef。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM