do_effects.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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_PORTA_DN:
  160. {
  161. if (tick == 0)
  162. {
  163. int32_t prev = te_channel->note;
  164. te_channel->note -= opcode & 0xf;
  165. if (prev < te_channel->note)
  166. te_channel->note = 0;
  167. te_channel->target_note = te_channel->note;
  168. }
  169. break;
  170. }
  171. case TE_EFFECT_EXT_PORTA_UP:
  172. {
  173. if (tick == 0)
  174. {
  175. uint32_t prev = te_channel->note;
  176. te_channel->note += opcode & 0xf;
  177. if (prev > te_channel->note)
  178. te_channel->note = 0xffff;
  179. te_channel->target_note = te_channel->note;
  180. }
  181. break;
  182. }
  183. case TE_EFFECT_EXT_RETRIGGER:
  184. {
  185. if ((opcode & 0xf) > 0 && (tick % (opcode & 0xf)) == 0)
  186. {
  187. uint8_t prev_vol_tr = te_channel->volume;
  188. uint8_t prev_vol_cyd = se_channel->adsr.volume;
  189. tracker_engine_trigger_instrument_internal(tracker_engine, channel, te_channel->instrument, te_channel->last_note);
  190. te_channel->volume = prev_vol_tr;
  191. se_channel->adsr.volume = prev_vol_cyd;
  192. }
  193. break;
  194. }
  195. case TE_EFFECT_EXT_FINE_VOLUME_DOWN:
  196. {
  197. if (tick == 0)
  198. {
  199. te_channel->volume -= opcode & 0xf;
  200. if (te_channel->volume > MAX_ADSR_VOLUME)
  201. te_channel->volume = 0;
  202. 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;
  203. se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  204. }
  205. break;
  206. }
  207. case TE_EFFECT_EXT_FINE_VOLUME_UP:
  208. {
  209. if (tick == 0)
  210. {
  211. te_channel->volume += opcode & 0xf;
  212. if (te_channel->volume > MAX_ADSR_VOLUME)
  213. te_channel->volume = MAX_ADSR_VOLUME;
  214. 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;
  215. se_channel->adsr.volume = (int32_t)se_channel->adsr.volume * (int32_t)tracker_engine->master_volume / MAX_ADSR_VOLUME;
  216. }
  217. break;
  218. }
  219. case TE_EFFECT_EXT_NOTE_CUT:
  220. {
  221. if ((opcode & 0xf) <= tick)
  222. {
  223. se_channel->adsr.volume = 0;
  224. te_channel->volume = 0;
  225. }
  226. break;
  227. }
  228. }
  229. break;
  230. }
  231. case TE_EFFECT_SET_SPEED_PROG_PERIOD:
  232. {
  233. if (tick == 0)
  234. {
  235. if (from_program)
  236. {
  237. te_channel->program_period = opcode & 0xff;
  238. }
  239. else
  240. {
  241. tracker_engine->song->speed = opcode & 0xff;
  242. }
  243. }
  244. break;
  245. }
  246. default:
  247. break;
  248. }
  249. }