notations 0.0.40 → 0.0.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/README.md +38 -0
  2. package/lib/cjs/beats.d.ts +111 -0
  3. package/lib/cjs/beats.js +523 -0
  4. package/lib/cjs/beats.js.map +1 -0
  5. package/lib/cjs/carnatic/LineView.d.ts +20 -0
  6. package/lib/cjs/carnatic/LineView.js +69 -0
  7. package/lib/cjs/carnatic/LineView.js.map +1 -0
  8. package/lib/cjs/carnatic/NotationView.d.ts +31 -0
  9. package/lib/cjs/carnatic/NotationView.js +250 -0
  10. package/lib/cjs/carnatic/NotationView.js.map +1 -0
  11. package/lib/cjs/carnatic/atomviews.d.ts +14 -7
  12. package/lib/cjs/carnatic/atomviews.js +170 -137
  13. package/lib/cjs/carnatic/atomviews.js.map +1 -1
  14. package/lib/cjs/carnatic/beatviews.d.ts +10 -14
  15. package/lib/cjs/carnatic/beatviews.js +53 -108
  16. package/lib/cjs/carnatic/beatviews.js.map +1 -1
  17. package/lib/cjs/carnatic/embelishments.d.ts +38 -32
  18. package/lib/cjs/carnatic/embelishments.js +35 -20
  19. package/lib/cjs/carnatic/embelishments.js.map +1 -1
  20. package/lib/cjs/carnatic/index.d.ts +2 -0
  21. package/lib/cjs/carnatic/index.js +2 -0
  22. package/lib/cjs/carnatic/index.js.map +1 -1
  23. package/lib/cjs/core.d.ts +15 -73
  24. package/lib/cjs/core.js +200 -568
  25. package/lib/cjs/core.js.map +1 -1
  26. package/lib/cjs/cycle.d.ts +47 -0
  27. package/lib/cjs/cycle.js +492 -0
  28. package/lib/cjs/cycle.js.map +1 -0
  29. package/lib/cjs/entity.d.ts +24 -0
  30. package/lib/cjs/entity.js +135 -0
  31. package/lib/cjs/entity.js.map +1 -0
  32. package/lib/cjs/index.d.ts +3 -0
  33. package/lib/cjs/index.js +3 -0
  34. package/lib/cjs/index.js.map +1 -1
  35. package/lib/cjs/iterators.d.ts +14 -2
  36. package/lib/cjs/iterators.js +79 -7
  37. package/lib/cjs/iterators.js.map +1 -1
  38. package/lib/cjs/layouts.d.ts +7 -106
  39. package/lib/cjs/layouts.js +4 -478
  40. package/lib/cjs/layouts.js.map +1 -1
  41. package/lib/cjs/loader.d.ts +2 -1
  42. package/lib/cjs/loader.js +32 -27
  43. package/lib/cjs/loader.js.map +1 -1
  44. package/lib/cjs/notation.d.ts +3 -1
  45. package/lib/cjs/notation.js +6 -4
  46. package/lib/cjs/notation.js.map +1 -1
  47. package/lib/cjs/parser.js +6 -5
  48. package/lib/cjs/parser.js.map +1 -1
  49. package/lib/cjs/shapes.d.ts +35 -14
  50. package/lib/cjs/shapes.js +153 -43
  51. package/lib/cjs/shapes.js.map +1 -1
  52. package/lib/cjs/utils.d.ts +2 -1
  53. package/lib/cjs/utils.js +20 -8
  54. package/lib/cjs/utils.js.map +1 -1
  55. package/lib/esm/beats.d.ts +111 -0
  56. package/lib/esm/beats.js +501 -0
  57. package/lib/esm/beats.js.map +1 -0
  58. package/lib/esm/carnatic/LineView.d.ts +20 -0
  59. package/lib/esm/carnatic/LineView.js +47 -0
  60. package/lib/esm/carnatic/LineView.js.map +1 -0
  61. package/lib/esm/carnatic/NotationView.d.ts +31 -0
  62. package/lib/esm/carnatic/NotationView.js +228 -0
  63. package/lib/esm/carnatic/NotationView.js.map +1 -0
  64. package/lib/esm/carnatic/atomviews.d.ts +14 -7
  65. package/lib/esm/carnatic/atomviews.js +170 -137
  66. package/lib/esm/carnatic/atomviews.js.map +1 -1
  67. package/lib/esm/carnatic/beatviews.d.ts +10 -14
  68. package/lib/esm/carnatic/beatviews.js +54 -109
  69. package/lib/esm/carnatic/beatviews.js.map +1 -1
  70. package/lib/esm/carnatic/embelishments.d.ts +38 -32
  71. package/lib/esm/carnatic/embelishments.js +34 -19
  72. package/lib/esm/carnatic/embelishments.js.map +1 -1
  73. package/lib/esm/carnatic/index.d.ts +2 -0
  74. package/lib/esm/carnatic/index.js +2 -0
  75. package/lib/esm/carnatic/index.js.map +1 -1
  76. package/lib/esm/core.d.ts +15 -73
  77. package/lib/esm/core.js +196 -564
  78. package/lib/esm/core.js.map +1 -1
  79. package/lib/esm/cycle.d.ts +47 -0
  80. package/lib/esm/cycle.js +470 -0
  81. package/lib/esm/cycle.js.map +1 -0
  82. package/lib/esm/entity.d.ts +24 -0
  83. package/lib/esm/entity.js +132 -0
  84. package/lib/esm/entity.js.map +1 -0
  85. package/lib/esm/index.d.ts +3 -0
  86. package/lib/esm/index.js +3 -0
  87. package/lib/esm/index.js.map +1 -1
  88. package/lib/esm/iterators.d.ts +14 -2
  89. package/lib/esm/iterators.js +75 -3
  90. package/lib/esm/iterators.js.map +1 -1
  91. package/lib/esm/layouts.d.ts +7 -106
  92. package/lib/esm/layouts.js +1 -475
  93. package/lib/esm/layouts.js.map +1 -1
  94. package/lib/esm/loader.d.ts +2 -1
  95. package/lib/esm/loader.js +28 -24
  96. package/lib/esm/loader.js.map +1 -1
  97. package/lib/esm/notation.d.ts +3 -1
  98. package/lib/esm/notation.js +3 -1
  99. package/lib/esm/notation.js.map +1 -1
  100. package/lib/esm/parser.js +6 -5
  101. package/lib/esm/parser.js.map +1 -1
  102. package/lib/esm/shapes.d.ts +35 -14
  103. package/lib/esm/shapes.js +152 -42
  104. package/lib/esm/shapes.js.map +1 -1
  105. package/lib/esm/utils.d.ts +2 -1
  106. package/lib/esm/utils.js +19 -7
  107. package/lib/esm/utils.js.map +1 -1
  108. package/package.json +33 -28
  109. package/styles/NotationView.scss +35 -0
package/lib/esm/core.js CHANGED
@@ -24,44 +24,6 @@ var __assign = (this && this.__assign) || function () {
24
24
  };
25
25
  return __assign.apply(this, arguments);
26
26
  };
27
- var __generator = (this && this.__generator) || function (thisArg, body) {
28
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
29
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
30
- function verb(n) { return function (v) { return step([n, v]); }; }
31
- function step(op) {
32
- if (f) throw new TypeError("Generator is already executing.");
33
- while (_) try {
34
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
35
- if (y = 0, t) op = [op[0] & 2, t.value];
36
- switch (op[0]) {
37
- case 0: case 1: t = op; break;
38
- case 4: _.label++; return { value: op[1], done: false };
39
- case 5: _.label++; y = op[1]; op = [0]; continue;
40
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
41
- default:
42
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
43
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
44
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
45
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
46
- if (t[2]) _.ops.pop();
47
- _.trys.pop(); continue;
48
- }
49
- op = body.call(thisArg, _);
50
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
51
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
52
- }
53
- };
54
- var __values = (this && this.__values) || function(o) {
55
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
56
- if (m) return m.call(o);
57
- if (o && typeof o.length === "number") return {
58
- next: function () {
59
- if (o && i >= o.length) o = void 0;
60
- return { value: o && o[i++], done: !o };
61
- }
62
- };
63
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
64
- };
65
27
  var __read = (this && this.__read) || function (o, n) {
66
28
  var m = typeof Symbol === "function" && o[Symbol.iterator];
67
29
  if (!m) return o;
@@ -87,132 +49,21 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
87
49
  }
88
50
  return to.concat(ar || Array.prototype.slice.call(from));
89
51
  };
52
+ var __values = (this && this.__values) || function(o) {
53
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
54
+ if (m) return m.call(o);
55
+ if (o && typeof o.length === "number") return {
56
+ next: function () {
57
+ if (o && i >= o.length) o = void 0;
58
+ return { value: o && o[i++], done: !o };
59
+ }
60
+ };
61
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
62
+ };
90
63
  import * as TSU from "@panyam/tsutils";
64
+ import { Entity, TimedEntity } from "./entity";
91
65
  var ZERO = TSU.Num.Fraction.ZERO;
92
66
  var ONE = TSU.Num.Fraction.ONE;
93
- var Entity = (function () {
94
- function Entity(config) {
95
- if (config === void 0) { config = null; }
96
- this.uuid = Entity.counter++;
97
- this.parent = null;
98
- config = config || {};
99
- this.metadata = config.metadata || {};
100
- }
101
- Entity.prototype.debugValue = function () {
102
- if (Object.keys(this.metadata).length > 0)
103
- return { metadata: this.metadata, type: this.type };
104
- else
105
- return { type: this.type };
106
- };
107
- Entity.prototype.children = function () {
108
- return [];
109
- };
110
- Object.defineProperty(Entity.prototype, "childCount", {
111
- get: function () {
112
- return this.children().length;
113
- },
114
- enumerable: false,
115
- configurable: true
116
- });
117
- Entity.prototype.addChild = function (child, index) {
118
- if (index === void 0) { index = -1; }
119
- if (index < 0) {
120
- this.children().push(child);
121
- }
122
- else {
123
- this.children().splice(index, 0, child);
124
- }
125
- return this;
126
- };
127
- Entity.prototype.childAt = function (index) {
128
- return this.children()[index];
129
- };
130
- Entity.prototype.indexOfChild = function (entity) {
131
- var e_1, _a;
132
- var i = 0;
133
- try {
134
- for (var _b = __values(this.children()), _c = _b.next(); !_c.done; _c = _b.next()) {
135
- var child = _c.value;
136
- if (child == entity)
137
- return i;
138
- i++;
139
- }
140
- }
141
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
142
- finally {
143
- try {
144
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
145
- }
146
- finally { if (e_1) throw e_1.error; }
147
- }
148
- return -1;
149
- };
150
- Entity.prototype.removeChildAt = function (index) {
151
- var children = this.children();
152
- var out = children[index];
153
- children.splice(index, 1);
154
- return out;
155
- };
156
- Entity.prototype.setChildAt = function (index, entity) {
157
- this.children()[index] = entity;
158
- return this;
159
- };
160
- Entity.prototype.setMetadata = function (key, value) {
161
- this.metadata[key] = value;
162
- return this;
163
- };
164
- Entity.prototype.getMetadata = function (key, recurse) {
165
- if (recurse === void 0) { recurse = true; }
166
- if (key in this.metadata) {
167
- return this.metadata[key];
168
- }
169
- if (recurse && this.parent) {
170
- return this.parent.getMetadata(key);
171
- }
172
- return null;
173
- };
174
- Object.defineProperty(Entity.prototype, "type", {
175
- get: function () {
176
- return this.constructor.name;
177
- },
178
- enumerable: false,
179
- configurable: true
180
- });
181
- Entity.prototype.toString = function () {
182
- return "Entity(id = " + this.uuid + ")";
183
- };
184
- Entity.prototype.equals = function (another, expect) {
185
- if (expect === void 0) { expect = false; }
186
- if (this.type != another.type)
187
- return false;
188
- return true;
189
- };
190
- Entity.prototype.clone = function () {
191
- var out = this.newInstance();
192
- this.copyTo(out);
193
- return out;
194
- };
195
- Entity.prototype.copyTo = function (another) {
196
- another.metadata = __assign({}, this.metadata);
197
- };
198
- Entity.prototype.newInstance = function () {
199
- return new this.constructor();
200
- };
201
- Entity.counter = 0;
202
- return Entity;
203
- }());
204
- export { Entity };
205
- var TimedEntity = (function (_super) {
206
- __extends(TimedEntity, _super);
207
- function TimedEntity() {
208
- return _super !== null && _super.apply(this, arguments) || this;
209
- }
210
- TimedEntity.prototype.equals = function (another) {
211
- return _super.prototype.equals.call(this, another) && this.duration.equals(another.duration);
212
- };
213
- return TimedEntity;
214
- }(Entity));
215
- export { TimedEntity };
216
67
  export var AtomType;
217
68
  (function (AtomType) {
218
69
  AtomType["NOTE"] = "Note";
@@ -230,17 +81,24 @@ var Atom = (function (_super) {
230
81
  var _this = _super.call(this) || this;
231
82
  _this.nextSibling = null;
232
83
  _this.prevSibling = null;
84
+ _this.parentGroup = null;
85
+ _this.isContinuation = false;
233
86
  _this._duration = duration || ONE;
234
87
  return _this;
235
88
  }
236
89
  Atom.prototype.debugValue = function () {
237
- return this.duration.isOne
238
- ? _super.prototype.debugValue.call(this)
239
- : __assign(__assign({}, _super.prototype.debugValue.call(this)), { duration: this.duration.factorized.toString() });
90
+ var out = _super.prototype.debugValue.call(this);
91
+ if (!this.duration.isOne) {
92
+ out.duration = this.duration.factorized.toString();
93
+ }
94
+ if (this.isContinuation) {
95
+ out.isContinuation = true;
96
+ }
97
+ return out;
240
98
  };
241
99
  Atom.prototype.copyTo = function (another) {
242
100
  _super.prototype.copyTo.call(this, another);
243
- another.duration = new TSU.Num.Fraction(this.duration.num, this.duration.den);
101
+ another._duration = new TSU.Num.Fraction(this.duration.num, this.duration.den);
244
102
  };
245
103
  Object.defineProperty(Atom.prototype, "duration", {
246
104
  get: function () {
@@ -262,6 +120,18 @@ var LeafAtom = (function (_super) {
262
120
  _this.beforeRest = false;
263
121
  return _this;
264
122
  }
123
+ LeafAtom.prototype.splitAt = function (duration) {
124
+ if (this.duration.cmp(duration) > 0) {
125
+ var spillOver = this.createSpilloverSpace(this.duration.minus(duration));
126
+ spillOver.isContinuation = true;
127
+ this.duration = duration;
128
+ return spillOver;
129
+ }
130
+ return null;
131
+ };
132
+ LeafAtom.prototype.createSpilloverSpace = function (duration) {
133
+ return new Space(duration);
134
+ };
265
135
  LeafAtom.prototype.debugValue = function () {
266
136
  return this.beforeRest ? __assign(__assign({}, _super.prototype.debugValue.call(this)), { beforeRest: true }) : _super.prototype.debugValue.call(this);
267
137
  };
@@ -290,7 +160,7 @@ var Space = (function (_super) {
290
160
  return __assign(__assign({}, _super.prototype.debugValue.call(this)), { isSilent: this.isSilent });
291
161
  };
292
162
  Space.prototype.toString = function () {
293
- return "Space(" + this.duration + "-" + this.isSilent + ")";
163
+ return "Space(".concat(this.duration, "-").concat(this.isSilent, ")");
294
164
  };
295
165
  Space.prototype.copyTo = function (another) {
296
166
  _super.prototype.copyTo.call(this, another);
@@ -299,6 +169,11 @@ var Space = (function (_super) {
299
169
  Space.prototype.equals = function (another) {
300
170
  return _super.prototype.equals.call(this, another) && this.isSilent == another.isSilent;
301
171
  };
172
+ Space.prototype.createSpilloverSpace = function (duration) {
173
+ var out = _super.prototype.createSpilloverSpace.call(this, duration);
174
+ out.isSilent = this.isSilent;
175
+ return out;
176
+ };
302
177
  return Space;
303
178
  }(LeafAtom));
304
179
  export { Space };
@@ -319,7 +194,7 @@ var Literal = (function (_super) {
319
194
  return out;
320
195
  };
321
196
  Literal.prototype.toString = function () {
322
- return "Lit(" + this.duration + "-" + this.value + ")";
197
+ return "Lit(".concat(this.duration, "-").concat(this.value, ")");
323
198
  };
324
199
  Literal.prototype.equals = function (another) {
325
200
  return _super.prototype.equals.call(this, another) && this.value == another.value;
@@ -341,10 +216,11 @@ var Syllable = (function (_super) {
341
216
  return lit;
342
217
  var out = new Syllable(lit.value, lit.duration);
343
218
  out.embelishments = lit.embelishments;
219
+ out.beforeRest = lit.beforeRest;
344
220
  return out;
345
221
  };
346
222
  Syllable.prototype.toString = function () {
347
- return "Syll(" + this.duration + "-" + this.value + ")";
223
+ return "Syll(".concat(this.duration, "-").concat(this.value, ")");
348
224
  };
349
225
  return Syllable;
350
226
  }(Literal));
@@ -367,6 +243,7 @@ var Note = (function (_super) {
367
243
  return lit;
368
244
  var out = new Note(lit.value, lit.duration);
369
245
  out.embelishments = lit.embelishments;
246
+ out.beforeRest = lit.beforeRest;
370
247
  return out;
371
248
  };
372
249
  Note.prototype.debugValue = function () {
@@ -378,7 +255,7 @@ var Note = (function (_super) {
378
255
  return out;
379
256
  };
380
257
  Note.prototype.toString = function () {
381
- return "Note(" + this.duration + "-" + this.value + "-" + this.octave + ")";
258
+ return "Note(".concat(this.duration, "-").concat(this.value, "-").concat(this.octave, ")");
382
259
  };
383
260
  Note.prototype.equals = function (another) {
384
261
  return _super.prototype.equals.call(this, another) && this.octave == another.octave && this.shift == another.shift;
@@ -393,18 +270,28 @@ var Note = (function (_super) {
393
270
  export { Note };
394
271
  var Group = (function (_super) {
395
272
  __extends(Group, _super);
396
- function Group(duration) {
397
- if (duration === void 0) { duration = ONE; }
273
+ function Group() {
398
274
  var atoms = [];
399
- for (var _i = 1; _i < arguments.length; _i++) {
400
- atoms[_i - 1] = arguments[_i];
275
+ for (var _i = 0; _i < arguments.length; _i++) {
276
+ atoms[_i] = arguments[_i];
401
277
  }
402
- var _this = _super.call(this, duration) || this;
278
+ var _this = _super.call(this, atoms.length == 0 ? ZERO : ONE) || this;
403
279
  _this.durationIsMultiplier = false;
404
280
  _this.atoms = new TSU.Lists.ValueList();
405
- _this.addAtoms.apply(_this, __spreadArray([], __read(atoms), false));
281
+ _this.addAtoms.apply(_this, __spreadArray([false], __read(atoms), false));
406
282
  return _this;
407
283
  }
284
+ Group.prototype.equals = function (another, expect) {
285
+ if (expect === void 0) { expect = false; }
286
+ if (!_super.prototype.equals.call(this, another))
287
+ return false;
288
+ return this.atoms.equals(another.atoms, function (a1, a2) { return a1.equals(a2); });
289
+ };
290
+ Group.prototype.copyTo = function (another) {
291
+ _super.prototype.copyTo.call(this, another);
292
+ another.durationIsMultiplier = this.durationIsMultiplier;
293
+ this.atoms.forEach(function (atom) { return another.atoms.add(atom.clone()); });
294
+ };
408
295
  Object.defineProperty(Group.prototype, "duration", {
409
296
  get: function () {
410
297
  if (this.durationIsMultiplier) {
@@ -414,18 +301,72 @@ var Group = (function (_super) {
414
301
  return this._duration;
415
302
  }
416
303
  },
417
- set: function (d) {
418
- this._duration = d;
419
- },
420
304
  enumerable: false,
421
305
  configurable: true
422
306
  });
307
+ Group.prototype.setDurationAsMultiplier = function (asMultiplier) {
308
+ if (asMultiplier === void 0) { asMultiplier = true; }
309
+ this.durationIsMultiplier = asMultiplier;
310
+ return this;
311
+ };
312
+ Group.prototype.setDuration = function (d, asMultiplier) {
313
+ if (asMultiplier === void 0) { asMultiplier = false; }
314
+ this._duration = d;
315
+ this.durationIsMultiplier = asMultiplier;
316
+ return this;
317
+ };
423
318
  Group.prototype.debugValue = function () {
424
319
  var out = __assign(__assign({}, _super.prototype.debugValue.call(this)), { atoms: Array.from(this.atoms.values(), function (a) { return a.debugValue(); }) });
425
320
  if (this.durationIsMultiplier)
426
321
  out.durationIsMultiplier = true;
427
322
  return out;
428
323
  };
324
+ Group.prototype.splitAt = function (requiredDuration) {
325
+ if (this.duration.isLTE(requiredDuration) || requiredDuration.isLTE(ZERO)) {
326
+ return null;
327
+ }
328
+ var targetGroup = new Group();
329
+ if (this.durationIsMultiplier) {
330
+ targetGroup.durationIsMultiplier = true;
331
+ targetGroup._duration = this._duration;
332
+ }
333
+ var remainingDur = this.duration;
334
+ var totalChildDuration = this.totalChildDuration;
335
+ var durationFactor = this.durationIsMultiplier
336
+ ? ONE.divby(this._duration)
337
+ : this._duration.divby(totalChildDuration, true);
338
+ while (remainingDur.isGT(requiredDuration) && this.atoms.last) {
339
+ var lastChild = this.atoms.last;
340
+ var childDuration = lastChild.duration.times(durationFactor);
341
+ var newDuration = remainingDur.minus(childDuration);
342
+ if (newDuration.isGTE(requiredDuration)) {
343
+ this.removeAtoms(true, lastChild);
344
+ targetGroup.insertAtomsAt(targetGroup.atoms.first, true, lastChild);
345
+ if (newDuration.equals(requiredDuration)) {
346
+ return targetGroup;
347
+ }
348
+ }
349
+ else {
350
+ var newRequiredDur = requiredDuration.minus(newDuration, true).divby(durationFactor, true);
351
+ var spillOver = lastChild.splitAt(newRequiredDur);
352
+ if (spillOver == null) {
353
+ throw new Error("Spill over cannot be null here");
354
+ }
355
+ if (!this.durationIsMultiplier) {
356
+ this._duration = requiredDuration;
357
+ }
358
+ else {
359
+ if (this._duration.isZero)
360
+ throw new Error("How can this be?");
361
+ }
362
+ spillOver.isContinuation = true;
363
+ targetGroup.insertAtomsAt(targetGroup.atoms.first, true, spillOver);
364
+ return targetGroup;
365
+ }
366
+ remainingDur = newDuration;
367
+ }
368
+ return targetGroup;
369
+ };
429
370
  Object.defineProperty(Group.prototype, "totalChildDuration", {
430
371
  get: function () {
431
372
  var out = ZERO;
@@ -435,25 +376,24 @@ var Group = (function (_super) {
435
376
  enumerable: false,
436
377
  configurable: true
437
378
  });
438
- Group.prototype.equals = function (another, expect) {
439
- if (expect === void 0) { expect = false; }
440
- if (!_super.prototype.equals.call(this, another))
441
- return false;
442
- return this.atoms.equals(another.atoms, function (a1, a2) { return a1.equals(a2); });
443
- };
444
- Group.prototype.copyTo = function (another) {
445
- _super.prototype.copyTo.call(this, another);
446
- this.atoms.forEach(function (atom) { return another.atoms.add(atom.clone()); });
447
- };
448
- Group.prototype.addAtoms = function () {
449
- var e_2, _a;
379
+ Group.prototype.insertAtomsAt = function (beforeAtom, adjustDuration) {
380
+ var e_1, _a;
381
+ if (adjustDuration === void 0) { adjustDuration = false; }
450
382
  var atoms = [];
451
- for (var _i = 0; _i < arguments.length; _i++) {
452
- atoms[_i] = arguments[_i];
383
+ for (var _i = 2; _i < arguments.length; _i++) {
384
+ atoms[_i - 2] = arguments[_i];
453
385
  }
386
+ adjustDuration = adjustDuration && !this.durationIsMultiplier;
387
+ var oldChildDuration = adjustDuration ? this.totalChildDuration : ONE;
454
388
  try {
455
389
  for (var atoms_1 = __values(atoms), atoms_1_1 = atoms_1.next(); !atoms_1_1.done; atoms_1_1 = atoms_1.next()) {
456
390
  var atom = atoms_1_1.value;
391
+ if (atom.parentGroup != null) {
392
+ if (atom.parentGroup != this) {
393
+ throw new Error("Atom belongs to another parent. Remove it first");
394
+ }
395
+ atom.parentGroup.removeAtoms(false, atom);
396
+ }
457
397
  if (atom.type == AtomType.REST) {
458
398
  var last = this.atoms.last;
459
399
  if (last && last.type != AtomType.GROUP && last.type != AtomType.LABEL) {
@@ -461,391 +401,83 @@ var Group = (function (_super) {
461
401
  }
462
402
  }
463
403
  else {
464
- atom.parent = this;
465
- this.atoms.add(atom);
404
+ atom.parentGroup = this;
405
+ this.atoms.add(atom, beforeAtom);
466
406
  }
467
407
  }
468
408
  }
469
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
409
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
470
410
  finally {
471
411
  try {
472
412
  if (atoms_1_1 && !atoms_1_1.done && (_a = atoms_1.return)) _a.call(atoms_1);
473
413
  }
474
- finally { if (e_2) throw e_2.error; }
475
- }
476
- return this;
477
- };
478
- return Group;
479
- }(Atom));
480
- export { Group };
481
- var CycleCursor = (function () {
482
- function CycleCursor(cycle, barIndex, beatIndex, instance) {
483
- if (barIndex === void 0) { barIndex = 0; }
484
- if (beatIndex === void 0) { beatIndex = 0; }
485
- if (instance === void 0) { instance = 0; }
486
- this.cycle = cycle;
487
- this.barIndex = barIndex;
488
- this.beatIndex = beatIndex;
489
- this.instance = instance;
490
- }
491
- Object.defineProperty(CycleCursor.prototype, "next", {
492
- get: function () {
493
- var currBar = this.cycle.bars[this.barIndex];
494
- var result = [
495
- [this.barIndex, this.beatIndex, this.instance],
496
- currBar.beatLengths[this.beatIndex],
497
- ];
498
- this.instance++;
499
- if (!currBar.beatCounts[this.beatIndex] || this.instance >= currBar.beatCounts[this.beatIndex]) {
500
- this.instance = 0;
501
- this.beatIndex++;
502
- if (this.beatIndex >= currBar.beatLengths.length) {
503
- this.beatIndex = 0;
504
- this.barIndex++;
505
- if (this.barIndex >= this.cycle.bars.length) {
506
- this.barIndex = 0;
507
- }
508
- }
509
- }
510
- return result;
511
- },
512
- enumerable: false,
513
- configurable: true
514
- });
515
- Object.defineProperty(CycleCursor.prototype, "prev", {
516
- get: function () {
517
- var currBar = this.cycle.bars[this.barIndex];
518
- var result = [
519
- [this.barIndex, this.beatIndex, this.instance],
520
- currBar.beatLengths[this.beatIndex],
521
- ];
522
- this.instance--;
523
- if (this.instance < 0) {
524
- this.beatIndex--;
525
- if (this.beatIndex < 0) {
526
- this.barIndex--;
527
- if (this.barIndex < 0) {
528
- this.barIndex = this.cycle.bars.length - 1;
529
- }
530
- this.beatIndex = this.cycle.bars[this.barIndex].beatCount - 1;
531
- }
532
- this.instance = (this.cycle.bars[this.barIndex].beatCounts[this.beatIndex] || 1) - 1;
533
- }
534
- return result;
535
- },
536
- enumerable: false,
537
- configurable: true
538
- });
539
- return CycleCursor;
540
- }());
541
- export { CycleCursor };
542
- var Bar = (function (_super) {
543
- __extends(Bar, _super);
544
- function Bar(config) {
545
- var e_3, _a, e_4, _b;
546
- if (config === void 0) { config = null; }
547
- var _this = _super.call(this, (config = config || {})) || this;
548
- _this.beatLengths = [];
549
- _this.beatCounts = [];
550
- _this.name = config.name || "";
551
- try {
552
- for (var _c = __values(config.beatLengths || []), _d = _c.next(); !_d.done; _d = _c.next()) {
553
- var bl = _d.value;
554
- if (typeof bl === "number") {
555
- _this.beatLengths.push(TSU.Num.Frac(bl));
556
- }
557
- else {
558
- _this.beatLengths.push(bl);
559
- }
560
- }
561
- }
562
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
563
- finally {
564
- try {
565
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
566
- }
567
- finally { if (e_3) throw e_3.error; }
414
+ finally { if (e_1) throw e_1.error; }
568
415
  }
569
- try {
570
- for (var _e = __values(config.beatCounts || []), _f = _e.next(); !_f.done; _f = _e.next()) {
571
- var bc = _f.value;
572
- _this.beatCounts.push(bc);
416
+ if (adjustDuration) {
417
+ if (this._duration.isZero) {
418
+ if (this.durationIsMultiplier)
419
+ throw new Error("How can this be?");
420
+ this._duration = this.totalChildDuration;
573
421
  }
574
- }
575
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
576
- finally {
577
- try {
578
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
422
+ else {
423
+ var scaleFactor = this.totalChildDuration.divby(oldChildDuration);
424
+ this._duration = this._duration.times(scaleFactor, true);
579
425
  }
580
- finally { if (e_4) throw e_4.error; }
581
- }
582
- while (_this.beatCounts.length < _this.beatLengths.length) {
583
- _this.beatCounts.push(1);
584
- }
585
- return _this;
586
- }
587
- Bar.prototype.debugValue = function () {
588
- return __assign(__assign({}, _super.prototype.debugValue.call(this)), { name: name, beatLengths: this.beatLengths });
589
- };
590
- Bar.prototype.equals = function (another) {
591
- if (!_super.prototype.equals.call(this, another))
592
- return false;
593
- if (this.beatLengths.length != another.beatLengths.length)
594
- return false;
595
- if (this.beatCounts.length != another.beatCounts.length)
596
- return false;
597
- for (var i = 0; i < this.beatLengths.length; i++) {
598
- if (!this.beatLengths[i].equals(another.beatLengths[i]))
599
- return false;
600
426
  }
601
- for (var i = 0; i < this.beatCounts.length; i++) {
602
- if (this.beatCounts[i] != another.beatCounts[i])
603
- return false;
604
- }
605
- return true;
606
- };
607
- Bar.prototype.copyTo = function (another) {
608
- _super.prototype.copyTo.call(this, another);
609
- another.name = this.name;
610
- another.beatLengths = __spreadArray([], __read(this.beatLengths), false);
611
- another.beatCounts = __spreadArray([], __read(this.beatCounts), false);
612
- };
613
- Object.defineProperty(Bar.prototype, "beatCount", {
614
- get: function () {
615
- return this.beatLengths.length;
616
- },
617
- enumerable: false,
618
- configurable: true
619
- });
620
- Object.defineProperty(Bar.prototype, "totalBeatCount", {
621
- get: function () {
622
- var out = 0;
623
- for (var i = 0; i < this.beatLengths.length; i++) {
624
- out += this.beatCounts[i] || 1;
625
- }
626
- return out;
627
- },
628
- enumerable: false,
629
- configurable: true
630
- });
631
- Object.defineProperty(Bar.prototype, "duration", {
632
- get: function () {
633
- var total = ZERO;
634
- for (var i = 0; i < this.beatLengths.length; i++) {
635
- total = total.plus(this.beatLengths[i].timesNum(this.beatCounts[i] || 1));
636
- }
637
- return total;
638
- },
639
- enumerable: false,
640
- configurable: true
641
- });
642
- return Bar;
643
- }(TimedEntity));
644
- export { Bar };
645
- var Cycle = (function (_super) {
646
- __extends(Cycle, _super);
647
- function Cycle(config) {
648
- if (config === void 0) { config = null; }
649
- var _this = _super.call(this, (config = config || {})) || this;
650
- _this.name = config.name || "";
651
- _this.bars = config.bars || [];
652
- return _this;
653
- }
654
- Cycle.prototype.debugValue = function () {
655
- return __assign(__assign({}, _super.prototype.debugValue.call(this)), { name: name, bars: this.bars.map(function (p) { return p.debugValue(); }) });
656
- };
657
- Cycle.prototype.children = function () {
658
- return this.bars;
427
+ return this;
659
428
  };
660
- Cycle.prototype.equals = function (another) {
661
- if (!_super.prototype.equals.call(this, another)) {
662
- return false;
663
- }
664
- if (this.bars.length != another.bars.length)
665
- return false;
666
- for (var i = 0; i < this.bars.length; i++) {
667
- if (!this.bars[i].equals(another.bars[i]))
668
- return false;
429
+ Group.prototype.addAtoms = function (adjustDuration) {
430
+ if (adjustDuration === void 0) { adjustDuration = false; }
431
+ var atoms = [];
432
+ for (var _i = 1; _i < arguments.length; _i++) {
433
+ atoms[_i - 1] = arguments[_i];
669
434
  }
670
- return true;
435
+ return this.insertAtomsAt.apply(this, __spreadArray([null, adjustDuration], __read(atoms), false));
671
436
  };
672
- Cycle.prototype.getAtIndex = function (globalIndex) {
673
- var cycle = 0;
674
- while (globalIndex < 0) {
675
- globalIndex += this.totalBeatCount;
676
- cycle--;
677
- }
678
- if (globalIndex >= this.totalBeatCount) {
679
- cycle = Math.floor(globalIndex / this.totalBeatCount);
437
+ Group.prototype.removeAtoms = function (adjustDuration) {
438
+ var e_2, _a;
439
+ if (adjustDuration === void 0) { adjustDuration = false; }
440
+ var atoms = [];
441
+ for (var _i = 1; _i < arguments.length; _i++) {
442
+ atoms[_i - 1] = arguments[_i];
680
443
  }
681
- globalIndex = globalIndex % this.totalBeatCount;
682
- var offset = ZERO;
683
- for (var barIndex = 0; barIndex < this.bars.length; barIndex++) {
684
- var bar = this.bars[barIndex];
685
- if (globalIndex >= bar.totalBeatCount) {
686
- globalIndex -= bar.totalBeatCount;
687
- offset = offset.plus(bar.duration);
688
- }
689
- else {
690
- for (var beatIndex = 0; beatIndex < bar.beatCount; beatIndex++) {
691
- var beatLength = bar.beatLengths[beatIndex];
692
- var beatCount = bar.beatCounts[beatIndex] || 1;
693
- if (globalIndex >= beatCount) {
694
- globalIndex -= beatCount;
695
- offset = offset.plus(beatLength.timesNum(beatCount));
696
- }
697
- else {
698
- var instance = globalIndex;
699
- return [cycle, [barIndex, beatIndex, instance], offset.plus(beatLength.timesNum(instance))];
700
- }
444
+ adjustDuration = adjustDuration && !this.durationIsMultiplier;
445
+ var oldChildDuration = adjustDuration ? this.totalChildDuration : ONE;
446
+ try {
447
+ for (var atoms_2 = __values(atoms), atoms_2_1 = atoms_2.next(); !atoms_2_1.done; atoms_2_1 = atoms_2.next()) {
448
+ var atom = atoms_2_1.value;
449
+ if (atom.parentGroup == this) {
450
+ this.atoms.remove(atom);
451
+ atom.parentGroup = null;
452
+ }
453
+ else if (atom.parentGroup != null) {
454
+ throw new Error("Atom cannot be removed as it does not belong to this group");
701
455
  }
702
456
  }
703
457
  }
704
- throw new Error("Should not be here!");
705
- };
706
- Cycle.prototype.getPosition = function (globalOffset) {
707
- var duration = this.duration;
708
- var cycleNum = 0;
709
- if (globalOffset.isLT(ZERO)) {
710
- while (globalOffset.isLT(ZERO)) {
711
- cycleNum--;
712
- globalOffset = globalOffset.plus(duration);
458
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
459
+ finally {
460
+ try {
461
+ if (atoms_2_1 && !atoms_2_1.done && (_a = atoms_2.return)) _a.call(atoms_2);
713
462
  }
463
+ finally { if (e_2) throw e_2.error; }
714
464
  }
715
- else if (globalOffset.isGTE(duration)) {
716
- var realOffset = globalOffset.mod(duration);
717
- globalOffset = globalOffset.minus(realOffset).divby(duration);
718
- TSU.assert(globalOffset.isWhole);
719
- cycleNum = globalOffset.floor;
720
- globalOffset = realOffset;
721
- }
722
- var globalIndex = 0;
723
- for (var barIndex = 0; barIndex < this.bars.length; barIndex++) {
724
- var bar = this.bars[barIndex];
725
- var barDuration = bar.duration;
726
- if (globalOffset.isGTE(barDuration)) {
727
- globalOffset = globalOffset.minus(barDuration);
465
+ if (adjustDuration) {
466
+ if (this._duration.isZero) {
467
+ if (this.durationIsMultiplier)
468
+ throw new Error("How can this be?");
469
+ this._duration = this.totalChildDuration;
728
470
  }
729
471
  else {
730
- for (var beatIndex = 0; beatIndex < bar.beatCount; beatIndex++) {
731
- var beatLength = bar.beatLengths[beatIndex];
732
- var beatCount = bar.beatCounts[beatIndex] || 1;
733
- for (var instance = 0; instance < beatCount; instance++, globalIndex++) {
734
- if (globalOffset.isGTE(beatLength)) {
735
- globalOffset = globalOffset.minus(beatLength);
736
- }
737
- else {
738
- return [cycleNum, [barIndex, beatIndex, instance], globalOffset, globalIndex];
739
- }
740
- }
741
- }
472
+ var scaleFactor = this.totalChildDuration.divby(oldChildDuration);
473
+ this._duration = this._duration.times(scaleFactor, true);
742
474
  }
743
- globalIndex += bar.totalBeatCount;
744
475
  }
745
- throw new Error("Should not be here!");
746
- };
747
- Cycle.prototype.iterateBeats = function (startBar, startBeat, startInstance) {
748
- var barIndex, beatIndex, instanceIndex, currBar;
749
- if (startBar === void 0) { startBar = 0; }
750
- if (startBeat === void 0) { startBeat = 0; }
751
- if (startInstance === void 0) { startInstance = 0; }
752
- return __generator(this, function (_a) {
753
- switch (_a.label) {
754
- case 0:
755
- barIndex = startBar;
756
- beatIndex = startBeat;
757
- instanceIndex = startInstance;
758
- _a.label = 1;
759
- case 1:
760
- if (!true) return [3, 3];
761
- currBar = this.bars[barIndex];
762
- return [4, [[barIndex, beatIndex, instanceIndex], currBar.beatLengths[beatIndex]]];
763
- case 2:
764
- _a.sent();
765
- instanceIndex++;
766
- if (!currBar.beatCounts[beatIndex] || instanceIndex >= currBar.beatCounts[beatIndex]) {
767
- instanceIndex = 0;
768
- beatIndex++;
769
- if (beatIndex >= currBar.beatLengths.length) {
770
- beatIndex = 0;
771
- barIndex++;
772
- if (barIndex >= this.bars.length) {
773
- barIndex = 0;
774
- }
775
- }
776
- }
777
- return [3, 1];
778
- case 3: return [2];
779
- }
780
- });
781
- };
782
- Cycle.prototype.copyTo = function (another) {
783
- _super.prototype.copyTo.call(this, another);
784
- another.name = this.name;
785
- another.bars = this.bars.map(function (x) { return x.clone(); });
476
+ return this;
786
477
  };
787
- Object.defineProperty(Cycle.prototype, "beatCount", {
788
- get: function () {
789
- var e_5, _a;
790
- var out = 0;
791
- try {
792
- for (var _b = __values(this.bars), _c = _b.next(); !_c.done; _c = _b.next()) {
793
- var bar = _c.value;
794
- out += bar.beatCount;
795
- }
796
- }
797
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
798
- finally {
799
- try {
800
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
801
- }
802
- finally { if (e_5) throw e_5.error; }
803
- }
804
- return out;
805
- },
806
- enumerable: false,
807
- configurable: true
808
- });
809
- Object.defineProperty(Cycle.prototype, "totalBeatCount", {
810
- get: function () {
811
- var e_6, _a;
812
- var out = 0;
813
- try {
814
- for (var _b = __values(this.bars), _c = _b.next(); !_c.done; _c = _b.next()) {
815
- var bar = _c.value;
816
- out += bar.totalBeatCount;
817
- }
818
- }
819
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
820
- finally {
821
- try {
822
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
823
- }
824
- finally { if (e_6) throw e_6.error; }
825
- }
826
- return out;
827
- },
828
- enumerable: false,
829
- configurable: true
830
- });
831
- Object.defineProperty(Cycle.prototype, "duration", {
832
- get: function () {
833
- return this.bars.reduce(function (x, y) { return x.plus(y.duration); }, ZERO);
834
- },
835
- enumerable: false,
836
- configurable: true
837
- });
838
- Cycle.DEFAULT = new Cycle({
839
- name: "Adi Thalam",
840
- bars: [
841
- new Bar({ name: "Laghu", beatLengths: [1, 1, 1, 1] }),
842
- new Bar({ name: "Dhrutam", beatLengths: [1, 1] }),
843
- new Bar({ name: "Dhrutam", beatLengths: [1, 1] }),
844
- ],
845
- });
846
- return Cycle;
847
- }(TimedEntity));
848
- export { Cycle };
478
+ return Group;
479
+ }(Atom));
480
+ export { Group };
849
481
  var Line = (function (_super) {
850
482
  __extends(Line, _super);
851
483
  function Line() {
@@ -857,7 +489,7 @@ var Line = (function (_super) {
857
489
  }
858
490
  Object.defineProperty(Line.prototype, "isEmpty", {
859
491
  get: function () {
860
- var e_7, _a;
492
+ var e_3, _a;
861
493
  try {
862
494
  for (var _b = __values(this.roles), _c = _b.next(); !_c.done; _c = _b.next()) {
863
495
  var r = _c.value;
@@ -865,12 +497,12 @@ var Line = (function (_super) {
865
497
  return false;
866
498
  }
867
499
  }
868
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
500
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
869
501
  finally {
870
502
  try {
871
503
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
872
504
  }
873
- finally { if (e_7) throw e_7.error; }
505
+ finally { if (e_3) throw e_3.error; }
874
506
  }
875
507
  return true;
876
508
  },
@@ -909,7 +541,7 @@ var Line = (function (_super) {
909
541
  };
910
542
  Object.defineProperty(Line.prototype, "duration", {
911
543
  get: function () {
912
- var e_8, _a;
544
+ var e_4, _a;
913
545
  var max = ZERO;
914
546
  try {
915
547
  for (var _b = __values(this.roles), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -917,12 +549,12 @@ var Line = (function (_super) {
917
549
  max = TSU.Num.Fraction.max(role.duration, max);
918
550
  }
919
551
  }
920
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
552
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
921
553
  finally {
922
554
  try {
923
555
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
924
556
  }
925
- finally { if (e_8) throw e_8.error; }
557
+ finally { if (e_4) throw e_4.error; }
926
558
  }
927
559
  return max;
928
560
  },
@@ -953,15 +585,15 @@ var Role = (function (_super) {
953
585
  return { name: this.name, atoms: this.atoms.map(function (a) { return a.debugValue(); }) };
954
586
  };
955
587
  Role.prototype.addAtoms = function () {
956
- var e_9, _a;
588
+ var e_5, _a;
957
589
  var atoms = [];
958
590
  for (var _i = 0; _i < arguments.length; _i++) {
959
591
  atoms[_i] = arguments[_i];
960
592
  }
961
593
  var last = null;
962
594
  try {
963
- for (var atoms_2 = __values(atoms), atoms_2_1 = atoms_2.next(); !atoms_2_1.done; atoms_2_1 = atoms_2.next()) {
964
- var atom = atoms_2_1.value;
595
+ for (var atoms_3 = __values(atoms), atoms_3_1 = atoms_3.next(); !atoms_3_1.done; atoms_3_1 = atoms_3.next()) {
596
+ var atom = atoms_3_1.value;
965
597
  if (atom.type == AtomType.REST) {
966
598
  if (last && last.type != AtomType.GROUP && last.type != AtomType.LABEL) {
967
599
  last.beforeRest = true;
@@ -973,12 +605,12 @@ var Role = (function (_super) {
973
605
  last = atom;
974
606
  }
975
607
  }
976
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
608
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
977
609
  finally {
978
610
  try {
979
- if (atoms_2_1 && !atoms_2_1.done && (_a = atoms_2.return)) _a.call(atoms_2);
611
+ if (atoms_3_1 && !atoms_3_1.done && (_a = atoms_3.return)) _a.call(atoms_3);
980
612
  }
981
- finally { if (e_9) throw e_9.error; }
613
+ finally { if (e_5) throw e_5.error; }
982
614
  }
983
615
  };
984
616
  Role.prototype.copyTo = function (another) {