logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

voices.c (12341B)


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