]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - music_sampler.py
Fix threading problems with ipython
[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 = 1024, 600
10 screen = pygame.display.set_mode(size)
11 screen.fill((250, 250, 250))
12
13 draw_lock = helpers.Lock("draw")
14
15 mapping = helpers.Mapping(screen, draw_lock)
16 helpers.parse_config(mapping)
17
18 mapping.draw()
19
20 draw_lock.acquire()
21 pygame.display.flip()
22 draw_lock.release()
23
24 contexts = [
25 'normal'
26 ]
27
28 context = 'normal'
29
30 #### Normal workflow ####
31 while 1:
32 event = pygame.event.wait()
33
34 if event.type == pygame.QUIT or (
35 event.type == pygame.KEYDOWN and
36 event.mod == 4160 and
37 event.key == pygame.K_c):
38 for thread in threading.enumerate():
39 if thread.getName()[0:2] != "MS":
40 continue
41 thread.join()
42
43 pygame.quit()
44 sys.exit()
45
46 if context == 'normal':
47 if event.type == pygame.KEYDOWN:
48 key = mapping.find_by_key_num(event.key)
49 if key is not None:
50 threading.Thread(name = "MSKeyAction", target=key.do_actions).start()
51 elif event.type == pygame.MOUSEBUTTONUP:
52 key = mapping.find_by_collidepoint(pygame.mouse.get_pos())
53 if key is not None:
54 threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()
55
56 draw_lock.acquire()
57 pygame.display.flip()
58 draw_lock.release()
59
60 #### In Ipython ####
61 # for thread in threading.enumerate():
62 # if thread.getName()[0:2] != "MS":
63 # continue
64 # thread.join()