2011-08-21 27 views
1

我正在嘗試創建一個iOS音頻項目,我需要使用XCode的Extras/CoreAudio/PublicUtility文件夾中提供的CARingBuffer類。 問題是當我在viewController的頭文件中包含CARingBuffer.h並且聲明瞭CARingBuffer對象時,我收到4個編譯錯誤。如何在iOS(iPhone,iPad)項目中使用CARingBuffer類?

重現我的問題很簡單。只需創建一個基於視圖的新應用程序,並在viewController的頭文件中#include「CARingBuffer.h」。

這裏是我的testViewController.h的內容:根據在CARingBuffer

#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 

婁位於(奇怪)4個編譯錯誤:

#import <UIKit/UIKit.h> 

    #include "CARingBuffer.h" 

    @interface testViewController : UIViewController { 

    } 

    @end 

這裏是我的testViewController.m的內容XCode 4:

1)初始化元素不是線上常量:

const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1; 

2)預期';'後頂層聲明符,應爲 '=' ...或 '屬性附加傷害' 'CARingBuffer' 之前:

class CARingBuffer { 

3)初始化器元件是不恆定的上一行:

const UInt32 kGeneralRingTimeBoundsQueueMask = kGeneralRingTimeBoundsQueueSize - 1; 

4)預期';'之後頂層聲明符,預計 '=' ...或 'CARingBuffer' 前 '屬性附加傷害':

class CARingBuffer { 

在此先感謝您的幫助。

回答

0

您需要將包含環形緩衝區的類重新命名爲.mm文件。

這告訴編譯器使用目標C++。

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); 
相關問題