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