Oscillator.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Oscillator.h - declaration of class Oscillator
  3. *
  4. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  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.
  12. *
  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.
  17. *
  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.
  22. *
  23. */
  24. #ifndef OSCILLATOR_H
  25. #define OSCILLATOR_H
  26. #include "lmmsconfig.h"
  27. #include <math.h>
  28. #ifdef LMMS_HAVE_STDLIB_H
  29. #include <stdlib.h>
  30. #endif
  31. #include "SampleBuffer.h"
  32. #include "lmms_constants.h"
  33. class IntModel;
  34. class LMMS_EXPORT Oscillator
  35. {
  36. MM_OPERATORS
  37. public:
  38. enum WaveShapes
  39. {
  40. SineWave,
  41. TriangleWave,
  42. SawWave,
  43. SquareWave,
  44. MoogSawWave,
  45. ExponentialWave,
  46. WhiteNoise,
  47. UserDefinedWave,
  48. NumWaveShapes
  49. } ;
  50. enum ModulationAlgos
  51. {
  52. PhaseModulation,
  53. AmplitudeModulation,
  54. SignalMix,
  55. SynchronizedBySubOsc,
  56. FrequencyModulation,
  57. NumModulationAlgos
  58. } ;
  59. Oscillator( const IntModel * _wave_shape_model,
  60. const IntModel * _mod_algo_model,
  61. const float & _freq,
  62. const float & _detuning,
  63. const float & _phase_offset,
  64. const float & _volume,
  65. Oscillator * _m_subOsc = NULL );
  66. virtual ~Oscillator()
  67. {
  68. delete m_subOsc;
  69. }
  70. inline void setUserWave( const SampleBuffer * _wave )
  71. {
  72. m_userWave = _wave;
  73. }
  74. void update( sampleFrame * _ab, const fpp_t _frames,
  75. const ch_cnt_t _chnl );
  76. // now follow the wave-shape-routines...
  77. static inline sample_t sinSample( const float _sample )
  78. {
  79. return sinf( _sample * F_2PI );
  80. }
  81. static inline sample_t triangleSample( const float _sample )
  82. {
  83. const float ph = fraction( _sample );
  84. if( ph <= 0.25f )
  85. {
  86. return ph * 4.0f;
  87. }
  88. else if( ph <= 0.75f )
  89. {
  90. return 2.0f - ph * 4.0f;
  91. }
  92. return ph * 4.0f - 4.0f;
  93. }
  94. static inline sample_t sawSample( const float _sample )
  95. {
  96. return -1.0f + fraction( _sample ) * 2.0f;
  97. }
  98. static inline sample_t squareSample( const float _sample )
  99. {
  100. return ( fraction( _sample ) > 0.5f ) ? -1.0f : 1.0f;
  101. }
  102. static inline sample_t moogSawSample( const float _sample )
  103. {
  104. const float ph = fraction( _sample );
  105. if( ph < 0.5f )
  106. {
  107. return -1.0f + ph * 4.0f;
  108. }
  109. return 1.0f - 2.0f * ph;
  110. }
  111. static inline sample_t expSample( const float _sample )
  112. {
  113. float ph = fraction( _sample );
  114. if( ph > 0.5f )
  115. {
  116. ph = 1.0f - ph;
  117. }
  118. return -1.0f + 8.0f * ph * ph;
  119. }
  120. static inline sample_t noiseSample( const float )
  121. {
  122. // Precise implementation
  123. // return 1.0f - rand() * 2.0f / RAND_MAX;
  124. // Fast implementation
  125. return 1.0f - fast_rand() * 2.0f / FAST_RAND_MAX;
  126. }
  127. inline sample_t userWaveSample( const float _sample ) const
  128. {
  129. return m_userWave->userWaveSample( _sample );
  130. }
  131. private:
  132. const IntModel * m_waveShapeModel;
  133. const IntModel * m_modulationAlgoModel;
  134. const float & m_freq;
  135. const float & m_detuning;
  136. const float & m_volume;
  137. const float & m_ext_phaseOffset;
  138. Oscillator * m_subOsc;
  139. float m_phaseOffset;
  140. float m_phase;
  141. const SampleBuffer * m_userWave;
  142. void updateNoSub( sampleFrame * _ab, const fpp_t _frames,
  143. const ch_cnt_t _chnl );
  144. void updatePM( sampleFrame * _ab, const fpp_t _frames,
  145. const ch_cnt_t _chnl );
  146. void updateAM( sampleFrame * _ab, const fpp_t _frames,
  147. const ch_cnt_t _chnl );
  148. void updateMix( sampleFrame * _ab, const fpp_t _frames,
  149. const ch_cnt_t _chnl );
  150. void updateSync( sampleFrame * _ab, const fpp_t _frames,
  151. const ch_cnt_t _chnl );
  152. void updateFM( sampleFrame * _ab, const fpp_t _frames,
  153. const ch_cnt_t _chnl );
  154. float syncInit( sampleFrame * _ab, const fpp_t _frames,
  155. const ch_cnt_t _chnl );
  156. inline bool syncOk( float _osc_coeff );
  157. template<WaveShapes W>
  158. void updateNoSub( sampleFrame * _ab, const fpp_t _frames,
  159. const ch_cnt_t _chnl );
  160. template<WaveShapes W>
  161. void updatePM( sampleFrame * _ab, const fpp_t _frames,
  162. const ch_cnt_t _chnl );
  163. template<WaveShapes W>
  164. void updateAM( sampleFrame * _ab, const fpp_t _frames,
  165. const ch_cnt_t _chnl );
  166. template<WaveShapes W>
  167. void updateMix( sampleFrame * _ab, const fpp_t _frames,
  168. const ch_cnt_t _chnl );
  169. template<WaveShapes W>
  170. void updateSync( sampleFrame * _ab, const fpp_t _frames,
  171. const ch_cnt_t _chnl );
  172. template<WaveShapes W>
  173. void updateFM( sampleFrame * _ab, const fpp_t _frames,
  174. const ch_cnt_t _chnl );
  175. template<WaveShapes W>
  176. inline sample_t getSample( const float _sample );
  177. inline void recalcPhase();
  178. } ;
  179. #endif