-
Notifications
You must be signed in to change notification settings - Fork 7
/
audio_simple.py
executable file
·51 lines (37 loc) · 1.06 KB
/
audio_simple.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Simple audio library using simpleaudio
Demo/Test:
./audio_simple.py
Dependencies:
pip3 install simpleaudio
"""
import logging
import os
import simpleaudio
class Audio:
def __init__(self, files):
self.samples = {}
for fn in files:
basename = os.path.basename(fn)
key, ext = os.path.splitext(basename)
logging.info("loading [%s] -> [%s]" % (fn, key))
self.samples[key] = simpleaudio.WaveObject.from_wave_file(fn)
def Names(self):
return self.samples.keys()
def Play(self, name, blocking=False):
p = self.samples[name].play()
if blocking:
p.wait_done()
if __name__ == "__main__":
import time
import glob
def main():
logging.basicConfig(level=logging.INFO)
cwd = os.path.dirname(__file__)
audio = Audio(glob.glob(cwd + "/Sounds/*.wav"))
for n in audio.Names():
logging.info("play %s" % n)
audio.Play(n, True)
time.sleep(2.0)
main()