diff options
Diffstat (limited to 'music_sampler.py')
-rw-r--r-- | music_sampler.py | 123 |
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 @@ | |||
1 | import sys | 1 | from kivy.app import App |
2 | import pygame | 2 | from kivy.uix.floatlayout import FloatLayout |
3 | import helpers | 3 | from kivy.uix.relativelayout import RelativeLayout |
4 | import threading | 4 | from kivy.properties import ListProperty, StringProperty |
5 | 5 | from kivy.clock import Clock | |
6 | pygame.mixer.pre_init(frequency = 44100) | 6 | from kivy.core.window import Window |
7 | pygame.init() | 7 | |
8 | 8 | from helpers.key import Key | |
9 | size = width, height = 913, 563 | 9 | from helpers.mapping import Mapping |
10 | screen = pygame.display.set_mode(size) | 10 | |
11 | screen.fill((229, 228, 226)) | 11 | class PlayList(RelativeLayout): |
12 | 12 | playlist = ListProperty([]) | |
13 | draw_lock = helpers.Lock("draw") | 13 | |
14 | 14 | def __init__(self, **kwargs): | |
15 | mapping = helpers.Mapping(screen, draw_lock) | 15 | super(PlayList, self).__init__(**kwargs) |
16 | channel_number, open_files = helpers.parse_config(mapping) | 16 | Clock.schedule_interval(self.update_playlist, 0.5) |
17 | pygame.mixer.set_num_channels(channel_number) | 17 | |
18 | 18 | def update_playlist(self, dt): | |
19 | mapping.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 | ||
21 | draw_lock.acquire() | ||
22 | pygame.display.flip() | ||
23 | draw_lock.release() | ||
24 | 32 | ||
25 | contexts = [ | 33 | class ActionList(RelativeLayout): |
26 | 'normal' | 34 | action_title = StringProperty("") |
27 | ] | 35 | action_list = ListProperty([]) |
28 | 36 | ||
29 | context = '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] |
32 | while 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': | 53 | class 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() | 56 | class 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() | 62 | if __name__ == '__main__': |
80 | draw_lock.release() | 63 | MusicSamplerApp().run() |