You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 lines
9.6KB

  1. // Copyright 2012 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // Oscillator - digital style waveforms.
  28. #ifndef BRAIDS_DIGITAL_OSCILLATOR_H_
  29. #define BRAIDS_DIGITAL_OSCILLATOR_H_
  30. #include "stmlib/stmlib.h"
  31. #include "braids/excitation.h"
  32. #include "braids/svf.h"
  33. #include <cstring>
  34. namespace braids {
  35. static const size_t kWGBridgeLength = 1024;
  36. static const size_t kWGNeckLength = 4096;
  37. static const size_t kWGBoreLength = 2048;
  38. static const size_t kWGJetLength = 1024;
  39. static const size_t kWGFBoreLength = 4096;
  40. static const size_t kCombDelayLength = 8192;
  41. static const size_t kNumFormants = 5;
  42. static const size_t kNumPluckVoices = 3;
  43. static const size_t kNumOverlappingFof = 3;
  44. static const size_t kNumBellPartials = 11;
  45. static const size_t kNumDrumPartials = 6;
  46. static const size_t kNumAdditiveHarmonics = 12;
  47. enum DigitalOscillatorShape {
  48. OSC_SHAPE_TRIPLE_RING_MOD,
  49. OSC_SHAPE_SAW_SWARM,
  50. OSC_SHAPE_COMB_FILTER,
  51. OSC_SHAPE_TOY,
  52. OSC_SHAPE_DIGITAL_FILTER_LP,
  53. OSC_SHAPE_DIGITAL_FILTER_PK,
  54. OSC_SHAPE_DIGITAL_FILTER_BP,
  55. OSC_SHAPE_DIGITAL_FILTER_HP,
  56. OSC_SHAPE_VOSIM,
  57. OSC_SHAPE_VOWEL,
  58. OSC_SHAPE_VOWEL_FOF,
  59. OSC_SHAPE_HARMONICS,
  60. OSC_SHAPE_FM,
  61. OSC_SHAPE_FEEDBACK_FM,
  62. OSC_SHAPE_CHAOTIC_FEEDBACK_FM,
  63. OSC_SHAPE_STRUCK_BELL,
  64. OSC_SHAPE_STRUCK_DRUM,
  65. OSC_SHAPE_KICK,
  66. OSC_SHAPE_HAT,
  67. OSC_SHAPE_SNARE,
  68. OSC_SHAPE_PLUCKED,
  69. OSC_SHAPE_BOWED,
  70. OSC_SHAPE_BLOWN,
  71. OSC_SHAPE_FLUTED,
  72. OSC_SHAPE_WAVETABLES,
  73. OSC_SHAPE_WAVE_MAP,
  74. OSC_SHAPE_WAVE_LINE,
  75. OSC_SHAPE_WAVE_PARAPHONIC,
  76. OSC_SHAPE_FILTERED_NOISE,
  77. OSC_SHAPE_TWIN_PEAKS_NOISE,
  78. OSC_SHAPE_CLOCKED_NOISE,
  79. OSC_SHAPE_GRANULAR_CLOUD,
  80. OSC_SHAPE_PARTICLE_NOISE,
  81. OSC_SHAPE_DIGITAL_MODULATION,
  82. OSC_SHAPE_QUESTION_MARK_LAST
  83. };
  84. struct ResoSquareState {
  85. uint32_t modulator_phase_increment;
  86. uint32_t modulator_phase;
  87. uint32_t square_modulator_phase;
  88. int32_t integrator;
  89. bool polarity;
  90. };
  91. struct VowelSynthesizerState {
  92. uint32_t formant_increment[3];
  93. uint32_t formant_phase[3];
  94. uint32_t formant_amplitude[3];
  95. uint16_t consonant_frames;
  96. uint16_t noise;
  97. };
  98. struct SawSwarmState {
  99. uint32_t phase[6];
  100. int32_t filter_state[2][2];
  101. int32_t dc_blocked;
  102. int32_t lp;
  103. int32_t bp;
  104. };
  105. struct HarmonicsState {
  106. int32_t amplitude[kNumAdditiveHarmonics];
  107. };
  108. struct AdditiveState {
  109. uint32_t partial_phase[kNumBellPartials];
  110. uint32_t partial_phase_increment[kNumBellPartials];
  111. int32_t partial_amplitude[kNumBellPartials];
  112. int32_t target_partial_amplitude[kNumBellPartials];
  113. int16_t previous_sample;
  114. size_t current_partial;
  115. int32_t lp_noise[3];
  116. };
  117. struct PluckState {
  118. size_t size;
  119. size_t write_ptr;
  120. size_t shift;
  121. size_t mask;
  122. size_t pluck_position;
  123. size_t initialization_ptr;
  124. uint32_t phase;
  125. uint32_t phase_increment;
  126. uint32_t max_phase_increment;
  127. int16_t previous_sample;
  128. uint8_t polyphony_assigner;
  129. };
  130. struct FeedbackFmState {
  131. uint32_t modulator_phase;
  132. int16_t previous_sample;
  133. };
  134. struct ParticleNoiseState {
  135. uint16_t amplitude;
  136. int32_t filter_state[3][2];
  137. int32_t filter_scale[3];
  138. int32_t filter_coefficient[3];
  139. };
  140. struct PhysicalModellingState {
  141. uint16_t delay_ptr;
  142. uint16_t excitation_ptr;
  143. int32_t lp_state;
  144. int32_t filter_state[2];
  145. int16_t previous_sample;
  146. };
  147. struct Grain {
  148. uint32_t phase;
  149. uint32_t phase_increment;
  150. uint32_t envelope_phase;
  151. uint32_t envelope_phase_increment;
  152. };
  153. struct FofState {
  154. int32_t next_saw_sample;
  155. int16_t previous_sample;
  156. int32_t svf_lp[kNumFormants];
  157. int32_t svf_bp[kNumFormants];
  158. };
  159. struct ToyState {
  160. uint8_t held_sample;
  161. uint16_t decimation_counter;
  162. };
  163. struct SvfState {
  164. int32_t bp;
  165. int32_t lp;
  166. };
  167. struct DigitalModulationState {
  168. uint32_t symbol_phase;
  169. uint16_t symbol_count;
  170. int32_t filter_state;
  171. uint8_t data_byte;
  172. };
  173. struct ClockedNoiseState {
  174. uint32_t cycle_phase;
  175. uint32_t cycle_phase_increment;
  176. uint32_t rng_state;
  177. int32_t seed;
  178. int16_t sample;
  179. };
  180. struct HatState {
  181. uint32_t phase[6];
  182. uint32_t rng_state;
  183. };
  184. union DigitalOscillatorState {
  185. ResoSquareState res;
  186. VowelSynthesizerState vow;
  187. SawSwarmState saw;
  188. PluckState plk[4];
  189. FeedbackFmState ffm;
  190. ParticleNoiseState pno;
  191. PhysicalModellingState phy;
  192. Grain grain[4];
  193. FofState fof;
  194. ToyState toy;
  195. SvfState svf;
  196. AdditiveState add;
  197. DigitalModulationState dmd;
  198. ClockedNoiseState clk;
  199. HatState hat;
  200. HarmonicsState hrm;
  201. uint32_t modulator_phase;
  202. };
  203. class DigitalOscillator {
  204. public:
  205. typedef void (DigitalOscillator::*RenderFn)(const uint8_t*, int16_t*, size_t);
  206. DigitalOscillator() { }
  207. ~DigitalOscillator() { }
  208. inline void Init() {
  209. memset(&state_, 0, sizeof(state_));
  210. pulse_[0].Init();
  211. pulse_[1].Init();
  212. pulse_[2].Init();
  213. pulse_[3].Init();
  214. svf_[0].Init();
  215. svf_[1].Init();
  216. svf_[2].Init();
  217. phase_ = 0;
  218. strike_ = true;
  219. init_ = true;
  220. }
  221. inline void set_shape(DigitalOscillatorShape shape) {
  222. shape_ = shape;
  223. }
  224. inline void set_pitch(int16_t pitch) {
  225. // Smooth HF noise when the pitch CV is noisy.
  226. if (pitch_ > (90 << 7) && pitch > (90 << 7)) {
  227. pitch_ = (static_cast<int32_t>(pitch_) + pitch) >> 1;
  228. } else {
  229. pitch_ = pitch;
  230. }
  231. }
  232. inline void set_parameters(
  233. int16_t parameter_1,
  234. int16_t parameter_2) {
  235. parameter_[0] = parameter_1;
  236. parameter_[1] = parameter_2;
  237. }
  238. inline uint32_t phase_increment() const {
  239. return phase_increment_;
  240. }
  241. inline void Strike() {
  242. strike_ = true;
  243. }
  244. void Render(const uint8_t* sync, int16_t* buffer, size_t size);
  245. private:
  246. void RenderTripleRingMod(const uint8_t*, int16_t*, size_t);
  247. void RenderSawSwarm(const uint8_t*, int16_t*, size_t);
  248. void RenderComb(const uint8_t*, int16_t*, size_t);
  249. void RenderToy(const uint8_t*, int16_t*, size_t);
  250. void RenderDigitalFilter(const uint8_t*, int16_t*, size_t);
  251. void RenderVosim(const uint8_t*, int16_t*, size_t);
  252. void RenderVowel(const uint8_t*, int16_t*, size_t);
  253. void RenderVowelFof(const uint8_t*, int16_t*, size_t);
  254. void RenderHarmonics(const uint8_t*, int16_t*, size_t);
  255. void RenderFm(const uint8_t*, int16_t*, size_t);
  256. void RenderFeedbackFm(const uint8_t*, int16_t*, size_t);
  257. void RenderChaoticFeedbackFm(const uint8_t*, int16_t*, size_t);
  258. void RenderStruckBell(const uint8_t*, int16_t*, size_t);
  259. void RenderStruckDrum(const uint8_t*, int16_t*, size_t);
  260. void RenderPlucked(const uint8_t*, int16_t*, size_t);
  261. void RenderBowed(const uint8_t*, int16_t*, size_t);
  262. void RenderBlown(const uint8_t*, int16_t*, size_t);
  263. void RenderFluted(const uint8_t*, int16_t*, size_t);
  264. void RenderWavetables(const uint8_t*, int16_t*, size_t);
  265. void RenderWaveMap(const uint8_t*, int16_t*, size_t);
  266. void RenderWaveLine(const uint8_t*, int16_t*, size_t);
  267. void RenderWaveParaphonic(const uint8_t*, int16_t*, size_t);
  268. void RenderTwinPeaksNoise(const uint8_t*, int16_t*, size_t);
  269. void RenderFilteredNoise(const uint8_t*, int16_t*, size_t);
  270. void RenderClockedNoise(const uint8_t*, int16_t*, size_t);
  271. void RenderGranularCloud(const uint8_t*, int16_t*, size_t);
  272. void RenderParticleNoise(const uint8_t*, int16_t*, size_t);
  273. void RenderDigitalModulation(const uint8_t*, int16_t*, size_t);
  274. void RenderKick(const uint8_t*, int16_t*, size_t);
  275. void RenderSnare(const uint8_t*, int16_t*, size_t);
  276. void RenderCymbal(const uint8_t*, int16_t*, size_t);
  277. void RenderQuestionMark(const uint8_t*, int16_t*, size_t);
  278. // void RenderYourAlgo(const uint8_t*, int16_t*, size_t);
  279. uint32_t ComputePhaseIncrement(int16_t midi_pitch);
  280. uint32_t ComputeDelay(int16_t midi_pitch);
  281. int16_t InterpolateFormantParameter(
  282. const int16_t table[][kNumFormants][kNumFormants],
  283. int16_t x,
  284. int16_t y,
  285. uint8_t formant);
  286. uint32_t phase_;
  287. uint32_t phase_increment_;
  288. uint32_t delay_;
  289. int16_t parameter_[2];
  290. int16_t previous_parameter_[2];
  291. int32_t smoothed_parameter_;
  292. int16_t pitch_;
  293. uint8_t active_voice_;
  294. bool init_;
  295. bool strike_;
  296. DigitalOscillatorShape shape_;
  297. DigitalOscillatorShape previous_shape_;
  298. DigitalOscillatorState state_;
  299. Excitation pulse_[4];
  300. Svf svf_[3];
  301. union {
  302. int16_t comb[kCombDelayLength];
  303. int16_t ks[1025 * 4];
  304. struct {
  305. int8_t bridge[kWGBridgeLength];
  306. int8_t neck[kWGNeckLength];
  307. } bowed;
  308. int16_t bore[kWGBoreLength];
  309. struct {
  310. int8_t jet[kWGJetLength];
  311. int8_t bore[kWGFBoreLength];
  312. } fluted;
  313. } delay_lines_;
  314. static RenderFn fn_table_[];
  315. DISALLOW_COPY_AND_ASSIGN(DigitalOscillator);
  316. };
  317. } // namespace braids
  318. #endif // BRAIDS_DIGITAL_OSCILLATOR_H_