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.

utils.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import glob
  2. import os
  3. import pygame
  4. def draw_header(o):
  5. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  6. o.center_text(o.header_text, o.h1, o.width, 25, o.light_grey)
  7. o.center_text("----------------------", o.h2, o.width, 55, o.light_grey)
  8. def play_seq(o, message):
  9. _id = 0
  10. o.clear_notes_on()
  11. for s in o.soundSlots:
  12. if s.notes[message[0]][message[1]][0] == 1:
  13. if o.erase:
  14. if _id in o.erase_buf:
  15. #pass
  16. s.notes[message[0]][message[1]][0] = 0
  17. print('erasing')
  18. #o.undo_buf.append(s.notes[message[0]][message[1]][0])
  19. else:
  20. o.notes_on[_id] = 1
  21. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  22. elif o.mute:
  23. if _id in o.mute_buf:
  24. #pass
  25. #s.notes[message[0]][message[1]][0] = 0
  26. print('muting')
  27. #o.undo_buf.append(s.notes[message[0]][message[1]][0])
  28. else:
  29. o.notes_on[_id] = 1
  30. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  31. else:
  32. o.notes_on[_id] = 1
  33. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  34. _id += 1
  35. def erase_note(o, message, pressed):
  36. _id = 0
  37. #o.clear_notes_on()
  38. for s in o.soundSlots:
  39. if s.notes[message[0]][message[1]][0] == 1:
  40. o.notes_on[_id] = 1
  41. o.soundSlots[_id].play(s.notes[message[0]][message[1]][1])
  42. _id += 1
  43. def text_box1(o, a, b):
  44. w1 = 0
  45. w2 = (o.width / 2) - 50
  46. h1 = 45
  47. h2 = h1 + 10
  48. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  49. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  50. def text_box2(o, a, b):
  51. w1 = 0
  52. w2 = (o.width / 2) - 50
  53. h1 = 90
  54. h2 = h1 + 10
  55. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  56. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  57. def text_box3(o, a, b):
  58. w1 = (o.width / 2) + 40
  59. w2 = o.width
  60. h1 = 45
  61. h2 = h1 + 10
  62. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  63. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  64. def text_box4(o, a, b):
  65. w1 = (o.width / 2) + 40
  66. w2 = o.width
  67. h1 = 90
  68. h2 = h1 + 10
  69. o.center_block(a, o.h2, [w1,h1,w2,h2], o.light_grey)
  70. o.center_block(b, o.h2, [w1,h1+10,w2,h2+25], o.light_grey)
  71. def text_center(o, a, b):
  72. w1 = 0
  73. w2 = o.width
  74. h1 = 45
  75. h2 = h1 + 10
  76. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  77. o.center_block(a, o.h1, [w1,h1,w2,h2], o.light_grey)
  78. o.center_block(b, o.h1, [w1,h1+20,w2,h2+45], o.light_grey)
  79. def breakup_song_text(song):
  80. line1 = ""
  81. line2 = ""
  82. line3 = ""
  83. line4 = ""
  84. if len(song) < 9:
  85. for x in song:
  86. line1 += str(x) + '.'
  87. elif len(song) < 17:
  88. _iter = 0
  89. for x in song:
  90. if _iter < 8:
  91. line1 += str(x) + '.'
  92. else:
  93. line2 += str(x) + '.'
  94. _iter += 1
  95. elif len(song) < 25:
  96. _iter = 0
  97. for x in song:
  98. if _iter < 8:
  99. line1 += str(x) + '.'
  100. elif _iter < 16:
  101. line2 += str(x) + '.'
  102. else:
  103. line3 += str(x) + '.'
  104. _iter += 1
  105. elif len(song) < 33:
  106. _iter = 0
  107. for x in song:
  108. if _iter < 8:
  109. line1 += str(x) + '.'
  110. elif _iter < 16:
  111. line2 += str(x) + '.'
  112. elif _iter < 24:
  113. line3 += str(x) + '.'
  114. else:
  115. line4 += str(x) + '.'
  116. _iter += 1
  117. else:
  118. _iter = 0
  119. cur_id = len(song) - 1
  120. _iter = 0
  121. ns = song.copy()
  122. while len(ns) > 31:
  123. del ns[0]
  124. for x in ns:
  125. if _iter < 7:
  126. line1 += str(x) + '.'
  127. elif _iter < 15:
  128. line2 += str(x) + '.'
  129. elif _iter < 23:
  130. line3 += str(x) + '.'
  131. else:
  132. line4 += str(x) + '.'
  133. _iter += 1
  134. line1 = ".." + line1
  135. return [line1, line2, line3, line4]
  136. def get_folders(path):
  137. globbed = glob.glob(path + "/*/")
  138. globbed.sort()
  139. # dirs = os.listdir(o.load_sound_dir)
  140. # dirs.sort()
  141. # for i in dirs:
  142. # if os.path.isdir(o.load_sound_dir + i):
  143. # i += "/asdfasdfasf"
  144. # print(i, ' is a directory')
  145. # else:
  146. # print(i, ' is not a directory')
  147. # print(dirs)
  148. # return dirs
  149. return globbed
  150. def get_files(path, ext):
  151. path += '/'
  152. out = []
  153. files = os.listdir(path)
  154. files.sort()
  155. print('from utils ', files)
  156. for file in files:
  157. if os.path.isdir(path + file):
  158. out.append(file)
  159. else:
  160. pass
  161. for file in files:
  162. if file.lower().endswith(ext):
  163. if os.path.isdir(path + file):
  164. pass
  165. else:
  166. out.append(file)
  167. else:
  168. pass
  169. out2 = []
  170. for f in out:
  171. if f[0] == '.':
  172. pass
  173. else:
  174. out2.append(f)
  175. return out2
  176. def scroll_ud(o, self):
  177. since_up = 0
  178. if o.keyState[0] == 2:
  179. if o.press_ticks_up == None:
  180. o.press_ticks_up = pygame.time.get_ticks()
  181. since_up = pygame.time.get_ticks() - o.press_ticks_up
  182. else:
  183. o.press_ticks_up = None
  184. o.press_ticks_up = pygame.time.get_ticks()
  185. if o.keyState[0] == 1 or since_up > 250:
  186. if self.cur_el > 0:
  187. self.cur_el -= 1
  188. self.draw_main(o)
  189. o.update_display(0)
  190. o.press_ticks_up = pygame.time.get_ticks()
  191. since_down = 0
  192. if o.keyState[1] == 2:
  193. if o.press_ticks_down == None:
  194. o.press_ticks_down = pygame.time.get_ticks()
  195. since_down = pygame.time.get_ticks() - o.press_ticks_down
  196. else:
  197. o.press_ticks_down = None
  198. o.press_ticks_down = pygame.time.get_ticks()
  199. if o.keyState[1] == 1 or since_down > 250:
  200. if self.cur_el < (len(self.menu) - 1):
  201. self.cur_el += 1
  202. self.draw_main(o)
  203. o.update_display(0)
  204. o.press_ticks_down = pygame.time.get_ticks()