instrument_program.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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: break;
  108. }
  109. }
  110. return;
  111. }
  112. if (event->input.key == InputKeyDown && event->input.type == InputTypeShort)
  113. {
  114. if(!(tracker->editing))
  115. {
  116. if(tracker->current_program_step + 1 < INST_PROG_LEN)
  117. {
  118. tracker->current_program_step++;
  119. if(tracker->program_position < tracker->current_program_step - 8)
  120. {
  121. tracker->program_position = tracker->current_program_step - 8;
  122. }
  123. }
  124. else
  125. {
  126. tracker->current_program_step = 0;
  127. tracker->program_position = 0;
  128. }
  129. }
  130. if(tracker->editing)
  131. {
  132. Instrument* inst = tracker->song.instrument[tracker->current_instrument];
  133. uint16_t opcode = inst->program[tracker->current_program_step];
  134. switch(tracker->current_digit)
  135. {
  136. case 0: //MSB
  137. {
  138. uint8_t param = ((opcode & 0x7f00) >> 8);
  139. if(param < (TE_PROGRAM_JUMP >> 8) && param > 0)
  140. {
  141. param--;
  142. inst->program[tracker->current_program_step] &= 0x80ff;
  143. inst->program[tracker->current_program_step] |= ((uint16_t)param << 8);
  144. }
  145. 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)
  146. {
  147. inst->program[tracker->current_program_step] = TE_PROGRAM_LOOP_END | (inst->program[tracker->current_program_step] & 0x8000);
  148. }
  149. if((inst->program[tracker->current_program_step] & 0x7fff) == TE_PROGRAM_END)
  150. {
  151. //param = (TE_PROGRAM_JUMP >> 8);
  152. inst->program[tracker->current_program_step] = TE_PROGRAM_JUMP | (inst->program[tracker->current_program_step] & 0x8000);
  153. }
  154. if((inst->program[tracker->current_program_step] & 0x7fff) == TE_PROGRAM_NOP)
  155. {
  156. //param = (TE_PROGRAM_END >> 8);
  157. inst->program[tracker->current_program_step] = TE_PROGRAM_END | (inst->program[tracker->current_program_step] & 0x8000);
  158. }
  159. break;
  160. }
  161. case 1: //upper digit of param, e.g. eXx
  162. {
  163. int8_t nibble = ((opcode & 0x00f0) >> 4);
  164. if(nibble - 1 >= 0)
  165. {
  166. nibble--;
  167. }
  168. else
  169. {
  170. nibble = 0xf;
  171. }
  172. inst->program[tracker->current_program_step] &= 0xff0f;
  173. inst->program[tracker->current_program_step] |= (nibble << 4);
  174. break;
  175. }
  176. case 2: //lower digit of param, e.g. exX
  177. {
  178. int8_t nibble = (opcode & 0x000f);
  179. if(nibble - 1 >= 0)
  180. {
  181. nibble--;
  182. }
  183. else
  184. {
  185. nibble = 0xf;
  186. }
  187. inst->program[tracker->current_program_step] &= 0xfff0;
  188. inst->program[tracker->current_program_step] |= nibble;
  189. break;
  190. }
  191. default: break;
  192. }
  193. }
  194. return;
  195. }
  196. }