]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - music_sampler.py
Added music name, currently playing musics, pause/unpause
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler.py
1 import sys
2 import pygame
3 import helpers
4 import threading
5
6 pygame.mixer.pre_init(frequency = 44100)
7 pygame.init()
8
9 size = width, height = 913, 563
10 screen = pygame.display.set_mode(size)
11 screen.fill((229, 228, 226))
12
13 draw_lock = helpers.Lock("draw")
14
15 mapping = helpers.Mapping(screen, draw_lock)
16 channel_number, open_files = helpers.parse_config(mapping)
17 pygame.mixer.set_num_channels(channel_number)
18
19 mapping.draw()
20
21 draw_lock.acquire()
22 pygame.display.flip()
23 draw_lock.release()
24
25 contexts = [
26 'normal'
27 ]
28
29 context = 'normal'
30
31 #### Normal workflow ####
32 while 1:
33 event = pygame.event.wait()
34
35 if event.type == pygame.QUIT or (
36 event.type == pygame.KEYDOWN and
37 event.mod == 4160 and
38 event.key == pygame.K_c):
39 for thread in threading.enumerate():
40 if thread.getName()[0:2] != "MS":
41 continue
42 thread.join()
43
44 pygame.quit()
45 sys.exit()
46
47 if context == 'normal':
48 if event.type == pygame.KEYDOWN:
49 key = mapping.find_by_key_num(event.key)
50 if key is not None:
51 threading.Thread(name = "MSKeyAction", target=key.do_actions).start()
52 threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()
53 elif event.type == pygame.MOUSEBUTTONUP:
54 key = mapping.find_by_collidepoint(pygame.mouse.get_pos())
55 if key is not None:
56 threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()
57
58 draw_lock.acquire()
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
83 pygame.display.flip()
84 draw_lock.release()
85
86 #### In Ipython ####
87 # for thread in threading.enumerate():
88 # if thread.getName()[0:2] != "MS":
89 # continue
90 # thread.join()