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

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