开发者

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 int开发者_如何学Goo .caf.

Any help would be great, as there's very little out there for this topic.


Have a look at 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜