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