如何播放和读取.caf PCM音频文件 [英] How to play and read .caf PCM audio file

查看:326
本文介绍了如何播放和读取.caf PCM音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可以从iPod库中选择一首歌曲,然后将该歌曲作为".caf"文件复制到该应用程序的目录中.现在,我需要播放并同时从Accelerate框架将该文件读取到Apple FFT中,以便像频谱图一样可视化数据.这是FFT的代码:

I have an app that selects a song from the iPod Library then copies that song into the app's directory as a '.caf' file. I now need to play and at the same time read that file into Apples FFT from the Accelerate framework so I can visualize the data like a spectrogram. Here is the code for the FFT:

void FFTAccelerate::doFFTReal(float samples[], float amp[], int numSamples)
{
int i;
vDSP_Length log2n = log2f(numSamples);

//Convert float array of reals samples to COMPLEX_SPLIT array A
vDSP_ctoz((COMPLEX*)samples,2,&A,1,numSamples/2);

//Perform FFT using fftSetup and A
//Results are returned in A
vDSP_fft_zrip(fftSetup, &A, 1, log2n, FFT_FORWARD);

//Convert COMPLEX_SPLIT A result to float array to be returned
amp[0] = A.realp[0]/(numSamples*2);
for(i=1;i<numSamples;i++)
    amp[i]=sqrt(A.realp[i]*A.realp[i]+A.imagp[i]*A.imagp[i])/numSamples;
}

//Constructor
FFTAccelerate::FFTAccelerate (int numSamples)
{
vDSP_Length log2n = log2f(numSamples);
fftSetup = vDSP_create_fftsetup(log2n, FFT_RADIX2);
int nOver2 = numSamples/2;
A.realp = (float *) malloc(nOver2*sizeof(float));
A.imagp = (float *) malloc(nOver2*sizeof(float));
}

我的问题是如何在播放.caf音频文件的同时循环播放FFT?我只需要一个频道.我猜我需要获取这首歌的1024个样本,在FTT中进行处理,然后再向下移动文件并获取另外1024个样本.但我不明白如何读取音频文件来执行此操作.该文件的采样率为44100.0 hz,采用线性PCM格式,16位,并且如果有帮助,我相信也可以进行交织...

My question is how to I loop through the '.caf' audio file to feed the FFT while at the same time playing the song? I only need one channel. Im guessing I need to get 1024 samples of the song, process that in the FTT and then move further down the file and grab another 1024 samples. But I dont understand how to read an audio file to do this. The file has a sample rate of 44100.0 hz, is in linear PCM format, 16 Bit and I believe is also interleaved if that helps...

推荐答案

尝试使用ExtendedAudioFile API(需要AudioToolbox.framework).

Try the ExtendedAudioFile API (requires AudioToolbox.framework).

#include <AudioToolbox/ExtendedAudioFile.h>

NSURL   *urlToCAF = ...;

ExtAudioFileRef caf;
OSStatus    status;

status = ExtAudioFileOpenURL((__bridge CFURLRef)urlToCAF, &caf);
if(noErr == status) {
    // request float format
    const UInt32 NumFrames = 1024;
    const int ChannelsPerFrame = 1;  // Mono, 2 for Stereo

    // request float format
    AudioStreamBasicDescription clientFormat;
    clientFormat.mChannelsPerFrame = ChannelsPerFrame;
    clientFormat.mSampleRate = 44100;

    clientFormat.mFormatID = kAudioFormatLinearPCM;
    clientFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved; // float
    int cmpSize = sizeof(float);
    int frameSize = cmpSize*ChannelsPerFrame;
    clientFormat.mBitsPerChannel = cmpSize*8;
    clientFormat.mBytesPerPacket = frameSize;
    clientFormat.mFramesPerPacket = 1;
    clientFormat.mBytesPerFrame = frameSize;

    status = ExtAudioFileSetProperty(caf, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);
     if(noErr != status) { /* handle it */ }

    while(1) {
        float   buf[ChannelsPerFrame*NumFrames];
        AudioBuffer ab = { ChannelsPerFrame, sizeof(buf), buf };
        AudioBufferList abl;
        abl.mNumberBuffers = 1;
        abl.mBuffers[0] = ab;

        UInt32  ioNumFrames = NumFrames;
        status = ExtAudioFileRead(caf, &ioNumFrames, &abl);

        if(noErr == status) {
            // process ioNumFrames here in buf
            if(0 == ioNumFrames) {
                // EOF!
                break;
            } else if(ioNumFrames < NumFrames) {
                // TODO: pad buf with zeroes out to NumFrames 
            } else {
                 float amp[NumFrames]; // scratch space
                 doFFTReal(buf, amp, NumFrames);
            }
        }
    }

    // later
    status = ExtAudioFileDispose(caf);
    if(noErr != status) { /* hmm */ }
}

这篇关于如何播放和读取.caf PCM音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆