forked from quasar098/midi-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
267 lines (219 loc) · 8.34 KB
/
utils.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# noinspection PyUnresolvedReferences
from typing import Union, Optional, BinaryIO
# noinspection PyUnresolvedReferences
from errors import *
from config import Config, get_colors
from translations import TRANSLATIONS
# noinspection PyUnresolvedReferences
from time import time as get_current_time
import moderngl
import mido
import pygame
from sys import platform
import subprocess
from enum import Enum
from os.path import join
from math import sin, pi
from sys import setrecursionlimit
setrecursionlimit(10000) # increase if more notes
# vsync framerate if on windows, else 60
if platform == "win32":
try:
# noinspection PyPackageRequirements
import win32api
FRAMERATE = win32api.EnumDisplaySettings(win32api.EnumDisplayDevices().DeviceName, -1).DisplayFrequency
except ModuleNotFoundError:
print("The win32api python module is not found! Install it with \"pip install pywin32\"")
FRAMERATE = 60
elif "linux" in platform:
# noinspection PyPackageRequirements
from Xlib import display
# noinspection PyPackageRequirements
from Xlib.ext import randr
d = display.Display()
default_screen = d.get_default_screen()
info = d.screen(default_screen)
resources = randr.get_screen_resources(info.root)
active_modes = set()
for crtc in resources.crtcs:
crtc_info = randr.get_crtc_info(info.root, crtc, resources.config_timestamp)
if crtc_info.mode:
active_modes.add(crtc_info.mode)
for mode in resources.modes:
if mode.id in active_modes:
FRAMERATE = round(mode.dot_clock / (mode.h_total * mode.v_total))
break
else:
FRAMERATE = 60
# camera follow modes
class CameraFollow(Enum):
Center = 0 # center the square
Lazy = 1 # lazy camera, used by Crazy Nutter 101
Smoothed = 2 # smoothed camera, interpolates camera a little bit between current and previous every frame
Predictive = 3 # smoothed camera, but you can see where the square will bounce better
def read_osu_file(filedata: bytes):
filedata = filedata.decode("utf-8")
filelines = filedata.splitlines(False)
started_counting = False
timestamps = []
for line in filelines:
if not started_counting:
if "HitObjects" in line:
started_counting = True
continue
if len(line) < 3:
continue
args = line.split(",")
if (int(args[3]) & 3) == 0:
continue
timestamps.append(int(args[2])/1000)
return timestamps
def surf_to_texture(in_surface: pygame.Surface) -> moderngl.Texture:
tex = Config.ctx.texture(in_surface.get_size(), 4)
tex.filter = (moderngl.NEAREST, moderngl.NEAREST)
tex.swizzle = 'BGRA'
tex.write(in_surface.get_view('1'))
return tex
def update_screen(screen: pygame.Surface, glsl_program: moderngl.Program, render_object: moderngl.VertexArray):
frame_tex = surf_to_texture(screen)
frame_tex.use(0)
glsl_program['tex'] = 0
if "ascii.glsl" in Config.shader_file_name:
if Config.ascii_tex is None:
Config.ascii_tex = surf_to_texture(pygame.image.load('./assets/shaders/ascii.png').convert_alpha())
Config.ascii_tex.use(1)
try:
glsl_program['asciipng'] = 1
except KeyError:
pass
render_object.render(mode=moderngl.TRIANGLE_STRIP)
pygame.display.flip()
frame_tex.release()
def open_file(filename):
if platform == "win32":
from os import startfile
startfile(filename)
else:
opener = "open" if platform == "darwin" else "xdg-open"
subprocess.call([opener, filename])
# noinspection PyUnresolvedReferences
def read_midi_file(file):
midi_file = mido.MidiFile(file=file)
notes = []
current_time = 0
for msg in midi_file:
if msg.type == 'note_on' and msg.velocity != 0:
timestamp = current_time + msg.time
notes.append(round(timestamp*1000)/1000)
current_time += msg.time
return notes
# remove values that are too close to each other
def remove_too_close_values(lst: list[float], threshold=30) -> list[float]:
"""Assumes the list is sorted"""
new = []
before = None
for _ in lst:
if before is None:
before = _
new.append(_)
continue
if before+threshold/1000 > _:
continue
before = _
new.append(_)
return new
def fix_overlap(rects: list[pygame.Rect], callback=None):
if callback is None:
callback = lambda _: None
xvs = set()
yvs = set()
for rect in rects:
xvs.add(rect.right)
xvs.add(rect.left)
yvs.add(rect.top)
yvs.add(rect.bottom)
xvs = sorted(list(xvs))
yvs = sorted(list(yvs))
outputs = []
for xv1 in range(len(xvs)-1):
xv2 = xv1+1
for yv1 in range(len(yvs)-1):
yv2 = yv1+1
r = pygame.Rect(xvs[xv1], yvs[yv1], xvs[xv2]-xvs[xv1], yvs[yv2]-yvs[yv1])
if r.collidelist(rects)+1:
outputs.append(r)
if callback(f"Checking minirectangles ({int(100*xv1*len(yvs)/(len(xvs)*len(yvs)))}% done)"):
raise UserCancelsLoadingError()
callback("Merging adjacent minirectangles")
for ai in range(len(outputs)-1):
a = outputs[ai+1]
b = outputs[ai]
if a.width == 0 or a.height == 0 or b.width == 0 or b.height == 0:
continue
if not (a.right == b.left or a.left == b.right or a.top == b.bottom or b.bottom == a.top):
continue
if a.x == b.x and a.width == b.width:
a.y = min(a.y, b.y)
a.height = a.height+b.height
b.height = 0
continue
outputs = [out for out in outputs if out.width > 0 and out.height > 0]
outputs.sort(key=lambda _: _.y*100000+_.x)
for ai in range(len(outputs)-1):
a = outputs[ai+1]
b = outputs[ai]
if a.width == 0 or a.height == 0 or b.width == 0 or b.height == 0:
continue
if not (a.right == b.left or a.left == b.right or a.top == b.bottom or b.bottom == a.top):
continue
if a.y == b.y and a.height == b.height:
a.x = min(a.x, b.x)
a.width = a.width+b.width
b.width = 0
continue
callback("Finished loading")
return [out for out in outputs if out.width > 0 and out.height > 0]
_font_registry: dict[str, pygame.font.Font] = {}
def lang_key(key: str):
"""
useful for different languages, so everyone can be able to read the text.
translations are found in translations.py btw
"""
english_language = TRANSLATIONS["english"]
my_language = TRANSLATIONS.get(Config.language, {})
if key not in english_language:
print(f"warning: there is no english text for {key} yet!!")
return my_language.get(key, english_language.get(key, "<missing>"))
def get_font(size: int = 24, font_file_name: str = None) -> pygame.font.Font:
if font_file_name is None:
font_file_name = lang_key("font")
font_path = f'./assets/fonts/{font_file_name}'
fn_id = f"[{font_path}/{size}]"
if fn_id not in _font_registry:
try:
_font_registry[fn_id] = pygame.font.Font(font_path, size)
except FileNotFoundError:
print(f"Font {fn_id} not found!")
_font_registry[fn_id] = pygame.font.SysFont("Arial", size)
return _font_registry[fn_id]
_channels = []
_sound_registry: dict[str, pygame.mixer.Sound] = {}
def play_sound(snd_name: str, vol: float = 0.5):
if len(_channels) == 0:
pygame.mixer.set_num_channels(40)
for _ in range(1, 20):
_channels.append(pygame.mixer.Channel(_))
if snd_name not in _sound_registry:
_sound_registry[snd_name] = pygame.mixer.Sound(join("assets", snd_name))
chan = pygame.mixer.find_channel(False)
if not chan:
return False
if chan.get_busy():
return False
chan.set_volume(vol)
chan.play(_sound_registry[snd_name])
return True
def interpolate_fn(n):
"""Interpolate sigmoidally from 0-1"""
n = min(max(n, 0), 1)
return sin(pi * (n - 0.5)) / 2 + 0.5