aeidon/calculator.py

Source code for module aeidon.calculator from file aeidon/calculator.py.

  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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# -*- coding: utf-8 -*-

# Copyright (C) 2005 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Time and frame calculator."""

import aeidon

__all__ = ("Calculator",)


class Calculator:

    """
    Time and frame calculator.

    Times are handled as strings, frames as integers and seconds as floats.
    Only one instance of :class:`Calculator` exists for a given framerate.
    """

    _instances = {}

    def __new__(cls, framerate=None):
        """
        Return possibly existing instance for `framerate`.

        `framerate` can be either an :attr:`aeidon.framerates` item
        (preferred to be able to reuse instances) or an exact float value.
        """
        if framerate is None:
            framerate = aeidon.framerates.FPS_23_976
        if not framerate in aeidon.framerates:
            # Always return a new instance for float values.
            return object.__new__(cls)
        if not framerate in cls._instances:
            cls._instances[framerate] = object.__new__(cls)
        return cls._instances[framerate]

    def __init__(self, framerate=None):
        """
        Initialize a :class:`Calculator` instance.

        `framerate` can be either an :attr:`aeidon.framerates` item
        (preferred to be able to reuse instances) or an exact float value.
        """
        if framerate is None:
            framerate = aeidon.framerates.FPS_23_976
        if framerate in aeidon.framerates:
            self._framerate = framerate.value
        else:
            # Use non-constant values as is.
            self._framerate = float(framerate)

    def add(self, x, y):
        """Add position `y` to `x`."""
        if aeidon.is_time(x):
            x = self.to_seconds(x)
            y = self.to_seconds(y)
            return self.seconds_to_time(x + y)
        if aeidon.is_frame(x):
            return x + self.to_frame(y)
        if aeidon.is_seconds(x):
            return x + self.to_seconds(y)
        raise ValueError("Invalid type for x: {}"
                         .format(repr(type(x))))

    def frame_to_seconds(self, frame):
        """Convert `frame` to seconds."""
        return aeidon.as_seconds(frame / self._framerate)

    def frame_to_time(self, frame):
        """Convert `frame` to time."""
        seconds = self.frame_to_seconds(frame)
        return self.seconds_to_time(seconds)

    def get_middle(self, x, y):
        """Return time, frame or seconds halfway between `x` and `y`."""
        if aeidon.is_time(x):
            x = self.time_to_seconds(x)
            y = self.to_seconds(y)
            return self.seconds_to_time((x+y)/2)
        if aeidon.is_frame(x):
            y = self.to_frame(y)
            return aeidon.as_frame(round((x+y)/2, 0))
        if aeidon.is_seconds(x):
            y = self.to_seconds(y)
            return aeidon.as_seconds(((x+y)/2))
        raise ValueError("Invalid type for x: {}"
                         .format(repr(type(x))))

    def is_earlier(self, x, y):
        """Return ``True`` if `x` is earlier than `y`."""
        if aeidon.is_time(x):
            x = self.time_to_seconds(x)
            return self.is_earlier(x, y)
        if aeidon.is_frame(x):
            return (x < self.to_frame(y))
        if aeidon.is_seconds(x):
            return (x < self.to_seconds(y))
        raise ValueError("Invalid type for x: {}"
                         .format(repr(type(x))))

    def is_later(self, x, y):
        """Return ``True`` if `x` is later than `y`."""
        if aeidon.is_time(x):
            x = self.time_to_seconds(x)
            return self.is_later(x, y)
        if aeidon.is_frame(x):
            return (x > self.to_frame(y))
        if aeidon.is_seconds(x):
            return (x > self.to_seconds(y))
        raise ValueError("Invalid type for x: {}"
                         .format(repr(type(x))))

    def is_valid_time(self, time):
        """Return ``True`` if `time` is a valid time string."""
        if time.startswith("-"):
            time = time[1:]
        try:
            hours    = int(time[ :2])
            minutes  = int(time[3:5])
            seconds  = int(time[6:8])
            mseconds = int(time[9: ])
        except ValueError:
            return False
        return (0 <= hours    <=  99 and
                0 <= minutes  <=  59 and
                0 <= seconds  <=  59 and
                0 <= mseconds <= 999)

    def normalize_time(self, time):
        """
        Convert `time` to valid format.

        >>> calc = aeidon.Calculator()
        >>> calc.normalize_time("1:2:3,4")
        '01:02:03.400'
        """
        time = time.strip()
        sign = ("-" if time.startswith("-") else "")
        time = time.replace("-", "")
        time = time.replace(",", ".")
        hours, minutes, seconds = time.split(":")
        return ("{}{:02.0f}:{:02.0f}:{:02.0f}.{:03.0f}"
                .format(sign,
                        int(hours),
                        int(minutes),
                        int(float(seconds)),
                        (float(seconds) % 1) * 1000))

    def round(self, pos, ndigits):
        """
        Round `pos` to given precision in decimal digits.

        `ndigits` may be negative. For frames zero will be used
        if given `ndigits` is greater than zero.
        """
        if aeidon.is_time(pos):
            pos = self.time_to_seconds(pos)
            pos = round(pos, ndigits)
            return self.seconds_to_time(pos)
        if aeidon.is_frame(pos):
            ndigits = min(0, ndigits)
            pos = round(pos, ndigits)
            return aeidon.as_frame(pos)
        if aeidon.is_seconds(pos):
            pos = round(pos, ndigits)
            return aeidon.as_seconds(pos)
        raise ValueError("Invalid type for pos: {}"
                         .format(repr(type(pos))))

    def seconds_to_frame(self, seconds):
        """Convert `seconds` to frame."""
        return int(round(seconds * self._framerate, 0))

    def seconds_to_time(self, seconds):
        """Convert `seconds` to time."""
        sign = ("-" if seconds < 0 else "")
        seconds = abs(round(seconds, 3))
        if seconds > 359999.999:
            return "{}99:59:59.999".format(sign)
        return ("{}{:02.0f}:{:02.0f}:{:02.0f}.{:03.0f}"
                .format(sign,
                        seconds // 3600,
                        (seconds % 3600) // 60,
                        int(seconds % 60),
                        (seconds % 1) * 1000))

    def time_to_frame(self, time):
        """Convert `time` to frame."""
        seconds = self.time_to_seconds(time)
        return self.seconds_to_frame(seconds)

    def time_to_seconds(self, time):
        """Convert `time` to seconds."""
        coefficient = (-1 if time.startswith("-") else 1)
        time = (time[1:] if time.startswith("-") else time)
        return coefficient * sum((float(time[ :2]) * 3600,
                                  float(time[3:5]) * 60,
                                  float(time[6:8]),
                                  float(time[9: ]) / 1000))

    def to_frame(self, pos):
        """Convert `pos` to frame."""
        if aeidon.is_time(pos):
            return self.time_to_frame(pos)
        if aeidon.is_frame(pos):
            return pos
        if aeidon.is_seconds(pos):
            return self.seconds_to_frame(pos)
        raise ValueError("Invalid type for pos: {}"
                         .format(repr(type(pos))))

    def to_seconds(self, pos):
        """Convert `pos` to seconds."""
        if aeidon.is_time(pos):
            return self.time_to_seconds(pos)
        if aeidon.is_frame(pos):
            return self.frame_to_seconds(pos)
        if aeidon.is_seconds(pos):
            return pos
        raise ValueError("Invalid type for pos: {}"
                         .format(repr(type(pos))))

    def to_time(self, pos):
        """Convert `pos` to time."""
        if aeidon.is_time(pos):
            return pos
        if aeidon.is_frame(pos):
            return self.frame_to_time(pos)
        if aeidon.is_seconds(pos):
            return self.seconds_to_time(pos)
        raise ValueError("Invalid type for pos: {}"
                         .format(repr(type(pos))))