Updated SWH plugins and added missing hermes_filter plugin
[lmms/mlankhorst.git] / include / Oscillator.h
blobedf7c0341f5477a14c44076746052dd3f7d42bec
1 /*
2 * Oscillator.h - declaration of class Oscillator
4 * Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
6 * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program (see COPYING); if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
25 #ifndef _OSCILLATOR_H
26 #define _OSCILLATOR_H
28 #include "lmmsconfig.h"
30 #include <math.h>
32 #ifdef LMMS_HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
36 #include "sample_buffer.h"
37 #include "lmms_constants.h"
40 class sampleBuffer;
41 class IntModel;
44 class EXPORT Oscillator
46 public:
47 enum WaveShapes
49 SineWave,
50 TriangleWave,
51 SawWave,
52 SquareWave,
53 MoogSawWave,
54 ExponentialWave,
55 WhiteNoise,
56 UserDefinedWave,
57 NumWaveShapes
58 } ;
60 enum ModulationAlgos
62 PhaseModulation,
63 AmplitudeModulation,
64 SignalMix,
65 SynchronizedBySubOsc,
66 FrequencyModulation,
67 NumModulationAlgos
68 } ;
71 Oscillator( const IntModel * _wave_shape_model,
72 const IntModel * _mod_algo_model,
73 const float & _freq,
74 const float & _detuning,
75 const float & _phase_offset,
76 const float & _volume,
77 Oscillator * _m_subOsc = NULL );
78 virtual ~Oscillator()
80 delete m_subOsc;
84 inline void setUserWave( const sampleBuffer * _wave )
86 m_userWave = _wave;
89 void update( sampleFrame * _ab, const fpp_t _frames,
90 const ch_cnt_t _chnl );
92 // now follow the wave-shape-routines...
94 static inline sample_t sinSample( const float _sample )
96 return sinf( _sample * F_2PI );
99 static inline sample_t triangleSample( const float _sample )
101 const float ph = fraction( _sample );
102 if( ph <= 0.25f )
104 return ph * 4.0f;
106 else if( ph <= 0.75f )
108 return 2.0f - ph * 4.0f;
110 return ph * 4.0f - 4.0f;
113 static inline sample_t sawSample( const float _sample )
115 return -1.0f + fraction( _sample ) * 2.0f;
118 static inline sample_t squareSample( const float _sample )
120 return ( fraction( _sample ) > 0.5f ) ? -1.0f : 1.0f;
123 static inline sample_t moogSawSample( const float _sample )
125 const float ph = fraction( _sample );
126 if( ph < 0.5f )
128 return -1.0f + ph * 4.0f;
130 return 1.0f - 2.0f * ph;
133 static inline sample_t expSample( const float _sample )
135 float ph = fraction( _sample );
136 if( ph > 0.5f )
138 ph = 1.0f - ph;
140 return -1.0f + 8.0f * ph * ph;
143 static inline sample_t noiseSample( const float )
145 // Precise implementation
146 // return 1.0f - rand() * 2.0f / RAND_MAX;
148 // Fast implementation
149 return 1.0f - fast_rand() * 2.0f / FAST_RAND_MAX;
152 inline sample_t userWaveSample( const float _sample ) const
154 return m_userWave->userWaveSample( _sample );
158 private:
159 const IntModel * m_waveShapeModel;
160 const IntModel * m_modulationAlgoModel;
161 const float & m_freq;
162 const float & m_detuning;
163 const float & m_volume;
164 const float & m_ext_phaseOffset;
165 Oscillator * m_subOsc;
166 float m_phaseOffset;
167 float m_phase;
168 const sampleBuffer * m_userWave;
171 void updateNoSub( sampleFrame * _ab, const fpp_t _frames,
172 const ch_cnt_t _chnl );
173 void updatePM( sampleFrame * _ab, const fpp_t _frames,
174 const ch_cnt_t _chnl );
175 void updateAM( sampleFrame * _ab, const fpp_t _frames,
176 const ch_cnt_t _chnl );
177 void updateMix( sampleFrame * _ab, const fpp_t _frames,
178 const ch_cnt_t _chnl );
179 void updateSync( sampleFrame * _ab, const fpp_t _frames,
180 const ch_cnt_t _chnl );
181 void updateFM( sampleFrame * _ab, const fpp_t _frames,
182 const ch_cnt_t _chnl );
184 float syncInit( sampleFrame * _ab, const fpp_t _frames,
185 const ch_cnt_t _chnl );
186 inline bool syncOk( float _osc_coeff );
188 template<WaveShapes W>
189 void updateNoSub( sampleFrame * _ab, const fpp_t _frames,
190 const ch_cnt_t _chnl );
191 template<WaveShapes W>
192 void updatePM( sampleFrame * _ab, const fpp_t _frames,
193 const ch_cnt_t _chnl );
194 template<WaveShapes W>
195 void updateAM( sampleFrame * _ab, const fpp_t _frames,
196 const ch_cnt_t _chnl );
197 template<WaveShapes W>
198 void updateMix( sampleFrame * _ab, const fpp_t _frames,
199 const ch_cnt_t _chnl );
200 template<WaveShapes W>
201 void updateSync( sampleFrame * _ab, const fpp_t _frames,
202 const ch_cnt_t _chnl );
203 template<WaveShapes W>
204 void updateFM( sampleFrame * _ab, const fpp_t _frames,
205 const ch_cnt_t _chnl );
207 template<WaveShapes W>
208 inline sample_t getSample( const float _sample );
210 inline void recalcPhase();
215 #endif