instrument.c 15 KB

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