voices.c 11 KB

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