instrument.c 15 KB

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