do_effects.c 12 KB

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