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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #print('we should play ', this)
  75. o.song = [this]
  76. o.songCycle = cycle(o.song)
  77. o.songStart = this
  78. if not o.playing:
  79. o.start_playback()
  80. u.play_seq(o, [o.curPattern, 0])
  81. o.ePattern = (o.pat_bank * 16) +_id
  82. u.draw_header(o)
  83. self.draw_square()
  84. u.text_box1(o, "Pat", str(o.ePattern))
  85. u.text_box4(o, "Bank", str(o.pat_bank))
  86. o.update_display(0)
  87. _id += 1
  88. def ReceiveMessage(self, message):
  89. o = self.FSM.owner
  90. u.play_seq(o, message)
  91. if message[0] != self.cur_playing:
  92. self.cur_playing = message[0]
  93. if o.keyState[16] > 0 or o.keyState[17] > 0:
  94. pass
  95. else:
  96. u.draw_header(o)
  97. self.draw_square()
  98. u.text_box1(o, "Pat", str(o.ePattern))
  99. u.text_box4(o, "Bank", str(o.pat_bank))
  100. o.update_display(0)
  101. # if o.pat_odub:
  102. # print('add to record queue ', o.song[0])
  103. # else:
  104. # print('not recording')
  105. #print(message)
  106. if o.pat_odub:
  107. if message[1] == 0:
  108. if len(o.song) > 0:
  109. #print('adding to rec', o.song[0])
  110. o.song_in.append(o.song[0])
  111. #print('adding to song_in ', o.song_in)
  112. def draw_square(self):
  113. o = self.FSM.owner
  114. size = 22
  115. og_x = (o.width / 2) - (size * 2)
  116. o_x = og_x
  117. o_y = 127
  118. _id = 0
  119. for n in o.soundSlots[o.eSound].notes[o.ePattern]:
  120. if (_id + (o.pat_bank * 16)) == o.ePattern:
  121. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.olive, width=1)
  122. elif (_id + (o.pat_bank * 16)) == self.cur_playing:
  123. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.light_grey, width=1)
  124. elif self.active_patterns[_id] == 1:
  125. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.blue, width=1)
  126. else:
  127. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.dark_grey, width=1)
  128. o_x = o_x + size
  129. _id += 1
  130. if _id % 4 == 0:
  131. o_y -= size
  132. o_x = og_x
  133. def get_active_patterns(self, o):
  134. self.active_patterns = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  135. pat_iter = o.pat_bank * 16
  136. _iter = 0
  137. while pat_iter < ((o.pat_bank * 16) + 16):
  138. for s in o.soundSlots:
  139. pat_used = 0
  140. for p in s.notes[pat_iter]:
  141. if self.active_patterns[_iter] == 1:
  142. break
  143. if p[0] == 1:
  144. self.active_patterns[_iter] = 1
  145. break
  146. pat_iter += 1
  147. _iter += 1
  148. def Exit(self):
  149. o = self.FSM.owner
  150. o.pub.unregister("beat", self)
  151. # if len(o.song) < 1 and len(o.pat_buf) > 0:
  152. # o.song = o.pat_buf
  153. # o.pat_buf = []
  154. o.pat_perf = False
  155. o.song = o.pat_buf
  156. o.pat_buf = []
  157. o.songCycle = cycle(o.song)
  158. o.songStart = o.song[0]
  159. #print('song is ', o.song)