do_effects.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #include "do_effects.h"
  2. #include <furi.h>
  3. #include "../sound_engine/sound_engine.h"
  4. #include "../sound_engine/sound_engine_filter.h"
  5. #include "tracker_engine.h"
  6. #include "../flizzer_tracker_hal.h"
  7. void do_command(
  8. uint16_t opcode,
  9. TrackerEngine* tracker_engine,
  10. uint8_t channel,
  11. uint8_t tick,
  12. bool from_program) {
  13. UNUSED(from_program);
  14. TrackerEngineChannel* te_channel = &tracker_engine->channel[channel];
  15. SoundEngineChannel* se_channel = &tracker_engine->sound_engine->channel[channel];
  16. switch(opcode & 0x7f00) {
  17. case TE_EFFECT_ARPEGGIO: {
  18. if(tick == 0) {
  19. if(te_channel->fixed_note != 0xffff) {
  20. te_channel->note = te_channel->last_note;
  21. te_channel->fixed_note = 0xffff;
  22. }
  23. if((opcode & 0xff) == 0xf0)
  24. te_channel->arpeggio_note = te_channel->extarp1;
  25. else if((opcode & 0xff) == 0xf1)
  26. te_channel->arpeggio_note = te_channel->extarp2;
  27. else
  28. te_channel->arpeggio_note = (opcode & 0xff);
  29. }
  30. break;
  31. }
  32. case TE_EFFECT_PORTAMENTO_UP: {
  33. uint32_t prev = te_channel->note;
  34. te_channel->note += ((opcode & 0xff) << 2);
  35. if(prev > te_channel->note) te_channel->note = 0xffff;
  36. te_channel->target_note = te_channel->note;
  37. break;
  38. }
  39. case TE_EFFECT_PORTAMENTO_DOWN: {
  40. int32_t prev = te_channel->note;
  41. te_channel->note -= ((opcode & 0xff) << 2);
  42. if(prev < te_channel->note) te_channel->note = 0;
  43. te_channel->target_note = te_channel->note;
  44. break;
  45. }
  46. case TE_EFFECT_VIBRATO: {
  47. if(tick == 0) {
  48. if(opcode & 0xff) {
  49. te_channel->flags |= TE_ENABLE_VIBRATO;
  50. te_channel->vibrato_speed = (opcode & 0xf0);
  51. te_channel->vibrato_depth = ((opcode & 0x0f) << 4);
  52. }
  53. else {
  54. te_channel->flags &= ~(TE_ENABLE_VIBRATO);
  55. }
  56. }
  57. break;
  58. }
  59. case TE_EFFECT_PWM: {
  60. if(tick == 0) {
  61. if(opcode & 0xff) {
  62. te_channel->flags |= TE_ENABLE_PWM;
  63. te_channel->pwm_speed = (opcode & 0xf0);
  64. te_channel->pwm_depth = ((opcode & 0x0f) << 4);
  65. }
  66. else {
  67. te_channel->flags &= ~(TE_ENABLE_PWM);
  68. }
  69. }
  70. break;
  71. }
  72. case TE_EFFECT_SET_PW: {
  73. if(tick == 0) {
  74. te_channel->pw = ((opcode & 0xff) << 4);
  75. }
  76. break;
  77. }
  78. case TE_EFFECT_PW_UP: {
  79. int16_t temp_pw = te_channel->pw + (int16_t)(opcode & 0xff);
  80. if(temp_pw < 0) temp_pw = 0;
  81. if(temp_pw > 0xfff) temp_pw = 0xfff;
  82. te_channel->pw = temp_pw;
  83. break;
  84. }
  85. case TE_EFFECT_PW_DOWN: {
  86. int16_t temp_pw = te_channel->pw - (int16_t)(opcode & 0xff);
  87. if(temp_pw < 0) temp_pw = 0;
  88. if(temp_pw > 0xfff) temp_pw = 0xfff;
  89. te_channel->pw = temp_pw;
  90. break;
  91. }
  92. case TE_EFFECT_SET_CUTOFF: {
  93. if(tick == 0) {
  94. te_channel->filter_cutoff = ((opcode & 0xff) << 3);
  95. sound_engine_filter_set_coeff(
  96. &se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
  97. }
  98. break;
  99. }
  100. case TE_EFFECT_VOLUME_FADE: {
  101. if(!(te_channel->channel_flags & TEC_DISABLED)) {
  102. te_channel->volume -= (opcode & 0xf);
  103. if(te_channel->volume > MAX_ADSR_VOLUME) te_channel->volume = 0;
  104. te_channel->volume += ((opcode >> 4) & 0xf);
  105. if(te_channel->volume > MAX_ADSR_VOLUME) te_channel->volume = MAX_ADSR_VOLUME;
  106. se_channel->adsr.volume = (int32_t)te_channel->volume;
  107. se_channel->adsr.volume = (int32_t)se_channel->adsr.volume *
  108. (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  109. }
  110. break;
  111. }
  112. case TE_EFFECT_SET_WAVEFORM: {
  113. if(tick == 0) {
  114. se_channel->waveform = (opcode & 0x3f);
  115. }
  116. break;
  117. }
  118. case TE_EFFECT_SET_VOLUME: {
  119. if(tick == 0) {
  120. if(!(te_channel->channel_flags & TEC_DISABLED)) {
  121. te_channel->volume = opcode & 0xff;
  122. se_channel->adsr.volume = (int32_t)te_channel->volume;
  123. se_channel->adsr.volume = (int32_t)se_channel->adsr.volume *
  124. (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  125. }
  126. }
  127. break;
  128. }
  129. case TE_EFFECT_EXT: {
  130. switch(opcode & 0x7ff0) {
  131. case TE_EFFECT_EXT_TOGGLE_FILTER: {
  132. if(tick == 0) {
  133. if(opcode & 0xf) {
  134. se_channel->flags |= SE_ENABLE_FILTER;
  135. }
  136. else {
  137. se_channel->flags &= ~SE_ENABLE_FILTER;
  138. }
  139. }
  140. break;
  141. }
  142. case TE_EFFECT_EXT_PORTA_DN: {
  143. if(tick == 0) {
  144. int32_t prev = te_channel->note;
  145. te_channel->note -= (opcode & 0xf);
  146. if(prev < te_channel->note) te_channel->note = 0;
  147. te_channel->target_note = te_channel->note;
  148. }
  149. break;
  150. }
  151. case TE_EFFECT_EXT_PORTA_UP: {
  152. if(tick == 0) {
  153. uint32_t prev = te_channel->note;
  154. te_channel->note += (opcode & 0xf);
  155. if(prev > te_channel->note) te_channel->note = 0xffff;
  156. te_channel->target_note = te_channel->note;
  157. }
  158. break;
  159. }
  160. case TE_EFFECT_EXT_FILTER_MODE: {
  161. if(tick == 0) {
  162. se_channel->filter_mode = (opcode & 0xf);
  163. }
  164. break;
  165. }
  166. case TE_EFFECT_EXT_RETRIGGER: {
  167. if((opcode & 0xf) > 0 && (tick % (opcode & 0xf)) == 0) {
  168. uint8_t prev_vol_tr = te_channel->volume;
  169. uint8_t prev_vol_cyd = se_channel->adsr.volume;
  170. tracker_engine_trigger_instrument_internal(
  171. tracker_engine, channel, te_channel->instrument, te_channel->last_note);
  172. te_channel->volume = prev_vol_tr;
  173. se_channel->adsr.volume = prev_vol_cyd;
  174. }
  175. break;
  176. }
  177. case TE_EFFECT_EXT_FINE_VOLUME_DOWN: {
  178. if(tick == 0) {
  179. te_channel->volume -= (opcode & 0xf);
  180. if(te_channel->volume > MAX_ADSR_VOLUME) te_channel->volume = 0;
  181. se_channel->adsr.volume = (int32_t)te_channel->volume;
  182. se_channel->adsr.volume = (int32_t)se_channel->adsr.volume *
  183. (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  184. }
  185. break;
  186. }
  187. case TE_EFFECT_EXT_FINE_VOLUME_UP: {
  188. if(tick == 0) {
  189. te_channel->volume += (opcode & 0xf);
  190. if(te_channel->volume > MAX_ADSR_VOLUME) te_channel->volume = MAX_ADSR_VOLUME;
  191. se_channel->adsr.volume = (int32_t)te_channel->volume;
  192. se_channel->adsr.volume = (int32_t)se_channel->adsr.volume *
  193. (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  194. }
  195. break;
  196. }
  197. case TE_EFFECT_EXT_NOTE_CUT: {
  198. if((opcode & 0xf) <= tick) {
  199. se_channel->adsr.volume = 0;
  200. te_channel->volume = 0;
  201. }
  202. break;
  203. }
  204. case TE_EFFECT_EXT_PHASE_RESET: {
  205. if(tick == (opcode & 0xf)) {
  206. se_channel->accumulator = 0;
  207. se_channel->lfsr = RANDOM_SEED;
  208. }
  209. break;
  210. }
  211. }
  212. break;
  213. }
  214. case TE_EFFECT_SET_SPEED_PROG_PERIOD: {
  215. if(tick == 0) {
  216. if(from_program) {
  217. te_channel->program_period = opcode & 0xff;
  218. }
  219. else {
  220. tracker_engine->song->speed = opcode & 0xff;
  221. }
  222. }
  223. break;
  224. }
  225. case TE_EFFECT_CUTOFF_UP: {
  226. te_channel->filter_cutoff += (opcode & 0xff);
  227. if(te_channel->filter_cutoff > 0x7ff) {
  228. te_channel->filter_cutoff = 0x7ff;
  229. }
  230. sound_engine_filter_set_coeff(
  231. &se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
  232. break;
  233. }
  234. case TE_EFFECT_CUTOFF_DOWN: {
  235. te_channel->filter_cutoff -= (opcode & 0xff);
  236. if(te_channel->filter_cutoff > 0x7ff) // unsigned int overflow
  237. {
  238. te_channel->filter_cutoff = 0;
  239. }
  240. sound_engine_filter_set_coeff(
  241. &se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
  242. break;
  243. }
  244. case TE_EFFECT_SET_RESONANCE: {
  245. if(tick == 0) {
  246. te_channel->filter_resonance = (opcode & 0xff);
  247. sound_engine_filter_set_coeff(
  248. &se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
  249. }
  250. break;
  251. }
  252. case TE_EFFECT_RESONANCE_UP: {
  253. te_channel->filter_resonance += (opcode & 0xff);
  254. if(te_channel->filter_resonance > 0xff) {
  255. te_channel->filter_resonance = 0xff;
  256. }
  257. sound_engine_filter_set_coeff(
  258. &se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
  259. break;
  260. }
  261. case TE_EFFECT_RESONANCE_DOWN: {
  262. te_channel->filter_resonance -= (opcode & 0xff);
  263. if(te_channel->filter_resonance > 0xff) {
  264. te_channel->filter_resonance = 0;
  265. }
  266. sound_engine_filter_set_coeff(
  267. &se_channel->filter, te_channel->filter_cutoff, te_channel->filter_resonance);
  268. break;
  269. }
  270. case TE_EFFECT_SET_RING_MOD_SRC: {
  271. if(tick == 0) {
  272. se_channel->ring_mod = (opcode & 0xff);
  273. }
  274. break;
  275. }
  276. case TE_EFFECT_SET_HARD_SYNC_SRC: {
  277. if(tick == 0) {
  278. se_channel->hard_sync = (opcode & 0xff);
  279. }
  280. break;
  281. }
  282. case TE_EFFECT_SET_ATTACK: {
  283. if(tick == 0) {
  284. se_channel->adsr.a = (opcode & 0xff);
  285. if(se_channel->adsr.envelope_state == ATTACK) {
  286. se_channel->adsr.envelope_speed =
  287. envspd(tracker_engine->sound_engine, se_channel->adsr.a);
  288. }
  289. }
  290. break;
  291. }
  292. case TE_EFFECT_SET_DECAY: {
  293. if(tick == 0) {
  294. se_channel->adsr.d = (opcode & 0xff);
  295. if(se_channel->adsr.envelope_state == DECAY) {
  296. se_channel->adsr.envelope_speed =
  297. envspd(tracker_engine->sound_engine, se_channel->adsr.d);
  298. }
  299. }
  300. break;
  301. }
  302. case TE_EFFECT_SET_SUSTAIN: {
  303. if(tick == 0) {
  304. se_channel->adsr.s = (opcode & 0xff);
  305. }
  306. break;
  307. }
  308. case TE_EFFECT_SET_RELEASE: {
  309. if(tick == 0) {
  310. se_channel->adsr.r = (opcode & 0xff);
  311. if(se_channel->adsr.envelope_state == RELEASE) {
  312. se_channel->adsr.envelope_speed =
  313. envspd(tracker_engine->sound_engine, se_channel->adsr.r);
  314. }
  315. }
  316. break;
  317. }
  318. case TE_EFFECT_PROGRAM_RESTART: {
  319. if(tick == 0) {
  320. te_channel->program_counter = 0;
  321. te_channel->program_loop = 0;
  322. te_channel->program_period = 0;
  323. te_channel->program_tick = 0;
  324. }
  325. break;
  326. }
  327. case TE_EFFECT_SET_RATE: {
  328. if(tick == 0 && (opcode & 0xff) > 0) {
  329. tracker_engine_set_rate(opcode & 0xff);
  330. }
  331. break;
  332. }
  333. case TE_EFFECT_PORTA_UP_SEMITONE: {
  334. uint32_t prev = te_channel->note;
  335. te_channel->note += ((opcode & 0xff) << 8);
  336. if(prev > te_channel->note) te_channel->note = 0xffff;
  337. te_channel->target_note = te_channel->note;
  338. break;
  339. }
  340. case TE_EFFECT_PORTA_DOWN_SEMITONE: {
  341. int32_t prev = te_channel->note;
  342. te_channel->note -= ((opcode & 0xff) << 8);
  343. if(prev < te_channel->note) te_channel->note = 0;
  344. te_channel->target_note = te_channel->note;
  345. break;
  346. }
  347. case TE_EFFECT_ARPEGGIO_ABS: {
  348. te_channel->arpeggio_note = 0;
  349. te_channel->fixed_note = ((opcode & 0xff) << 8);
  350. break;
  351. }
  352. case TE_EFFECT_TRIGGER_RELEASE: {
  353. sound_engine_enable_gate(tracker_engine->sound_engine, se_channel, 0);
  354. break;
  355. }
  356. default:
  357. break;
  358. }
  359. }