]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - music_sampler.py
Added music name, currently playing musics, pause/unpause
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler.py
CommitLineData
d8ab67c7 1import sys
189bf90c 2import pygame
189bf90c 3import helpers
e7f8dab4 4import threading
189bf90c 5
8f88a3e4 6pygame.mixer.pre_init(frequency = 44100)
189bf90c
IB
7pygame.init()
8
9de92b6d 9size = width, height = 913, 563
189bf90c 10screen = pygame.display.set_mode(size)
9de92b6d 11screen.fill((229, 228, 226))
ba9ea93a
IB
12
13draw_lock = helpers.Lock("draw")
189bf90c 14
ba9ea93a 15mapping = helpers.Mapping(screen, draw_lock)
9de92b6d 16channel_number, open_files = helpers.parse_config(mapping)
d479af33 17pygame.mixer.set_num_channels(channel_number)
8f88a3e4 18
e7f8dab4 19mapping.draw()
189bf90c 20
ba9ea93a 21draw_lock.acquire()
189bf90c 22pygame.display.flip()
ba9ea93a 23draw_lock.release()
189bf90c
IB
24
25contexts = [
26 'normal'
27]
28
29context = 'normal'
30
23b7e0e5 31#### Normal workflow ####
189bf90c
IB
32while 1:
33 event = pygame.event.wait()
e7f8dab4 34
189bf90c
IB
35 if event.type == pygame.QUIT or (
36 event.type == pygame.KEYDOWN and
37 event.mod == 4160 and
38 event.key == pygame.K_c):
be27763f 39 for thread in threading.enumerate():
23b7e0e5 40 if thread.getName()[0:2] != "MS":
be27763f
IB
41 continue
42 thread.join()
43
189bf90c
IB
44 pygame.quit()
45 sys.exit()
46
47 if context == 'normal':
d8ab67c7 48 if event.type == pygame.KEYDOWN:
e7f8dab4 49 key = mapping.find_by_key_num(event.key)
d8ab67c7 50 if key is not None:
23b7e0e5 51 threading.Thread(name = "MSKeyAction", target=key.do_actions).start()
9de92b6d 52 threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()
d8ab67c7 53 elif event.type == pygame.MOUSEBUTTONUP:
e7f8dab4 54 key = mapping.find_by_collidepoint(pygame.mouse.get_pos())
d8ab67c7 55 if key is not None:
23b7e0e5 56 threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()
189bf90c 57
ba9ea93a 58 draw_lock.acquire()
9de92b6d
IB
59 if getattr(sys, 'frozen', False):
60 icon_police = pygame.font.Font(sys._MEIPASS + "/Symbola.ttf", 19)
61 police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14)
62 else:
63 icon_police = pygame.font.Font("Symbola.ttf", 19)
64 police = pygame.font.Font("Ubuntu-Regular.ttf", 14)
65
66 surface = pygame.Surface((208, 250)).convert()
67 surface.fill((250, 250, 250))
68 offset = 0
69 for music_file in open_files.values():
70 police.set_bold(False)
71 if music_file.is_playing():
72 icon = icon_police.render("⏵", True, (0,0,0))
73 if music_file.is_paused():
74 icon = icon_police.render("⏸", True, (0,0,0))
75 else:
76 police.set_bold(True)
77 text = police.render(music_file.name, True, (0,0,0))
78 surface.blit(icon, (0, offset))
79 surface.blit(text, (20, offset))
80 offset += police.get_linesize()
81 screen.blit(surface, (700, 308))
82
189bf90c 83 pygame.display.flip()
ba9ea93a 84 draw_lock.release()
189bf90c 85
23b7e0e5
IB
86#### In Ipython ####
87# for thread in threading.enumerate():
88# if thread.getName()[0:2] != "MS":
89# continue
90# thread.join()