do_effects.c 15 KB

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