instrument_program.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "instrument_program.h"
  2. #include "../macros.h"
  3. void instrument_program_edit_event(FlizzerTrackerApp *tracker, FlizzerTrackerEvent *event)
  4. {
  5. if (event->input.key == InputKeyOk && event->input.type == InputTypeShort)
  6. {
  7. tracker->editing = !(tracker->editing);
  8. return;
  9. }
  10. if (event->input.key == InputKeyRight && event->input.type == InputTypeShort && tracker->editing)
  11. {
  12. tracker->current_digit = my_min(2, tracker->current_digit + 1);
  13. return;
  14. }
  15. if (event->input.key == InputKeyOk && event->input.type == InputTypeLong && tracker->editing)
  16. {
  17. Instrument *inst = tracker->song.instrument[tracker->current_instrument];
  18. if (tracker->current_program_step < INST_PROG_LEN - 1)
  19. {
  20. if ((inst->program[tracker->current_program_step] & 0x7fff) < TE_PROGRAM_LOOP_BEGIN && ((inst->program[tracker->current_program_step + 1] & 0x7fff) < TE_PROGRAM_LOOP_BEGIN || (inst->program[tracker->current_program_step + 1] & 0x7f00) == TE_PROGRAM_LOOP_END)) // so we can unite with loop end as in klystrack
  21. {
  22. inst->program[tracker->current_program_step] ^= 0x8000; // flipping unite bit
  23. }
  24. }
  25. return;
  26. }
  27. if (event->input.key == InputKeyLeft && event->input.type == InputTypeShort && tracker->editing)
  28. {
  29. tracker->current_digit = fmax(0, (int16_t)tracker->current_digit - 1);
  30. return;
  31. }
  32. if (event->input.key == InputKeyBack && event->input.type == InputTypeShort && tracker->editing)
  33. {
  34. Instrument *inst = tracker->song.instrument[tracker->current_instrument];
  35. inst->program[tracker->current_program_step] = TE_PROGRAM_NOP;
  36. }
  37. if (event->input.key == InputKeyUp && event->input.type == InputTypeShort)
  38. {
  39. if (!(tracker->editing))
  40. {
  41. if ((int16_t)tracker->current_program_step - 1 >= 0)
  42. {
  43. tracker->current_program_step--;
  44. if (tracker->program_position > tracker->current_program_step)
  45. {
  46. tracker->program_position = tracker->current_program_step;
  47. }
  48. }
  49. else
  50. {
  51. tracker->current_program_step = INST_PROG_LEN - 1;
  52. tracker->program_position = INST_PROG_LEN - 1 - 7;
  53. }
  54. }
  55. if (tracker->editing)
  56. {
  57. Instrument *inst = tracker->song.instrument[tracker->current_instrument];
  58. uint16_t opcode = inst->program[tracker->current_program_step];
  59. switch (tracker->current_digit)
  60. {
  61. case 0: // MSB
  62. {
  63. uint8_t param = ((opcode & 0x7f00) >> 8);
  64. if (param < 0xff)
  65. {
  66. param++;
  67. }
  68. if ((inst->program[tracker->current_program_step] & 0x7fff) == TE_PROGRAM_NOP)
  69. {
  70. param = 0;
  71. inst->program[tracker->current_program_step] = 0;
  72. }
  73. param &= 0x7f;
  74. inst->program[tracker->current_program_step] &= 0x80ff;
  75. inst->program[tracker->current_program_step] |= ((uint16_t)param << 8);
  76. break;
  77. }
  78. case 1: // upper digit of param, e.g. eXx
  79. {
  80. int8_t nibble = ((opcode & 0x00f0) >> 4);
  81. if (nibble + 1 <= 0xf)
  82. {
  83. nibble++;
  84. }
  85. else
  86. {
  87. nibble = 0;
  88. }
  89. inst->program[tracker->current_program_step] &= 0xff0f;
  90. inst->program[tracker->current_program_step] |= (nibble << 4);
  91. break;
  92. }
  93. case 2: // lower digit of param, e.g. exX
  94. {
  95. int8_t nibble = (opcode & 0x000f);
  96. if (nibble + 1 <= 0xf)
  97. {
  98. nibble++;
  99. }
  100. else
  101. {
  102. nibble = 0;
  103. }
  104. inst->program[tracker->current_program_step] &= 0xfff0;
  105. inst->program[tracker->current_program_step] |= nibble;
  106. break;
  107. }
  108. default:
  109. break;
  110. }
  111. }
  112. return;
  113. }
  114. if (event->input.key == InputKeyDown && event->input.type == InputTypeShort)
  115. {
  116. if (!(tracker->editing))
  117. {
  118. if (tracker->current_program_step + 1 < INST_PROG_LEN)
  119. {
  120. tracker->current_program_step++;
  121. if (tracker->program_position < tracker->current_program_step - 8)
  122. {
  123. tracker->program_position = tracker->current_program_step - 8;
  124. }
  125. }
  126. else
  127. {
  128. tracker->current_program_step = 0;
  129. tracker->program_position = 0;
  130. }
  131. }
  132. if (tracker->editing)
  133. {
  134. Instrument *inst = tracker->song.instrument[tracker->current_instrument];
  135. uint16_t opcode = inst->program[tracker->current_program_step];
  136. switch (tracker->current_digit)
  137. {
  138. case 0: // MSB
  139. {
  140. uint8_t param = ((opcode & 0x7f00) >> 8);
  141. if (param < (TE_PROGRAM_JUMP >> 8) && param > 0)
  142. {
  143. param--;
  144. inst->program[tracker->current_program_step] &= 0x80ff;
  145. inst->program[tracker->current_program_step] |= ((uint16_t)param << 8);
  146. }
  147. if ((inst->program[tracker->current_program_step] & 0x7f00) == TE_PROGRAM_JUMP && (inst->program[tracker->current_program_step] & 0x7fff) != TE_PROGRAM_END && (inst->program[tracker->current_program_step] & 0x7fff) != TE_PROGRAM_NOP)
  148. {
  149. inst->program[tracker->current_program_step] = TE_PROGRAM_LOOP_END | (inst->program[tracker->current_program_step] & 0x8000);
  150. }
  151. if ((inst->program[tracker->current_program_step] & 0x7fff) == TE_PROGRAM_END)
  152. {
  153. // param = (TE_PROGRAM_JUMP >> 8);
  154. inst->program[tracker->current_program_step] = TE_PROGRAM_JUMP | (inst->program[tracker->current_program_step] & 0x8000);
  155. }
  156. if ((inst->program[tracker->current_program_step] & 0x7fff) == TE_PROGRAM_NOP)
  157. {
  158. // param = (TE_PROGRAM_END >> 8);
  159. inst->program[tracker->current_program_step] = TE_PROGRAM_END | (inst->program[tracker->current_program_step] & 0x8000);
  160. }
  161. if ((inst->program[tracker->current_program_step] & 0x7f00) == (TE_PROGRAM_LOOP_BEGIN - 0x100))
  162. {
  163. // param = (TE_PROGRAM_END >> 8);
  164. inst->program[tracker->current_program_step] = TE_EFFECT_TRIGGER_RELEASE | (inst->program[tracker->current_program_step] & 0x8000);
  165. }
  166. break;
  167. }
  168. case 1: // upper digit of param, e.g. eXx
  169. {
  170. int8_t nibble = ((opcode & 0x00f0) >> 4);
  171. if (nibble - 1 >= 0)
  172. {
  173. nibble--;
  174. }
  175. else
  176. {
  177. nibble = 0xf;
  178. }
  179. inst->program[tracker->current_program_step] &= 0xff0f;
  180. inst->program[tracker->current_program_step] |= (nibble << 4);
  181. break;
  182. }
  183. case 2: // lower digit of param, e.g. exX
  184. {
  185. int8_t nibble = (opcode & 0x000f);
  186. if (nibble - 1 >= 0)
  187. {
  188. nibble--;
  189. }
  190. else
  191. {
  192. nibble = 0xf;
  193. }
  194. inst->program[tracker->current_program_step] &= 0xfff0;
  195. inst->program[tracker->current_program_step] |= nibble;
  196. break;
  197. }
  198. default:
  199. break;
  200. }
  201. }
  202. return;
  203. }
  204. }