sequence.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "sequence.h"
  2. void delete_sequence_step(FlizzerTrackerApp *tracker)
  3. {
  4. uint8_t sequence_position = tracker->tracker_engine.sequence_position;
  5. uint8_t *pattern = &tracker->tracker_engine.song->sequence.sequence_step[sequence_position].pattern_indices[tracker->current_channel];
  6. *pattern = 0;
  7. }
  8. void edit_sequence_step(FlizzerTrackerApp *tracker, int8_t delta)
  9. {
  10. uint8_t digit = tracker->current_digit;
  11. uint8_t sequence_position = tracker->tracker_engine.sequence_position;
  12. uint8_t pattern_index = tracker->tracker_engine.song->sequence.sequence_step[sequence_position].pattern_indices[tracker->current_channel];
  13. uint8_t *pattern = &tracker->tracker_engine.song->sequence.sequence_step[sequence_position].pattern_indices[tracker->current_channel];
  14. uint8_t temp_pattern = *pattern;
  15. switch (digit)
  16. {
  17. case 0: // upper nibble
  18. {
  19. int8_t nibble = ((pattern_index & 0xf0) >> 4);
  20. if (nibble + delta < 0)
  21. {
  22. nibble = 0xf;
  23. }
  24. else if (nibble + delta > 0xf)
  25. {
  26. nibble = 0;
  27. }
  28. else
  29. {
  30. nibble += delta;
  31. }
  32. temp_pattern &= 0xf0;
  33. temp_pattern |= (nibble << 4);
  34. break;
  35. }
  36. case 1: // lower nibble
  37. {
  38. int8_t nibble = (pattern_index & 0x0f);
  39. if (nibble + delta < 0)
  40. {
  41. nibble = 0xf;
  42. }
  43. else if (nibble + delta > 0xf)
  44. {
  45. nibble = 0;
  46. }
  47. else
  48. {
  49. nibble += delta;
  50. }
  51. temp_pattern &= 0xf0;
  52. temp_pattern |= nibble;
  53. break;
  54. }
  55. }
  56. if (check_and_allocate_pattern(&tracker->song, temp_pattern))
  57. {
  58. *pattern = temp_pattern;
  59. }
  60. }
  61. void sequence_edit_event(FlizzerTrackerApp *tracker, FlizzerTrackerEvent *event)
  62. {
  63. if (event->input.key == InputKeyOk && event->input.type == InputTypeShort && !tracker->tracker_engine.playing)
  64. {
  65. tracker->editing = !tracker->editing;
  66. }
  67. if (event->input.key == InputKeyRight && event->input.type == InputTypeShort && tracker->editing)
  68. {
  69. tracker->current_digit++;
  70. if (tracker->current_digit > 1)
  71. {
  72. tracker->current_channel++;
  73. tracker->current_digit = 0;
  74. if (tracker->current_channel > SONG_MAX_CHANNELS - 1)
  75. {
  76. tracker->current_channel = 0;
  77. }
  78. }
  79. }
  80. if (event->input.key == InputKeyLeft && event->input.type == InputTypeShort && tracker->editing)
  81. {
  82. tracker->current_digit--;
  83. if (tracker->current_digit > 1) // unsigned int overflow
  84. {
  85. tracker->current_channel--;
  86. tracker->current_digit = 1;
  87. if (tracker->current_channel > SONG_MAX_CHANNELS - 1) // unsigned int overflow
  88. {
  89. tracker->current_channel = SONG_MAX_CHANNELS - 1;
  90. }
  91. }
  92. }
  93. if (event->input.key == InputKeyDown && event->input.type == InputTypeShort)
  94. {
  95. if (!(tracker->editing))
  96. {
  97. tracker->tracker_engine.sequence_position++;
  98. if (tracker->tracker_engine.sequence_position >= tracker->tracker_engine.song->num_sequence_steps)
  99. {
  100. tracker->tracker_engine.sequence_position = 0;
  101. }
  102. }
  103. if (tracker->editing)
  104. {
  105. edit_sequence_step(tracker, -1);
  106. }
  107. }
  108. if (event->input.key == InputKeyUp && event->input.type == InputTypeShort)
  109. {
  110. if (!(tracker->editing))
  111. {
  112. int16_t temp_sequence_position = tracker->tracker_engine.sequence_position - 1;
  113. if (temp_sequence_position < 0)
  114. {
  115. tracker->tracker_engine.sequence_position = tracker->tracker_engine.song->num_sequence_steps - 1;
  116. }
  117. else
  118. {
  119. tracker->tracker_engine.sequence_position--;
  120. }
  121. }
  122. if (tracker->editing)
  123. {
  124. edit_sequence_step(tracker, 1);
  125. }
  126. }
  127. if (event->input.key == InputKeyUp && event->input.type == InputTypeLong && !(tracker->editing)) // set loop begin or loop end for the song
  128. {
  129. TrackerSong *song = &tracker->song;
  130. if (song->loop_start == song->loop_end && song->loop_end == 0) // if both are 0
  131. {
  132. song->loop_end = tracker->tracker_engine.sequence_position;
  133. }
  134. else
  135. {
  136. if (tracker->tracker_engine.sequence_position < song->loop_end)
  137. {
  138. song->loop_start = tracker->tracker_engine.sequence_position;
  139. }
  140. if (tracker->tracker_engine.sequence_position > song->loop_start)
  141. {
  142. song->loop_end = tracker->tracker_engine.sequence_position;
  143. }
  144. }
  145. }
  146. if (event->input.key == InputKeyDown && event->input.type == InputTypeLong && !(tracker->editing)) // erase loop begin and loop end points
  147. {
  148. TrackerSong *song = &tracker->song;
  149. song->loop_start = song->loop_end = 0;
  150. }
  151. if (event->input.key == InputKeyBack && event->input.type == InputTypeShort && tracker->editing)
  152. {
  153. delete_sequence_step(tracker);
  154. }
  155. }