do_effects.c 11 KB

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