raspberry pi zero based drum machine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sSelectPattern.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import FSM
  2. import utils as u
  3. import menus as m
  4. from itertools import cycle
  5. #====================================
  6. State = type("State", (object,), {})
  7. #====================================
  8. class State(object):
  9. def __init__(self, FSM):
  10. self.FSM = FSM
  11. def Enter(self):
  12. pass
  13. def Execute(self):
  14. pass
  15. def Exit(self):
  16. pass
  17. #====================================
  18. class SelectPattern(State):
  19. def __init__(self,FSM):
  20. super(SelectPattern, self).__init__(FSM)
  21. def Enter(self):
  22. self.FSM.stateLife = 1
  23. o = self.FSM.owner
  24. o.header_text = "Select Pattern"
  25. o.pub.register("beat", self)
  26. self.cur_playing = 0
  27. self.active_patterns = []
  28. self.get_active_patterns(o)
  29. o.pat_buf = o.song
  30. self.new_song = []
  31. if o.keyState[16] > 0 or o.keyState[17] > 0:
  32. pass
  33. else:
  34. u.draw_header(o)
  35. self.draw_square()
  36. u.text_box1(o, "Pat", str(o.ePattern))
  37. u.text_box4(o, "Bank", str(o.pat_bank))
  38. o.update_display(0)
  39. super(SelectPattern, self).Enter()
  40. def Execute(self):
  41. o = self.FSM.owner
  42. m.menu1_actions(self, o)
  43. m.menu2_actions_pattern(self, o)
  44. if o.keyState[16] == 1:
  45. m.draw_menu1(o)
  46. o.update_display(0)
  47. elif o.keyState[16] == 4:
  48. u.draw_header(o)
  49. self.draw_square()
  50. u.text_box1(o, "Pat", str(o.ePattern))
  51. u.text_box4(o, "Bank", str(o.pat_bank))
  52. o.update_display(0)
  53. if o.keyState[17] == 1:
  54. m.draw_menu2_pattern(o)
  55. o.update_display(0)
  56. elif o.keyState[17] == 4:
  57. u.draw_header(o)
  58. self.get_active_patterns(o)
  59. self.draw_square()
  60. u.text_box1(o, "Pat", str(o.ePattern))
  61. u.text_box4(o, "Bank", str(o.pat_bank))
  62. o.update_display(0)
  63. if o.keyState[16] > 0 or o.keyState[17] > 0:
  64. pass
  65. else:
  66. _id = 0
  67. for k in o.keyState:
  68. if _id > 15:
  69. pass
  70. else:
  71. if k == 1:
  72. if o.pat_perf:
  73. this = o.ePattern = (o.pat_bank * 16) +_id
  74. o.song = [this]
  75. o.songCycle = cycle(o.song)
  76. o.songStart = this
  77. if not o.playing:
  78. o.start_playback()
  79. u.play_seq(o, [o.curPattern, 0])
  80. o.ePattern = (o.pat_bank * 16) +_id
  81. u.draw_header(o)
  82. self.draw_square()
  83. u.text_box1(o, "Pat", str(o.ePattern))
  84. u.text_box4(o, "Bank", str(o.pat_bank))
  85. o.update_display(0)
  86. _id += 1
  87. def ReceiveMessage(self, message):
  88. o = self.FSM.owner
  89. u.play_seq(o, message)
  90. if message[0] != self.cur_playing:
  91. self.cur_playing = message[0]
  92. if o.keyState[16] > 0 or o.keyState[17] > 0:
  93. pass
  94. else:
  95. u.draw_header(o)
  96. self.draw_square()
  97. u.text_box1(o, "Pat", str(o.ePattern))
  98. u.text_box4(o, "Bank", str(o.pat_bank))
  99. o.update_display(0)
  100. if o.pat_odub:
  101. if message[1] == 0:
  102. if len(o.song) > 0:
  103. o.song_in.append(o.song[0])
  104. def draw_square(self):
  105. o = self.FSM.owner
  106. size = 22
  107. og_x = (o.width / 2) - (size * 2)
  108. o_x = og_x
  109. o_y = 127
  110. _id = 0
  111. for n in o.soundSlots[o.eSound].notes[o.ePattern]:
  112. if (_id + (o.pat_bank * 16)) == o.ePattern:
  113. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.olive, width=1)
  114. elif (_id + (o.pat_bank * 16)) == self.cur_playing:
  115. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.light_grey, width=1)
  116. elif self.active_patterns[_id] == 1:
  117. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.blue, width=1)
  118. else:
  119. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.dark_grey, width=1)
  120. o_x = o_x + size
  121. _id += 1
  122. if _id % 4 == 0:
  123. o_y -= size
  124. o_x = og_x
  125. def get_active_patterns(self, o):
  126. self.active_patterns = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  127. pat_iter = o.pat_bank * 16
  128. _iter = 0
  129. while pat_iter < ((o.pat_bank * 16) + 16):
  130. for s in o.soundSlots:
  131. pat_used = 0
  132. for p in s.notes[pat_iter]:
  133. if self.active_patterns[_iter] == 1:
  134. break
  135. if p[0] == 1:
  136. self.active_patterns[_iter] = 1
  137. break
  138. pat_iter += 1
  139. _iter += 1
  140. def Exit(self):
  141. o = self.FSM.owner
  142. o.pub.unregister("beat", self)
  143. o.pat_perf = False
  144. o.song = o.pat_buf
  145. o.pat_buf = []
  146. o.songCycle = cycle(o.song)
  147. o.songStart = o.song[0]