繁体   English   中英

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

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

我正在开发一个声音分析器程序,我希望根据每帧的振幅将音频数据转换为整数(或浮点数)数组,这是我使用的算法所必需的。 我有将声音录制到.caf中的应用程序。

任何帮助都将非常有用,因为该主题的帮助很少。

看看ExtAudioFile。 这是一个如何使用它读取整数样本的粗略示例:

-(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);
        }

然后获得实际的短整数数组

-(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);
}

文件是在标头中声明的ExtAudioFileRef。

暂无
暂无

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

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