Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Context.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/Node.h"
27 #include "cinder/audio/InputNode.h"
29 
30 #include <mutex>
31 #include <set>
32 
33 namespace cinder { namespace audio {
34 
35 class DeviceManager;
36 
38 
44 class Context : public std::enable_shared_from_this<Context> {
45  public:
46  virtual ~Context();
47 
49  static Context* master();
51  static DeviceManager* deviceManager();
53  static void setMaster( Context *masterContext, DeviceManager *deviceManager );
54 
59 
61  template<typename NodeT>
62  std::shared_ptr<NodeT> makeNode( NodeT *node );
63 
65  virtual void setOutput( const OutputNodeRef &output );
67  virtual const OutputNodeRef& getOutput();
69  std::mutex& getMutex() const { return mMutex; }
70 
72  virtual void enable();
74  virtual void disable();
76  void setEnabled( bool enable = true );
78  bool isEnabled() const { return mEnabled; }
79 
81  virtual void connectionsDidChange( const NodeRef &node ) {}
82 
84  size_t getSampleRate() { return getOutput()->getOutputSampleRate(); }
86  size_t getFramesPerBlock() { return getOutput()->getOutputFramesPerBlock(); }
87 
89  uint64_t getNumProcessedFrames() const { return mNumProcessedFrames; }
91  double getNumProcessedSeconds() { return (double)getNumProcessedFrames() / (double)getSampleRate(); }
92 
94  void initializeNode( const NodeRef &node );
96  void uninitializeNode( const NodeRef &node );
98  void initializeAllNodes();
100  void uninitializeAllNodes();
102  virtual void disconnectAllNodes();
103 
106  void addAutoPulledNode( const NodeRef &node );
109  void removeAutoPulledNode( const NodeRef &node );
111  void postProcess();
112 
115 
116  protected:
117  Context();
118 
119  private:
120  void disconnectRecursive( const NodeRef &node, std::set<NodeRef> &traversedNodes );
121  void initRecursisve( const NodeRef &node, std::set<NodeRef> &traversedNodes );
122  void uninitRecursisve( const NodeRef &node, std::set<NodeRef> &traversedNodes );
123  const std::vector<Node *>& getAutoPulledNodes(); // called if there are any nodes besides output that need to be pulled
124  void processAutoPulledNodes();
125  void incrementFrameCount();
126 
127  static void registerClearStatics();
128 
129  std::atomic<uint64_t> mNumProcessedFrames;
130  OutputNodeRef mOutput;
131 
132  // other nodes that don't have any outputs and need to be explictly pulled
133  std::set<NodeRef> mAutoPulledNodes;
134  std::vector<Node *> mAutoPullCache;
135  bool mAutoPullRequired, mAutoPullCacheDirty;
136  BufferDynamic mAutoPullBuffer;
137 
138  mutable std::mutex mMutex;
139  bool mEnabled;
140 
141  // - Context is stored in Node classes as a weak_ptr, so it needs to (for now) be created as a shared_ptr
142  static std::shared_ptr<Context> sMasterContext;
143  static std::unique_ptr<DeviceManager> sDeviceManager; // TODO: consider turning DeviceManager into a HardwareContext class
144 };
145 
146 template<typename NodeT>
147 std::shared_ptr<NodeT> Context::makeNode( NodeT *node )
148 {
149  std::shared_ptr<NodeT> result( node );
150  result->setContext( shared_from_this() );
151  return result;
152 }
153 
155 inline Context* master() { return Context::master(); }
156 
157 
163  ScopedEnableContext( Context *context, bool enable );
165 private:
166  Context* mContext;
167  bool mWasEnabled;
168 };
169 
170 } } // namespace cinder::audio
static Context * master()
Returns the master Context that manages hardware I/O and real-time processing, which is platform spec...
Definition: Context.cpp:72
void setEnabled(bool enable=true)
start / stop audio processing via boolean
Definition: Context.cpp:154
virtual void disconnectAllNodes()
Disconnect all Node's related by this Context.
Definition: Context.cpp:180
void enable(GLenum state)
Enables the OpenGL State state. Equivalent to calling to glEnable( state );.
Definition: dx.h:198
void removeAutoPulledNode(const NodeRef &node)
Definition: Context.cpp:262
RAII-style utility class to set a Context's enabled state and have it restored at the end of the curr...
Definition: Context.h:159
GLsizei const GLchar ** string
Definition: GLee.h:2427
Context * master()
Returns the master Context that manages hardware I/O and real-time processing, which is platform spec...
Definition: Context.h:155
double getNumProcessedSeconds()
Returns the total number of seconds that have been processed in the dsp loop.
Definition: Context.h:91
Manages the creation, connections, and lifecycle of audio::Node's.
Definition: Context.h:44
std::mutex & getMutex() const
Returns the mutex used to synchronize the audio thread. This is also used internally by the Node clas...
Definition: Context.h:69
Context()
Definition: Context.cpp:118
ScopedEnableContext(Context *context)
Constructs an object that will store context's enabled state and restore it at the end of the current...
Definition: Context.cpp:360
void postProcess()
OutputNode implmenentations should call this at the end of each rendering block.
Definition: Context.cpp:272
std::shared_ptr< NodeT > makeNode(NodeT *node)
Interface for creating new Node's of type NodeT, which are thereafter owned by this Context...
Definition: Context.h:147
std::shared_ptr< class OutputDeviceNode > OutputDeviceNodeRef
Definition: OutputNode.h:32
void uninitializeAllNodes()
Uninitialize all Node's related by this Context.
Definition: Context.cpp:171
virtual void connectionsDidChange(const NodeRef &node)
Called by node when it's connections have changed, default implementation is empty.
Definition: Context.h:81
static DeviceRef getDefaultOutput()
Returns a reference to the default output Device on your system.
Definition: Device.cpp:48
~ScopedEnableContext()
Definition: Context.cpp:377
Platform-specific Singleton for managing hardware devices. Applications normally should not need to u...
Definition: Device.h:110
virtual OutputDeviceNodeRef createOutputDeviceNode(const DeviceRef &device=Device::getDefaultOutput(), const Node::Format &format=Node::Format())=0
Creates and returns a platform-specific OutputDeviceNode, which delivers audio to the hardware output...
size_t getFramesPerBlock()
Returns the number of frames processed in one block by this Node, which is governed by the current Ou...
Definition: Context.h:86
uint64_t getNumProcessedFrames() const
Returns the total number of frames that have been processed in the dsp loop.
Definition: Context.h:89
Definition: Node.h:72
std::shared_ptr< class Device > DeviceRef
Definition: Device.h:36
bool isEnabled() const
Returns whether or not this Context is current enabled and processing audio.
Definition: Context.h:78
virtual InputDeviceNodeRef createInputDeviceNode(const DeviceRef &device=Device::getDefaultInput(), const Node::Format &format=Node::Format())=0
Creates and returns a platform-specific InputDeviceNode, which captures audio from the hardware input...
void addAutoPulledNode(const NodeRef &node)
Definition: Context.cpp:250
static DeviceRef getDefaultInput()
Returns a reference to the default input Device on your system.
Definition: Device.cpp:53
virtual const OutputNodeRef & getOutput()
Returns the OutputNode for the Context (currently always an OutputDeviceNode that sends audio to your...
Definition: Context.cpp:194
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 void enable()
Enables audio processing. Effectively the same as calling getOutput()->enable()
Definition: Context.cpp:130
std::shared_ptr< class Node > NodeRef
Definition: Node.h:39
void uninitializeNode(const NodeRef &node)
Un-initializes node, ensuring that Node::uninitialze() gets called.
Definition: Context.cpp:206
virtual ~Context()
Definition: Context.cpp:123
void initializeNode(const NodeRef &node)
Initializes node, ensuring that Node::initialze() gets called and that its internal buffers are ready...
Definition: Context.cpp:201
std::string printGraphToString()
Returns a string representation of the Node graph for debugging purposes.
Definition: Context.cpp:340
std::shared_ptr< class OutputNode > OutputNodeRef
Definition: OutputNode.h:31
void initializeAllNodes()
Initialize all Node's related by this Context.
Definition: Context.cpp:162
static void setMaster(Context *masterContext, DeviceManager *deviceManager)
Allows the user to set the master Context and DeviceManager, overriding the defaults.
Definition: Context.cpp:112
virtual void setOutput(const OutputNodeRef &output)
Sets the new output of this Context to output. You should do this before making any connections becau...
Definition: Context.cpp:189
size_t getSampleRate()
Returns the samplerate of this Context, which is governed by the current OutputNode.
Definition: Context.h:84
virtual void disable()
Enables audio processing. Effectively the same as calling getOutput()->disable()
Definition: Context.cpp:145
static DeviceManager * deviceManager()
Returns the platform-specific DeviceManager singleton instance, which is platform specific...
Definition: Context.cpp:91