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.

sMain.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import FSM
  2. import utils as u
  3. import menus as m
  4. import pygame
  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 Main(State):
  19. def __init__(self,FSM):
  20. super(Main, self).__init__(FSM)
  21. def Enter(self):
  22. print('hello main')
  23. self.FSM.owner.pub.register("beat", self)
  24. self.FSM.stateLife = 1
  25. o = self.FSM.owner
  26. o.header_text = "Sound Select"
  27. if o.keyState[16] > 0 or o.keyState[17] > 0:
  28. pass
  29. else:
  30. u.draw_header(o)
  31. self.draw_square()
  32. u.text_box1(o, "Pat", str(o.curPattern))
  33. u.text_box2(o, "BPM", str(o.bpm))
  34. u.text_box4(o, "Bank", str(o.note_bank))
  35. u.text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  36. o.update_display(0)
  37. self.recording = True
  38. self.record_queue = []
  39. self.undo_queue = []
  40. o.erase = False
  41. o.erase_queue = []
  42. o.mute = False
  43. o.mute_queue = []
  44. super(Main, self).Enter()
  45. def Execute(self):
  46. o = self.FSM.owner
  47. m.menu1_actions(self, o)
  48. m.menu2_actions_sample(self, o)
  49. if o.keyState[16] == 1:
  50. m.draw_menu1(o)
  51. o.update_display(0)
  52. elif o.keyState[16] == 4:
  53. u.draw_header(o)
  54. self.draw_square()
  55. u.text_box1(o, "Pat", str(o.curPattern))
  56. u.text_box2(o, "BPM", str(o.bpm))
  57. u.text_box4(o, "Bank", str(o.note_bank))
  58. u.text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  59. o.button_repeater(1, [])
  60. o.update_display(0)
  61. if o.keyState[17] == 1:
  62. m.draw_menu2_sample(o)
  63. o.update_display(0)
  64. elif o.keyState[17] == 4:
  65. u.draw_header(o)
  66. self.draw_square()
  67. u.text_box1(o, "Pat", str(o.curPattern))
  68. u.text_box2(o, "BPM", str(o.bpm))
  69. u.text_box4(o, "Bank", str(o.note_bank))
  70. u.text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  71. o.update_display(0)
  72. _id = 0
  73. for k in o.keyState:
  74. if _id > 15:
  75. pass
  76. else:
  77. if o.keyState[16] > 0 or o.keyState[17] > 0:
  78. pass
  79. else:
  80. note = _id + o.note_bank * 16
  81. if k == 1:
  82. self.record_queue = pygame.time.get_ticks()
  83. if not o.erase and not o.mute and o.prev:
  84. o.soundSlots[note].play(o.note_vol)
  85. o.eSound = note
  86. u.draw_header(o)
  87. #u.text_box1(o, "SVol", str(o.slots[o.eSound].volume))
  88. u.text_box1(o, "Pat", str(o.curPattern))
  89. u.text_box2(o, "BPM", str(o.bpm))
  90. u.text_box4(o, "Bank", str(o.note_bank))
  91. u.text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  92. self.draw_square()
  93. o.update_display(0)
  94. if o.erase:
  95. o.erase_buf.append(note)
  96. if o.mute:
  97. o.mute_buf.append(note)
  98. if k == 3:
  99. if note in o.erase_buf:
  100. o.erase_buf.remove(note)
  101. if note in o.mute_buf:
  102. o.mute_buf.remove(note)
  103. _id += 1
  104. def ReceiveMessage(self, message):
  105. o = self.FSM.owner
  106. if o.erase == True:
  107. u.play_seq(o, message)
  108. else:
  109. u.play_seq(o, message)
  110. if o.keyState[16] > 0 or o.keyState[17] > 0:
  111. pass
  112. else:
  113. u.draw_header(o)
  114. self.draw_square()
  115. u.text_box1(o, "Pat", str(o.curPattern))
  116. u.text_box2(o, "BPM", str(o.bpm))
  117. u.text_box4(o, "Bank", str(o.note_bank))
  118. u.text_box3(o, "Pitch", str(o.soundSlots[o.eSound].pitch))
  119. o.update_display(0)
  120. if self.record_queue != []:
  121. cur_time = pygame.time.get_ticks()
  122. note_pos = (cur_time - self.record_queue) - (o.half_bpm / 2)
  123. if o.odub == True:
  124. if note_pos > (o.half_bpm):
  125. _note = 0
  126. if message[1] == 0:
  127. _note = 15
  128. else:
  129. _note = message[1] - 1
  130. if o.soundSlots[o.eSound].notes[message[0]][_note][0] != 1:
  131. o.soundSlots[o.eSound].notes[message[0]][_note][0] = 1
  132. o.soundSlots[o.eSound].notes[message[0]][_note][1] = o.note_vol
  133. o.undo_buf.append([o.eSound, message[0], _note])
  134. else:
  135. if o.soundSlots[o.eSound].notes[message[0]][message[1]][0] != 1:
  136. o.soundSlots[o.eSound].notes[message[0]][message[1]][0] = 1
  137. o.soundSlots[o.eSound].notes[message[0]][message[1]][1] = o.note_vol
  138. o.undo_buf.append([o.eSound, message[0], message[1]])
  139. self.record_queue = []
  140. def draw_square(self):
  141. o = self.FSM.owner
  142. size = 22
  143. og_x = (o.width / 2) - (size * 2)
  144. o_x = og_x
  145. o_y = 127
  146. _id = o.note_bank * 16
  147. while _id < ((o.note_bank * 16) + 16):
  148. if _id == o.eSound:
  149. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.light_grey, width=1)
  150. elif o.notes_on[_id] == 1:
  151. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.grey, width=1)
  152. elif self.has_sample(o, _id) == False:
  153. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.dark_grey, width=1)
  154. else:
  155. o.draw.rectangle((o_x, o_y, o_x + size, o_y - size), outline=o.light_grey, fill=o.blue, width=1)
  156. o_x = o_x + size
  157. _id += 1
  158. if _id % 4 == 0:
  159. o_y -= size
  160. o_x = og_x
  161. def has_sample(self, o, slot):
  162. if o.soundSlots[slot].file != None:
  163. return True
  164. else:
  165. return False
  166. def Exit(self):
  167. self.FSM.owner.pub.unregister("beat", self)