aboutsummaryrefslogtreecommitdiffstats
path: root/abcontroller.py
blob: e4088b4adfe82a07c7a804a65aaa2bfb3a0a352c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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 = _AB(0.0, 0.0) # a/b positions of active limit
        self._loadedIndex = None
        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]

    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]
            self._loadedIndex = index

    def setLimits(self, aLimit, bLimit):
        self._currentLimits = _AB(aLimit, bLimit)
        self._loadedIndex = None

    def positionChanged(self, position):
        if position > self._currentLimits.b and self._setPositionCallback and self._enabled:
            self._setPositionCallback(self._currentLimits.a)

    def setEnable(self, enable):
        self._enabled = enable

    def getStoredLimits(self, song):
        return self._limits.get(song)

    def getCurrentLimits(self):
        return self._currentLimits

    def getLoadedIndex(self):
        return self._loadedIndex

    def clear(self):
        self.__init__(enabled=self._enabled, callback=self._setPositionCallback)