voices.c 12 KB

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