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 4.2KB

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