Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MonitorNode.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"
27 #include "cinder/audio/dsp/Dsp.h"
29 
30 #include "cinder/Thread.h"
31 
32 namespace cinder { namespace audio {
33 
34 namespace dsp {
35  class Fft;
36 }
37 
38 typedef std::shared_ptr<class MonitorNode> MonitorNodeRef;
39 typedef std::shared_ptr<class MonitorSpectralNode> MonitorSpectralNodeRef;
40 
50 class MonitorNode : public NodeAutoPullable {
51  public:
52  struct Format : public Node::Format {
53  Format() : mWindowSize( 0 ) {}
54 
57  Format& windowSize( size_t size ) { mWindowSize = size; return *this; }
59  size_t getWindowSize() const { return mWindowSize; }
60 
61  // reimpl Node::Format
62  Format& channels( size_t ch ) { Node::Format::channels( ch ); return *this; }
64  Format& autoEnable( bool autoEnable = true ) { Node::Format::autoEnable( autoEnable ); return *this; }
65 
66  protected:
67  size_t mWindowSize;
68  };
69 
70  MonitorNode( const Format &format = Format() );
71  virtual ~MonitorNode();
72 
75  const Buffer& getBuffer();
77  size_t getWindowSize() const { return mWindowSize; }
79  float getVolume();
81  float getVolume( size_t channel );
82 
83  protected:
84  void initialize() override;
85  void process( Buffer *buffer ) override;
86 
88  void fillCopiedBuffer();
89 
90  std::vector<dsp::RingBuffer> mRingBuffers; // one per channel
91  Buffer mCopiedBuffer; // used to safely read audio frames on a non-audio thread
92  size_t mWindowSize;
94 };
95 
98  public:
99  struct Format : public MonitorNode::Format {
100  Format() : MonitorNode::Format(), mFftSize( 0 ), mWindowType( dsp::WindowType::BLACKMAN ) {}
101 
104  Format& fftSize( size_t size ) { mFftSize = size; return *this; }
108  Format& windowSize( size_t size ) { MonitorNode::Format::windowSize( size ); return *this; }
109 
110  size_t getFftSize() const { return mFftSize; }
112 
113  // reimpl Node::Format
114  Format& channels( size_t ch ) { Node::Format::channels( ch ); return *this; }
116  Format& autoEnable( bool autoEnable = true ) { Node::Format::autoEnable( autoEnable ); return *this; }
117 
118  protected:
119  size_t mFftSize;
121  };
122 
123  MonitorSpectralNode( const Format &format = Format() );
124  virtual ~MonitorSpectralNode();
125 
127  const std::vector<float>& getMagSpectrum();
129  size_t getNumBins() const { return mFftSize / 2; }
131  size_t getFftSize() const { return mFftSize; }
133  float getFreqForBin( size_t bin );
135  float getSmoothingFactor() const { return mSmoothingFactor; }
137  void setSmoothingFactor( float factor );
138 
139  protected:
140  void initialize() override;
141 
142  private:
143  std::unique_ptr<dsp::Fft> mFft;
144  Buffer mFftBuffer; // windowed samples before transform
145  BufferSpectral mBufferSpectral; // transformed samples
146  std::vector<float> mMagSpectrum; // computed magnitude spectrum from frequency-domain samples
147  AlignedArrayPtr mWindowingTable;
148  size_t mFftSize;
149  dsp::WindowType mWindowType;
150  float mSmoothingFactor;
151 };
152 
153 } } // namespace cinder::audio
Format & autoEnable(bool autoEnable=true)
Definition: MonitorNode.h:64
MonitorSpectralNode(const Format &format=Format())
Definition: MonitorNode.cpp:100
size_t mFftSize
Definition: MonitorNode.h:119
size_t getNumBins() const
Returns the number of frequency bins in the analyzed magnitude spectrum. Equivilant to fftSize / 2...
Definition: MonitorNode.h:129
GLenum mode
Definition: GLee.h:3042
ChannelMode
Definition: Node.h:63
Format & channels(size_t ch)
Definition: MonitorNode.h:114
void fillCopiedBuffer()
Copies audio frames from the RingBuffer into mCopiedBuffer, which is suitable for operation on the ma...
Definition: MonitorNode.cpp:88
Format & windowType(dsp::WindowType type)
defaults to WindowType::BLACKMAN
Definition: MonitorNode.h:106
Format & windowSize(size_t size)
Definition: MonitorNode.h:108
size_t mRingBufferPaddingFactor
Definition: MonitorNode.h:93
Format & channelMode(ChannelMode mode)
Definition: MonitorNode.h:63
Node for retrieving time-domain audio PCM samples.
Definition: MonitorNode.h:50
size_t mWindowSize
Definition: MonitorNode.h:67
Format & channels(size_t ch)
Definition: MonitorNode.h:62
const Buffer & getBuffer()
Definition: MonitorNode.cpp:70
std::shared_ptr< class MonitorSpectralNode > MonitorSpectralNodeRef
Definition: MonitorNode.h:39
const std::vector< float > & getMagSpectrum()
Returns the magnitude spectrum of the currently sampled audio stream, suitable for consuming on the m...
Definition: MonitorNode.cpp:134
void setSmoothingFactor(float factor)
Sets the factor (0 - 1, default = 0.5) used when smoothing the magnitude spectrum between sequential ...
Definition: MonitorNode.cpp:172
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: MonitorNode.cpp:61
size_t mWindowSize
Definition: MonitorNode.h:92
float getSmoothingFactor() const
Returns the factor (0 - 1, default = 0.5) used when smoothing the magnitude spectrum between sequenti...
Definition: MonitorNode.h:135
Format & autoEnable(bool autoEnable=true)
Definition: MonitorNode.h:116
MonitorNode(const Format &format=Format())
Definition: MonitorNode.cpp:39
Format & channels(size_t ch)
Sets the number of channels for the Node.
Definition: Node.h:76
Format & fftSize(size_t size)
Definition: MonitorNode.h:104
Format()
Definition: MonitorNode.h:53
size_t getFftSize() const
Definition: MonitorNode.h:110
Format & channelMode(ChannelMode mode)
Definition: MonitorNode.h:115
Format & channelMode(ChannelMode mode)
Controls how channels will be matched between connected Node's, if necessary.
Definition: Node.h:78
virtual ~MonitorNode()
Definition: MonitorNode.cpp:44
GLuint buffer
Definition: GLee.h:2065
Buffer mCopiedBuffer
Definition: MonitorNode.h:91
float getFreqForBin(size_t bin)
Returns the corresponding frequency for bin. Computed as.
Definition: MonitorNode.cpp:177
dsp::WindowType mWindowType
Definition: MonitorNode.h:120
std::shared_ptr< class MonitorNode > MonitorNodeRef
Definition: MonitorNode.h:38
std::unique_ptr< float, FreeDeleter< float > > AlignedArrayPtr
Definition: Buffer.h:279
size_t getWindowSize() const
Returns the window size, which is the number of samples that are copied from the audio stream...
Definition: MonitorNode.h:77
Format()
Definition: MonitorNode.h:100
virtual ~MonitorSpectralNode()
Definition: MonitorNode.cpp:105
Definition: Node.h:72
a Node that can be pulled without being connected to any outputs.
Definition: Node.h:242
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: MonitorNode.cpp:48
GLuint GLuint GLsizei GLenum type
Definition: GLee.h:963
int int channel
Definition: GLee.h:17150
A Scope that performs spectral (Fourier) analysis.
Definition: MonitorNode.h:97
size_t getFftSize() const
Returns the size of the FFT used for spectral analysis.
Definition: MonitorNode.h:131
std::vector< dsp::RingBuffer > mRingBuffers
Definition: MonitorNode.h:90
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: MonitorNode.cpp:109
GLenum GLsizei GLenum format
Definition: GLee.h:969
Definition: MonitorNode.h:99
Format & autoEnable(bool autoEnable=true)
Whether or not the Node will be auto-enabled when connection changes occur. Default is true for base ...
Definition: Node.h:80
float getVolume()
Compute the average (RMS) volume across all channels.
Definition: MonitorNode.cpp:76
dsp::WindowType getWindowType() const
Definition: MonitorNode.h:111
size_t getWindowSize() const
Returns the window size.
Definition: MonitorNode.h:59
Format & windowSize(size_t size)
Definition: MonitorNode.h:57
Definition: MonitorNode.h:52
GLsizeiptr size
Definition: GLee.h:2089
WindowType
Describes the avaiable windowing functions.
Definition: Dsp.h:49