voices.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* Copyright 2016 Jack Humbert
  2. * Copyright 2020 JohSchneider
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "voices.h"
  18. #include "audio.h"
  19. #include <stdlib.h>
  20. uint8_t note_timbre = TIMBRE_DEFAULT;
  21. bool glissando = false;
  22. bool vibrato = false;
  23. float vibrato_strength = 0.5;
  24. float vibrato_rate = 0.125;
  25. uint16_t voices_timer = 0;
  26. #ifdef AUDIO_VOICE_DEFAULT
  27. voice_type voice = AUDIO_VOICE_DEFAULT;
  28. #else
  29. voice_type voice = default_voice;
  30. #endif
  31. void set_voice(voice_type v) { voice = v; }
  32. void voice_iterate() { voice = (voice + 1) % number_of_voices; }
  33. void voice_deiterate() { voice = (voice - 1 + number_of_voices) % number_of_voices; }
  34. #ifdef AUDIO_VOICES
  35. float mod(float a, int b) {
  36. float r = fmod(a, b);
  37. return r < 0 ? r + b : r;
  38. }
  39. // Effect: 'vibrate' a given target frequency slightly above/below its initial value
  40. float voice_add_vibrato(float average_freq) {
  41. float vibrato_counter = mod(timer_read() / (100 * vibrato_rate), VIBRATO_LUT_LENGTH);
  42. return average_freq * pow(vibrato_lut[(int)vibrato_counter], vibrato_strength);
  43. }
  44. // Effect: 'slides' the 'frequency' from the starting-point, to the target frequency
  45. float voice_add_glissando(float from_freq, float to_freq) {
  46. if (to_freq != 0 && from_freq < to_freq && from_freq < to_freq * pow(2, -440 / to_freq / 12 / 2)) {
  47. return from_freq * pow(2, 440 / from_freq / 12 / 2);
  48. } else if (to_freq != 0 && from_freq > to_freq && from_freq > to_freq * pow(2, 440 / to_freq / 12 / 2)) {
  49. return from_freq * pow(2, -440 / from_freq / 12 / 2);
  50. } else {
  51. return to_freq;
  52. }
  53. }
  54. #endif
  55. float voice_envelope(float frequency) {
  56. // envelope_index ranges from 0 to 0xFFFF, which is preserved at 880.0 Hz
  57. // __attribute__((unused)) uint16_t compensated_index = (uint16_t)((float)envelope_index * (880.0 / frequency));
  58. #ifdef AUDIO_VOICES
  59. uint16_t envelope_index = timer_elapsed(voices_timer); // TODO: multiply in some factor?
  60. uint16_t compensated_index = envelope_index / 100; // TODO: correct factor would be?
  61. #endif
  62. switch (voice) {
  63. case default_voice:
  64. glissando = false;
  65. // note_timbre = TIMBRE_50; //Note: leave the user the possibility to adjust the timbre with 'audio_set_timbre'
  66. break;
  67. #ifdef AUDIO_VOICES
  68. case vibrating:
  69. glissando = false;
  70. vibrato = true;
  71. break;
  72. case something:
  73. glissando = false;
  74. switch (compensated_index) {
  75. case 0 ... 9:
  76. note_timbre = TIMBRE_12;
  77. break;
  78. case 10 ... 19:
  79. note_timbre = TIMBRE_25;
  80. break;
  81. case 20 ... 200:
  82. note_timbre = 12 + 12;
  83. break;
  84. default:
  85. note_timbre = 12;
  86. break;
  87. }
  88. break;
  89. case drums:
  90. glissando = false;
  91. // switch (compensated_index) {
  92. // case 0 ... 10:
  93. // note_timbre = 50;
  94. // break;
  95. // case 11 ... 20:
  96. // note_timbre = 50 * (21 - compensated_index) / 10;
  97. // break;
  98. // default:
  99. // note_timbre = 0;
  100. // break;
  101. // }
  102. // frequency = (rand() % (int)(frequency * 1.2 - frequency)) + (frequency * 0.8);
  103. if (frequency < 80.0) {
  104. } else if (frequency < 160.0) {
  105. // Bass drum: 60 - 100 Hz
  106. frequency = (rand() % (int)(40)) + 60;
  107. switch (envelope_index) {
  108. case 0 ... 10:
  109. note_timbre = 50;
  110. break;
  111. case 11 ... 20:
  112. note_timbre = 50 * (21 - envelope_index) / 10;
  113. break;
  114. default:
  115. note_timbre = 0;
  116. break;
  117. }
  118. } else if (frequency < 320.0) {
  119. // Snare drum: 1 - 2 KHz
  120. frequency = (rand() % (int)(1000)) + 1000;
  121. switch (envelope_index) {
  122. case 0 ... 5:
  123. note_timbre = 50;
  124. break;
  125. case 6 ... 20:
  126. note_timbre = 50 * (21 - envelope_index) / 15;
  127. break;
  128. default:
  129. note_timbre = 0;
  130. break;
  131. }
  132. } else if (frequency < 640.0) {
  133. // Closed Hi-hat: 3 - 5 KHz
  134. frequency = (rand() % (int)(2000)) + 3000;
  135. switch (envelope_index) {
  136. case 0 ... 15:
  137. note_timbre = 50;
  138. break;
  139. case 16 ... 20:
  140. note_timbre = 50 * (21 - envelope_index) / 5;
  141. break;
  142. default:
  143. note_timbre = 0;
  144. break;
  145. }
  146. } else if (frequency < 1280.0) {
  147. // Open Hi-hat: 3 - 5 KHz
  148. frequency = (rand() % (int)(2000)) + 3000;
  149. switch (envelope_index) {
  150. case 0 ... 35:
  151. note_timbre = 50;
  152. break;
  153. case 36 ... 50:
  154. note_timbre = 50 * (51 - envelope_index) / 15;
  155. break;
  156. default:
  157. note_timbre = 0;
  158. break;
  159. }
  160. }
  161. break;
  162. case butts_fader:
  163. glissando = true;
  164. switch (compensated_index) {
  165. case 0 ... 9:
  166. frequency = frequency / 4;
  167. note_timbre = TIMBRE_12;
  168. break;
  169. case 10 ... 19:
  170. frequency = frequency / 2;
  171. note_timbre = TIMBRE_12;
  172. break;
  173. case 20 ... 200:
  174. note_timbre = 12 - (uint8_t)(pow(((float)compensated_index - 20) / (200 - 20), 2) * 12.5);
  175. break;
  176. default:
  177. note_timbre = 0;
  178. break;
  179. }
  180. break;
  181. // case octave_crunch:
  182. // switch (compensated_index) {
  183. // case 0 ... 9:
  184. // case 20 ... 24:
  185. // case 30 ... 32:
  186. // frequency = frequency / 2;
  187. // note_timbre = TIMBRE_12;
  188. // break;
  189. // case 10 ... 19:
  190. // case 25 ... 29:
  191. // case 33 ... 35:
  192. // frequency = frequency * 2;
  193. // note_timbre = TIMBRE_12;
  194. // break;
  195. // default:
  196. // note_timbre = TIMBRE_12;
  197. // break;
  198. // }
  199. // break;
  200. case duty_osc:
  201. // This slows the loop down a substantial amount, so higher notes may freeze
  202. glissando = true;
  203. switch (compensated_index) {
  204. default:
  205. # define OCS_SPEED 10
  206. # define OCS_AMP .25
  207. // sine wave is slow
  208. // note_timbre = (sin((float)compensated_index/10000*OCS_SPEED) * OCS_AMP / 2) + .5;
  209. // triangle wave is a bit faster
  210. note_timbre = (uint8_t)abs((compensated_index * OCS_SPEED % 3000) - 1500) * (OCS_AMP / 1500) + (1 - OCS_AMP) / 2;
  211. break;
  212. }
  213. break;
  214. case duty_octave_down:
  215. glissando = true;
  216. note_timbre = (uint8_t)(100 * (envelope_index % 2) * .125 + .375 * 2);
  217. if ((envelope_index % 4) == 0) note_timbre = 50;
  218. if ((envelope_index % 8) == 0) note_timbre = 0;
  219. break;
  220. case delayed_vibrato:
  221. glissando = true;
  222. note_timbre = TIMBRE_50;
  223. # define VOICE_VIBRATO_DELAY 150
  224. # define VOICE_VIBRATO_SPEED 50
  225. switch (compensated_index) {
  226. case 0 ... VOICE_VIBRATO_DELAY:
  227. break;
  228. default:
  229. // TODO: merge/replace with voice_add_vibrato above
  230. frequency = frequency * vibrato_lut[(int)fmod((((float)compensated_index - (VOICE_VIBRATO_DELAY + 1)) / 1000 * VOICE_VIBRATO_SPEED), VIBRATO_LUT_LENGTH)];
  231. break;
  232. }
  233. break;
  234. // case delayed_vibrato_octave:
  235. // if ((envelope_index % 2) == 1) {
  236. // note_timbre = 55;
  237. // } else {
  238. // note_timbre = 45;
  239. // }
  240. // #define VOICE_VIBRATO_DELAY 150
  241. // #define VOICE_VIBRATO_SPEED 50
  242. // switch (compensated_index) {
  243. // case 0 ... VOICE_VIBRATO_DELAY:
  244. // break;
  245. // default:
  246. // frequency = frequency * VIBRATO_LUT[(int)fmod((((float)compensated_index - (VOICE_VIBRATO_DELAY + 1))/1000*VOICE_VIBRATO_SPEED), VIBRATO_LUT_LENGTH)];
  247. // break;
  248. // }
  249. // break;
  250. // case duty_fifth_down:
  251. // note_timbre = TIMBRE_50;
  252. // if ((envelope_index % 3) == 0)
  253. // note_timbre = TIMBRE_75;
  254. // break;
  255. // case duty_fourth_down:
  256. // note_timbre = 0;
  257. // if ((envelope_index % 12) == 0)
  258. // note_timbre = TIMBRE_75;
  259. // if (((envelope_index % 12) % 4) != 1)
  260. // note_timbre = TIMBRE_75;
  261. // break;
  262. // case duty_third_down:
  263. // note_timbre = TIMBRE_50;
  264. // if ((envelope_index % 5) == 0)
  265. // note_timbre = TIMBRE_75;
  266. // break;
  267. // case duty_fifth_third_down:
  268. // note_timbre = TIMBRE_50;
  269. // if ((envelope_index % 5) == 0)
  270. // note_timbre = TIMBRE_75;
  271. // if ((envelope_index % 3) == 0)
  272. // note_timbre = TIMBRE_25;
  273. // break;
  274. #endif // AUDIO_VOICES
  275. default:
  276. break;
  277. }
  278. #ifdef AUDIO_VOICES
  279. if (vibrato && (vibrato_strength > 0)) {
  280. frequency = voice_add_vibrato(frequency);
  281. }
  282. if (glissando) {
  283. // TODO: where to keep track of the start-frequency?
  284. // frequency = voice_add_glissando(??, frequency);
  285. }
  286. #endif // AUDIO_VOICES
  287. return frequency;
  288. }
  289. // Vibrato functions
  290. void voice_set_vibrato_rate(float rate) { vibrato_rate = rate; }
  291. void voice_increase_vibrato_rate(float change) { vibrato_rate *= change; }
  292. void voice_decrease_vibrato_rate(float change) { vibrato_rate /= change; }
  293. void voice_set_vibrato_strength(float strength) { vibrato_strength = strength; }
  294. void voice_increase_vibrato_strength(float change) { vibrato_strength *= change; }
  295. void voice_decrease_vibrato_strength(float change) { vibrato_strength /= change; }
  296. // Timbre functions
  297. void voice_set_timbre(uint8_t timbre) {
  298. if ((timbre > 0) && (timbre < 100)) {
  299. note_timbre = timbre;
  300. }
  301. }
  302. uint8_t voice_get_timbre(void) { return note_timbre; }