-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsoundboard.py
33 lines (27 loc) · 920 Bytes
/
soundboard.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
import yaml
import io
from pydub import AudioSegment
from pydub.playback import play
class AudioPlayer:
def __init__(self, filename):
self.filename = filename
self.sound = AudioSegment.from_file(self.filename)
def start(self):
play(self.sound)
class SoundBoard:
def __init__(self):
try:
self.config = yaml.safe_load(open("soundboard.yml"))
except:
self.config = {}
for sound in self.config["sounds"]:
try:
sound["player"] = AudioPlayer(sound["file"])
except Exception as e:
print(f"Error loading sound: {e}" + sound["id"])
def play(self, sound_id):
for sound in self.config["sounds"]:
if sound["id"] == sound_id:
sound["player"].start()
return
print("Sound not found: " + sound_id)