-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNote.py
More file actions
46 lines (35 loc) · 1.13 KB
/
Copy pathNote.py
File metadata and controls
46 lines (35 loc) · 1.13 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
from importlib.resources import is_resource
from utils import NOTE_MAP
from Tone import Tone
import threading
import time
class Note:
def __init__(self, note, duration=1):
self.duration = duration
if note == 'rest':
self.is_resting = True
return
else:
self.is_resting = False
# Ensures the first character of note is uppercase
main_note = note[0].upper()
self.note = main_note + note[1:]
self.frequency = NOTE_MAP[self.note]
def play(self, speaker=None):
if not self.is_resting:
Tone.sine(self.frequency, duration=self.duration, speaker=speaker)
else:
time.sleep(self.duration)
@staticmethod
def rest(duration):
return Note('rest', duration)
@staticmethod
def play_chord(notes):
threads = []
for note in notes:
thread = threading.Thread(target=note.play)
threads.append(thread)
for thread in threads:
thread.start()
for thread in threads:
thread.join()