-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotas.py
More file actions
119 lines (103 loc) · 2.86 KB
/
notas.py
File metadata and controls
119 lines (103 loc) · 2.86 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import sys
import select
import pygame
WIDTH = 640
HEIGHT = 480
pygame.init()
fpsClock = pygame.time.Clock()
window = pygame.display.set_mode((WIDTH, HEIGHT))
black = pygame.Color(0, 0, 0)
gray = pygame.Color(128, 128, 128)
white = pygame.Color(255, 255, 255)
green = pygame.Color(0, 255, 0)
red = pygame.Color(255, 0, 0)
blue = pygame.Color(0, 0, 255)
pink = pygame.Color(255, 0, 255)
darkGreen = pygame.Color(0, 128, 0)
darkRed = pygame.Color(128, 0, 0)
darkBlue = pygame.Color(0, 0, 128)
darkPink = pygame.Color(128, 0, 128)
intervalColors = [
blue, # octave
red, # 2nd m
pink, # 2nd M
green, # 3rd m
green, # 3rd M
blue, # 4th P
red, # 4th A/5th D
blue, # 5th P
green, # 6th m
green, # 6th M
pink, # 7th m
red, # 7th M
]
inactiveColors = [
darkBlue, # octave
darkRed, # 2nd m
darkPink, # 2nd M
darkGreen, # 3rd m
darkGreen, # 3rd M
darkBlue, # 4th P
darkRed, # 4th A/5th D
darkBlue, # 5th P
darkGreen, # 6th m
darkGreen, # 6th M
darkPink, # 7th m
darkRed, # 7th M
]
fd = os.open('/dev/midi1', os.O_RDONLY)
poll = select.poll()
poll.register(fd, select.POLLIN)
step = 0
notes = []
def add_intervals(new_note):
for n in notes:
if n.active:
n.intervals.append(new_note)
class Note(object):
def __init__(self, step, pitch):
self.pitch = pitch
self.step = step
self.active = True
self.intervals = []
def x(self):
return WIDTH - (step - self.step)
def y(self):
return HEIGHT - 4 * self.pitch
while True:
step = step + 1
data = poll.poll(0)
while data:
code = ord(os.read(fd, 1))
if code in (144, 128):
pitch, force = [ord(x) for x in os.read(fd, 2)]
if code == 144:
new_note = Note(step, pitch)
notes.append(new_note)
add_intervals(new_note)
else:
# !!Use a dict for pitches
for n in notes:
if n.pitch == pitch:
n.active = False
data = poll.poll(0)
notes = [x for x in notes if x.step > step - WIDTH]
window.fill(black)
for n in notes:
color = white if n.active else gray
pygame.draw.circle(window, color, (n.x(), n.y()), 2)
for other in n.intervals:
if n.active and other.active:
colorArray = intervalColors
else:
colorArray = inactiveColors
color = colorArray[(other.pitch - n.pitch) % 12]
pygame.draw.line(window, color, (n.x(), n.y()),
(other.x(), other.y()), 1)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
fpsClock.tick(30)