如何在iOS(iPhone,iPad)项目中使用CARingBuffer类?

时间:2011-08-21 13:58:24

标签: iphone ios ipad audio buffer

我目前正在尝试创建一个iOS音频项目,我需要使用XCode的Extras / CoreAudio / PublicUtility文件夹中提供的CARingBuffer类。 问题是当我在我的viewController的头文件中包含CARingBuffer.h并且我声明了一个CARingBuffer对象时,我收到了4个编译错误。

要重现我的问题,这很简单。只需创建一个基于视图的新应用程序,并尝试在viewController的标题中#include“CARingBuffer.h”。

这是我的testViewController.h的内容:

    #import <UIKit/UIKit.h>

    #include "CARingBuffer.h"

    @interface testViewController : UIViewController {

    }

    @end

这是我的testViewController.m的内容:

    #import "testViewController.h"

    @implementation testViewController

    - (void)dealloc
    {
        [super dealloc];
    }

    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];

        // Release any cached data, images, etc that aren't in use.
    }

    #pragma mark - View lifecycle

    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically                                                         from a nib.
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    */

    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations
        return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

    @end

根据XCode 4,在CARingBuffer中找到4个编译错误(奇怪地):

1)初始化元素不是常量在线:

    const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1;

2)预期';'在顶级声明者之后,在'CARingBuffer'之前预期'='...或'属性':

    class CARingBuffer {

3)初始化元素不是常量在线:

    const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1;

4)预期';'在顶级声明者之后,在'CARingBuffer'之前预期'='...或'属性':

    class CARingBuffer {

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

另请查看此alternative

答案 1 :(得分:0)

您需要将包含环形缓冲区的类重命名为.mm文件。

这告诉编译器使用目标c ++。

答案 2 :(得分:0)

您需要将testViewController.m更改为testViewController.mm,因为CARingBuffer是c ++类。   关于如何使用它,这里是CARingBuffer的一个扩展:CARingBufferEx

//header file
#include "CARingBuffer.h"
class CARingBufferEx : public CARingBuffer {

public:
    CARingBufferEx();
    ~CARingBufferEx();
    CARingBufferError   Store(const AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber);

    CARingBufferError   Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber);

private:
    SInt64 firstInputSampleTime;
    SInt64 firstOutputSampleTime;
    SInt64 offset;

};

//Class 
#include "CARingBufferEx.h"
#include "stdio.h"

CARingBufferEx::CARingBufferEx():firstInputSampleTime(-1), firstOutputSampleTime(-1), offset(0) {
}
CARingBufferEx::~CARingBufferEx() {

}

CARingBufferError CARingBufferEx::Store(const AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber) {
    if (firstInputSampleTime < 0) {
        firstInputSampleTime = frameNumber;
        if (firstOutputSampleTime > 0 && offset == 0) {
            offset = firstInputSampleTime - firstOutputSampleTime;
        }
    }
    return CARingBuffer::Store(abl, nFrames, frameNumber);
}

CARingBufferError CARingBufferEx::Fetch(AudioBufferList *abl, UInt32 nFrames, SampleTime frameNumber) {
    if (firstOutputSampleTime < 0) {
        firstOutputSampleTime = frameNumber;
        if (firstInputSampleTime > 0 && offset == 0) {
            offset = firstInputSampleTime - firstOutputSampleTime;
        }
    }
    return CARingBuffer::Fetch(abl, nFrames, frameNumber + offset);
}

用法:

  CARingBufferEx*         _musicMixerRingBuffer;
  _musicMixerRingBuffer = new CARingBufferEx();
  _musicMixerRingBuffer->Allocate(2, sizeof(AudioUnitSampleType), 1024 * 50);
  //1024 is length for one package. and 50 means this buffer contains 50 packages at most.

  //store 
  //ioData is AudioBufferList ,inTimeStamp is AudioTimeStamp 
  musicMixerRingBuffer->Store(ioData, inNumberFrames, inTimeStamp->mSampleTime);
  //Fetch
  musicMixerRingBuffer->Fetch(ioData, inNumberFrames, inTimeStamp->mSampleTime);