Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ContextAudioUnit.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014, The Cinder Project
3 
4  This code is intended to be used with the Cinder C++ library, http://libcinder.org
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
7  the following conditions are met:
8 
9  * Redistributions of source code must retain the above copyright notice, this list of conditions and
10  the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12  the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
15  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
18  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  POSSIBILITY OF SUCH DAMAGE.
22 */
23 
24 #pragma once
25 
26 #include "cinder/audio/Context.h"
29 
30 #include <AudioUnit/AudioUnit.h>
31 
32 namespace cinder { namespace audio { namespace cocoa {
33 
34 class DeviceAudioUnit;
35 class ContextAudioUnit;
36 
38  public:
39  NodeAudioUnit() : mAudioUnit( nullptr ), mOwnsAudioUnit( true ) {}
40  virtual ~NodeAudioUnit();
41 
42  virtual ::AudioUnit getAudioUnit() const { return mAudioUnit; }
43 
44  protected:
45  void initAu();
46  void uninitAu();
47 
48  ::AudioUnit mAudioUnit;
51 
52  struct RenderData {
53  RenderData() : node( nullptr ), context( nullptr ) {}
54  ~RenderData() { node = nullptr; context = nullptr; }
55 
58  } mRenderData;
59 };
60 
62  public:
63  OutputDeviceNodeAudioUnit( const DeviceRef &device, const Format &format = Format() );
64  virtual ~OutputDeviceNodeAudioUnit() = default;
65 
66  void enableProcessing() override;
67  void disableProcessing() override;
68 
69  protected:
70  void initialize() override;
71  void uninitialize() override;
72  bool supportsProcessInPlace() const override { return false; }
73 
74  private:
75  static OSStatus renderCallback( void *data, ::AudioUnitRenderActionFlags *flags, const ::AudioTimeStamp *timeStamp, UInt32 busNumber, UInt32 numFrames, ::AudioBufferList *bufferList );
76 
77  bool mSynchronousIO;
78 
80 };
81 
83  public:
84  InputDeviceNodeAudioUnit( const DeviceRef &device, const Format &format = Format() );
85  virtual ~InputDeviceNodeAudioUnit();
86 
87  void enableProcessing() override;
88  void disableProcessing() override;
89 
90  protected:
91  void initialize() override;
92  void uninitialize() override;
93  void process( Buffer *buffer ) override;
94 
95  private:
96  static OSStatus inputCallback( void *data, ::AudioUnitRenderActionFlags *flags, const ::AudioTimeStamp *timeStamp, UInt32 bus, UInt32 numFrames, ::AudioBufferList *bufferList );
97 
98  dsp::RingBuffer mRingBuffer;
99  size_t mRingBufferPaddingFactor;
100  AudioBufferListPtr mBufferList;
101  bool mSynchronousIO;
102 
103 };
104 
105 // TODO: when stopped / mEnabled = false; kAudioUnitProperty_BypassEffect should be used
106 class EffectAudioUnitNode : public Node, public NodeAudioUnit {
107  public:
108  EffectAudioUnitNode( UInt32 subType, const Format &format = Format() );
109  virtual ~EffectAudioUnitNode();
110 
111  void setParameter( ::AudioUnitParameterID paramId, float val );
112 
113  protected:
114  void initialize() override;
115  void uninitialize() override;
116  void process( Buffer *buffer ) override;
117 
118  private:
119  static OSStatus renderCallback( void *data, ::AudioUnitRenderActionFlags *flags, const ::AudioTimeStamp *timeStamp, UInt32 busNumber, UInt32 numFrames, ::AudioBufferList *bufferList );
120 
121  UInt32 mEffectSubType;
122  AudioBufferListPtr mBufferList;
123 };
124 
125 class ContextAudioUnit : public Context {
126  public:
127  virtual ~ContextAudioUnit();
128 
129  virtual OutputDeviceNodeRef createOutputDeviceNode( const DeviceRef &device, const Node::Format &format = Node::Format() ) override;
130  virtual InputDeviceNodeRef createInputDeviceNode( const DeviceRef &device, const Node::Format &format = Node::Format() ) override;
131 
133  void setCurrentTimeStamp( const ::AudioTimeStamp *timeStamp ) { mCurrentTimeStamp = timeStamp; }
135  const ::AudioTimeStamp* getCurrentTimeStamp() { return mCurrentTimeStamp; }
136 
137  private:
138 
139  const ::AudioTimeStamp *mCurrentTimeStamp;
140 };
141 
142 } } } // namespace cinder::audio::cocoa
ContextAudioUnit * context
Definition: ContextAudioUnit.h:57
void setParameter(::AudioUnitParameterID paramId, float val)
Definition: ContextAudioUnit.cpp:382
void disableProcessing() override
Callled when this Node should disable processing. Initiated from Node::disable(). ...
Definition: ContextAudioUnit.cpp:119
virtual ::AudioUnit getAudioUnit() const
Definition: ContextAudioUnit.h:42
InputDeviceNodeAudioUnit(const DeviceRef &device, const Format &format=Format())
Definition: ContextAudioUnit.cpp:166
void enableProcessing() override
Callled when this Node should enable processing. Initiated from Node::enable().
Definition: ContextAudioUnit.cpp:113
OutputDeviceNodeAudioUnit(const DeviceRef &device, const Format &format=Format())
Definition: ContextAudioUnit.cpp:71
virtual OutputDeviceNodeRef createOutputDeviceNode(const DeviceRef &device, const Node::Format &format=Node::Format()) override
Creates and returns a platform-specific OutputDeviceNode, which delivers audio to the hardware output...
Definition: ContextAudioUnit.cpp:391
bool mOwnsAudioUnit
Definition: ContextAudioUnit.h:49
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: ContextAudioUnit.cpp:274
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: GLee.h:1011
Ringbuffer (aka circular buffer) data structure for use in concurrent audio scenarios.
Definition: RingBuffer.h:42
Manages the creation, connections, and lifecycle of audio::Node's.
Definition: Context.h:44
Definition: ContextAudioUnit.h:52
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: ContextAudioUnit.cpp:332
void disableProcessing() override
Callled when this Node should disable processing. Initiated from Node::disable(). ...
Definition: ContextAudioUnit.cpp:266
RenderData()
Definition: ContextAudioUnit.h:53
Buffer * mProcessBuffer
Definition: ContextAudioUnit.h:50
std::shared_ptr< class OutputDeviceNode > OutputDeviceNodeRef
Definition: OutputNode.h:32
GLuint GLfloat * val
Definition: GLee.h:14636
Definition: ContextAudioUnit.h:125
Definition: ContextAudioUnit.h:37
Interface representing a Node that communicates with a hardware input device. This is typically a mic...
Definition: InputNode.h:54
~RenderData()
Definition: ContextAudioUnit.h:54
GLuint buffer
Definition: GLee.h:2065
Definition: ContextAudioUnit.h:82
void uninitAu()
Definition: ContextAudioUnit.cpp:61
const ::AudioTimeStamp * getCurrentTimeStamp()
all other NodeAudioUnit's need to pass this correctly formatted timestamp to AudioUnitRender ...
Definition: ContextAudioUnit.h:135
void uninitialize() override
Called once the contents of initialize are no longer relevant, i.e. connections have changed...
Definition: ContextAudioUnit.cpp:251
void initAu()
Definition: ContextAudioUnit.cpp:55
Interface representing a Node that communicates with a hardware output device. This is typically spea...
Definition: OutputNode.h:71
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: ContextAudioUnit.cpp:175
::AudioUnit mAudioUnit
Definition: ContextAudioUnit.h:48
Definition: Node.h:72
std::shared_ptr< class Device > DeviceRef
Definition: Device.h:36
virtual ~ContextAudioUnit()
Definition: ContextAudioUnit.cpp:401
Definition: ContextAudioUnit.h:106
void uninitialize() override
Called once the contents of initialize are no longer relevant, i.e. connections have changed...
Definition: ContextAudioUnit.cpp:355
Fundamental building block for creating an audio processing graph.
Definition: Node.h:59
std::unique_ptr<::AudioBufferList, AudioBufferListDeleter > AudioBufferListPtr
Definition: CinderCoreAudio.h:55
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: ContextAudioUnit.cpp:77
Definition: ContextAudioUnit.h:61
virtual InputDeviceNodeRef createInputDeviceNode(const DeviceRef &device, const Node::Format &format=Node::Format()) override
Creates and returns a platform-specific InputDeviceNode, which captures audio from the hardware input...
Definition: ContextAudioUnit.cpp:396
void uninitialize() override
Called once the contents of initialize are no longer relevant, i.e. connections have changed...
Definition: ContextAudioUnit.cpp:108
virtual ~EffectAudioUnitNode()
Definition: ContextAudioUnit.cpp:328
void setCurrentTimeStamp(const ::AudioTimeStamp *timeStamp)
set by the OutputNode
Definition: ContextAudioUnit.h:133
struct cinder::audio::cocoa::NodeAudioUnit::RenderData mRenderData
Node * node
Definition: ContextAudioUnit.h:56
GLXContext context
Definition: GLee.h:16985
std::shared_ptr< class InputDeviceNode > InputDeviceNodeRef
Definition: InputNode.h:33
GLenum GLsizei GLenum format
Definition: GLee.h:969
virtual ~InputDeviceNodeAudioUnit()
Definition: ContextAudioUnit.cpp:171
EffectAudioUnitNode(UInt32 subType, const Format &format=Format())
Definition: ContextAudioUnit.cpp:323
void enableProcessing() override
Callled when this Node should enable processing. Initiated from Node::enable().
Definition: ContextAudioUnit.cpp:258
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: ContextAudioUnit.cpp:361
bool supportsProcessInPlace() const override
Default implementation returns true, subclasses should return false if they must process out-of-place...
Definition: ContextAudioUnit.h:72
virtual ~NodeAudioUnit()
Definition: ContextAudioUnit.cpp:47
NodeAudioUnit()
Definition: ContextAudioUnit.h:39