Oscillator.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Oscillator.h - declaration of class Oscillator
  3. *
  4. * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. * 2018 Dave French <dave/dot/french3/at/googlemail/dot/com>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #ifndef OSCILLATOR_H
  26. #define OSCILLATOR_H
  27. #include <cassert>
  28. #include <fftw3.h>
  29. #include <math.h>
  30. #ifdef LMMS_HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #include "Engine.h"
  34. #include "lmms_constants.h"
  35. #include "lmmsconfig.h"
  36. #include "AudioEngine.h"
  37. #include "OscillatorConstants.h"
  38. #include "SampleBuffer.h"
  39. class IntModel;
  40. class LMMS_EXPORT Oscillator
  41. {
  42. MM_OPERATORS
  43. public:
  44. enum WaveShapes
  45. {
  46. SineWave,
  47. TriangleWave,
  48. SawWave,
  49. SquareWave,
  50. MoogSawWave,
  51. ExponentialWave,
  52. WhiteNoise,
  53. UserDefinedWave,
  54. NumWaveShapes, //!< Number of all available wave shapes
  55. FirstWaveShapeTable = TriangleWave, //!< First wave shape that has a pre-generated table
  56. NumWaveShapeTables = WhiteNoise - FirstWaveShapeTable, //!< Number of band-limited wave shapes to be generated
  57. };
  58. enum ModulationAlgos
  59. {
  60. PhaseModulation,
  61. AmplitudeModulation,
  62. SignalMix,
  63. SynchronizedBySubOsc,
  64. FrequencyModulation,
  65. NumModulationAlgos
  66. } ;
  67. Oscillator( const IntModel *wave_shape_model,
  68. const IntModel *mod_algo_model,
  69. const float &freq,
  70. const float &detuning_div_samplerate,
  71. const float &phase_offset,
  72. const float &volume,
  73. Oscillator *m_subOsc = nullptr);
  74. virtual ~Oscillator()
  75. {
  76. delete m_subOsc;
  77. }
  78. static void waveTableInit();
  79. static void destroyFFTPlans();
  80. static void generateAntiAliasUserWaveTable(SampleBuffer* sampleBuffer);
  81. inline void setUseWaveTable(bool n)
  82. {
  83. m_useWaveTable = n;
  84. }
  85. inline void setUserWave( const SampleBuffer * _wave )
  86. {
  87. m_userWave = _wave;
  88. }
  89. void update(sampleFrame* ab, const fpp_t frames, const ch_cnt_t chnl, bool modulator = false);
  90. // now follow the wave-shape-routines...
  91. static inline sample_t sinSample( const float _sample )
  92. {
  93. return sinf( _sample * F_2PI );
  94. }
  95. static inline sample_t triangleSample( const float _sample )
  96. {
  97. const float ph = absFraction( _sample );
  98. if( ph <= 0.25f )
  99. {
  100. return ph * 4.0f;
  101. }
  102. else if( ph <= 0.75f )
  103. {
  104. return 2.0f - ph * 4.0f;
  105. }
  106. return ph * 4.0f - 4.0f;
  107. }
  108. static inline sample_t sawSample( const float _sample )
  109. {
  110. return -1.0f + absFraction( _sample ) * 2.0f;
  111. }
  112. static inline sample_t squareSample( const float _sample )
  113. {
  114. return ( absFraction( _sample ) > 0.5f ) ? -1.0f : 1.0f;
  115. }
  116. static inline sample_t moogSawSample( const float _sample )
  117. {
  118. const float ph = absFraction( _sample );
  119. if( ph < 0.5f )
  120. {
  121. return -1.0f + ph * 4.0f;
  122. }
  123. return 1.0f - 2.0f * ph;
  124. }
  125. static inline sample_t expSample( const float _sample )
  126. {
  127. float ph = absFraction( _sample );
  128. if( ph > 0.5f )
  129. {
  130. ph = 1.0f - ph;
  131. }
  132. return -1.0f + 8.0f * ph * ph;
  133. }
  134. static inline sample_t noiseSample( const float )
  135. {
  136. // Precise implementation
  137. // return 1.0f - rand() * 2.0f / RAND_MAX;
  138. // Fast implementation
  139. return 1.0f - fast_rand() * 2.0f / FAST_RAND_MAX;
  140. }
  141. inline sample_t userWaveSample( const float _sample ) const
  142. {
  143. return m_userWave->userWaveSample( _sample );
  144. }
  145. struct wtSampleControl {
  146. float frame;
  147. f_cnt_t f1;
  148. f_cnt_t f2;
  149. int band;
  150. };
  151. inline wtSampleControl getWtSampleControl(const float sample) const
  152. {
  153. wtSampleControl control;
  154. control.frame = sample * OscillatorConstants::WAVETABLE_LENGTH;
  155. control.f1 = static_cast<f_cnt_t>(control.frame) % OscillatorConstants::WAVETABLE_LENGTH;
  156. if (control.f1 < 0)
  157. {
  158. control.f1 += OscillatorConstants::WAVETABLE_LENGTH;
  159. }
  160. control.f2 = control.f1 < OscillatorConstants::WAVETABLE_LENGTH - 1 ?
  161. control.f1 + 1 :
  162. 0;
  163. control.band = waveTableBandFromFreq(
  164. m_freq * m_detuning_div_samplerate * Engine::audioEngine()->processingSampleRate());
  165. return control;
  166. }
  167. inline sample_t wtSample(
  168. const sample_t table[OscillatorConstants::WAVE_TABLES_PER_WAVEFORM_COUNT][OscillatorConstants::WAVETABLE_LENGTH],
  169. const float sample) const
  170. {
  171. assert(table != nullptr);
  172. wtSampleControl control = getWtSampleControl(sample);
  173. return linearInterpolate(table[control.band][control.f1],
  174. table[control.band][control.f2], fraction(control.frame));
  175. }
  176. inline sample_t wtSample(const std::unique_ptr<OscillatorConstants::waveform_t>& table, const float sample) const
  177. {
  178. assert(table != nullptr);
  179. wtSampleControl control = getWtSampleControl(sample);
  180. return linearInterpolate((*table)[control.band][control.f1],
  181. (*table)[control.band][control.f2], fraction(control.frame));
  182. }
  183. inline sample_t wtSample(sample_t **table, const float sample) const
  184. {
  185. assert(table != nullptr);
  186. wtSampleControl control = getWtSampleControl(sample);
  187. return linearInterpolate(table[control.band][control.f1],
  188. table[control.band][control.f2], fraction(control.frame));
  189. }
  190. static inline int waveTableBandFromFreq(float freq)
  191. {
  192. // Frequency bands are indexed relative to default MIDI key frequencies.
  193. // I.e., 440 Hz (A4, key 69): 69 + 12 * log2(1) = 69
  194. // To always avoid aliasing, ceil() is used instead of round(). It ensures that the nearest wavetable with
  195. // lower than optimal number of harmonics is used when exactly matching wavetable is not available.
  196. int band = (69 + static_cast<int>(std::ceil(12.0f * std::log2(freq / 440.0f)))) / OscillatorConstants::SEMITONES_PER_TABLE;
  197. // Limit the returned value to a valid wavetable index range.
  198. // (qBound would bring Qt into the audio code, which not a preferable option due to realtime safety.
  199. // C++17 std::clamp() could be used in the future.)
  200. return band <= 1 ? 1 : band >= OscillatorConstants::WAVE_TABLES_PER_WAVEFORM_COUNT-1 ? OscillatorConstants::WAVE_TABLES_PER_WAVEFORM_COUNT-1 : band;
  201. }
  202. static inline float freqFromWaveTableBand(int band)
  203. {
  204. return 440.0f * std::pow(2.0f, (band * OscillatorConstants::SEMITONES_PER_TABLE - 69.0f) / 12.0f);
  205. }
  206. private:
  207. const IntModel * m_waveShapeModel;
  208. const IntModel * m_modulationAlgoModel;
  209. const float & m_freq;
  210. const float & m_detuning_div_samplerate;
  211. const float & m_volume;
  212. const float & m_ext_phaseOffset;
  213. Oscillator * m_subOsc;
  214. float m_phaseOffset;
  215. float m_phase;
  216. const SampleBuffer * m_userWave;
  217. bool m_useWaveTable;
  218. // There are many update*() variants; the modulator flag is stored as a member variable to avoid
  219. // adding more explicit parameters to all of them. Can be converted to a parameter if needed.
  220. bool m_isModulator;
  221. /* Multiband WaveTable */
  222. static sample_t s_waveTables[WaveShapes::NumWaveShapeTables][OscillatorConstants::WAVE_TABLES_PER_WAVEFORM_COUNT][OscillatorConstants::WAVETABLE_LENGTH];
  223. static fftwf_plan s_fftPlan;
  224. static fftwf_plan s_ifftPlan;
  225. static fftwf_complex * s_specBuf;
  226. static float s_sampleBuffer[OscillatorConstants::WAVETABLE_LENGTH];
  227. static void generateSawWaveTable(int bands, sample_t* table, int firstBand = 1);
  228. static void generateTriangleWaveTable(int bands, sample_t* table, int firstBand = 1);
  229. static void generateSquareWaveTable(int bands, sample_t* table, int firstBand = 1);
  230. static void generateFromFFT(int bands, sample_t* table);
  231. static void generateWaveTables();
  232. static void createFFTPlans();
  233. /* End Multiband wavetable */
  234. void updateNoSub( sampleFrame * _ab, const fpp_t _frames,
  235. const ch_cnt_t _chnl );
  236. void updatePM( sampleFrame * _ab, const fpp_t _frames,
  237. const ch_cnt_t _chnl );
  238. void updateAM( sampleFrame * _ab, const fpp_t _frames,
  239. const ch_cnt_t _chnl );
  240. void updateMix( sampleFrame * _ab, const fpp_t _frames,
  241. const ch_cnt_t _chnl );
  242. void updateSync( sampleFrame * _ab, const fpp_t _frames,
  243. const ch_cnt_t _chnl );
  244. void updateFM( sampleFrame * _ab, const fpp_t _frames,
  245. const ch_cnt_t _chnl );
  246. float syncInit( sampleFrame * _ab, const fpp_t _frames,
  247. const ch_cnt_t _chnl );
  248. inline bool syncOk( float _osc_coeff );
  249. template<WaveShapes W>
  250. void updateNoSub( sampleFrame * _ab, const fpp_t _frames,
  251. const ch_cnt_t _chnl );
  252. template<WaveShapes W>
  253. void updatePM( sampleFrame * _ab, const fpp_t _frames,
  254. const ch_cnt_t _chnl );
  255. template<WaveShapes W>
  256. void updateAM( sampleFrame * _ab, const fpp_t _frames,
  257. const ch_cnt_t _chnl );
  258. template<WaveShapes W>
  259. void updateMix( sampleFrame * _ab, const fpp_t _frames,
  260. const ch_cnt_t _chnl );
  261. template<WaveShapes W>
  262. void updateSync( sampleFrame * _ab, const fpp_t _frames,
  263. const ch_cnt_t _chnl );
  264. template<WaveShapes W>
  265. void updateFM( sampleFrame * _ab, const fpp_t _frames,
  266. const ch_cnt_t _chnl );
  267. template<WaveShapes W>
  268. inline sample_t getSample( const float _sample );
  269. inline void recalcPhase();
  270. } ;
  271. #endif