284eed1d3319edd6fb52555877e175df7b5a3aff
[vuplus_dvbapp] / lib / python / Plugins / SystemPlugins / AudioEffect / plugin.py
1 from Screens.Screen import Screen
2 from Components.ConfigList import ConfigListScreen
3 from Components.config import config, ConfigSubsection, ConfigSelection, getConfigListEntry
4 from Components.ActionMap import ActionMap
5 from Screens.MessageBox import MessageBox
6 from Components.Sources.StaticText import StaticText
7 from Plugins.Plugin import PluginDescriptor
8 from Tools.Directories import fileExists
9
10 AUDIOEFFECT_PROC_PATH = {
11         "3D_SURROUND"                                   : "/proc/stb/audio/3d_surround",
12         "AVL"                                                   : "/proc/stb/audio/avl",
13         "3D_SURROUND_CHOICE"                    : "/proc/stb/audio/3d_surround_choices",
14         "AVL_CHOICE"                                    : "/proc/stb/audio/avl_choices",
15         "3D_SURROUND_SPEAKER_POSITION"                  : "/proc/stb/audio/3d_surround_speaker_position",
16         "3D_SURROUND_SPEAKER_POSITION_CHOICE"   : "/proc/stb/audio/3d_surround_speaker_position_choices"
17 }
18
19 AUDIOOUT_ENTRY_NAME = {
20         "dac"   :       "Analog Audio"
21 }
22
23 AUDIOEFFECT_DEFAULT = "none"
24 AUDIOOUT_DEFAULT = "off"
25 SPEAKER_POSITION_DEFAULT = "wide"
26
27 SUPPORT_AUDIOEFFECT = False
28 SUPPORT_3D_SURROUND = False
29 SUPPORT_3D_SURROUND_SPEAKER_POSITION = False
30 SUPPORT_AVL = False
31
32 if fileExists(AUDIOEFFECT_PROC_PATH["3D_SURROUND"]) and fileExists(AUDIOEFFECT_PROC_PATH["3D_SURROUND_CHOICE"]):
33         SUPPORT_3D_SURROUND = True
34
35 if SUPPORT_3D_SURROUND is True:
36         if fileExists(AUDIOEFFECT_PROC_PATH["3D_SURROUND_SPEAKER_POSITION"]) and fileExists(AUDIOEFFECT_PROC_PATH["3D_SURROUND_SPEAKER_POSITION_CHOICE"]):
37                 SUPPORT_3D_SURROUND_SPEAKER_POSITION = True
38
39 if fileExists(AUDIOEFFECT_PROC_PATH["AVL"]) and fileExists(AUDIOEFFECT_PROC_PATH["AVL_CHOICE"]):
40         SUPPORT_AVL = True
41
42 if SUPPORT_3D_SURROUND or SUPPORT_AVL:
43         SUPPORT_AUDIOEFFECT = True
44
45 def getProcValue(procPath):
46         fd = open(procPath,'r')
47         curValue = fd.read().strip(' ').strip('\n')
48         fd.close()
49 #       print "[AudioEffect] get %s from %s" % (curValue, procPath)
50         return curValue
51
52 def setProcValue(procPath, value):
53 #       print "[AudioEffect] set %s to %s" % (value, procPath)
54         fd = open(procPath,'w')
55         fd.write(value)
56         fd.close()
57
58 def setConfigValue(procPath, value):
59 #       print "[AudioEffect][setConfigValue] try set %s to %s" % (value, procPath)
60         curValue = getProcValue(procPath)
61         if curValue != value:
62                 setProcValue(procPath, value)
63         return 0
64
65 def getEffectChoices():
66         choices = [ ("none", _("none")) ]
67         if SUPPORT_3D_SURROUND :
68                 choices.append( ("3D_Surround", _("3D Surround") ) )
69         if SUPPORT_AVL :
70                 choices.append( ("AVL", _("AVL (Automatic Volume Leveler)") ) )
71         return choices
72
73 def getAudioOutTypes():
74         if SUPPORT_3D_SURROUND:
75                 data = getProcValue(AUDIOEFFECT_PROC_PATH["3D_SURROUND_CHOICE"])
76         elif SUPPORT_AVL:
77                 data = getProcValue(AUDIOEFFECT_PROC_PATH["AVL_CHOICE"])
78         aTypes = []
79         for aType in data.split(' '):
80                 if aType == "none":
81                         continue
82                 aTypes.append( aType )
83         return aTypes
84
85 AUDIOOUT_TYPES = getAudioOutTypes()
86
87 def getSpeakerPosition():
88         choices = []
89         data = getProcValue(AUDIOEFFECT_PROC_PATH["3D_SURROUND_SPEAKER_POSITION_CHOICE"])
90         for choice in data.split(' '):
91                 choices.append( (choice, _(choice)) )
92         return choices
93
94 config.plugins.audioeffect = ConfigSubsection()
95 config.plugins.audioeffect.effect = ConfigSelection( default = AUDIOEFFECT_DEFAULT, choices = getEffectChoices() )
96 if SUPPORT_AUDIOEFFECT:
97         for aout in AUDIOOUT_TYPES:
98                 setattr(config.plugins.audioeffect, aout, ConfigSelection( default = AUDIOOUT_DEFAULT, choices = [("on", _("On")), ("off", _("Off"))] ) )
99         if SUPPORT_3D_SURROUND_SPEAKER_POSITION:
100                 config.plugins.audioeffect.speakerposition = ConfigSelection( default = SPEAKER_POSITION_DEFAULT, choices = getSpeakerPosition() )
101
102 def setAudioEffectConfigs():
103                 if not SUPPORT_AUDIOEFFECT:
104                         return
105                 _3DSurroundValue = None
106                 _AvlValue = None
107                 if config.plugins.audioeffect.effect.value == "none":
108                         _3DSurroundValue = "none"
109                         _AvlValue = "none"
110                 elif SUPPORT_AUDIOEFFECT:
111                         _audioOnList = []
112                         for aout in AUDIOOUT_TYPES:
113                                 if getattr(config.plugins.audioeffect, aout).value == "on":
114                                         _audioOnList.append(aout)
115                         if _audioOnList:
116                                 audioOnList = ' '.join(_audioOnList)
117                         else:
118                                 audioOnList = "none"
119                         if config.plugins.audioeffect.effect.value == "3D_Surround":
120                                 _3DSurroundValue = audioOnList
121                                 _AvlValue = "none"
122                         elif config.plugins.audioeffect.effect.value == "AVL":
123                                 _3DSurroundValue = "none"
124                                 _AvlValue = audioOnList
125
126                 if SUPPORT_3D_SURROUND:
127                         setConfigValue(AUDIOEFFECT_PROC_PATH["3D_SURROUND"], _3DSurroundValue)
128                 if SUPPORT_AVL:
129                         setConfigValue(AUDIOEFFECT_PROC_PATH["AVL"], _AvlValue)
130                 if SUPPORT_3D_SURROUND_SPEAKER_POSITION:
131                         _3DSpeakerPosition = config.plugins.audioeffect.speakerposition.value
132                         setConfigValue(AUDIOEFFECT_PROC_PATH["3D_SURROUND_SPEAKER_POSITION"], _3DSpeakerPosition)
133
134 class AudioEffect(Screen, ConfigListScreen):
135         skin =  """
136                 <screen position="center,center" size="540,300">
137                         <ePixmap pixmap="skin_default/buttons/red.png" position="30,10" size="140,40" alphatest="on" />
138                         <ePixmap pixmap="skin_default/buttons/green.png" position="200,10" size="140,40" alphatest="on" />
139                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="370,10" size="140,40" alphatest="on" />
140                         <widget source="key_red" render="Label" position="30,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" foregroundColor="#ffffff" transparent="1" />
141                         <widget source="key_green" render="Label" position="200,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" foregroundColor="#ffffff" transparent="1" />
142                         <widget source="key_yellow" render="Label" position="370,10" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#a08500" foregroundColor="#ffffff" transparent="1" />
143                         <widget name="config" zPosition="2" position="20,70" size="500,170" scrollbarMode="showOnDemand" transparent="1" />
144                         <widget source="description" render="Label" position="30,240" size="480,60" font="Regular;18" halign="center" valign="center" />
145                 </screen>
146         """
147
148         def __init__(self, session):            
149                 Screen.__init__(self, session)
150                 self.setTitle(_("Audio Effect Setup"))
151                 self.skin = AudioEffect.skin
152
153                 self.session = session
154
155                 self["key_red"] = StaticText(_("Cancel"))
156                 self["key_green"] = StaticText(_("Save"))
157                 self["key_yellow"] = StaticText(_("Default"))
158                 self["description"] = StaticText(_("Audio Effect Setup is not supported."))
159
160                 self["shortcuts"] = ActionMap(["AudioEffectActions" ],
161                 {
162                         "ok": self.keySave,
163                         "cancel": self.keyCancel,
164                         "red": self.keyCancel,
165                         "green": self.keySave,
166                         "yellow" : self.keyDefault,
167                 }, -2)
168
169                 self.setupList = []
170                 ConfigListScreen.__init__(self, self.setupList, session = self.session)
171                 self.configEffect = None
172                 self.createSetup()
173
174         def createSetup(self):
175                 if not SUPPORT_AUDIOEFFECT:
176                         return
177                 self.setupList = []
178                 self.configEffect = getConfigListEntry(_("Effect"), config.plugins.audioeffect.effect)
179                 self.setupList.append(self.configEffect)
180                 if config.plugins.audioeffect.effect.value != "none" :
181                         for aout in AUDIOOUT_TYPES:
182                                 entryName = AUDIOOUT_ENTRY_NAME.get(aout, aout.upper())
183                                 self.setupList.append(getConfigListEntry(_(entryName), getattr(config.plugins.audioeffect, aout)))
184                         if config.plugins.audioeffect.effect.value == "3D_Surround" and SUPPORT_3D_SURROUND_SPEAKER_POSITION is True:
185                                 self.setupList.append(getConfigListEntry(_("3D Surround Speaker Position"), config.plugins.audioeffect.speakerposition))
186                 self["config"].list = self.setupList
187                 self["config"].l.setList(self.setupList)
188                 if not self.showDescription in self["config"].onSelectionChanged:
189                         self["config"].onSelectionChanged.append(self.showDescription)
190
191         def keySave(self):
192                 self.saveAll()
193                 self.close()
194
195         def keyLeft(self):
196                 ConfigListScreen.keyLeft(self)
197                 setAudioEffectConfigs()
198                 if self.configEffect and (self["config"].getCurrent() == self.configEffect) :
199                         self.createSetup()
200
201         def keyRight(self):
202                 ConfigListScreen.keyRight(self)
203                 setAudioEffectConfigs()
204                 if self.configEffect and (self["config"].getCurrent() == self.configEffect) :
205                         self.createSetup()
206
207         def cancelConfirm(self, result):
208                 if not result:
209                         return
210
211                 for x in self["config"].list:
212                         x[1].cancel()
213                 setAudioEffectConfigs()
214                 self.close()
215
216         def keyCancel(self):
217                 if self["config"].isChanged():
218                         self.session.openWithCallback(self.cancelConfirm, MessageBox, _("Really close without saving settings?"))
219                 else:
220                         self.close()
221
222         def keyDefault(self):
223                 for (configName, configElement) in config.plugins.audioeffect.dict().items():
224                         configElement.value = configElement.default
225                 setAudioEffectConfigs()
226                 self.createSetup()
227
228         def showDescription(self):
229                 def getClassName(C):
230                         return C.__class__.__name__
231
232                 configName = "<%s>\n"%self["config"].getCurrent()[0]
233                 currentConfig = self["config"].getCurrent()[1]
234                 className = getClassName(currentConfig)
235                 text = ""
236                 if className == "ConfigSelection":
237                         text = configName
238                         for choice in currentConfig.choices.choices:
239                                 if text == configName:  
240                                         text += choice[1]
241                                 else:
242                                         text += ', ' + choice[1]
243                 self["description"].setText( _(text) )
244
245 def main(session, **kwargs):
246         session.open(AudioEffect)
247
248 def OnSessionStart(session, **kwargs):
249         setAudioEffectConfigs()
250
251 def Plugins(**kwargs):
252         pList = []
253         pList.append( PluginDescriptor(where=PluginDescriptor.WHERE_SESSIONSTART, fnc=OnSessionStart))
254         pList.append( PluginDescriptor(name=_("AudioEffect"), description=_("support 3D Surround and AVL effect."), where = PluginDescriptor.WHERE_PLUGINMENU, needsRestart = False, fnc=main) )
255         return pList