instrument.c 13 KB

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