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.

main.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. import sys
  2. sys.path.insert(0, './lib')
  3. from pygame import mixer
  4. import time
  5. import digitalio
  6. import board
  7. import adafruit_matrixkeypad
  8. import pygame
  9. from PIL import Image, ImageDraw, ImageFont
  10. import adafruit_rgb_display.st7789 as st7789
  11. import digitalio
  12. import board
  13. from adafruit_rgb_display.rgb import color565
  14. import adafruit_rgb_display.st7789 as st7789
  15. import FSM
  16. from itertools import cycle
  17. import observer
  18. from scikits.samplerate import resample
  19. from configobj import ConfigObj
  20. import json
  21. import ast
  22. import RPi.GPIO as GPIO
  23. from time import sleep
  24. import threading
  25. import os
  26. GPIO.setmode(GPIO.BCM)
  27. # set up GPIO output channel - internal led
  28. GPIO.setup(29, GPIO.OUT)
  29. cols = [digitalio.DigitalInOut(x) for x in (board.D21, board.D20, board.D16, board.D12)]
  30. rows = [digitalio.DigitalInOut(x) for x in (board.D26, board.D13, board.D6, board.D5)]
  31. keys = ((15, 14, 13, 12),
  32. (11, 10, 9, 8),
  33. (7, 6, 5, 4),
  34. (3, 2, 1, 0))
  35. keypad = adafruit_matrixkeypad.Matrix_Keypad(rows, cols, keys)
  36. pygame.init()
  37. # mixer.init()
  38. # mixer.set_num_channels(32)
  39. done = False
  40. clock = pygame.time.Clock()
  41. TIMER = pygame.USEREVENT + 1
  42. playhead = 0
  43. timer = pygame.time.get_ticks
  44. start = now = timer()
  45. cs_pin = digitalio.DigitalInOut(board.CE0)
  46. dc_pin = digitalio.DigitalInOut(board.D25)
  47. reset_pin = None
  48. BAUDRATE = 64000000 # The pi can be very fast!
  49. #BAUDRATE = 24000000
  50. # display = st7789.ST7789(
  51. # board.SPI(),
  52. # cs=cs_pin,
  53. # dc=dc_pin,
  54. # rst=reset_pin,
  55. # baudrate=BAUDRATE,
  56. # width=135,
  57. # height=240,
  58. # x_offset=53,
  59. # y_offset=40,
  60. # )
  61. backlight = digitalio.DigitalInOut(board.D22)
  62. backlight.switch_to_output()
  63. backlight.value = True
  64. buttonA = digitalio.DigitalInOut(board.D23)
  65. buttonB = digitalio.DigitalInOut(board.D24)
  66. buttonA.switch_to_input()
  67. buttonB.switch_to_input()
  68. spi = board.SPI()
  69. # Create blank image for drawing.
  70. # Make sure to create image with mode 'RGB' for full color.
  71. # Get drawing object to draw on image.
  72. # Draw a black filled box to clear the image.
  73. #draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
  74. #disp.image(image, rotation)
  75. # Draw some shapes.
  76. # First define some constants to allow easy resizing of shapes.
  77. # Move left to right keeping track of the current x position for drawing shapes.
  78. x = 0
  79. # Turn on the backlight
  80. backlight = digitalio.DigitalInOut(board.D22)
  81. backlight.switch_to_output()
  82. backlight.value = True
  83. class Prog:
  84. def __init__(self):
  85. self.FSM = FSM.ProgFSM(self)
  86. #self.start_mixer()
  87. #self.font = ImageFont.truetype("/home/pi/examples/HLM.ttf", 64)
  88. #self.h1 = ImageFont.truetype("/home/pi/examples/HLM.ttf", 26)
  89. self.h1 = ImageFont.truetype("/home/pi/zpc_ct/fonts/Pixellari.ttf", 30)
  90. self.h2 = ImageFont.truetype("/home/pi/zpc_ct/fonts/Pixellari.ttf", 20)
  91. self.h3 = ImageFont.truetype("/home/pi/zpc_ct/fonts/Pixellari.ttf", 54)
  92. self.disp = st7789.ST7789(
  93. spi,
  94. cs=cs_pin,
  95. dc=dc_pin,
  96. rst=reset_pin,
  97. baudrate=BAUDRATE,
  98. width=135,
  99. height=240,
  100. x_offset=53,
  101. y_offset=40,
  102. )
  103. self.height = self.disp.width # we swap height/width to rotate it to landscape!
  104. self.width = self.disp.height
  105. self.rotation = 270
  106. self.padding = -2
  107. self.top = self.padding
  108. self.bottom = self.height - self.padding
  109. self.image = Image.new("RGB", (self.width, self.height))
  110. self.draw = ImageDraw.Draw(self.image)
  111. self.playhead = 0
  112. self.playing = False
  113. self.bpm = 90
  114. self.bpm_inc = 1
  115. self.half_bpm = 0.0
  116. self.note_bank = 0
  117. self.pat_bank = 0
  118. self.volume = 10
  119. self.note_vol = 16
  120. self.note_pitch = 0.0
  121. self.soundSlots = []
  122. #self.slots = []
  123. self.keys = []
  124. self.keyState = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  125. #self.song = [2, 3, 0, 1, 0, 1, 0, 1, 0, 1]
  126. self.song = [0]
  127. self.songCycle = cycle(self.song)
  128. self.curPattern = next(self.songCycle)
  129. self.songStart = self.curPattern
  130. self.eSound = 0
  131. self.ePattern = 0
  132. self.patternFollow = False
  133. self.patternClipboard = []
  134. self.soundClipboard = []
  135. self.last_load_sound_id = None
  136. self.odub = False
  137. self.undo_buf = []
  138. self.black = "#000000"
  139. self.bg_color = "#336699"
  140. #self.color_a = "#FFFFFF"
  141. self.color_b = "#929230"
  142. #self.color_c = "#FFFF00"
  143. self.dark_grey = "#404040"
  144. self.grey = "#808080"
  145. self.light_grey = "#D3D3D3"
  146. self.blue = "#336699"
  147. self.dark_blue = "#2A2AD5"
  148. self.pink = "#D52A80"
  149. #self.red = "#D52A2A"
  150. self.olive = "#80D52A"
  151. #self.green = "#2AD52A"
  152. self.notes_on = []
  153. self.clear_notes_on()
  154. self.press_ticks_up = None
  155. self.press_ticks_down = None
  156. self.pub = observer.Publisher(['beat', 'the joint'])
  157. self.repeater_states = []
  158. _iter = 0
  159. while _iter < 18:
  160. self.repeater_states.append(None)
  161. _iter += 1
  162. #self.song_file_name = "default.sng"
  163. self.mconf = ConfigObj("config.txt")
  164. self.sconf = ConfigObj("/home/pi/zpc_ct/user/songs/" + self.mconf['default_song'])
  165. self.theme = ConfigObj("/home/pi/zpc_ct/user/themes/" + self.mconf['theme'] + ".thm")
  166. self.load_song_dir = '/home/pi/zpc_ct/user/sounds/'
  167. self.light_grey = self.theme['light_grey']
  168. self.apply_theme()
  169. self.display_thread = threading.Thread(target=self.update_display, args=([0]))
  170. class SoundSlot:
  171. def __init__(self, file, obj_id, o):
  172. self.file = file
  173. self.id = obj_id
  174. self.o = o
  175. self.mixerSound = None
  176. self.soundArray = None
  177. self.og_sound = None
  178. if file != None:
  179. #self.mixerSound = pygame.mixer.Sound(self.file)
  180. #self.soundArray = pygame.sndarray.array(self.mixerSound)
  181. #self.og_sound = self.mixerSound
  182. self.create_sound()
  183. #self.pattern = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]
  184. #self.notes = self.init_notes()
  185. self.pitch = 0
  186. self.volume = 16
  187. self.start = 0
  188. self.end = 0
  189. def create_sound(self):
  190. self.mixerSound = pygame.mixer.Sound(self.file)
  191. self.soundArray = pygame.sndarray.array(self.mixerSound)
  192. self.og_sound = self.mixerSound
  193. def play(self, vol):
  194. # snd_array = pygame.sndarray.array(self.mixerSound)
  195. # snd_resample = resample(snd_array, 2.5, "sinc_fastest").astype(snd_array.dtype)
  196. # snd_out = pygame.sndarray.make_sound(snd_resample)
  197. # snd_out.play()
  198. #lst = ast.literal_eval(self.o.sconf.as_list('volumes')[self.id])
  199. #print('---------- ', 'this is a volume')
  200. #print(lst)
  201. #print('note vol ', self.volume)
  202. #o.sconf
  203. if self.file != None:
  204. if vol != 0:
  205. vol = (vol / 16) * (self.volume / 16) * (self.o.volume / 16)
  206. self.mixerSound.set_volume(vol)
  207. pygame.mixer.Sound.play(self.mixerSound)
  208. def set_pitch(self):
  209. if self.pitch == 0:
  210. self.mixerSound = self.og_sound
  211. else:
  212. snd_resample = resample(self.soundArray, ((self.pitch * - 1) / 10) + 1, "sinc_fastest").astype(self.soundArray.dtype)
  213. self.mixerSound = pygame.sndarray.make_sound(snd_resample)
  214. # snd_array = pygame.sndarray.array(self.mixerSound)
  215. print('setting pitch to ', self.pitch)
  216. def init_notes(self):
  217. #print('initing notes')
  218. outp = []
  219. _id = 0
  220. while _id < 64:
  221. outp2 = []
  222. _id2 = 0
  223. while _id2 < 16:
  224. outp2.append([0,16])
  225. _id2 += 1
  226. outp.append(outp2)
  227. _id += 1
  228. return outp
  229. def init_slots(self):
  230. # slot_id, file, volume, pitch, notes
  231. _iter = 0
  232. self.slots = []
  233. while _iter < 64:
  234. #def __init__(self, file, obj_id, o):
  235. p1 = self.SoundSlot(None, _iter, self)
  236. self.slots.append(p1)
  237. _iter += 1
  238. #print('adding slot ', _iter)
  239. def Execute(self):
  240. self.keys = keypad.pressed_keys
  241. self.update_keys()
  242. self.FSM.Execute()
  243. def update_keys(self):
  244. _id = 0
  245. for k in self.keyState:
  246. if k == 0:
  247. if _id in self.keys:
  248. self.keyState[_id] = 1
  249. elif k == 1:
  250. if _id in self.keys:
  251. self.keyState[_id] = 2
  252. else:
  253. self.keyState[_id] = 3
  254. elif k == 2:
  255. if _id in self.keys:
  256. self.keyState[_id] = 2
  257. else:
  258. self.keyState[_id] = 3
  259. else:
  260. self.keyState[_id] = 0
  261. _id += 1
  262. if buttonA.value == 0:
  263. if self.keyState[16] == 0:
  264. self.keyState[16] = 1
  265. elif self.keyState[16] == 1:
  266. self.keyState[16] = 2
  267. else:
  268. self.keyState[16] = 2
  269. else:
  270. if self.keyState[16] == 3:
  271. self.keyState[16] = 4
  272. elif self.keyState[16] == 2:
  273. self.keyState[16] = 3
  274. elif self.keyState[16] == 4:
  275. self.keyState[16] = 0
  276. if buttonB.value == 0:
  277. if self.keyState[17] == 0:
  278. self.keyState[17] = 1
  279. elif self.keyState[17] == 1:
  280. self.keyState[17] = 2
  281. else:
  282. self.keyState[17] = 2
  283. else:
  284. if self.keyState[17] == 3:
  285. self.keyState[17] = 4
  286. elif self.keyState[17] == 2:
  287. self.keyState[17] = 3
  288. elif self.keyState[17] == 4:
  289. self.keyState[17] = 0
  290. def button_repeater(self, state, enables):
  291. if state == 0:
  292. pass
  293. # _iter = 0
  294. # for b in self.keyState:
  295. # if _iter in enables:
  296. # if b == 2:
  297. # if self.repeater_states[_iter] == None:
  298. # #self.repeater_states[_iter] = pygame.time.get_ticks()
  299. # pass
  300. # else:
  301. # t = pygame.time.get_ticks() - self.repeater_states[_iter]
  302. # if t > 250:
  303. # #self.repeater_states[_iter] = pygame.time.get_ticks()
  304. # #b = 0
  305. # print('new tick ', _iter, ' ', b)
  306. # _iter += 1
  307. #self.repeater_states
  308. elif state == 1:
  309. for i in self.repeater_states:
  310. i = None
  311. print('reset repeater')
  312. def update_bpm(self):
  313. bpm = (60000 / self.bpm) / 4
  314. self.half_bpm = bpm / 2
  315. if self.playing:
  316. pygame.time.set_timer(TIMER, int(bpm))
  317. def start_playback(self):
  318. self.playing = True
  319. self.playhead = -1
  320. self.playhead = 0
  321. self.songCycle = cycle(self.song)
  322. self.curPattern = next(self.songCycle)
  323. self.songStart = self.curPattern
  324. bpm = (60000 / self.bpm) / 4
  325. pygame.time.set_timer(TIMER, int(bpm))
  326. def stop_playback(self):
  327. self.playing = False
  328. self.playhead = -1
  329. self.playhead = 0
  330. self.curPattern = self.song[0]
  331. pygame.time.set_timer(TIMER, 0)
  332. def center_text(self, text, font, width, y, color):
  333. w,h = font.getsize(text)
  334. self.draw.text(((width-w)/2,(y-h)/2), text, font=font, fill=color)
  335. def center_block(self, message, font, bounding_box, color):
  336. x1, y1, x2, y2 = bounding_box # For easy reading
  337. w, h = self.draw.textsize(message, font=font)
  338. x = (x2 - x1 - w)/2 + x1
  339. y = (y2 - y1 - h)/2 + y1
  340. self.draw.text((x, y), message, align='center', font=font, fill=color)
  341. def apply_theme(self):
  342. self.grey = self.theme["grey"]
  343. self.light_grey = self.theme["light_grey"]
  344. self.dark_grey = self.theme["dark_grey"]
  345. self.blue = self.theme["blue"]
  346. self.pink = self.theme["pink"]
  347. self.olive = self.theme["olive"]
  348. #self.black = self.theme["black"]
  349. #self.dark_blue = self.theme["dark_blue"]
  350. #self.green = self.theme["green"]
  351. #self.red = self.theme["red"]
  352. def save_song(self):
  353. base_dir = "/home/pi/zpc_ct/user/songs/"
  354. title = self.sconf['title']
  355. fname = base_dir + self.sconf['title'] + '.sng'
  356. if os.path.exists(fname):
  357. os.utime(fname, None)
  358. print(fname, ' exists')
  359. else:
  360. print('make a damn file ', fname)
  361. open(fname, 'a').close()
  362. self.sconf = ConfigObj(fname)
  363. self.sconf['volume'] = self.volume
  364. self.sconf['song'] = self.song
  365. self.sconf['bpm'] = self.bpm
  366. self.sconf['title'] = title
  367. notes = []
  368. for n in self.soundSlots:
  369. notes.append([n.id, n.file, n.volume, n.pitch, n.notes])
  370. self.sconf['sounds'] = notes
  371. self.sconf.write()
  372. def save_config(self):
  373. base_dir = "/home/pi/zpc_ct/"
  374. print('before writing')
  375. print(self.mconf['theme'])
  376. self.mconf.write()
  377. def load_config(self):
  378. base_dir = "/home/pi/zpc_ct/"
  379. self.sconf = ConfigObj(base_dir + 'config.txt')
  380. self.theme = ConfigObj("/home/pi/zpc_ct/user/themes/" + self.mconf['theme'] + ".thm")
  381. def load_song(self):
  382. #self.stop_playback()
  383. self.quit_mixer()
  384. self.start_mixer()
  385. base_dir = "/home/pi/zpc_ct/user/songs/"
  386. self.sconf = ConfigObj(base_dir + self.mconf['default_song'] + '.sng')
  387. print(base_dir + self.mconf['default_song'] + '.sng')
  388. self.last_load_sound_id = None
  389. print('song bpm is ', self.sconf['bpm'])
  390. self.bpm = self.sconf.as_int('bpm')
  391. song = []
  392. for i in self.sconf['song']:
  393. song.append(int(i))
  394. obj_id = 0
  395. self.soundSlots = []
  396. _iter = 0
  397. while _iter < 64:
  398. #for s in self.sconf.as_list('sounds'):
  399. snd = self.sconf.as_list('sounds')[_iter]
  400. #print('-----')
  401. snd_lst = ast.literal_eval(snd)
  402. obj_id = snd_lst[0]
  403. filename = snd_lst[1]
  404. volume = int(snd_lst[2])
  405. pitch = int(snd_lst[3])
  406. notes = snd_lst[4]
  407. _iter += 1
  408. p1 = self.SoundSlot(filename, obj_id, self)
  409. p1.volume = volume
  410. p1.pitch = pitch
  411. if pitch != 0:
  412. p1.set_pitch()
  413. self.soundSlots.append(p1)
  414. p1.notes = notes
  415. _iter = 0
  416. self.song = song
  417. self.songCycle = cycle(self.song)
  418. self.curPattern = next(self.songCycle)
  419. self.songStart = self.curPattern
  420. self.update_bpm()
  421. # if self.playing:
  422. # self.stop_playback()
  423. # self.start_playback()
  424. def update_display(self, x):
  425. # if self.display_thread.is_alive():
  426. # pass
  427. # #self.disp.image(self.image, self.rotation)
  428. # else:
  429. # self.display_thread = threading.Thread(target=self.ud, args=([0]))
  430. # self.display_thread.start()
  431. # time.sleep(.001)
  432. try:
  433. self.disp.image(self.image, self.rotation)
  434. except:
  435. print('display failed')
  436. def ud(self, x):
  437. self.disp.image(self.image, self.rotation)
  438. def clear_notes_on(self):
  439. _iter = 0
  440. self.notes_on = []
  441. while _iter < 64:
  442. self.notes_on.append(0)
  443. _iter += 1
  444. def start_mixer(self):
  445. print('starting mixer')
  446. mixer.init()
  447. mixer.set_num_channels(32)
  448. def quit_mixer(self):
  449. if mixer.get_init() != None:
  450. print('quitting mixer')
  451. mixer.quit()
  452. p = Prog()
  453. #while True:
  454. while not done:
  455. p.Execute()
  456. if pygame.event.get(pygame.USEREVENT + 1):
  457. #if p.playhead == 0:
  458. #print('pattern ', p.curPattern)
  459. p.playhead += 1
  460. if p.playhead == 16:
  461. p.playhead = 0
  462. p.curPattern = next(p.songCycle)
  463. #blink internal led
  464. if p.playhead % 8 == 0:
  465. GPIO.setup(29, GPIO.OUT)
  466. GPIO.output(29, GPIO.LOW)
  467. else:
  468. GPIO.setup(29, GPIO.OUT)
  469. GPIO.output(29, GPIO.HIGH)
  470. p.pub.dispatch('beat', [p.curPattern, p.playhead])
  471. clock.tick_busy_loop()
  472. #print(clock.get_fps())
  473. pygame.quit()