fix not working autoresolution plugin without restart e2
[enigma2-plugins.git] / ac3lipsync / src / AC3main.py
1 from AC3utils import AC3, PCM, AC3GLOB, PCMGLOB, AC3PCM, SKIN
2 from AC3delay import AC3delay
3 from enigma import ePoint
4 from HelpableNumberActionMap import HelpableNumberActionMap
5 from Components.Label import Label,MultiColorLabel
6 from Components.Pixmap import MultiPixmap
7 from Components.ProgressBar import ProgressBar
8 from Components.config import config
9 from MovableScreen import MovableScreen
10 from Screens.ChoiceBox import ChoiceBox
11 from Screens.HelpMenu import HelpableScreen
12 from Screens.MessageBox import MessageBox
13 from Screens.Screen import Screen
14 from Screens.InfoBarGenerics import InfoBarAudioSelection
15 from __init__ import _
16
17 class AC3LipSync(Screen, HelpableScreen, MovableScreen, InfoBarAudioSelection):
18
19     def __init__(self, session, plugin_path):
20         Screen.__init__(self, session)
21         self.onShow.append(self.__onShow)
22         self.skin = SKIN
23         self.skin_path = plugin_path
24
25         #Initialisiere Infobargenerics
26         InfoBarAudioSelection.__init__(self)
27
28         # Configuration values
29         self.upperBound = int(config.plugins.AC3LipSync.outerBounds.getValue())
30         self.lowerBound = -1 * self.upperBound
31         self.arrowStepSize = int(config.plugins.AC3LipSync.arrowStepSize.getValue())
32         self.stepSize = {}
33         self.stepSize["3"] = int(config.plugins.AC3LipSync.stepSize13.getValue())
34         self.stepSize["1"] = -1 * self.stepSize["3"]
35         self.stepSize["6"] = int(config.plugins.AC3LipSync.stepSize46.getValue())
36         self.stepSize["4"] = -1 * self.stepSize["6"]
37         self.stepSize["9"] = int(config.plugins.AC3LipSync.stepSize79.getValue())
38         self.stepSize["7"] = -1 * self.stepSize["9"]
39         self.keyStep = {}
40         self.keyStep["0"] = 0
41         self.keyStep["2"] = int(config.plugins.AC3LipSync.absoluteStep2.getValue()) 
42         self.keyStep["5"] = int(config.plugins.AC3LipSync.absoluteStep5.getValue()) 
43         self.keyStep["8"] = int(config.plugins.AC3LipSync.absoluteStep8.getValue()) 
44
45         # AC3delay instance
46         self.AC3delay = AC3delay()
47
48         # Last saved values
49         self.savedValue = {}
50         # Current Values
51         self.currentValue = {}
52         
53         #OptionFields
54         self["ChannelImg"] = MultiPixmap()
55         self["GlobalImg"] = MultiPixmap()
56         
57         self["ChannelLabel"] = MultiColorLabel( _("Service delay"))
58         self["GlobalLabel"] = MultiColorLabel( _("Global delay"))
59
60         # Slider
61         self["AudioSliderBar"] = ProgressBar()
62         self["AudioSlider"] = Label(_("%i ms")%self.AC3delay.systemDelay[self.AC3delay.whichAudio])
63         
64         #Service Information
65         self["ServiceInfoLabel"] = Label(_("Channel audio:"))
66         self["ServiceInfo"] = Label()
67         self.setChannelInfoText()
68
69         # Buttons
70         self["key_red"] = Label(_("Cancel"))
71         self["key_green"] = Label(_("OK"))
72         self["key_yellow"] = Label(_("Switch audio"))
73         self["key_blue"] = Label(_("Save to key"))
74
75         # Actions
76         self["actions"] = HelpableNumberActionMap(self, "PluginAudioSyncActions",
77         {
78             "menu":     (self.keyMenu,              _("Open plugin menu")),
79             "ok":       (self.keyOk,                _("Save values and close plugin")),
80             "cancel":   (self.keyCancel,            _("Discard changes and close plugin")),
81             "left":     (self.keyLeft,              _("Change active delay")),
82             "right":    (self.keyRight,             _("Change active delay")),
83             "up":       (self.keyUp,                _("Increase delay")),
84             "down":     (self.keyDown,              _("Decrease delay")),
85             "red":      (self.keyCancel,            _("Discard changes and close plugin")),
86             "green":    (self.keyOk,                _("Save values and close plugin")),
87             "yellow":   (self.keyAudioSelection,    _("Select channel audio")),
88             "blue":     (self.menuSaveDelayToKey,    _("Save current delay to key")),
89             "1":        (self.keyNumberRelative,    _("Decrease delay by %i ms (can be set)")%self.stepSize["1"]),
90             "3":        (self.keyNumberRelative,    _("Increase delay by %i ms (can be set)")%self.stepSize["3"]),
91             "4":        (self.keyNumberRelative,    _("Decrease delay by %i ms (can be set)")%self.stepSize["4"]),
92             "6":        (self.keyNumberRelative,    _("Increase delay by %i ms (can be set)")%self.stepSize["6"]),
93             "7":        (self.keyNumberRelative,    _("Decrease delay by %i ms (can be set)")%self.stepSize["7"]),
94             "9":        (self.keyNumberRelative,    _("Increase delay by %i ms (can be set)")%self.stepSize["9"]),
95             "0":        (self.keyNumberAbsolute,    _("Set delay to %i ms (can be set)")%self.keyStep["0"]),
96             "2":        (self.keyNumberAbsolute,    _("Set delay to %i ms (can be set)")%self.keyStep["2"]),
97             "5":        (self.keyNumberAbsolute,    _("Set delay to %i ms (can be set)")%self.keyStep["5"]),
98             "8":        (self.keyNumberAbsolute,    _("Set delay to %i ms (can be set)")%self.keyStep["8"])
99         }, -1)
100
101         HelpableScreen.__init__(self)
102         MovableScreen.__init__(self, config.plugins.AC3LipSync, [self["actions"]], 600, 460)
103         
104     def __onShow(self):
105         for sAudio in AC3PCM:
106             iDelay = self.AC3delay.getSystemDelay(sAudio)
107             self.savedValue[sAudio] = iDelay
108             self.currentValue[sAudio] = iDelay
109
110         self["AudioSliderBar"].setRange([(self.lowerBound), (self.upperBound)])
111         self.setActiveSlider()
112         self.movePosition()
113
114     def keyLeft(self):
115         if self.AC3delay.whichAudio == PCMGLOB:
116             self.AC3delay.whichAudio = PCM
117         elif self.AC3delay.whichAudio == AC3GLOB:
118             self.AC3delay.whichAudio = AC3
119         
120         self.setActiveSlider()
121
122     def keyRight(self):
123         if self.AC3delay.whichAudio == AC3:
124             self.AC3delay.whichAudio = AC3GLOB
125         elif self.AC3delay.whichAudio == PCM:
126             self.AC3delay.whichAudio = PCMGLOB
127
128         self.setActiveSlider()
129
130     def setActiveSlider(self):
131         # Reset colors of all tabs
132         if self.AC3delay.whichAudio in (AC3,PCM):
133             self["ChannelImg"].setPixmapNum(1)
134             self["GlobalImg"].setPixmapNum(0)
135             self["ChannelLabel"].setForegroundColorNum(1)
136             self["GlobalLabel"].setForegroundColorNum(0)        
137         else:    
138             self["ChannelImg"].setPixmapNum(0)
139             self["GlobalImg"].setPixmapNum(1)
140             self["ChannelLabel"].setForegroundColorNum(0)
141             self["GlobalLabel"].setForegroundColorNum(1)
142         
143         iCurDelay = self.currentValue[self.AC3delay.whichAudio]
144         iDelay = iCurDelay - self.lowerBound
145         self["AudioSliderBar"].setValue(iDelay)
146         self["AudioSlider"].setText(_("%i ms")%iCurDelay)
147
148     def keyDown(self):
149         if self.AC3delay.whichAudio == AC3GLOB or self.AC3delay.whichAudio == PCMGLOB:
150             iStep = -25
151         else:
152             iStep = -1 * self.arrowStepSize
153         self.changeSliderValue(iStep)
154         
155     def keyUp(self):
156         if self.AC3delay.whichAudio == AC3GLOB or self.AC3delay.whichAudio == PCMGLOB:
157             iStep = 25
158         else:
159             iStep = self.arrowStepSize
160         self.changeSliderValue(iStep)
161
162     def keyNumberAbsolute(self, number):
163         sAudio = self.AC3delay.whichAudio
164         sNumber = str(number)
165         if self.AC3delay.whichAudio == AC3GLOB or self.AC3delay.whichAudio == PCMGLOB:
166             iStep = ( self.keyStep[sNumber] // 25 ) * 25
167         else:
168             iStep = self.keyStep[sNumber]        
169         iSliderValue = iStep-self.lowerBound
170         self.setSliderInfo(iSliderValue)
171         self.AC3delay.setSystemDelay(sAudio, self.currentValue[sAudio], True)
172
173     def keyNumberRelative(self, number):
174         sNumber = str(number)
175         if self.AC3delay.whichAudio == AC3GLOB or self.AC3delay.whichAudio == PCMGLOB:
176             iStep = ( self.stepSize[sNumber] // 25 ) * 25
177         else:
178             iStep = self.stepSize[sNumber]
179
180         self.changeSliderValue(iStep)
181
182     def changeSliderValue(self,iValue):
183         sAudio = self.AC3delay.whichAudio
184         iSliderValue = int(self["AudioSliderBar"].getValue())
185         iSliderValue += iValue
186         if iSliderValue < 0:
187             iSliderValue = 0
188         elif iSliderValue > (self.upperBound - self.lowerBound):
189             iSliderValue = (self.upperBound - self.lowerBound)
190         self.setSliderInfo(iSliderValue)
191         self.AC3delay.setSystemDelay(sAudio, self.currentValue[sAudio], True)        
192
193     def keyAudioSelection(self):
194         self.audioSelection()
195
196     def keyOk(self):
197         self.close()
198
199     def keyCancel(self):
200         for sAudio in AC3PCM:
201             iSliderValue = self.currentValue[sAudio]
202             if iSliderValue <> self.savedValue[sAudio]:
203                 self.AC3delay.whichAudio = sAudio
204                 self.AC3delay.setSystemDelay(sAudio, self.savedValue[sAudio], False)
205         self.close()
206
207     def keyMenu(self):
208         sAudio = self.AC3delay.whichAudio
209         iDelay = self["AudioSliderBar"].getValue()+self.lowerBound
210         keyList = [
211             (_("Move plugin screen"),"1")
212         ]
213
214         self.session.openWithCallback(self.DoShowMenu,ChoiceBox,_("Menu"),keyList)
215     
216     def DoShowMenu(self, answer):
217         if answer is not None:
218             if answer[1] == "1":
219                 self.startMoving()
220             else:
221                 sResponse = _("Invalid selection")
222                 iType = MessageBox.TYPE_ERROR
223                 self.session.open(MessageBox, sResponse , iType)
224                 
225     def menuSaveDelayToKey(self):
226         sAudio = self.AC3delay.whichAudio
227         iDelay = self["AudioSliderBar"].getValue()+self.lowerBound
228
229         AC3SetCustomValue(self.session,iDelay,self.keyStep)
230         
231     def setSliderInfo(self, iDelay):
232         sAudio = self.AC3delay.whichAudio
233         self.currentValue[sAudio] = iDelay + self.lowerBound
234         iCurDelay = self.currentValue[sAudio]
235         self["AudioSliderBar"].setValue(iDelay)
236         self["AudioSlider"].setText(_("%i ms")%iCurDelay)
237
238     def setChannelInfoText(self):
239         sActiveAudio = str(self.AC3delay.selectedAudioInfo[0])
240         sBitstreamDelay = _("%i ms") %self.AC3delay.systemDelay[AC3]
241         sPCMDelay = _("%i ms") %self.AC3delay.systemDelay[PCM]
242
243         self["ServiceInfo"].setText(sActiveAudio)
244
245     def audioSelected(self, audio):
246         InfoBarAudioSelection.audioSelected(self, audio)
247         if audio is not None:
248             self.AC3delay.getAudioInformation()
249             self.setChannelInfoText()
250             self.setActiveSlider()
251             
252 class AC3SetCustomValue:
253     def __init__(self, session, iDelay, keyStep):
254         self.keyStep = keyStep
255         self.session = session
256         self.iDelay = iDelay
257         self.session.openWithCallback(self.DoSetCustomValue,ChoiceBox,_("Select the key you want to set to %i ms") %(iDelay),self.getKeyList())
258
259     def getKeyList(self):
260         keyList = []
261         for i,iValue in self.keyStep.iteritems():
262             if i != "0":
263                 keyList.append((_("Key %(key)s (current value: %(value)i ms)") %dict(key=i, value=iValue),i))
264         return keyList
265
266     def DoSetCustomValue(self,answer):
267         if answer is None:
268             self.session.open(MessageBox,_("Setting key canceled"), MessageBox.TYPE_INFO)
269         elif answer[1] in ("2" , "5" , "8"):
270             if answer[1] == "2":
271                 config.plugins.AC3LipSync.absoluteStep2.setValue(self.iDelay)
272                 config.plugins.AC3LipSync.absoluteStep2.save()
273             elif  answer[1] == "5":
274                 config.plugins.AC3LipSync.absoluteStep5.setValue(self.iDelay)
275                 config.plugins.AC3LipSync.absoluteStep5.save()
276             elif  answer[1] == "8":
277                 config.plugins.AC3LipSync.absoluteStep8.setValue(self.iDelay)
278                 config.plugins.AC3LipSync.absoluteStep8.save()
279             self.keyStep[answer[1]] = self.iDelay
280             self.session.open(MessageBox,_("Key %(Key)s successfully set to %(delay)i ms") %dict(Key=answer[1],delay=self.iDelay), MessageBox.TYPE_INFO, 5)
281         else:
282             self.session.open(MessageBox,_("Invalid selection"), MessageBox.TYPE_ERROR, 5)