-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscootsound.py
64 lines (56 loc) · 2.62 KB
/
scootsound.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
import threading
# audio libraries
# be sure to import threading libraries prior to these imports
# as the subprocess behavior will be different
from pydub import AudioSegment
from pydub.playback import play
class ScootSound:
"""
Class to manage and play different audio effects for the scooter.
Attributes:
sound_meltdown (AudioSegment): An audio segment for the 'meltdown' mode.
sound_disco (AudioSegment): An audio segment for the 'disco' mode.
sound_underlight (AudioSegment): An audio segment for the 'underlight' mode.
sound_fireplace (AudioSegment): An audio segment for the 'fireplace' mode.
sound_lights_out (AudioSegment): An audio segment for the 'lights out' mode.
Depencencies:
threading
pydub
:param enabled: bool: Enable audio output.
"""
def __init__(self, enabled: bool = True):
"""
Initializes the ScootSound class with empty audio segments.
"""
self.enabled = enabled
self.sound_meltdown = AudioSegment.empty()
self.sound_disco = AudioSegment.empty()
self.sound_underlight = AudioSegment.empty()
self.sound_fireplace = AudioSegment.empty()
self.sound_lights_out = AudioSegment.empty()
def import_from_disk(self):
"""
Imports audio files from the disk into their corresponding attributes.
Assumes the existence of MP3 files in the 'static/sounds/' directory.
This method blocks while reading and parsing audio, which may be lengthy.
"""
if self.enabled:
self.sound_meltdown = AudioSegment.from_mp3("static/sounds/meltdown.mp3")
self.sound_disco = AudioSegment.from_mp3("static/sounds/disco.mp3")
self.sound_underlight = AudioSegment.from_mp3("static/sounds/underlight.mp3")
self.sound_fireplace = AudioSegment.from_mp3("static/sounds/fireplace.mp3")
self.sound_energyweapon = AudioSegment.from_mp3("static/sounds/energyweapon.mp3")
self.sound_lights_out = AudioSegment.from_mp3("static/sounds/lights-out.mp3")
def play(self, segment: AudioSegment) -> threading.Thread:
"""
Plays an audio segment in a new daemon thread.
:param segment (AudioSegment): The audio segment to be played.
:return threading.Thread: The thread in which the audio segment is being played.
"""
thread = threading.Thread()
if self.enabled:
thread = threading.Thread(target = lambda: play(segment), daemon = True)
else:
thread = threading.Thread(target = lambda: None)
thread.start()
return thread