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.

408 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_[7], 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_[0] = mod_velocity_ << 9;
  120. mod_aux_[1] = mod_wheel_ << 9;
  121. mod_aux_[5] = static_cast<uint16_t>(mod_pitch_bend_) << 2;
  122. mod_aux_[6] = (lfo * mod_wheel_ >> 7) + 32768;
  123. mod_aux_[7] = lfo + 32768;
  124. if (retrigger_delay_) {
  125. --retrigger_delay_;
  126. }
  127. if (trigger_pulse_) {
  128. --trigger_pulse_;
  129. }
  130. if (trigger_phase_increment_) {
  131. trigger_phase_ += trigger_phase_increment_;
  132. if (trigger_phase_ < trigger_phase_increment_) {
  133. trigger_phase_ = 0;
  134. trigger_phase_increment_ = 0;
  135. }
  136. }
  137. if (note != note_ || dirty_) {
  138. note_dac_code_ = NoteToDacCode(note);
  139. note_ = note;
  140. dirty_ = false;
  141. }
  142. }
  143. void Voice::NoteOn(
  144. int16_t note,
  145. uint8_t velocity,
  146. uint8_t portamento,
  147. bool trigger) {
  148. note_source_ = note_portamento_;
  149. note_target_ = note;
  150. if (!portamento) {
  151. note_source_ = note_target_;
  152. }
  153. portamento_phase_ = 0;
  154. if (portamento <= 50) {
  155. portamento_phase_increment_ = lut_portamento_increments[portamento << 1];
  156. portamento_exponential_shape_ = true;
  157. } else {
  158. uint32_t base_increment = lut_portamento_increments[(portamento - 51) << 1];
  159. uint32_t delta = abs(note_target_ - note_source_) + 1;
  160. portamento_phase_increment_ = (1536 * (base_increment >> 11) / delta) << 11;
  161. CONSTRAIN(portamento_phase_increment_, 1, 2147483647);
  162. portamento_exponential_shape_ = false;
  163. }
  164. mod_velocity_ = velocity;
  165. if (gate_ && trigger) {
  166. retrigger_delay_ = 2;
  167. }
  168. if (trigger) {
  169. trigger_pulse_ = trigger_duration_ * 8;
  170. trigger_phase_ = 0;
  171. trigger_phase_increment_ = lut_portamento_increments[trigger_duration_];
  172. }
  173. gate_ = true;
  174. }
  175. void Voice::NoteOff() {
  176. gate_ = false;
  177. }
  178. void Voice::ControlChange(uint8_t controller, uint8_t value) {
  179. switch (controller) {
  180. case kCCModulationWheelMsb:
  181. mod_wheel_ = value;
  182. break;
  183. case kCCBreathController:
  184. mod_aux_[3] = value << 9;
  185. break;
  186. case kCCFootPedalMsb:
  187. mod_aux_[4] = value << 9;
  188. break;
  189. }
  190. }
  191. uint16_t Voice::trigger_dac_code() const {
  192. if (trigger_phase_ <= trigger_phase_increment_) {
  193. return calibrated_dac_code_[3]; // 0V.
  194. } else {
  195. int32_t velocity_coefficient = trigger_scale_ ? mod_velocity_ << 8 : 32768;
  196. int32_t value = 0;
  197. switch(trigger_shape_) {
  198. case TRIGGER_SHAPE_SQUARE:
  199. value = 32767;
  200. break;
  201. case TRIGGER_SHAPE_LINEAR:
  202. value = 32767 - (trigger_phase_ >> 17);
  203. break;
  204. default:
  205. {
  206. const int16_t* table = waveform_table[
  207. trigger_shape_ - TRIGGER_SHAPE_EXPONENTIAL];
  208. value = Interpolate824(table, trigger_phase_);
  209. }
  210. break;
  211. }
  212. value = value * velocity_coefficient >> 15;
  213. int32_t max = calibrated_dac_code_[8];
  214. int32_t min = calibrated_dac_code_[3];
  215. return min + ((max - min) * value >> 15);
  216. }
  217. }
  218. static const uint16_t kHighestNote = 128 * 128;
  219. static const uint16_t kPitchTableStart = 116 * 128;
  220. void Oscillator::Init(int32_t scale, int32_t offset) {
  221. audio_buffer_.Init();
  222. phase_ = 0;
  223. next_sample_ = 0;
  224. high_ = false;
  225. scale_ = scale;
  226. offset_ = offset;
  227. integrator_state_ = 0;
  228. }
  229. uint32_t Oscillator::ComputePhaseIncrement(int16_t midi_pitch) {
  230. if (midi_pitch >= kHighestNote) {
  231. midi_pitch = kHighestNote - 1;
  232. }
  233. int32_t ref_pitch = midi_pitch;
  234. ref_pitch -= kPitchTableStart;
  235. size_t num_shifts = 0;
  236. while (ref_pitch < 0) {
  237. ref_pitch += kOctave;
  238. ++num_shifts;
  239. }
  240. uint32_t a = lut_oscillator_increments[ref_pitch >> 4];
  241. uint32_t b = lut_oscillator_increments[(ref_pitch >> 4) + 1];
  242. uint32_t phase_increment = a + \
  243. (static_cast<int32_t>(b - a) * (ref_pitch & 0xf) >> 4);
  244. phase_increment >>= num_shifts;
  245. return phase_increment;
  246. }
  247. void Oscillator::RenderSilence() {
  248. size_t size = kAudioBlockSize;
  249. while (size--) {
  250. audio_buffer_.Overwrite(offset_);
  251. }
  252. }
  253. void Oscillator::RenderSine(uint32_t phase_increment) {
  254. size_t size = kAudioBlockSize;
  255. while (size--) {
  256. phase_ += phase_increment;
  257. int32_t sample = Interpolate1022(wav_sine, phase_);
  258. audio_buffer_.Overwrite(offset_ - (scale_ * sample >> 16));
  259. }
  260. }
  261. void Oscillator::RenderNoise() {
  262. size_t size = kAudioBlockSize;
  263. while (size--) {
  264. int16_t sample = Random::GetSample();
  265. audio_buffer_.Overwrite(offset_ - (scale_ * sample >> 16));
  266. }
  267. }
  268. void Oscillator::RenderSaw(uint32_t phase_increment) {
  269. uint32_t phase = phase_;
  270. int32_t next_sample = next_sample_;
  271. size_t size = kAudioBlockSize;
  272. while (size--) {
  273. int32_t this_sample = next_sample;
  274. next_sample = 0;
  275. phase += phase_increment;
  276. if (phase < phase_increment) {
  277. uint32_t t = phase / (phase_increment >> 16);
  278. this_sample -= ThisBlepSample(t);
  279. next_sample -= NextBlepSample(t);
  280. }
  281. next_sample += phase >> 17;
  282. this_sample = (this_sample - 16384) << 1;
  283. audio_buffer_.Overwrite(offset_ - (scale_ * this_sample >> 16));
  284. }
  285. next_sample_ = next_sample;
  286. phase_ = phase;
  287. }
  288. void Oscillator::RenderSquare(
  289. uint32_t phase_increment,
  290. uint32_t pw,
  291. bool integrate) {
  292. uint32_t phase = phase_;
  293. int32_t next_sample = next_sample_;
  294. int32_t integrator_state = integrator_state_;
  295. int16_t integrator_coefficient = phase_increment >> 18;
  296. size_t size = kAudioBlockSize;
  297. while (size--) {
  298. int32_t this_sample = next_sample;
  299. next_sample = 0;
  300. phase += phase_increment;
  301. if (!high_) {
  302. if (phase >= pw) {
  303. uint32_t t = (phase - pw) / (phase_increment >> 16);
  304. this_sample += ThisBlepSample(t);
  305. next_sample += NextBlepSample(t);
  306. high_ = true;
  307. }
  308. }
  309. if (high_ && (phase < phase_increment)) {
  310. uint32_t t = phase / (phase_increment >> 16);
  311. this_sample -= ThisBlepSample(t);
  312. next_sample -= NextBlepSample(t);
  313. high_ = false;
  314. }
  315. next_sample += phase < pw ? 0 : 32767;
  316. this_sample = (this_sample - 16384) << 1;
  317. if (integrate) {
  318. integrator_state += integrator_coefficient * (this_sample - integrator_state) >> 15;
  319. this_sample = integrator_state << 3;
  320. }
  321. audio_buffer_.Overwrite(offset_ - (scale_ * this_sample >> 16));
  322. }
  323. integrator_state_ = integrator_state;
  324. next_sample_ = next_sample;
  325. phase_ = phase;
  326. }
  327. void Oscillator::Render(uint8_t mode, int16_t note, bool gate) {
  328. if (mode == 0 || audio_buffer_.writable() < kAudioBlockSize) {
  329. return;
  330. }
  331. if ((mode & 0x80) && !gate) {
  332. RenderSilence();
  333. return;
  334. }
  335. uint32_t phase_increment = ComputePhaseIncrement(note);
  336. switch ((mode & 0x0f) - 1) {
  337. case 0:
  338. RenderSaw(phase_increment);
  339. break;
  340. case 1:
  341. RenderSquare(phase_increment, 0x40000000, false);
  342. break;
  343. case 2:
  344. RenderSquare(phase_increment, 0x80000000, false);
  345. break;
  346. case 3:
  347. RenderSquare(phase_increment, 0x80000000, true);
  348. break;
  349. case 4:
  350. RenderSine(phase_increment);
  351. break;
  352. default:
  353. RenderNoise();
  354. break;
  355. }
  356. }
  357. } // namespace yarns