From 06f9bb6100ff4ae02d75f9cde358a28f5885407a Mon Sep 17 00:00:00 2001 From: "Dr.Best" Date: Wed, 11 Aug 2010 17:17:59 +0000 Subject: [PATCH] new option: remember service volume value now you can choose between two modes: "Automatic volume adjust" and "Remember service volume value" --- .../src/AutomaticVolumeAdjustment.py | 156 ++++++++++++--------- .../src/AutomaticVolumeAdjustmentConfig.py | 21 ++- .../src/AutomaticVolumeAdjustmentSetup.py | 43 ++++-- automaticvolumeadjustment/src/plugin.py | 10 +- 4 files changed, 157 insertions(+), 73 deletions(-) diff --git a/automaticvolumeadjustment/src/AutomaticVolumeAdjustment.py b/automaticvolumeadjustment/src/AutomaticVolumeAdjustment.py index 2a8def0..634eed7 100644 --- a/automaticvolumeadjustment/src/AutomaticVolumeAdjustment.py +++ b/automaticvolumeadjustment/src/AutomaticVolumeAdjustment.py @@ -28,7 +28,7 @@ from Components.ServiceEventTracker import ServiceEventTracker from enigma import iPlayableService, iServiceInformation, eDVBVolumecontrol, eServiceCenter, eServiceReference from ServiceReference import ServiceReference from Components.VolumeControl import VolumeControl -from AutomaticVolumeAdjustmentConfig import AutomaticVolumeAdjustmentConfig +from AutomaticVolumeAdjustmentConfig import AutomaticVolumeAdjustmentConfig, getVolumeDict class AutomaticVolumeAdjustment(Screen): instance = None @@ -59,23 +59,34 @@ class AutomaticVolumeAdjustment(Screen): def initializeConfigValues(self, configVA, fromOutside): print "[AutomaticVolumeAdjustment] initialize config values..." self.serviceList = { } - for c in configVA.config.Entries: - self.serviceList[c.servicereference.value] = int(c.adjustvalue.value) + self.modus = configVA.config.modus.value # get modus + if self.modus == "0": # Automatic volume adjust mode + for c in configVA.config.Entries: + self.serviceList[c.servicereference.value] = int(c.adjustvalue.value) # adjust volume + else: # Remember channel volume mode + self.serviceList = getVolumeDict() self.defaultValue = int(configVA.config.adustvalue.value) self.enabled = configVA.config.enable.value self.maxMPEGVolume = configVA.config.mpeg_max_volume.value self.showVolumeBar = configVA.config.show_volumebar.value - VolumeControlInit(self.enabled, self.maxMPEGVolume) # overwrite VolumeControl Class, when max MPEG Volume was set (<> 100) + if self.modus == "0": # Automatic volume adjust mode + VolumeControlInit(self.enabled, self.maxMPEGVolume) # overwrite VolumeControl Class, when max MPEG Volume was set (<> 100) if not self.pluginStarted and self.enabled and fromOutside: self.newService = True self.__evUpdatedInfo() def __evEnd(self): if self.pluginStarted and self.enabled: - # if played service had AC3||DTS audio and volume value was changed with RC, take new delta value from the config - if self.currentVolume and self.volctrl.getVolume() != self.currentVolume: - self.lastAdjustedValue = self.serviceList.get(self.session.nav.getCurrentlyPlayingServiceReference().toString(), self.defaultValue) - + if self.modus == "0": # Automatic volume adjust mode + # if played service had AC3||DTS audio and volume value was changed with RC, take new delta value from the config + if self.currentVolume and self.volctrl.getVolume() != self.currentVolume: + self.lastAdjustedValue = self.serviceList.get(self.session.nav.getCurrentlyPlayingServiceReference().toString(), self.defaultValue) + else: # Remember channel volume mode + # save current volume in dict, but for valid ref only + ref = self.getPlayingServiceReference() + if ref.valid(): + self.serviceList[ref.toString()] = self.volctrl.getVolume() + def __evStart(self): self.newService = True @@ -84,62 +95,57 @@ class AutomaticVolumeAdjustment(Screen): print "[AutomaticVolumeAdjustment] service changed" self.newService = False self.currentVolume = 0 # init - self.currentAC3DTS = self.isCurrentAudioAC3DTS() - if self.pluginStarted: - if self.currentAC3DTS: # ac3 dts? - vol = self.volctrl.getVolume() - currentvol = vol # remember current vol - vol -= self.lastAdjustedValue # go back to origin value first - ref = self.session.nav.getCurrentlyPlayingServiceReference() - if ref.getPath(): # check if a movie is playing - # it is , get the eServicereference if available - self.serviceHandler = eServiceCenter.getInstance() - info = self.serviceHandler.info(ref) - if info: - ref = eServiceReference(info.getInfoString(ref, iServiceInformation.sServiceref)) # get new eServicereference from meta file - ajvol = self.serviceList.get(ref.toString(), self.defaultValue) # get delta from config - if ajvol < 0: # adjust vol down - if vol + ajvol < 0: - ajvol = (-1) * vol - else: # adjust vol up - if vol >= 100 - ajvol: # check if delta + vol < 100 - ajvol = 100 - vol # correct delta value - self.lastAdjustedValue = ajvol # save delta value - if (vol + ajvol != currentvol): # only when current vol != new volume - if ajvol == 0: - ajvol = vol - currentvol # correction for debug -print only - self.volctrl.setVolume(vol+self.lastAdjustedValue, vol+self.lastAdjustedValue) - if self.volumeControlInstance is not None: - self.volumeControlInstance.volumeDialog.setValue(vol+self.lastAdjustedValue) - if self.showVolumeBar: - self.volumeControlInstance.volumeDialog.show() - self.volumeControlInstance.hideVolTimer.start(3000, True) - print "[AutomaticVolumeAdjustment] Change volume for service: %s (+%d) to %d"%(ServiceReference(ref).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), ajvol, self.volctrl.getVolume()) - self.currentVolume = self.volctrl.getVolume() # ac3||dts service , save current volume - else: - # mpeg or whatever audio - if self.lastAdjustedValue != 0: - # go back to origin value + if self.modus == "0": # Automatic volume adjust mode + self.currentAC3DTS = self.isCurrentAudioAC3DTS() + if self.pluginStarted: + if self.currentAC3DTS: # ac3 dts? vol = self.volctrl.getVolume() - ajvol = vol-self.lastAdjustedValue - if ajvol > self.maxMPEGVolume: - ajvol = self.maxMPEGVolume - self.volctrl.setVolume(ajvol, ajvol) - if self.volumeControlInstance is not None: - self.volumeControlInstance.volumeDialog.setValue(ajvol) - if self.showVolumeBar: - self.volumeControlInstance.volumeDialog.show() - self.volumeControlInstance.hideVolTimer.start(3000, True) - print "[AutomaticVolumeAdjustment] Change volume for service: %s (-%d) to %d"%(ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference()).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), vol-ajvol, self.volctrl.getVolume()) - self.lastAdjustedValue = 0 # mpeg audio, no delta here - # save new volume in config - config.audio.volume.value = self.volctrl.getVolume() - config.audio.volume.save() - else: - # starting plugin, if service audio is ac3 or dts --> get delta from config...volume value is set by enigma2-system at start - if self.currentAC3DTS: - self.lastAdjustedValue = self.serviceList.get(self.session.nav.getCurrentlyPlayingServiceReference().toString(), self.defaultValue) - self.currentVolume = self.volctrl.getVolume() # ac3||dts service , save current volume + currentvol = vol # remember current vol + vol -= self.lastAdjustedValue # go back to origin value first + ref = self.getPlayingServiceReference() + ajvol = self.serviceList.get(ref.toString(), self.defaultValue) # get delta from config + if ajvol < 0: # adjust vol down + if vol + ajvol < 0: + ajvol = (-1) * vol + else: # adjust vol up + if vol >= 100 - ajvol: # check if delta + vol < 100 + ajvol = 100 - vol # correct delta value + self.lastAdjustedValue = ajvol # save delta value + if (vol + ajvol != currentvol): + if ajvol == 0: + ajvol = vol - currentvol # correction for debug -print only + self.setVolume(vol+self.lastAdjustedValue) + print "[AutomaticVolumeAdjustment] Change volume for service: %s (+%d) to %d"%(ServiceReference(ref).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), ajvol, self.volctrl.getVolume()) + self.currentVolume = self.volctrl.getVolume() # ac3||dts service , save current volume + else: + # mpeg or whatever audio + if self.lastAdjustedValue != 0: + # go back to origin value + vol = self.volctrl.getVolume() + ajvol = vol-self.lastAdjustedValue + if ajvol > self.maxMPEGVolume: + ajvol = self.maxMPEGVolume + self.setVolume(ajvol) + print "[AutomaticVolumeAdjustment] Change volume for service: %s (-%d) to %d"%(ServiceReference(self.session.nav.getCurrentlyPlayingServiceReference()).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), vol-ajvol, self.volctrl.getVolume()) + self.lastAdjustedValue = 0 # mpeg audio, no delta here + return # get out of here, nothing to do anymore + else: # modus = Remember channel volume + if self.pluginStarted: + ref = self.getPlayingServiceReference() + if ref.valid(): + # get value from dict + lastvol = self.serviceList.get(ref.toString(), -1) + if lastvol != -1 and lastvol != self.volctrl.getVolume(): + # set volume value + self.setVolume(lastvol) + print "[AutomaticVolumeAdjustment] Set last used volume value for service %s to %d"%(ServiceReference(ref).getServiceName().replace('\xc2\x86', '').replace('\xc2\x87', ''), self.volctrl.getVolume()) + return # get out of here, nothing to do anymore + if not self.pluginStarted: + if self.modus == "0": # Automatic volume adjust mode + # starting plugin, if service audio is ac3 or dts --> get delta from config...volume value is set by enigma2-system at start + if self.currentAC3DTS: + self.lastAdjustedValue = self.serviceList.get(self.session.nav.getCurrentlyPlayingServiceReference().toString(), self.defaultValue) + self.currentVolume = self.volctrl.getVolume() # ac3||dts service , save current volume # only images >= 05.08.2010, must use try/except try: self.volumeControlInstance = VolumeControl.instance except: pass @@ -158,6 +164,30 @@ class AutomaticVolumeAdjustment(Screen): except: return False return False + + def getPlayingServiceReference(self): + ref = self.session.nav.getCurrentlyPlayingServiceReference() + if ref.getPath(): # check if a movie is playing + # it is , get the eServicereference if available + self.serviceHandler = eServiceCenter.getInstance() + info = self.serviceHandler.info(ref) + if info: + # no need here to know if eServiceReference is valid... + ref = eServiceReference(info.getInfoString(ref, iServiceInformation.sServiceref)) # get new eServicereference from meta file + return ref + + def setVolume(self, value): + # set new volume + self.volctrl.setVolume(value, value) + if self.volumeControlInstance is not None: + self.volumeControlInstance.volumeDialog.setValue(value) # update progressbar value + if self.showVolumeBar: + # show volume bar + self.volumeControlInstance.volumeDialog.show() + self.volumeControlInstance.hideVolTimer.start(3000, True) + # save new volume value in E2-settings + config.audio.volume.value = self.volctrl.getVolume() + config.audio.volume.save() # VolumeControl Class --> overwrite setVolume diff --git a/automaticvolumeadjustment/src/AutomaticVolumeAdjustmentConfig.py b/automaticvolumeadjustment/src/AutomaticVolumeAdjustmentConfig.py index 667364b..c809b69 100644 --- a/automaticvolumeadjustment/src/AutomaticVolumeAdjustmentConfig.py +++ b/automaticvolumeadjustment/src/AutomaticVolumeAdjustmentConfig.py @@ -21,8 +21,26 @@ # distributed other than under the conditions noted above. # from Components.config import ConfigSubsection, ConfigText, \ - config, ConfigInteger, Config, ConfigSubList, ConfigDirectory, NoSave, ConfigYesNo, ConfigSelectionNumber + config, ConfigInteger, Config, ConfigSubList, ConfigDirectory, NoSave, ConfigYesNo, ConfigSelectionNumber, ConfigSelection from os import path as os_path, open as os_open, close as os_close, O_RDWR as os_O_RDWR, O_CREAT as os_O_CREAT +from pickle import load as pickle_load, dump as pickle_dump + +CONFIG_FILE_VOLUME = '/usr/lib/enigma2/python/Plugins/SystemPlugins/AutomaticVolumeAdjustment/config_volume' + +def getVolumeDict(): + if os_path.exists(CONFIG_FILE_VOLUME): + pkl_file = open(CONFIG_FILE_VOLUME, 'rb') + if pkl_file: + volumedict = pickle_load(pkl_file) + pkl_file.close() + return volumedict + return {} + +def saveVolumeDict(dict): + pkl_file = open(CONFIG_FILE_VOLUME, 'wb') + if pkl_file: + pickle_dump(dict, pkl_file) + pkl_file.close() class AutomaticVolumeAdjustmentConfig(): def __init__(self): @@ -41,6 +59,7 @@ class AutomaticVolumeAdjustmentConfig(): self.config.entriescount = ConfigInteger(0) self.config.Entries = ConfigSubList() self.config.enable = ConfigYesNo(default = False) + self.config.modus = ConfigSelection(choices = [("0", _("Automatic volume adjust")), ("1", _("Remember service volume value"))], default = "0") self.config.adustvalue = ConfigSelectionNumber(-50, 50, 5, default = 25) self.config.mpeg_max_volume = ConfigSelectionNumber(10, 100, 5, default = 100) self.config.show_volumebar = ConfigYesNo(default = False) diff --git a/automaticvolumeadjustment/src/AutomaticVolumeAdjustmentSetup.py b/automaticvolumeadjustment/src/AutomaticVolumeAdjustmentSetup.py index 6235e30..7f0566c 100644 --- a/automaticvolumeadjustment/src/AutomaticVolumeAdjustmentSetup.py +++ b/automaticvolumeadjustment/src/AutomaticVolumeAdjustmentSetup.py @@ -60,18 +60,45 @@ class AutomaticVolumeAdjustmentConfigScreen(ConfigListScreen, Screen): }, -2) self["key_red"] = StaticText(_("Cancel")) self["key_green"] = StaticText(_("OK")) - self["key_blue"] = StaticText(_("Services")) + self["key_blue"] = StaticText() self.configVA = AutomaticVolumeAdjustmentConfig() - self.list = [] - self.list.append(getConfigListEntry(_("Enable"), self.configVA.config.enable)) - self.list.append(getConfigListEntry(_("Default volume adjustment value for AC3/DTS"), self.configVA.config.adustvalue)) - self.list.append(getConfigListEntry(_("Max. volume for mpeg audio"), self.configVA.config.mpeg_max_volume)) - self.list.append(getConfigListEntry(_("Show volumebar when volume-value was changed"), self.configVA.config.show_volumebar)) self.automaticVolumeAdjustmentInstance = AutomaticVolumeAdjustment.instance - ConfigListScreen.__init__(self, self.list, session) + self.list = [] + ConfigListScreen.__init__(self, self.list, session = session) + self.createSetup("config") + + def createSetup(self, widget): + self.list = [] + self.config_enable = getConfigListEntry(_("Enable"), self.configVA.config.enable) + self.list.append(self.config_enable) + if self.configVA.config.enable.value: + self.config_modus = getConfigListEntry(_("Modus"), self.configVA.config.modus) + self.list.append(self.config_modus) + if self.configVA.config.modus.value == "0": + self.list.append(getConfigListEntry(_("Default volume adjustment value for AC3/DTS"), self.configVA.config.adustvalue)) + self.list.append(getConfigListEntry(_("Max. volume for mpeg audio"), self.configVA.config.mpeg_max_volume)) + self["key_blue"].text = _("Services") + else: + self["key_blue"].text = "" + self.list.append(getConfigListEntry(_("Show volumebar when volume-value was changed"), self.configVA.config.show_volumebar)) + self[widget].list = self.list + self[widget].l.setList(self.list) + + def newConfig(self): + if self["config"].getCurrent() in (self.config_enable, self.config_modus): + self.createSetup("config") + + def keyLeft(self): + ConfigListScreen.keyLeft(self) + self.newConfig() + + def keyRight(self): + ConfigListScreen.keyRight(self) + self.newConfig() def blue(self): - self.session.open(AutomaticVolumeAdjustmentEntriesListConfigScreen, self.configVA) + if self.configVA.config.modus.value == "0": + self.session.open(AutomaticVolumeAdjustmentEntriesListConfigScreen, self.configVA) def keySave(self): for x in self["config"].list: diff --git a/automaticvolumeadjustment/src/plugin.py b/automaticvolumeadjustment/src/plugin.py index fd22300..2acbd1f 100644 --- a/automaticvolumeadjustment/src/plugin.py +++ b/automaticvolumeadjustment/src/plugin.py @@ -23,11 +23,19 @@ from Plugins.Plugin import PluginDescriptor from AutomaticVolumeAdjustmentSetup import AutomaticVolumeAdjustmentConfigScreen from AutomaticVolumeAdjustment import AutomaticVolumeAdjustment +from AutomaticVolumeAdjustmentConfig import saveVolumeDict def autostart(reason, **kwargs): if "session" in kwargs: session = kwargs["session"] AutomaticVolumeAdjustment(session) + +def autoend(reason, **kwargs): + # save config values for last used volume modus + if reason == 1: + if AutomaticVolumeAdjustment.instance: + if AutomaticVolumeAdjustment.instance.enabled and AutomaticVolumeAdjustment.instance.modus != "0": + saveVolumeDict(AutomaticVolumeAdjustment.instance.serviceList) def setup(session, **kwargs): session.open(AutomaticVolumeAdjustmentConfigScreen) # start setup @@ -38,5 +46,5 @@ def startSetup(menuid): return [(_("Automatic Volume Adjustment"), setup, "AutomaticVolumeAdjustment", 46)] def Plugins(**kwargs): - return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART], fnc = autostart), PluginDescriptor(name="Automatic Volume Adjustment", description=_("Automatic Volume Adjustment"), where = PluginDescriptor.WHERE_MENU, fnc=startSetup) ] + return [PluginDescriptor(where = [PluginDescriptor.WHERE_SESSIONSTART], fnc = autostart), PluginDescriptor(where = [PluginDescriptor.WHERE_AUTOSTART], fnc = autoend), PluginDescriptor(name="Automatic Volume Adjustment", description=_("Automatic Volume Adjustment"), where = PluginDescriptor.WHERE_MENU, fnc=startSetup) ] -- 2.7.4