instrument.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. #include "instrument.h"
  2. #include "songinfo.h"
  3. void edit_instrument_param(FlizzerTrackerApp* tracker, uint8_t selected_param, int8_t delta) {
  4. if(!(tracker->current_digit)) {
  5. delta *= 16;
  6. }
  7. Instrument* inst = tracker->song.instrument[tracker->current_instrument];
  8. switch(selected_param) {
  9. case INST_CURRENTINSTRUMENT: {
  10. int16_t inst = tracker->current_instrument;
  11. int8_t inst_delta = delta > 0 ? 1 : -1;
  12. inst += inst_delta;
  13. clamp(inst, 0, 0, tracker->song.num_instruments);
  14. if(check_and_allocate_instrument(&tracker->song, (uint8_t)inst)) {
  15. tracker->current_instrument = inst;
  16. }
  17. break;
  18. }
  19. case INST_INSTRUMENTNAME: {
  20. text_input_set_header_text(tracker->text_input, "Instrument name:");
  21. text_input_set_result_callback(
  22. tracker->text_input,
  23. return_from_keyboard_callback,
  24. tracker,
  25. (char*)&inst->name,
  26. MUS_INST_NAME_LEN + 1,
  27. false);
  28. view_dispatcher_switch_to_view(tracker->view_dispatcher, VIEW_KEYBOARD);
  29. break;
  30. }
  31. case INST_CURRENT_NOTE: {
  32. int8_t note_delta = 0;
  33. if(delta < 0) {
  34. if(tracker->current_digit) {
  35. note_delta = -12;
  36. }
  37. else {
  38. note_delta = -1;
  39. }
  40. }
  41. if(delta > 0) {
  42. if(tracker->current_digit) {
  43. note_delta = 12;
  44. }
  45. else {
  46. note_delta = 1;
  47. }
  48. }
  49. clamp(inst->base_note, note_delta, 0, MAX_NOTE);
  50. break;
  51. }
  52. case INST_FINETUNE: {
  53. int8_t fine_delta = 0;
  54. if(delta < 0) {
  55. if(tracker->current_digit) {
  56. fine_delta = -1;
  57. }
  58. else {
  59. fine_delta = -10;
  60. }
  61. }
  62. if(delta > 0) {
  63. if(tracker->current_digit) {
  64. fine_delta = 1;
  65. }
  66. else {
  67. fine_delta = 10;
  68. }
  69. }
  70. inst->finetune += fine_delta;
  71. break;
  72. }
  73. case INST_SLIDESPEED: {
  74. if((int16_t)inst->slide_speed + (int16_t)delta >= 0 &&
  75. (int16_t)inst->slide_speed + (int16_t)delta <= 0xff) {
  76. inst->slide_speed += delta;
  77. }
  78. break;
  79. }
  80. case INST_SETPW: {
  81. flipbit(inst->flags, TE_SET_PW);
  82. break;
  83. }
  84. case INST_PW: {
  85. if((int16_t)inst->pw + (int16_t)delta >= 0 && (int16_t)inst->pw + (int16_t)delta <= 0xff) {
  86. inst->pw += delta;
  87. }
  88. break;
  89. }
  90. case INST_SETCUTOFF: {
  91. flipbit(inst->flags, TE_SET_CUTOFF);
  92. break;
  93. }
  94. case INST_WAVE_NOISE: {
  95. flipbit(inst->waveform, SE_WAVEFORM_NOISE);
  96. break;
  97. }
  98. case INST_WAVE_PULSE: {
  99. flipbit(inst->waveform, SE_WAVEFORM_PULSE);
  100. break;
  101. }
  102. case INST_WAVE_TRIANGLE: {
  103. flipbit(inst->waveform, SE_WAVEFORM_TRIANGLE);
  104. break;
  105. }
  106. case INST_WAVE_SAWTOOTH: {
  107. flipbit(inst->waveform, SE_WAVEFORM_SAW);
  108. break;
  109. }
  110. case INST_WAVE_NOISE_METAL: {
  111. flipbit(inst->waveform, SE_WAVEFORM_NOISE_METAL);
  112. break;
  113. }
  114. case INST_WAVE_SINE: {
  115. flipbit(inst->waveform, SE_WAVEFORM_SINE);
  116. break;
  117. }
  118. case INST_ATTACK: {
  119. if((int16_t)inst->adsr.a + (int16_t)delta >= 0 &&
  120. (int16_t)inst->adsr.a + (int16_t)delta <= 0xff) {
  121. inst->adsr.a += delta;
  122. }
  123. break;
  124. }
  125. case INST_DECAY: {
  126. if((int16_t)inst->adsr.d + (int16_t)delta >= 0 &&
  127. (int16_t)inst->adsr.d + (int16_t)delta <= 0xff) {
  128. inst->adsr.d += delta;
  129. }
  130. break;
  131. }
  132. case INST_SUSTAIN: {
  133. if((int16_t)inst->adsr.s + (int16_t)delta >= 0 &&
  134. (int16_t)inst->adsr.s + (int16_t)delta <= 0xff) {
  135. inst->adsr.s += delta;
  136. }
  137. break;
  138. }
  139. case INST_RELEASE: {
  140. if((int16_t)inst->adsr.r + (int16_t)delta >= 0 &&
  141. (int16_t)inst->adsr.r + (int16_t)delta <= 0xff) {
  142. inst->adsr.r += delta;
  143. }
  144. break;
  145. }
  146. case INST_VOLUME: {
  147. if((int16_t)inst->adsr.volume + (int16_t)delta >= 0 &&
  148. (int16_t)inst->adsr.volume + (int16_t)delta <= 0xff) {
  149. inst->adsr.volume += delta;
  150. }
  151. break;
  152. }
  153. case INST_ENABLEFILTER: {
  154. flipbit(inst->sound_engine_flags, SE_ENABLE_FILTER);
  155. break;
  156. }
  157. case INST_FILTERCUTOFF: {
  158. if((int16_t)inst->filter_cutoff + (int16_t)delta >= 0 &&
  159. (int16_t)inst->filter_cutoff + (int16_t)delta <= 0xff) {
  160. inst->filter_cutoff += delta;
  161. }
  162. break;
  163. }
  164. case INST_FILTERRESONANCE: {
  165. if((int16_t)inst->filter_resonance + (int16_t)delta >= 0 &&
  166. (int16_t)inst->filter_resonance + (int16_t)delta <= 0xff) {
  167. inst->filter_resonance += delta;
  168. }
  169. break;
  170. }
  171. case INST_FILTERTYPE: {
  172. int8_t flt_delta = (delta > 0 ? 1 : -1);
  173. if((int16_t)inst->filter_type + (int16_t)flt_delta >= 0 &&
  174. (int16_t)inst->filter_type + (int16_t)flt_delta < FIL_MODES) {
  175. inst->filter_type += flt_delta;
  176. }
  177. break;
  178. }
  179. case INST_ENABLERINGMOD: {
  180. flipbit(inst->sound_engine_flags, SE_ENABLE_RING_MOD);
  181. break;
  182. }
  183. case INST_RINGMODSRC: {
  184. if((int16_t)inst->ring_mod + (int16_t)delta >= 0 &&
  185. (int16_t)inst->ring_mod + (int16_t)delta < SONG_MAX_CHANNELS) {
  186. inst->ring_mod += delta;
  187. }
  188. if((int16_t)inst->ring_mod + (int16_t)delta < 0) {
  189. inst->ring_mod = 0xff; // 0xff = self
  190. }
  191. if((int16_t)inst->ring_mod == 0xff && (int16_t)delta > 0) {
  192. inst->ring_mod = 0;
  193. }
  194. break;
  195. }
  196. case INST_ENABLEHARDSYNC: {
  197. flipbit(inst->sound_engine_flags, SE_ENABLE_HARD_SYNC);
  198. break;
  199. }
  200. case INST_HARDSYNCSRC: {
  201. if((int16_t)inst->hard_sync + (int16_t)delta >= 0 &&
  202. (int16_t)inst->hard_sync + (int16_t)delta < SONG_MAX_CHANNELS) {
  203. inst->hard_sync += delta;
  204. }
  205. if((int16_t)inst->hard_sync + (int16_t)delta < 0) {
  206. inst->hard_sync = 0xff; // 0xff = self
  207. }
  208. if((int16_t)inst->hard_sync == 0xff && (int16_t)delta > 0) {
  209. inst->hard_sync = 0;
  210. }
  211. break;
  212. }
  213. case INST_RETRIGGERONSLIDE: {
  214. flipbit(inst->flags, TE_RETRIGGER_ON_SLIDE);
  215. break;
  216. }
  217. case INST_ENABLEKEYSYNC: {
  218. flipbit(inst->sound_engine_flags, SE_ENABLE_KEYDOWN_SYNC);
  219. break;
  220. }
  221. case INST_ENABLEVIBRATO: {
  222. flipbit(inst->flags, TE_ENABLE_VIBRATO);
  223. break;
  224. }
  225. case INST_VIBRATOSPEED: {
  226. if((int16_t)inst->vibrato_speed + (int16_t)delta >= 0 &&
  227. (int16_t)inst->vibrato_speed + (int16_t)delta <= 0xff) {
  228. inst->vibrato_speed += delta;
  229. }
  230. break;
  231. }
  232. case INST_VIBRATODEPTH: {
  233. if((int16_t)inst->vibrato_depth + (int16_t)delta >= 0 &&
  234. (int16_t)inst->vibrato_depth + (int16_t)delta <= 0xff) {
  235. inst->vibrato_depth += delta;
  236. }
  237. break;
  238. }
  239. case INST_VIBRATODELAY: {
  240. if((int16_t)inst->vibrato_delay + (int16_t)delta >= 0 &&
  241. (int16_t)inst->vibrato_delay + (int16_t)delta <= 0xff) {
  242. inst->vibrato_delay += delta;
  243. }
  244. break;
  245. }
  246. case INST_ENABLEPWM: {
  247. flipbit(inst->flags, TE_ENABLE_PWM);
  248. break;
  249. }
  250. case INST_PWMSPEED: {
  251. if((int16_t)inst->pwm_speed + (int16_t)delta >= 0 &&
  252. (int16_t)inst->pwm_speed + (int16_t)delta <= 0xff) {
  253. inst->pwm_speed += delta;
  254. }
  255. break;
  256. }
  257. case INST_PWMDEPTH: {
  258. if((int16_t)inst->pwm_depth + (int16_t)delta >= 0 &&
  259. (int16_t)inst->pwm_depth + (int16_t)delta <= 0xff) {
  260. inst->pwm_depth += delta;
  261. }
  262. break;
  263. }
  264. case INST_PWMDELAY: {
  265. if((int16_t)inst->pwm_delay + (int16_t)delta >= 0 &&
  266. (int16_t)inst->pwm_delay + (int16_t)delta <= 0xff) {
  267. inst->pwm_delay += delta;
  268. }
  269. break;
  270. }
  271. case INST_PROGRESTART: {
  272. flipbit(inst->flags, TE_PROG_NO_RESTART);
  273. break;
  274. }
  275. case INST_PROGRAMEPERIOD: {
  276. if((int16_t)inst->program_period + (int16_t)delta >= 0 &&
  277. (int16_t)inst->program_period + (int16_t)delta <= 0xff) {
  278. inst->program_period += delta;
  279. }
  280. break;
  281. }
  282. }
  283. }
  284. void instrument_edit_event(FlizzerTrackerApp* tracker, FlizzerTrackerEvent* event) {
  285. if(event->input.key == InputKeyOk && event->input.type == InputTypeShort &&
  286. !tracker->tracker_engine.playing) {
  287. tracker->editing = !(tracker->editing);
  288. return;
  289. }
  290. if(event->input.key == InputKeyOk && event->input.type == InputTypeLong && !tracker->editing) {
  291. reset_buffer(&tracker->sound_engine);
  292. tracker_engine_set_song(&tracker->tracker_engine, NULL);
  293. for(int i = 1; i < SONG_MAX_CHANNELS; i++) {
  294. tracker->tracker_engine.channel[i].channel_flags &= TEC_PLAYING;
  295. tracker->tracker_engine.sound_engine->channel[i].frequency = 0;
  296. tracker->tracker_engine.sound_engine->channel[i].waveform = 0;
  297. }
  298. Instrument* inst = tracker->song.instrument[tracker->current_instrument];
  299. tracker_engine_trigger_instrument_internal(
  300. &tracker->tracker_engine, 0, inst, (MIDDLE_C << 8));
  301. tracker->tracker_engine.playing = true;
  302. play();
  303. return;
  304. }
  305. if(event->input.key == InputKeyOk && event->input.type == InputTypeRelease &&
  306. !tracker->editing) {
  307. SoundEngineChannel* se_channel = &tracker->sound_engine.channel[0];
  308. sound_engine_enable_gate(&tracker->sound_engine, se_channel, false);
  309. return;
  310. }
  311. if(event->input.key == InputKeyRight && event->input.type == InputTypeShort) {
  312. switch(tracker->selected_param) {
  313. default: {
  314. tracker->current_digit++;
  315. if(tracker->current_digit > 1) {
  316. tracker->selected_param++;
  317. tracker->current_digit = 0;
  318. if(tracker->selected_param > INST_PARAMS - 1) {
  319. tracker->selected_param = 0;
  320. }
  321. }
  322. break;
  323. }
  324. case INST_CURRENTINSTRUMENT:
  325. case INST_INSTRUMENTNAME:
  326. case INST_SETPW:
  327. case INST_SETCUTOFF:
  328. case INST_WAVE_NOISE:
  329. case INST_WAVE_PULSE:
  330. case INST_WAVE_TRIANGLE:
  331. case INST_WAVE_SAWTOOTH:
  332. case INST_WAVE_NOISE_METAL:
  333. case INST_WAVE_SINE:
  334. case INST_ENABLEFILTER:
  335. case INST_FILTERTYPE:
  336. case INST_ENABLERINGMOD:
  337. case INST_RINGMODSRC:
  338. case INST_ENABLEHARDSYNC:
  339. case INST_HARDSYNCSRC:
  340. case INST_RETRIGGERONSLIDE:
  341. case INST_ENABLEKEYSYNC:
  342. case INST_ENABLEVIBRATO:
  343. case INST_ENABLEPWM:
  344. case INST_PROGRESTART: {
  345. tracker->selected_param++;
  346. tracker->current_digit = 1;
  347. if(tracker->selected_param > INST_PARAMS - 1) {
  348. tracker->selected_param = 0;
  349. }
  350. break;
  351. }
  352. }
  353. }
  354. if(event->input.key == InputKeyLeft && event->input.type == InputTypeShort) {
  355. switch(tracker->selected_param) {
  356. default: {
  357. tracker->current_digit--;
  358. if(tracker->current_digit > 1) // unsigned int overflow
  359. {
  360. tracker->selected_param--;
  361. tracker->current_digit = 1;
  362. if(tracker->selected_param > INST_PARAMS - 1) // unsigned int overflow
  363. {
  364. tracker->selected_param = INST_PARAMS - 1;
  365. }
  366. }
  367. break;
  368. }
  369. case INST_CURRENTINSTRUMENT:
  370. case INST_INSTRUMENTNAME:
  371. case INST_SETPW:
  372. case INST_SETCUTOFF:
  373. case INST_WAVE_NOISE:
  374. case INST_WAVE_PULSE:
  375. case INST_WAVE_TRIANGLE:
  376. case INST_WAVE_SAWTOOTH:
  377. case INST_WAVE_NOISE_METAL:
  378. case INST_WAVE_SINE:
  379. case INST_ENABLEFILTER:
  380. case INST_FILTERTYPE:
  381. case INST_ENABLERINGMOD:
  382. case INST_RINGMODSRC:
  383. case INST_ENABLEHARDSYNC:
  384. case INST_HARDSYNCSRC:
  385. case INST_RETRIGGERONSLIDE:
  386. case INST_ENABLEKEYSYNC:
  387. case INST_ENABLEVIBRATO:
  388. case INST_ENABLEPWM:
  389. case INST_PROGRESTART: {
  390. tracker->selected_param--;
  391. tracker->current_digit = 1;
  392. if(tracker->selected_param > INST_PARAMS - 1) // unsigned int overflow
  393. {
  394. tracker->selected_param = INST_PARAMS - 1;
  395. }
  396. break;
  397. }
  398. }
  399. return;
  400. }
  401. if(event->input.key == InputKeyDown && event->input.type == InputTypeShort) {
  402. if(tracker->editing) {
  403. edit_instrument_param(tracker, tracker->selected_param, -1);
  404. }
  405. return;
  406. }
  407. if(event->input.key == InputKeyUp && event->input.type == InputTypeShort) {
  408. if(tracker->editing) {
  409. edit_instrument_param(tracker, tracker->selected_param, 1);
  410. }
  411. return;
  412. }
  413. if(tracker->selected_param > INST_VIBRATODELAY) {
  414. tracker->inst_editor_shift = 6;
  415. }
  416. if(tracker->selected_param > INST_PWMDELAY) {
  417. tracker->inst_editor_shift = 12;
  418. }
  419. if(tracker->selected_param < INST_CURRENT_NOTE) {
  420. tracker->inst_editor_shift = 0;
  421. }
  422. }