aboutsummaryrefslogtreecommitdiff
path: root/music_sampler.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-25 23:37:49 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-25 23:40:51 +0200
commit4b2d79ca27dcbb85465829595ad81cec5fc63983 (patch)
tree683b3a9b19aa01de53b0dccc3ac143d876dcf088 /music_sampler.py
parent8612c5f8bb0fc9529bc489a6719654d4474db6d5 (diff)
downloadMusicSampler-4b2d79ca27dcbb85465829595ad81cec5fc63983.tar.gz
MusicSampler-4b2d79ca27dcbb85465829595ad81cec5fc63983.tar.zst
MusicSampler-4b2d79ca27dcbb85465829595ad81cec5fc63983.zip
Migrate to kivy
Diffstat (limited to 'music_sampler.py')
-rw-r--r--music_sampler.py123
1 files changed, 53 insertions, 70 deletions
diff --git a/music_sampler.py b/music_sampler.py
index 42c01e3..0d9a7a9 100644
--- a/music_sampler.py
+++ b/music_sampler.py
@@ -1,80 +1,63 @@
1import sys 1from kivy.app import App
2import pygame 2from kivy.uix.floatlayout import FloatLayout
3import helpers 3from kivy.uix.relativelayout import RelativeLayout
4import threading 4from kivy.properties import ListProperty, StringProperty
5 5from kivy.clock import Clock
6pygame.mixer.pre_init(frequency = 44100) 6from kivy.core.window import Window
7pygame.init() 7
8 8from helpers.key import Key
9size = width, height = 913, 563 9from helpers.mapping import Mapping
10screen = pygame.display.set_mode(size) 10
11screen.fill((229, 228, 226)) 11class PlayList(RelativeLayout):
12 12 playlist = ListProperty([])
13draw_lock = helpers.Lock("draw") 13
14 14 def __init__(self, **kwargs):
15mapping = helpers.Mapping(screen, draw_lock) 15 super(PlayList, self).__init__(**kwargs)
16channel_number, open_files = helpers.parse_config(mapping) 16 Clock.schedule_interval(self.update_playlist, 0.5)
17pygame.mixer.set_num_channels(channel_number) 17
18 18 def update_playlist(self, dt):
19mapping.draw() 19 if self.parent is None or 'Mapping' not in self.parent.ids:
20 return True
21
22 open_files = self.parent.ids['Mapping'].open_files
23 self.playlist = []
24 for music_file in open_files.values():
25 if not music_file.is_playing():
26 continue
27 if music_file.is_paused():
28 self.playlist.append(["⏸", music_file.name, False])
29 else:
30 self.playlist.append(["⏵", music_file.name, True])
20 31
21draw_lock.acquire()
22pygame.display.flip()
23draw_lock.release()
24 32
25contexts = [ 33class ActionList(RelativeLayout):
26 'normal' 34 action_title = StringProperty("")
27] 35 action_list = ListProperty([])
28 36
29context = 'normal' 37 def update_list(self, key, action_number = 0):
38 self.action_title = "actions linked to key {}:".format(key.key_sym)
39 self.action_list = []
30 40
31#### Normal workflow #### 41 action_descriptions = [action.description() for action in key.actions]
32while 1:
33 event = pygame.event.wait()
34 42
35 if event.type == pygame.QUIT or ( 43 for index, description in enumerate(action_descriptions):
36 event.type == pygame.KEYDOWN and 44 if index < int(action_number):
37 event.mod == 4160 and 45 icon = "✓"
38 event.key == pygame.K_c): 46 elif index + 0.5 == action_number:
39 for thread in threading.enumerate(): 47 icon = "✅"
40 if thread.getName()[0:2] != "MS": 48 else:
41 continue 49 icon = " "
42 thread.join()
43 50
44 pygame.quit() 51 self.action_list.append([icon, description])
45 sys.exit()
46 52
47 if context == 'normal': 53class Screen(FloatLayout):
48 if event.type == pygame.KEYDOWN: 54 pass
49 key = mapping.find_by_key_num(event.key)
50 if key is not None and not key.disabled:
51 threading.Thread(name = "MSKeyAction", target=key.do_actions, args = [screen]).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 55
58 draw_lock.acquire() 56class MusicSamplerApp(App):
59 police = helpers.font(14) 57 def build(self):
60 icon_police = helpers.font(14, font = "Symbola") 58 Window.size = (913, 563)
61 59
62 surface = pygame.Surface((208, 250)).convert() 60 return Screen()
63 surface.fill((250, 250, 250))
64 offset = 0
65 for music_file in open_files.values():
66 police.set_bold(False)
67 if music_file.is_playing():
68 if music_file.is_paused():
69 icon = icon_police.render("⏸", True, (0,0,0))
70 else:
71 icon = icon_police.render("⏵", True, (0,0,0))
72 police.set_bold(True)
73 text = police.render(music_file.name, True, (0,0,0))
74 surface.blit(icon, (0, offset))
75 surface.blit(text, (20, offset))
76 offset += police.get_linesize()
77 screen.blit(surface, (700, 308))
78 61
79 pygame.display.flip() 62if __name__ == '__main__':
80 draw_lock.release() 63 MusicSamplerApp().run()