-
Notifications
You must be signed in to change notification settings - Fork 0
/
playable_file.py
35 lines (28 loc) · 1.04 KB
/
playable_file.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
from os import path
import sounddevice as sd
import soundfile as sf
class PlayableFile(object):
def __init__(self, file_path, silent_fail=False):
self.silent_fail = silent_fail
if file_path is None:
raise AttributeError('missing a file to play')
if path.exists(file_path):
self._filepath = file_path
else:
# try get a local file
self._filename = file_path
self._filepath = path.dirname(__file__) + "/" + self._filename
self.__check_for_file()
def exists(self):
return path.exists(self._filepath)
def play(self):
self.__check_for_file()
print("Playing %s" % self._filepath)
data, fs = sf.read(file=self._filepath, dtype='float32')
sd.play(data, samplerate=44100)
status = sd.wait()
if status:
print("error %s" % status)
def __check_for_file(self):
if not self.exists() and not self.silent_fail:
raise AttributeError(self._filepath + ' does not exist')