aboutsummaryrefslogtreecommitdiff
path: root/music_sampler.py
blob: 6fed26b05e8ddb5406c465b052a70eb9a5624f44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sys
import pygame
import helpers
import threading

pygame.mixer.pre_init(frequency = 44100)
pygame.init()

size = width, height = 913, 563
screen = pygame.display.set_mode(size)
screen.fill((229, 228, 226))

draw_lock = helpers.Lock("draw")

mapping = helpers.Mapping(screen, draw_lock)
channel_number, open_files = helpers.parse_config(mapping)
pygame.mixer.set_num_channels(channel_number)

mapping.draw()

draw_lock.acquire()
pygame.display.flip()
draw_lock.release()

contexts = [
    'normal'
]

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 = mapping.find_by_key_num(event.key)
            if key is not None and not key.disabled:
                threading.Thread(name = "MSKeyAction", target=key.do_actions).start()
                threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()
        elif event.type == pygame.MOUSEBUTTONUP:
            key = mapping.find_by_collidepoint(pygame.mouse.get_pos())
            if key is not None:
                threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()

    draw_lock.acquire()
    police = helpers.font(14)
    icon_police = helpers.font(14, font = "Symbola")

    surface = pygame.Surface((208, 250)).convert()
    surface.fill((250, 250, 250))
    offset = 0
    for music_file in open_files.values():
        police.set_bold(False)
        if music_file.is_playing():
            if music_file.is_paused():
                icon = icon_police.render("⏸", True, (0,0,0))
            else:
                icon = icon_police.render("⏵", True, (0,0,0))
                police.set_bold(True)
            text = police.render(music_file.name, True, (0,0,0))
            surface.blit(icon, (0, offset))
            surface.blit(text, (20, offset))
            offset += police.get_linesize()
    screen.blit(surface, (700, 308))

    pygame.display.flip()
    draw_lock.release()