-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
75 lines (58 loc) · 2.08 KB
/
utils.py
File metadata and controls
75 lines (58 loc) · 2.08 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
from tkinter import filedialog, Tk
import pygame
from Config import NOTE_NAMES
def clamp(v, lo, hi):
return max(lo, min(v, hi))
def is_black(note):
return NOTE_NAMES[note % 12].endswith('#')
def open_midi_file(initial_dir="."):
root = Tk()
root.withdraw()
filename = filedialog.askopenfilename(
initialdir=initial_dir,
title="Import MIDI file",
filetypes=[("MIDI files", "*.mid *.midi")]
)
root.destroy()
return filename
def save_midi_file():
root = Tk()
root.withdraw()
filename = filedialog.asksaveasfilename(
title="Export MIDI file",
defaultextension=".mid",
filetypes=[("MIDI files", "*.mid *.midi")]
)
root.destroy()
return filename
def open_sf2_file(initial_dir="."):
root = Tk()
root.withdraw()
path = filedialog.askopenfilename(
initialdir=initial_dir,
title="Select SoundFont",
filetypes=(("SoundFont files", "*.sf2"), ("all files", "*.*"))
)
root.destroy()
return path
def load_icon(filename):
try:
icon = pygame.image.load(filename).convert_alpha()
return pygame.transform.scale(icon, (24, 24))
except:
return pygame.Surface((24, 24), pygame.SRCALPHA)
def get_note_name(midi_val, current_key="C Major"):
# Standard note names
sharps = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
flats = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B']
# Decide whether to use sharps or flats based on the key
# Simple rule: Keys like F, Bb, Eb, Ab, Db, Gb Major usually use flats
flat_keys = ["F Major", "Bb Major", "Eb Major", "Ab Major", "Db Major", "Gb Major",
"D Minor", "G Minor", "C Minor", "F Minor", "Bb Minor", "Eb Minor"]
note_list = flats if current_key in flat_keys else sharps
name = note_list[midi_val % 12]
octave = (midi_val // 12) - 1
return f"{name}{octave}"
def rgb_to_hex(rgb):
"""Converts (R, G, B) tuple to uppercase #RRGGBB string."""
return '#{:02x}{:02x}{:02x}'.format(rgb[0], rgb[1], rgb[2]).upper()