Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
WaveTable.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 
27 #include "cinder/audio/Buffer.h"
28 
29 #include <vector>
30 #include <tuple>
31 
32 namespace cinder { namespace audio {
33 
34 typedef std::shared_ptr<class WaveTable> WaveTableRef;
35 typedef std::shared_ptr<class WaveTable2d> WaveTable2dRef;
36 
38 class WaveTable {
39  public:
40  WaveTable( size_t mSampleRate, size_t tableSize );
41 
42  void resize( size_t tableSize );
43 
44  void fillSine();
45 
47  void setSampleRate( size_t sampleRate );
48  size_t getSampleRate() const { return mSampleRate; }
49 
50  size_t getTableSize() const { return mTableSize; }
51 
52  float lookup( float phase ) const;
53  float lookup( float *outputArray, size_t outputLength, float currentPhase, float freq ) const;
54  float lookup( float *outputArray, size_t outputLength, float currentPhase, const float *freqArray ) const;
55 
56  void copyTo( float *array ) const;
57  void copyFrom( const float *array );
58 
59  protected:
60  void fillSinesum( float *array, size_t length, const std::vector<float> &partialCoeffs );
61 
65 };
66 
68 class WaveTable2d : public WaveTable {
69  public:
70  WaveTable2d( size_t sampleRate, size_t tableSize, size_t numTables );
71 
74  void resize( size_t tableSize, size_t numTables );
75 
77 
78  float lookupBandlimited( float phase, float f0 ) const;
79  float lookupBandlimited( float *outputArray, size_t outputLength, float currentPhase, float f0 ) const;
80  float lookupBandlimited( float *outputArray, size_t outputLength, float currentPhase, const float *f0Array ) const;
81 
82  void copyTo( float *array, size_t tableIndex ) const;
83  void copyFrom( const float *array, size_t tableIndex );
84 
85  float calcBandlimitedTableIndex( float f0 ) const;
86 
87  size_t getNumTables() const { return mNumTables; }
88 
89  protected:
90  void calcLimits();
91  void fillBandLimitedTable( WaveformType type, float *table, size_t numPartials );
92  size_t getMaxHarmonicsForTable( size_t tableIndex ) const;
93 
94  const float* getBandLimitedTable( float f0 ) const;
95  std::tuple<const float*, const float*, float> getBandLimitedTablesLerp( float f0 ) const;
96 
97  size_t mNumTables;
99 };
100 
101 } } // namespace cinder::audio
void fillBandlimited(WaveformType type)
Definition: WaveTable.cpp:203
void copyFrom(const float *array, size_t tableIndex)
Definition: WaveTable.cpp:416
size_t mSampleRate
Definition: WaveTable.h:62
float calcBandlimitedTableIndex(float f0) const
Definition: WaveTable.cpp:280
void copyTo(float *array, size_t tableIndex) const
Definition: WaveTable.cpp:409
size_t getTableSize() const
Definition: WaveTable.h:50
Manages an array of tables that is used for bandlimited wavetable synthesis. Supports table lookup wi...
Definition: WaveTable.h:68
void fillSinesum(float *array, size_t length, const std::vector< float > &partialCoeffs)
Definition: WaveTable.cpp:117
void copyTo(float *array) const
Definition: WaveTable.cpp:168
void resize(size_t tableSize, size_t numTables)
Definition: WaveTable.cpp:187
GLenum GLsizei GLenum GLenum const GLvoid * table
Definition: GLee.h:969
WaveTable2d(size_t sampleRate, size_t tableSize, size_t numTables)
Definition: WaveTable.cpp:182
const float * getBandLimitedTable(float f0) const
Definition: WaveTable.cpp:292
void resize(size_t tableSize)
Definition: WaveTable.cpp:100
void calcLimits()
Definition: WaveTable.cpp:423
float mMaxMidiRange
Definition: WaveTable.h:98
WaveTable(size_t mSampleRate, size_t tableSize)
Definition: WaveTable.cpp:86
size_t getNumTables() const
Definition: WaveTable.h:87
float mMinMidiRange
Definition: WaveTable.h:98
void fillSine()
Definition: WaveTable.cpp:109
void fillBandLimitedTable(WaveformType type, float *table, size_t numPartials)
Definition: WaveTable.cpp:226
GLuint GLsizei GLsizei * length
Definition: GLee.h:2313
size_t mNumTables
Definition: WaveTable.h:97
Manages a table that is used for wavetable synthesis. Supports table lookup with linear interpolation...
Definition: WaveTable.h:38
std::shared_ptr< class WaveTable2d > WaveTable2dRef
Definition: WaveTable.h:35
float lookupBandlimited(float phase, float f0) const
Definition: WaveTable.cpp:328
GLuint GLuint GLsizei GLenum type
Definition: GLee.h:963
BufferDynamic mBuffer
Definition: WaveTable.h:64
size_t getMaxHarmonicsForTable(size_t tableIndex) const
Definition: WaveTable.cpp:269
WaveformType
Identifiers for the classic waveform types.
Definition: WaveformType.h:29
void copyFrom(const float *array)
Definition: WaveTable.cpp:173
std::shared_ptr< class WaveTable > WaveTableRef
Definition: WaveTable.h:34
float mSamplePeriod
Definition: WaveTable.h:63
float lookup(float phase) const
Definition: WaveTable.cpp:135
size_t mTableSize
Definition: WaveTable.h:62
std::tuple< const float *, const float *, float > getBandLimitedTablesLerp(float f0) const
Definition: WaveTable.cpp:298
size_t getSampleRate() const
Definition: WaveTable.h:48
void setSampleRate(size_t sampleRate)
Does not update data, lookup will be inaccurate until next fill.
Definition: WaveTable.cpp:92