Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Param.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/Buffer.h"
27 
28 #include <list>
29 #include <atomic>
30 #include <functional>
31 
32 namespace cinder { namespace audio {
33 
34 typedef std::shared_ptr<class Context> ContextRef;
35 typedef std::shared_ptr<class Node> NodeRef;
36 
38 typedef std::shared_ptr<class Event> EventRef;
40 typedef std::function<void ( float *, size_t, float, float, const std::pair<float, float>& )> RampFn;
41 
43 void rampLinear( float *array, size_t count, float t, float tIncr, const std::pair<float, float> &valueRange );
45 void rampInQuad( float *array, size_t count, float t, float tIncr, const std::pair<float, float> &valueRange );
47 void rampOutQuad( float *array, size_t count, float t, float tIncr, const std::pair<float, float> &valueRange );
48 
50 class Event {
51  public:
52  float getTimeBegin() const { return mTimeBegin; }
53  float getTimeEnd() const { return mTimeEnd; }
54  float getDuration() const { return mDuration; }
55  float getValueBegin() const { return mValueBegin; }
56  float getValueEnd() const { return mValueEnd; }
57  const RampFn& getRampFn() const { return mRampFn; }
58 
59  void cancel() { mIsCanceled = true; }
60  bool isComplete() const { return mIsComplete; }
61 
62  private:
63  Event( float timeBegin, float timeEnd, float valueBegin, float valueEnd, const RampFn &rampFn );
64 
65  float mTimeBegin, mTimeEnd, mDuration;
66  float mValueBegin, mValueEnd;
67  std::atomic<bool> mIsComplete, mIsCanceled;
68  RampFn mRampFn;
69 
70  friend class Param;
71 };
72 
74 
81 class Param {
82  public:
83 
85  struct Options {
86  Options() : mDelay( 0 ), mRampFn( rampLinear ) {}
87 
89  Options& delay( float delay ) { mDelay = delay; return *this; }
91  Options& rampFn( const RampFn &rampFn ) { mRampFn = rampFn; return *this; }
92 
94  float getDelay() const { return mDelay; }
96  const RampFn& getRampFn() const { return mRampFn; }
97 
98  private:
99  float mDelay;
100  RampFn mRampFn;
101  };
102 
104  Param( Node *parentNode, float initialValue = 0 );
105 
107  void setValue( float value );
109  float getValue() const { return mValue; }
112  const float* getValueArray() const;
113 
115  EventRef applyRamp( float valueEnd, float rampSeconds, const Options &options = Options() );
117  EventRef applyRamp( float valueBegin, float valueEnd, float rampSeconds, const Options &options = Options() );
119  EventRef appendRamp( float valueEnd, float rampSeconds, const Options &options = Options() );
120 
123  void setProcessor( const NodeRef &node );
124 
126  void reset();
128  size_t getNumEvents() const;
129 
133  bool eval();
137  bool eval( float timeBegin, float *array, size_t arrayLength, size_t sampleRate );
138 
140  float findDuration() const;
142  std::pair<float, float> findEndTimeAndValue() const;
143 
144  protected:
145 
146  // non-locking protected methods
147  void initInternalBuffer();
148  void resetImpl();
149  ContextRef getContext() const;
150 
151  std::list<EventRef> mEvents;
152  std::atomic<float> mValue;
156 };
157 
158 } } // namespace cinder::audio
float getValue() const
Returns the current value of the Param.
Definition: Param.h:109
float getTimeEnd() const
Definition: Param.h:53
void rampOutQuad(float *array, size_t count, float t, float tIncr, const std::pair< float, float > &valueRange)
Array-based quadradic (t^2) ease-out ramping function.
Definition: Param.cpp:53
std::pair< float, float > findEndTimeAndValue() const
Returns the end time and value of the latest scheduled Event, or [0, getValue()] if none are schedule...
Definition: Param.cpp:165
std::list< EventRef > mEvents
Definition: Param.h:151
Class representing a sample-accurate parameter control instruction.
Definition: Param.h:50
Options & delay(float delay)
Specifies a delay of delay in seconds.
Definition: Param.h:89
size_t getNumEvents() const
Returns the number of Event's that are currently scheduled.
Definition: Param.cpp:146
void initInternalBuffer()
Definition: Param.cpp:276
float getValueBegin() const
Definition: Param.h:55
void reset()
Resets Param, blowing away any Event's or processing Node.
Definition: Param.cpp:139
void rampLinear(float *array, size_t count, float t, float tIncr, const std::pair< float, float > &valueRange)
Array-based linear ramping function.
Definition: Param.cpp:35
void resetImpl()
Definition: Param.cpp:264
float getValueEnd() const
Definition: Param.h:56
float getTimeBegin() const
Definition: Param.h:52
float findDuration() const
Returns the total duration of any scheduled Event's, including delay, or 0 if none are scheduled...
Definition: Param.cpp:152
const RampFn & getRampFn() const
Returns the ramping function that will be used during evaluation.
Definition: Param.h:96
NodeRef mProcessor
Definition: Param.h:154
std::shared_ptr< class Context > ContextRef
Definition: Node.h:38
const float * getValueArray() const
Definition: Param.cpp:178
void setValue(float value)
Sets the value of the Param, blowing away any scheduled Event's or processing Node.
Definition: Param.cpp:73
float getDuration() const
Definition: Param.h:54
GLuint GLuint GLsizei count
Definition: GLee.h:963
const RampFn & getRampFn() const
Definition: Param.h:57
void rampInQuad(float *array, size_t count, float t, float tIncr, const std::pair< float, float > &valueRange)
Array-based quadradic (t^2) ease-in ramping function.
Definition: Param.cpp:44
Fundamental building block for creating an audio processing graph.
Definition: Node.h:59
GLsizei const GLfloat * value
Definition: GLee.h:2487
std::atomic< float > mValue
Definition: Param.h:152
bool eval()
Definition: Param.cpp:185
Optional parameters when applying or appending ramps.
Definition: Param.h:85
void cancel()
Definition: Param.h:59
std::function< void(float *, size_t, float, float, const std::pair< float, float > &)> RampFn
note: unless we want to add _VARIADIC_MAX=6 in preprocessor definitions to all projects, number of args here has to be 5 or less for vc11 support
Definition: Param.h:40
EventRef applyRamp(float valueEnd, float rampSeconds, const Options &options=Options())
Replaces any existing Event's with a Event from the current value to valueEnd over rampSeconds...
Definition: Param.cpp:81
std::shared_ptr< class Event > EventRef
A Reference to Event's returned by the ramping methods.
Definition: Param.h:38
ContextRef getContext() const
Definition: Param.cpp:282
float getDelay() const
Returns the delay specified in seconds.
Definition: Param.h:94
std::shared_ptr< class Node > NodeRef
Definition: Node.h:39
bool isComplete() const
Definition: Param.h:60
BufferDynamic mInternalBuffer
Definition: Param.h:155
void setProcessor(const NodeRef &node)
Definition: Param.cpp:121
Options & rampFn(const RampFn &rampFn)
Specifies the ramping function used during evaluation.
Definition: Param.h:91
GLdouble GLdouble t
Definition: GLee.h:1426
Node * mParentNode
Definition: Param.h:153
Allows an audio parameter to be controlled over time with sample accuracate curves.
Definition: Param.h:81
EventRef appendRamp(float valueEnd, float rampSeconds, const Options &options=Options())
Appends a Event from the end of the last scheduled Param (or the current time) to valueEnd over rampS...
Definition: Param.cpp:103
Param(Node *parentNode, float initialValue=0)
Constructs a Param with a pointer (weak reference) to the owning parent Node and an optional initialV...
Definition: Param.cpp:68
Options()
Definition: Param.h:86