sequence.c 5.1 KB

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