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.

405 lines
11KB

  1. // Copyright 2013 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. // Voice.
  28. #include "yarns/voice.h"
  29. #include <algorithm>
  30. #include <cstdio>
  31. #include <cstdlib>
  32. #include "stmlib/midi/midi.h"
  33. #include "stmlib/utils/dsp.h"
  34. #include "stmlib/utils/random.h"
  35. #include "yarns/resources.h"
  36. namespace yarns {
  37. using namespace stmlib;
  38. using namespace stmlib_midi;
  39. const int32_t kOctave = 12 << 7;
  40. const int32_t kMaxNote = 120 << 7;
  41. void Voice::Init() {
  42. note_ = -1;
  43. note_source_ = note_target_ = note_portamento_ = 60 << 7;
  44. gate_ = false;
  45. mod_velocity_ = 0;
  46. ResetAllControllers();
  47. modulation_rate_ = 0;
  48. pitch_bend_range_ = 2;
  49. vibrato_range_ = 0;
  50. lfo_phase_ = portamento_phase_ = 0;
  51. portamento_phase_increment_ = 1U << 31;
  52. portamento_exponential_shape_ = false;
  53. trigger_duration_ = 2;
  54. for (uint8_t i = 0; i < kNumOctaves; ++i) {
  55. calibrated_dac_code_[i] = 54586 - 5133 * i;
  56. }
  57. dirty_ = false;
  58. oscillator_.Init(
  59. calibrated_dac_code_[3] - calibrated_dac_code_[8],
  60. calibrated_dac_code_[3]);
  61. }
  62. void Voice::Calibrate(uint16_t* calibrated_dac_code) {
  63. std::copy(
  64. &calibrated_dac_code[0],
  65. &calibrated_dac_code[kNumOctaves],
  66. &calibrated_dac_code_[0]);
  67. }
  68. inline uint16_t Voice::NoteToDacCode(int32_t note) const {
  69. if (note <= 0) {
  70. note = 0;
  71. }
  72. if (note >= kMaxNote) {
  73. note = kMaxNote - 1;
  74. }
  75. uint8_t octave = 0;
  76. while (note >= kOctave) {
  77. note -= kOctave;
  78. ++octave;
  79. }
  80. // Note is now between 0 and kOctave
  81. // Octave indicates the octave. Look up in the DAC code table.
  82. int32_t a = calibrated_dac_code_[octave];
  83. int32_t b = calibrated_dac_code_[octave + 1];
  84. return a + ((b - a) * note / kOctave);
  85. }
  86. void Voice::ResetAllControllers() {
  87. mod_pitch_bend_ = 8192;
  88. mod_wheel_ = 0;
  89. std::fill(&mod_aux_[0], &mod_aux_[5], 0);
  90. }
  91. void Voice::Refresh() {
  92. // Compute base pitch with portamento.
  93. portamento_phase_ += portamento_phase_increment_;
  94. if (portamento_phase_ < portamento_phase_increment_) {
  95. portamento_phase_ = 0;
  96. portamento_phase_increment_ = 0;
  97. note_source_ = note_target_;
  98. }
  99. uint16_t portamento_level = portamento_exponential_shape_
  100. ? Interpolate824(lut_env_expo, portamento_phase_)
  101. : portamento_phase_ >> 16;
  102. int32_t note = note_source_ + \
  103. ((note_target_ - note_source_) * portamento_level >> 16);
  104. note_portamento_ = note;
  105. // Add pitch-bend.
  106. note += static_cast<int32_t>(mod_pitch_bend_ - 8192) * pitch_bend_range_ >> 6;
  107. // Add transposition/fine tuning.
  108. note += tuning_;
  109. // Add vibrato.
  110. if (modulation_rate_ < 100) {
  111. lfo_phase_ += lut_lfo_increments[modulation_rate_];
  112. } else {
  113. lfo_phase_ += lfo_pll_phase_increment_;
  114. }
  115. int32_t lfo = lfo_phase_ < 1UL << 31
  116. ? -32768 + (lfo_phase_ >> 15)
  117. : 0x17fff - (lfo_phase_ >> 15);
  118. note += lfo * mod_wheel_ * vibrato_range_ >> 15;
  119. mod_aux_[3] = (lfo * mod_wheel_ >> 7) + 32768;
  120. mod_aux_[4] = lfo + 32768;
  121. if (retrigger_delay_) {
  122. --retrigger_delay_;
  123. }
  124. if (trigger_pulse_) {
  125. --trigger_pulse_;
  126. }
  127. if (trigger_phase_increment_) {
  128. trigger_phase_ += trigger_phase_increment_;
  129. if (trigger_phase_ < trigger_phase_increment_) {
  130. trigger_phase_ = 0;
  131. trigger_phase_increment_ = 0;
  132. }
  133. }
  134. if (note != note_ || dirty_) {
  135. note_dac_code_ = NoteToDacCode(note);
  136. note_ = note;
  137. dirty_ = false;
  138. }
  139. }
  140. void Voice::NoteOn(
  141. int16_t note,
  142. uint8_t velocity,
  143. uint8_t portamento,
  144. bool trigger) {
  145. note_source_ = note_portamento_;
  146. note_target_ = note;
  147. if (!portamento) {
  148. note_source_ = note_target_;
  149. }
  150. portamento_phase_ = 0;
  151. if (portamento <= 50) {
  152. portamento_phase_increment_ = lut_portamento_increments[portamento << 1];
  153. portamento_exponential_shape_ = true;
  154. } else {
  155. uint32_t base_increment = lut_portamento_increments[(portamento - 51) << 1];
  156. uint32_t delta = abs(note_target_ - note_source_) + 1;
  157. portamento_phase_increment_ = (1536 * (base_increment >> 11) / delta) << 11;
  158. CONSTRAIN(portamento_phase_increment_, 1, 2147483647);
  159. portamento_exponential_shape_ = false;
  160. }
  161. mod_velocity_ = velocity;
  162. if (gate_ && trigger) {
  163. retrigger_delay_ = 2;
  164. }
  165. if (trigger) {
  166. trigger_pulse_ = trigger_duration_ * 8;
  167. trigger_phase_ = 0;
  168. trigger_phase_increment_ = lut_portamento_increments[trigger_duration_];
  169. }
  170. gate_ = true;
  171. }
  172. void Voice::NoteOff() {
  173. gate_ = false;
  174. }
  175. void Voice::ControlChange(uint8_t controller, uint8_t value) {
  176. switch (controller) {
  177. case kCCModulationWheelMsb:
  178. mod_wheel_ = value;
  179. break;
  180. case kCCBreathController:
  181. mod_aux_[1] = value << 9;
  182. break;
  183. case kCCFootPedalMsb:
  184. mod_aux_[2] = value << 9;
  185. break;
  186. }
  187. }
  188. uint16_t Voice::trigger_dac_code() const {
  189. if (trigger_phase_ <= trigger_phase_increment_) {
  190. return calibrated_dac_code_[3]; // 0V.
  191. } else {
  192. int32_t velocity_coefficient = trigger_scale_ ? mod_velocity_ << 8 : 32768;
  193. int32_t value = 0;
  194. switch(trigger_shape_) {
  195. case TRIGGER_SHAPE_SQUARE:
  196. value = 32767;
  197. break;
  198. case TRIGGER_SHAPE_LINEAR:
  199. value = 32767 - (trigger_phase_ >> 17);
  200. break;
  201. default:
  202. {
  203. const int16_t* table = waveform_table[
  204. trigger_shape_ - TRIGGER_SHAPE_EXPONENTIAL];
  205. value = Interpolate824(table, trigger_phase_);
  206. }
  207. break;
  208. }
  209. value = value * velocity_coefficient >> 15;
  210. int32_t max = calibrated_dac_code_[8];
  211. int32_t min = calibrated_dac_code_[3];
  212. return min + ((max - min) * value >> 15);
  213. }
  214. }
  215. static const uint16_t kHighestNote = 128 * 128;
  216. static const uint16_t kPitchTableStart = 116 * 128;
  217. void Oscillator::Init(int32_t scale, int32_t offset) {
  218. audio_buffer_.Init();
  219. phase_ = 0;
  220. next_sample_ = 0;
  221. high_ = false;
  222. scale_ = scale;
  223. offset_ = offset;
  224. integrator_state_ = 0;
  225. }
  226. uint32_t Oscillator::ComputePhaseIncrement(int16_t midi_pitch) {
  227. if (midi_pitch >= kHighestNote) {
  228. midi_pitch = kHighestNote - 1;
  229. }
  230. int32_t ref_pitch = midi_pitch;
  231. ref_pitch -= kPitchTableStart;
  232. size_t num_shifts = 0;
  233. while (ref_pitch < 0) {
  234. ref_pitch += kOctave;
  235. ++num_shifts;
  236. }
  237. uint32_t a = lut_oscillator_increments[ref_pitch >> 4];
  238. uint32_t b = lut_oscillator_increments[(ref_pitch >> 4) + 1];
  239. uint32_t phase_increment = a + \
  240. (static_cast<int32_t>(b - a) * (ref_pitch & 0xf) >> 4);
  241. phase_increment >>= num_shifts;
  242. return phase_increment;
  243. }
  244. void Oscillator::RenderSilence() {
  245. size_t size = kAudioBlockSize;
  246. while (size--) {
  247. audio_buffer_.Overwrite(offset_);
  248. }
  249. }
  250. void Oscillator::RenderSine(uint32_t phase_increment) {
  251. size_t size = kAudioBlockSize;
  252. while (size--) {
  253. phase_ += phase_increment;
  254. int32_t sample = Interpolate1022(wav_sine, phase_);
  255. audio_buffer_.Overwrite(offset_ - (scale_ * sample >> 16));
  256. }
  257. }
  258. void Oscillator::RenderNoise() {
  259. size_t size = kAudioBlockSize;
  260. while (size--) {
  261. int16_t sample = Random::GetSample();
  262. audio_buffer_.Overwrite(offset_ - (scale_ * sample >> 16));
  263. }
  264. }
  265. void Oscillator::RenderSaw(uint32_t phase_increment) {
  266. uint32_t phase = phase_;
  267. int32_t next_sample = next_sample_;
  268. size_t size = kAudioBlockSize;
  269. while (size--) {
  270. int32_t this_sample = next_sample;
  271. next_sample = 0;
  272. phase += phase_increment;
  273. if (phase < phase_increment) {
  274. uint32_t t = phase / (phase_increment >> 16);
  275. this_sample -= ThisBlepSample(t);
  276. next_sample -= NextBlepSample(t);
  277. }
  278. next_sample += phase >> 17;
  279. this_sample = (this_sample - 16384) << 1;
  280. audio_buffer_.Overwrite(offset_ - (scale_ * this_sample >> 16));
  281. }
  282. next_sample_ = next_sample;
  283. phase_ = phase;
  284. }
  285. void Oscillator::RenderSquare(
  286. uint32_t phase_increment,
  287. uint32_t pw,
  288. bool integrate) {
  289. uint32_t phase = phase_;
  290. int32_t next_sample = next_sample_;
  291. int32_t integrator_state = integrator_state_;
  292. int16_t integrator_coefficient = phase_increment >> 18;
  293. size_t size = kAudioBlockSize;
  294. while (size--) {
  295. int32_t this_sample = next_sample;
  296. next_sample = 0;
  297. phase += phase_increment;
  298. if (!high_) {
  299. if (phase >= pw) {
  300. uint32_t t = (phase - pw) / (phase_increment >> 16);
  301. this_sample += ThisBlepSample(t);
  302. next_sample += NextBlepSample(t);
  303. high_ = true;
  304. }
  305. }
  306. if (high_ && (phase < phase_increment)) {
  307. uint32_t t = phase / (phase_increment >> 16);
  308. this_sample -= ThisBlepSample(t);
  309. next_sample -= NextBlepSample(t);
  310. high_ = false;
  311. }
  312. next_sample += phase < pw ? 0 : 32767;
  313. this_sample = (this_sample - 16384) << 1;
  314. if (integrate) {
  315. integrator_state += integrator_coefficient * (this_sample - integrator_state) >> 15;
  316. this_sample = integrator_state << 3;
  317. }
  318. audio_buffer_.Overwrite(offset_ - (scale_ * this_sample >> 16));
  319. }
  320. integrator_state_ = integrator_state;
  321. next_sample_ = next_sample;
  322. phase_ = phase;
  323. }
  324. void Oscillator::Render(uint8_t mode, int16_t note, bool gate) {
  325. if (mode == 0 || audio_buffer_.writable() < kAudioBlockSize) {
  326. return;
  327. }
  328. if ((mode & 0x80) && !gate) {
  329. RenderSilence();
  330. return;
  331. }
  332. uint32_t phase_increment = ComputePhaseIncrement(note);
  333. switch ((mode & 0x0f) - 1) {
  334. case 0:
  335. RenderSaw(phase_increment);
  336. break;
  337. case 1:
  338. RenderSquare(phase_increment, 0x40000000, false);
  339. break;
  340. case 2:
  341. RenderSquare(phase_increment, 0x80000000, false);
  342. break;
  343. case 3:
  344. RenderSquare(phase_increment, 0x80000000, true);
  345. break;
  346. case 4:
  347. RenderSine(phase_increment);
  348. break;
  349. default:
  350. RenderNoise();
  351. break;
  352. }
  353. }
  354. } // namespace yarns