Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
GenNode.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/InputNode.h"
27 #include "cinder/audio/WaveTable.h"
28 
29 namespace cinder { namespace audio {
30 
32 typedef std::shared_ptr<class GenNode> GenNodeRef;
33 // Typedefs for all available GenNode types.
34 typedef std::shared_ptr<class GenNoiseNode> GenNoiseNodeRef;
35 typedef std::shared_ptr<class GenPhasorNode> GenPhasorNodeRef;
36 typedef std::shared_ptr<class GenSineNode> GenSineNodeRef;
37 typedef std::shared_ptr<class GenTriangleNode> GenTriangleNodeRef;
38 typedef std::shared_ptr<class GenTableNode> GenTableNodeRef;
39 typedef std::shared_ptr<class GenOscNode> GenOscNodeRef;
40 typedef std::shared_ptr<class GenPulseNode> GenPulseNodeRef;
41 
43 class GenNode : public InputNode {
44  public:
46  void setFreq( float freq ) { mFreq.setValue( freq ); }
48  float getFreq() const { return mFreq.getValue(); }
50  Param* getParamFreq() { return &mFreq; }
51 
52  protected:
53  GenNode( const Format &format = Format() );
54  GenNode( float freq, const Format &format = Format() );
55 
56  void initialize() override;
57  void initImpl();
58 
60 
62  float mPhase;
63 };
64 
66 class GenNoiseNode : public GenNode {
67  public:
70 
71  protected:
72  void process( Buffer *buffer ) override;
73 };
74 
76 class GenPhasorNode : public GenNode {
77  public:
80  GenPhasorNode( float freq, const Format &format = Format() ) : GenNode( freq, format ) {}
81 
82  protected:
83  void process( Buffer *buffer ) override;
84 };
85 
87 class GenSineNode : public GenNode {
88  public:
89  GenSineNode( const Format &format = Format() ) : GenNode( format ) {}
90  GenSineNode( float freq, const Format &format = Format() ) : GenNode( freq, format ) {}
91 
92  protected:
93  void process( Buffer *buffer ) override;
94 };
95 
97 class GenTriangleNode : public GenNode {
98  public:
99  GenTriangleNode( const Format &format = Format() );
100  GenTriangleNode( float freq, const Format &format = Format() );
101 
102  void setUpSlope( float up ) { mUpSlope = up; }
103  void setDownSlope( float down ) { mDownSlope = down; }
104 
105  float getUpSlope() const { return mUpSlope; }
106  float getDownSlope() const { return mDownSlope; }
107 
108  protected:
109  void process( Buffer *buffer ) override;
110 
111  private:
112  std::atomic<float> mUpSlope, mDownSlope;
113 };
114 
116 class GenTableNode : public GenNode {
117  public:
119  GenTableNode( float freq, const Format &format = Format() ) : GenNode( freq, format ) {}
120 
122  void setWaveTable( const WaveTableRef &waveTable ) { mWaveTable = waveTable; }
124  const WaveTableRef& getWaveTable() { return mWaveTable; }
125 
126  protected:
127  void initialize() override;
128  void process( Buffer *buffer ) override;
129 
131 };
132 
134 class GenOscNode : public GenNode {
135  public:
136  GenOscNode( const Format &format = Format() );
137  GenOscNode( float freq, const Format &format = Format() );
138  GenOscNode( WaveformType waveformType, float freq = 0, const Format &format = Format() );
139 
141  void setWaveform( WaveformType waveformType );
143  void setWaveTable( const WaveTable2dRef &waveTable ) { mWaveTable = waveTable; }
145  const WaveTable2dRef getWaveTable() const { return mWaveTable; }
146 
150  size_t getTableSize() const { return mWaveTable->getTableSize(); }
151 
152  protected:
153  void initialize() override;
154  void process( Buffer *buffer ) override;
155 
158 };
159 
161 class GenPulseNode : public GenNode {
162  public:
163  GenPulseNode( const Format &format = Format() );
164  GenPulseNode( float freq, const Format &format = Format() );
165 
167  void setWidth( float width ) { mWidth.setValue( width ); }
169  float getWidth() const { return mWidth.getValue(); }
171  Param* getParamWidth() { return &mWidth; }
172 
173  protected:
174  void initialize() override;
175  void process( Buffer *buffer ) override;
176 
180 };
181 
182 } } // namespace cinder::audio
float getValue() const
Returns the current value of the Param.
Definition: Param.h:109
void setWaveform(WaveformType waveformType)
Sets the WaveformType of the internal wavetable. This can be a heavy operation and requires thread sy...
Definition: GenNode.cpp:245
void setFreq(float freq)
Sets the frequency in hertz to a constant value of freq.
Definition: GenNode.h:46
GenOscNode(const Format &format=Format())
Definition: GenNode.cpp:213
Base class for InputNode's that generate audio samples. Gen's are always mono channel.
Definition: GenNode.h:43
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: GenNode.cpp:161
WaveformType getWaveForm() const
Returns the current WaveformType.
Definition: GenNode.h:148
WaveTable2dRef mWaveTable
Definition: GenNode.h:177
GenPulseNode(const Format &format=Format())
Definition: GenNode.cpp:272
GLenum GLsizei width
Definition: GLee.h:969
const WaveTable2dRef getWaveTable() const
Returns a reference to the current wavetable.
Definition: GenNode.h:145
GenNoiseNode(const Format &format=Format())
Constructs a GenNoiseNode with optional format.
Definition: GenNode.h:69
Phase generator, i.e. ramping waveform that runs from 0 to 1.
Definition: GenNode.h:76
Pulse waveform generator with variable pulse width. Based on wavetable lookup of two band-limited saw...
Definition: GenNode.h:161
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: GenNode.cpp:111
float getFreq() const
Returns the current frequency in hertz.
Definition: GenNode.h:48
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: GenNode.cpp:228
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: GenNode.cpp:191
GenPhasorNode(float freq, const Format &format=Format())
Definition: GenNode.h:80
GenNode(const Format &format=Format())
Definition: GenNode.cpp:42
Noise generator.
Definition: GenNode.h:66
std::shared_ptr< class GenPulseNode > GenPulseNodeRef
Definition: GenNode.h:40
std::shared_ptr< class GenOscNode > GenOscNodeRef
Definition: GenNode.h:39
void setValue(float value)
Sets the value of the Param, blowing away any scheduled Event's or processing Node.
Definition: Param.cpp:73
Sine waveform generator.
Definition: GenNode.h:87
GLuint buffer
Definition: GLee.h:2065
Triangle waveform generator.
Definition: GenNode.h:97
BufferDynamic mBuffer2
Definition: GenNode.h:178
void setWaveTable(const WaveTable2dRef &waveTable)
Assigns waveTable as the internal wavetable. This allows one to share a WaveTable2d across multiple N...
Definition: GenNode.h:143
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: GenNode.cpp:301
void setUpSlope(float up)
Definition: GenNode.h:102
float getUpSlope() const
Definition: GenNode.h:105
Param mWidth
Definition: GenNode.h:179
Definition: Node.h:72
void setWaveTable(const WaveTableRef &waveTable)
Assigns waveTable as the internal wavetable. This allows one to share a WaveTable across multiple Nod...
Definition: GenNode.h:122
GenTriangleNode(const Format &format=Format())
Definition: GenNode.cpp:140
std::shared_ptr< class GenPhasorNode > GenPhasorNodeRef
Definition: GenNode.h:35
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: GenNode.cpp:282
std::shared_ptr< class WaveTable2d > WaveTable2dRef
Definition: WaveTable.h:35
void setWidth(float width)
Set the pulse width (aka 'duty cycle'). Expected range is between 0:1.
Definition: GenNode.h:167
Basic table-lookup oscillator.
Definition: GenNode.h:116
std::shared_ptr< class GenSineNode > GenSineNodeRef
Definition: GenNode.h:36
Param * getParamWidth()
Returns the Param associated with the width (aka 'duty cycle'). Expected range is between [0:1]...
Definition: GenNode.h:171
GenPhasorNode(const Format &format=Format())
Constructs a GenPhasorNode with optional format.
Definition: GenNode.h:79
InputNode is the base class for Node's that produce audio. It cannot have any inputs.
Definition: InputNode.h:39
std::shared_ptr< class GenNoiseNode > GenNoiseNodeRef
Definition: GenNode.h:34
WaveformType
Identifiers for the classic waveform types.
Definition: WaveformType.h:29
GenTableNode(float freq, const Format &format=Format())
Definition: GenNode.h:119
float mSamplePeriod
Definition: GenNode.h:59
Param * getParamFreq()
Returns a pointer to the Param, which can be used to animate the frequency.
Definition: GenNode.h:50
std::shared_ptr< class WaveTable > WaveTableRef
Definition: WaveTable.h:34
void setDownSlope(float down)
Definition: GenNode.h:103
WaveTable2dRef mWaveTable
Definition: GenNode.h:156
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: GenNode.cpp:69
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: GenNode.cpp:201
GLenum GLsizei GLenum format
Definition: GLee.h:969
size_t getTableSize() const
Returns the size of the owned WaveTable2d.
Definition: GenNode.h:150
std::shared_ptr< class GenTableNode > GenTableNodeRef
Definition: GenNode.h:38
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: GenNode.cpp:82
float getDownSlope() const
Definition: GenNode.h:106
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: GenNode.cpp:60
General purpose, band-limited oscillator using wavetable lookup.
Definition: GenNode.h:134
float getWidth() const
Get the current pulse width.
Definition: GenNode.h:169
void initImpl()
Definition: GenNode.cpp:54
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: GenNode.cpp:260
float mPhase
Definition: GenNode.h:62
std::shared_ptr< class GenNode > GenNodeRef
Typedef for the base GenNode. If all you need to set on the GenNode is the frequency, you can reference the Node with this.
Definition: GenNode.h:32
GenSineNode(float freq, const Format &format=Format())
Definition: GenNode.h:90
Allows an audio parameter to be controlled over time with sample accuracate curves.
Definition: Param.h:81
WaveformType mWaveformType
Definition: GenNode.h:157
Param mFreq
Definition: GenNode.h:61
WaveTableRef mWaveTable
Definition: GenNode.h:130
const WaveTableRef & getWaveTable()
Returns a reference to the current wavetable.
Definition: GenNode.h:124
GenTableNode(const Format &format=Format())
Definition: GenNode.h:118
std::shared_ptr< class GenTriangleNode > GenTriangleNodeRef
Definition: GenNode.h:37
GenSineNode(const Format &format=Format())
Definition: GenNode.h:89