]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - music_sampler.py
Fix channels and description with blank lines
[perso/Immae/Projets/Python/MusicSampler.git] / music_sampler.py
index 7933d50acfb1f701bf00acbab5a9c0af4057b413..8dbbc28de8e35a9e10e3ba08f08a1a2a96a89c1c 100644 (file)
@@ -1,30 +1,26 @@
 import sys
 import pygame
-import pydub
 import helpers
+import threading
 
 pygame.mixer.pre_init(frequency = 44100)
 pygame.init()
 
 size = width, height = 1024, 600
-
 screen = pygame.display.set_mode(size)
-background = pygame.Surface(screen.get_size())
-background = background.convert()
-background.fill((250, 250, 250))
+screen.fill((250, 250, 250))
 
-action_surface = pygame.Surface((600, 250)).convert()
-action_surface.fill((0,0,0))
-helpers.parse_config()
+draw_lock = helpers.Lock("draw")
 
-for key_name in helpers.Mapping.KEYS:
-    key = helpers.Mapping.KEYS[key_name]
-    key.draw(background)
+mapping = helpers.Mapping(screen, draw_lock)
+channel_number = helpers.parse_config(mapping)
+pygame.mixer.set_num_channels(channel_number)
 
-screen.blit(background, (0, 0))
-screen.blit(action_surface, (10, 330))
+mapping.draw()
 
+draw_lock.acquire()
 pygame.display.flip()
+draw_lock.release()
 
 contexts = [
     'normal'
@@ -32,24 +28,38 @@ contexts = [
 
 context = 'normal'
 
+#### Normal workflow ####
 while 1:
     event = pygame.event.wait()
+
     if event.type == pygame.QUIT or (
             event.type == pygame.KEYDOWN and 
             event.mod  == 4160 and
             event.key  == pygame.K_c):
+        for thread in threading.enumerate():
+            if thread.getName()[0:2] != "MS":
+                continue
+            thread.join()
+
         pygame.quit()
         sys.exit()
 
     if context == 'normal':
         if event.type == pygame.KEYDOWN:
-            key = helpers.Key.find_by_key_num(event.key)
+            key = mapping.find_by_key_num(event.key)
             if key is not None:
-                key.do_actions()
+                threading.Thread(name = "MSKeyAction", target=key.do_actions).start()
         elif event.type == pygame.MOUSEBUTTONUP:
-            key = helpers.Key.find_by_collidepoint(pygame.mouse.get_pos())
+            key = mapping.find_by_collidepoint(pygame.mouse.get_pos())
             if key is not None:
-                key.list_actions(action_surface)
+                threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()
 
+    draw_lock.acquire()
     pygame.display.flip()
+    draw_lock.release()
 
+#### In Ipython ####
+# for thread in threading.enumerate():
+#     if thread.getName()[0:2] != "MS":
+#         continue
+#     thread.join()