-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsound_engine.py
54 lines (39 loc) · 1.38 KB
/
sound_engine.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
import time
from chord import Chord
from note import Note
from media_output import MediaOutput
class SoundEngine:
def __init__(self, output: MediaOutput):
self.output = output
self.output.init()
def play(self, notes: Chord | Note | list[Note | int] | int, interval=0, duration=5):
"""Play the notes at once."""
# Handle empty/None cases
if notes is None or (isinstance(notes, list) and len(notes) == 0):
time.sleep(duration)
return
# Convert Chord object to list of notes
if isinstance(notes, Chord):
notes = notes.notes
# Convert single Note/int to list
if not isinstance(notes, list):
notes = [notes]
# Get note indices based on input type
note_indices = []
first_note = notes[0]
if isinstance(first_note, Note):
note_indices = [n.index for n in notes]
elif isinstance(first_note, int):
note_indices = notes
else:
print(first_note)
raise Exception("Unknown note type")
# Use the media output to play notes
self.output.play_notes(note_indices, interval, duration)
def main():
from pygame_output import PyGameOutput
engine = SoundEngine(PyGameOutput())
chord = Chord('Dm7')
engine.play(chord, interval=0.1)
if __name__ == '__main__':
main()