-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_board.py
executable file
·86 lines (72 loc) · 3.53 KB
/
sound_board.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python3
import asyncio
import json
import os
from random import randint
#----------------------------------------------------------------------------------------
class Sound_board:
sound_dir = "/home/pi/python/spider/clips/"
sound_parmfile = sound_dir + "soundparms.json"
voice_volume = 40000 # range: 0 - max (1000000?)
max_volume = 40000 # range: 0 - max
max_volume_limit = 60000 # can scale up the volume but limit it to this.
def __init__(self):
try:
with open(self.sound_parmfile, "r") as f:
self.sound_clips = json.load(f)
except FileNotFoundError:
print(self.sound_parmfile + ": not found. Reloading.")
self._load_clips()
# print(self.sound_clips)
self.last_clip = None if len(self.sound_clips) == 0 else randint(0, len(self.sound_clips)-1)
# self.clips_init = True # lets us spin up through the clips if we use an async iterator
#---------------------------------------------------------------------------------
def print_clips(self, sound_clips):
for i, f in enumerate(sound_clips):
print(f"{i}\t{f}")
print()
def _load_clips(self):
self.sound_clips = []
if not os.path.isdir(self.sound_dir):
print(self.sound_dir + ": Sound dir does not exist.")
return # dir doesn't exist,
# return empty list = no sound files
for file in os.listdir(self.sound_dir):
_, file_extension = os.path.splitext(file) # discard filename part
if file_extension[1:] in {"mp3"}: # set of allowed sound files
entry = [self.sound_dir + file, 1.0] # it would be nicer as a tuple, but we can't store
# it in a json file.
self.sound_clips.append(entry)
#---------------------------------------------------------------------------------
def _get_sound_index(self):
self.last_clip += 1
if self.last_clip >= len(self.sound_clips):
self.last_clip = 0
return self.last_clip
#---------------------------------------------------------------------------------
async def _play_sound2(self, sound_file, volume):
t_sound = await asyncio.create_subprocess_exec("mpg123", "-q",
"-f " + str(volume),
sound_file, stdout=asyncio.subprocess.PIPE)
await t_sound.wait() # wait for the mpg123 process to finish
async def play_sound(self, my_globals, indx=None):
if self.last_clip is None: # no sound files
return
limit = 2 # Play no more than 2x.
sound_entry = self.sound_clips[self._get_sound_index()] if indx is None else self.sound_clips[indx]
sound_file = sound_entry[0]
scale = sound_entry[1]
volume = int(my_globals.spider_parms["VOLUME"] * scale) # scale up volume as needed.
volume = min(volume, self.max_volume_limit)
# print(f"{sound_file}, {volume}")
if not os.path.isfile(sound_file):
return # file does not exist.
for i in range(limit):
if my_globals.spider_parms["END_REQUEST"]:
break # OK, we're gone!
await self._play_sound2(sound_file, volume)
if i == limit - 1:
return # don't wait after the last iteration completed
await asyncio.sleep(1)
if __name__ == "__main__":
s = Sound_board()