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