instrument_program.c 8.1 KB

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