jevstrudel.git / packages / core / timespan.mjs

timespan.mjs - <short description TODO> Copyright (C) 2022 Strudel contributors - see https://codeberg.org/uzu/strudel/src/branch/main/packages/core/timespan.mjs This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero 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 Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.

7import Fraction from './fraction.mjs';
9export class TimeSpan {
10  constructor(begin, end) {
11    this.begin = Fraction(begin);
12    this.end = Fraction(end);
13  }
14
15  get spanCycles() {
16    const spans = [];
17    var begin = this.begin;
18    const end = this.end;
19    const end_sam = end.sam();

Support zero-width timespans

22    if (begin.equals(end)) {
23      return [new TimeSpan(begin, end)];
24    }
26    while (end.gt(begin)) {
27      // If begin and end are in the same cycle, we're done.
28      if (begin.sam().equals(end_sam)) {
29        spans.push(new TimeSpan(begin, this.end));
30        break;
31      }
32      // add a timespan up to the next sam
33      const next_begin = begin.nextSam();
34      spans.push(new TimeSpan(begin, next_begin));

continue with the next cycle

37      begin = next_begin;
38    }
39    return spans;
40  }
42  get duration() {
43    return this.end.sub(this.begin);
44  }
45
46  cycleArc() {
47    // Shifts a timespan to one of equal duration that starts within cycle zero.
48    // (Note that the output timespan probably does not start *at* Time 0 --
49    // that only happens when the input Arc starts at an integral Time.)
50    const b = this.begin.cyclePos();
51    const e = b.add(this.duration);
52    return new TimeSpan(b, e);
53  }
54
55  withTime(func_time) {
56    // Applies given function to both the begin and end time of the timespan"""
57    return new TimeSpan(func_time(this.begin), func_time(this.end));
58  }
59
60  withEnd(func_time) {
61    // Applies given function to the end time of the timespan"""
62    return new TimeSpan(this.begin, func_time(this.end));
63  }
64
65  withCycle(func_time) {
66    // Like withTime, but time is relative to relative to the cycle (i.e. the
67    // sam of the start of the timespan)
68    const sam = this.begin.sam();
69    const b = sam.add(func_time(this.begin.sub(sam)));
70    const e = sam.add(func_time(this.end.sub(sam)));
71    return new TimeSpan(b, e);
72  }
73
74  intersection(other) {
75    // Intersection of two timespans, returns undefined if they don't intersect.
76    const intersect_begin = this.begin.max(other.begin);
77    const intersect_end = this.end.min(other.end);
78
79    if (intersect_begin.gt(intersect_end)) {
80      return undefined;
81    }
82    if (intersect_begin.equals(intersect_end)) {
83      // Zero-width (point) intersection - doesn't intersect if it's at the end of a
84      // non-zero-width timespan.
85      if (intersect_begin.equals(this.end) && this.begin.lt(this.end)) {
86        return undefined;
87      }
88      if (intersect_begin.equals(other.end) && other.begin.lt(other.end)) {
89        return undefined;
90      }
91    }
92    return new TimeSpan(intersect_begin, intersect_end);
93  }
94
95  intersection_e(other) {
96    // Like 'sect', but raises an exception if the timespans don't intersect.
97    const result = this.intersection(other);
98    if (result == undefined) {
99      throw 'TimeSpans do not intersect';
100    }
101    return result;
102  }
103
104  midpoint() {
105    return this.begin.add(this.duration.div(Fraction(2)));
106  }
107
108  equals(other) {
109    return this.begin.equals(other.begin) && this.end.equals(other.end);
110  }
111
112  show() {
113    return this.begin.show() + ' → ' + this.end.show();
114  }
115}
116
117export default TimeSpan;