raspberry pi zero based drum machine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

sMain.py 5.5KB

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