-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path_P502_PyGameSnd.py
189 lines (178 loc) · 6.02 KB
/
_P502_PyGameSnd.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
#############################################################################
##################### PyGame Sound Player plugin for RPIEasy ################
#############################################################################
#
# Can be controlled by plugin_receivedata() like a DIMMER device - Selector Switch
# The sound file associated with the selected level will be played using the PyGame
# sound engine.
# If sound device is currently used by other plugin, it will be asked to stop.
#
# Available commands:
# playaudio,taskname,10 - Plays the sound file associated with level 10
#
# Copyright (C) 2018-2019 by Alexander Nagy - https://bitekmindenhol.blog.hu/
#
import plugin
import webserver
import rpieGlobals
import rpieTime
import misc
import Settings
import pygame
import time
class Plugin(plugin.PluginProto):
PLUGIN_ID = 502
PLUGIN_NAME = "Output - PyGame Sound Player"
PLUGIN_VALUENAME1 = "SoundID"
def __init__(self,taskindex): # general init
plugin.PluginProto.__init__(self,taskindex)
self.dtype = rpieGlobals.DEVICE_TYPE_SND
self.vtype = rpieGlobals.SENSOR_TYPE_DIMMER
self.valuecount = 1
self.senddataoption = True
self.recdataoption = True
self.timeroption = False
self.playing = False
self.loopcount = -1
def plugin_init(self,enableplugin=None):
plugin.PluginProto.plugin_init(self,enableplugin)
self.decimals[0] = 0
if Settings.SoundSystem["usable"]==False:
self.initialized = False
self.enabled = False
self.stop(True)
def webform_load(self):
if Settings.SoundSystem["usable"]==False:
webserver.addHtml("<tr><td><td><font color='red'>The sound system can not be used!</font>")
else:
maxlevel = rpieGlobals.PLUGIN_CONFIGVAR_MAX
if maxlevel>10:
maxlevel = 10
for c in range(1,maxlevel+1):
webserver.addFormTextBox("Level "+str(c*10),"p502_lvl_"+str(c*10),str(self.taskdevicepluginconfig[c]),120)
webserver.addBrowseButton("Browse","p502_lvl_"+str(c*10),startdir=str(self.taskdevicepluginconfig[c]))
webserver.addFormNote("Specify file names with relative pathname for every level, that is needed!")
optionvalues = []
options = []
for i in range(-1,20):
optionvalues.append(i)
if i==-1:
options.append("Forever")
else:
options.append(str(i))
try:
loop = int(self.loopcount)
except:
self.loopcount = -1
loop = -1
webserver.addFormSelector("Loop count","p502_loop",len(optionvalues),options,optionvalues,None,loop)
webserver.addFormNote("Default is Forever in this mode sound will repeat until LEVEL 0 asked. As it is running in background there is no way you know if playing is ended actually if using specific loop count!")
return True
def webform_save(self,params):
maxlevel = rpieGlobals.PLUGIN_CONFIGVAR_MAX
if maxlevel>10:
maxlevel = 10
for c in range(1,maxlevel+1):
try:
par = webserver.arg("p502_lvl_"+str(c*10),params)
if par.strip() != "":
self.taskdevicepluginconfig[c] = par
except:
pass
par = webserver.arg("p502_loop",params)
try:
self.loopcount = int(par)
except:
self.loopcount = -1
return True
def plugin_receivedata(self,data): # Watching for incoming mqtt commands
if (len(data)>0):
# print(data)
try:
self.set_value(1,int(data[0]),False)
except Exception as e:
print(str(e))
def set_value(self,valuenum,value,publish=True,suserssi=-1,susebattery=-1): # Also reacting and handling Taskvalueset
if self.initialized:
self.play(value)
plugin.PluginProto.set_value(self,valuenum,value,publish,suserssi,susebattery)
def plugin_write(self,cmd): # Handling commands
res = False
cmdarr = cmd.split(",")
cmdarr[0] = cmdarr[0].strip().lower()
if cmdarr[0]== "playaudio":
try:
rname = cmdarr[1].strip()
rnum = int(cmdarr[2].strip())
except:
rname = ""
rnum = 0
if rname != "" and rname.lower() == self.taskname.lower():
self.set_value(1,int(rnum),True)
res = True
return res
def play(self,level): # 0,10,20,30,40,50,60,70
level = int(level)
if (Settings.SoundSystem["inuse"] and Settings.SoundSystem["usingplugintaskindex"] != self.taskindex) and (level>0):
if Settings.SoundSystem["askplugintorelease"] != None:
try:
Settings.SoundSystem["askplugintorelease"](True) # ask to stop
except Exception as e:
print(e) # DEBUG
if (Settings.SoundSystem["inuse"] and Settings.SoundSystem["usingplugintaskindex"] == self.taskindex) or (self.playing):
self.stop(False)
if level<10:
return False
if Settings.SoundSystem["inuse"]: # endless loop? condition to exit?
time.sleep(0.2)
if Settings.SoundSystem["inuse"]==False:
if level == "" or level<10:
self.stop(False)
return False
elif level>9:
sfx = round(level / 10)
try:
pygame.mixer.init()
except:
return False
Settings.SoundSystem["inuse"] = True
Settings.SoundSystem["usingplugintaskindex"] = self.taskindex
Settings.SoundSystem["askplugintorelease"] = self.stop
fname = ""
try:
fname = self.taskdevicepluginconfig[sfx]
except:
fname = ""
if fname != "" and fname != 0:
try:
pygame.mixer.music.load(fname)
self.playing=True
except Exception as e:
misc.addLog(rpieGlobals.LOG_LEVEL_ERROR,str(e))
if self.playing:
try:
loop = int(self.loopcount)
except:
self.loopcount = -1
loop = -1
pygame.mixer.music.play(loop)
return self.playing
def stop(self,AddEvent=False):
if Settings.SoundSystem["inuse"] and Settings.SoundSystem["usingplugintaskindex"] == self.taskindex:
try:
if pygame.mixer.get_init():
if pygame.mixer.music.get_busy():
pygame.mixer.music.stop()
pygame.mixer.quit()
except:
pass
Settings.SoundSystem["inuse"] = False
Settings.SoundSystem["usingplugintaskindex"] = -1
Settings.SoundSystem["askplugintorelease"] = None
if AddEvent:
plugin.PluginProto.set_value(self,1,0)
self.playing = False
def plugin_exit(self):
self.stop(True)
return True