instrument_program.c 7.9 KB

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