from collections import namedtuple _AB = namedtuple("_AB", ["a", "b"]) class ABController: def __init__(self, enabled=True, callback=None): self._setPositionCallback = callback self._limits = dict() # dictionary of all songs self._songLimits = None # list of limits for selected song self._currentLimits = None # a/b positions of selected limit self._enabled = enabled def _ensureSongExists(self, path): if path not in self._limits: self._limits[path] = list() def setCurrentSong(self, path): self._ensureSongExists(path) self._songLimits = self._limits[path] self._currentLimits = None def storeLimits(self, aLimit, bLimit, song=None): if song is not None: self._ensureSongExists(song) songLimits = self._limits[song] else: songLimits = self._songLimits if songLimits is None: return ab = _AB(aLimit, bLimit) songLimits.append(ab) def loadLimits(self, index): if not self._songLimits: return if index >= 0 and index < len(self._songLimits): self._currentLimits = self._songLimits[index] def positionChanged(self, position): if not self._currentLimits: return if position > self._currentLimits.b and self._setPositionCallback and self._enabled: self._setPositionCallback(self._currentLimits.a) def setEnable(self, enable): self._enabled = enable def getLimits(self, song): return self._limits.get(song) def clear(self): self.__init__(enabled=self._enabled, callback=self._setPositionCallback)