-
Notifications
You must be signed in to change notification settings - Fork 7
/
audio_aplay.py
executable file
·53 lines (40 loc) · 1.15 KB
/
audio_aplay.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Simple audio library using the external aplay binary
Demo/Test:
./audio_aplay.py Sounds/*
Dependencies
sudo apt install alsa-utils
"""
import logging
import os
class Audio(object):
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] = fn
def Names(self):
return self.samples.keys()
def Play(self, name, blocking):
s = self.samples[name]
logging.info("playing [%s]" % s)
if blocking:
os.system("aplay '%s'" % s)
else:
os.spawnlp(os.P_NOWAIT, "aplay", "aplay", s)
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(1.0)
main()