-
Notifications
You must be signed in to change notification settings - Fork 14
/
staffModifiers.ts
615 lines (594 loc) · 18.8 KB
/
staffModifiers.ts
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// [Smoosic](https://github.com/AaronDavidNewman/Smoosic)
// Copyright (c) Aaron David Newman 2021.
/**
* Classes to modify a staff or system. Some staff modifiers can span multiple
* staves, so it can be a little arbitrary what is a score vs. staff modifier. But
* generally, a staff modifier is anything that has a beginning and end {@link SmoSelector}
* @module /smo/data/staffModifiers
*/
import { smoSerialize } from '../../common/serializationHelpers';
import { SmoSelector } from '../xform/selections';
import { SmoNote } from './note';
import { SmoAttrs, SvgPoint, SmoObjectParams, Clef, SvgBox, SmoModifierBase } from './common';
const VF = eval('Vex.Flow');
/**
* Base class that mostly standardizes the interface and deals with serialization.
* @param ctor constructor for derived class
* @param logicalBox bounding box in SVG coordinates, if rendered
* @param attrs object identification
* @param startSelector where the modifier starts
* @param endSelector where it ends
* @category SmoModifier
* */
export abstract class StaffModifierBase implements SmoModifierBase {
attrs: SmoAttrs;
ctor: string;
associatedStaff: number = 0;
startSelector: SmoSelector = SmoSelector.default;
endSelector: SmoSelector = SmoSelector.default;
logicalBox: SvgBox | null = null;
element: SVGSVGElement | null = null;
constructor(ctor: string) {
this.ctor = ctor;
this.attrs = {
id: VF.Element.newID(),
type: ctor
};
}
static deserialize(params: SmoObjectParams) {
const ctor = eval('globalThis.Smo.' + params.ctor);
const fixInstrument = params as any;
if (fixInstrument.subFamily) {
fixInstrument.instrument = fixInstrument.subFamily;
}
const rv = new ctor(params);
return rv;
}
abstract serialize(): any;
}
export type SoundSustain = 'percussive' | 'sustained';
export type oscillatorType = 'sample' | 'sine' | 'sawtooth' | 'square' | 'triangle' | 'custom';
export type oscillatorOptions = 'plucked' | 'bowed' | 'muted' | 'accented' | 'frequency-sweep' | 'na';
export interface SmoOscillatorInfo {
waveform: oscillatorType,
sustain: SoundSustain,
realOvertones: number[],
imaginaryOvertones: number[],
sample: string | null,
family: string,
instrument: string,
nativeFrequency: number,
dynamic: number,
options: oscillatorOptions[],
minDuration: number,
maxDuration: number
}
export type SmoOscillatorInfoNumberType = 'minDuration' | 'maxDuration' | 'dynamic' | 'nativeFrequency';
export type SmoOscillatorInfoNumberArType = 'realOvertones' | 'imaginaryOvertones';
export type SmoOscillatorInfoStringType = 'family';
export type SmoOscillatorInfoStringNullType = 'sample';
export type SmoOscillatorInfoWaveformType = 'waveform';
export type SmoOscillatorInfoSustainType = 'sustain';
export type SmoOscillatorInfoOptionsType = 'options';
export const SmoOscillatorInfoAllTypes = ['minDuration','maxDuration', 'dynamic', 'nativeFrequency', 'realOvertones', 'imaginaryOvertones', 'sample', 'family',
'waveform', 'sustain', 'options', 'instrument'];
export type SmoOscillatorAnyType = SmoOscillatorInfoNumberType | SmoOscillatorInfoNumberArType | SmoOscillatorInfoStringType | SmoOscillatorInfoStringNullType
| oscillatorType | SoundSustain;
/**
* Define an instrument. An instrument is associated with a part, but a part can have instrument changes
* and thus contain multiple instruments at different points in the score.
* Not all of these parameters are fully utilized yet, and there are plans to greatly expand what
* an SmoInstrument is. Note I may move this to PartInfo module.
* @category SmoParameters
*/
export interface SmoInstrumentParams {
startSelector: SmoSelector,
endSelector: SmoSelector,
instrumentName: string,
family: string,
instrument: string,
abbreviation: string,
keyOffset: number,
midichannel: number,
midiport: number,
clef: Clef,
mutes?: string,
}
export type SmoInstrumentNumParamType = 'keyOffset' | 'midichannel' | 'midiport';
export const SmoInstrumentNumParams: SmoInstrumentNumParamType[] = ['keyOffset', 'midichannel', 'midiport'];
export type SmoInstrumentStringParamType = 'instrumentName' | 'abbreviation' | 'family' | 'instrument';
export const SmoInstrumentStringParams: SmoInstrumentStringParamType[] = ['instrumentName', 'abbreviation', 'family', 'instrument'];
/**
* Define an instrument. An instrument is associated with a part, but a part can have instrument changes
* and thus contain multiple instruments at different points in the score.
* Not all of these parameters are fully utilized yet, and there are plans to greatly expand what
* an SmoInstrument is. Note I may move this to PartInfo module.
* @category SmoModifier
*/
export class SmoInstrument extends StaffModifierBase {
static get attributes() {
return ['startSelector', 'endSelector', 'keyOffset', 'midichannel', 'midiport', 'instrumentName', 'abbreviation', 'instrument', 'family'];
}
startSelector: SmoSelector;
endSelector: SmoSelector;
instrumentName: string = '';
abbreviation: string = '';
keyOffset: number = 0;
clef: Clef = 'treble';
midichannel: number;
midiport: number;
family: string;
instrument: string;
oscillators: SmoOscillatorInfo[] = [];
articulation?: string;
mutes?: string;
static get defaults(): SmoInstrumentParams {
return JSON.parse(JSON.stringify({
clef: 'treble',
keyOffset: 0,
instrumentName: '',
abbreviation: '',
family: 'keyboard',
instrument: 'piano',
midichannel: 0,
midiport: 0,
startSelector: SmoSelector.default,
endSelector: SmoSelector.default
}));
}
static get defaultOscillatorParam(): SmoOscillatorInfo {
return JSON.parse(JSON.stringify({
waveform: 'sample',
sustain: 'percussive',
realOvertones: [],
imaginaryOvertones: [],
sample: null,
family: 'none',
instrument: 'none',
nativeFrequency: 440,
dynamic: 100,
options: [],
minDuration: 0,
maxDuration: 0
}));
}
constructor(params: SmoInstrumentParams) {
super('SmoInstrument');
let name = '';
if (typeof ((params as any).instrument) === 'undefined') {
name = params.instrumentName;
} else {
name = (params as any).instrument;
}
this.instrumentName = name;
this.family = params.family;
this.instrument = params.instrument;
this.keyOffset = params.keyOffset;
this.clef = params.clef;
this.midiport = params.midiport;
this.midichannel = params.midichannel;
this.startSelector = params.startSelector;
this.endSelector = params.endSelector;
}
serialize() {
const params: any = {};
smoSerialize.serializedMergeNonDefault(SmoInstrument.defaults, SmoInstrument.attributes, this, params);
params.ctor = 'SmoInstrument';
return params;
}
eq(other: SmoInstrument): boolean {
let rv = true;
SmoInstrumentNumParams.forEach((param) => {
if (other[param] !== this[param]) {
rv = false;
}
});
SmoInstrumentStringParams.forEach((param) => {
if (other[param] !== this[param]) {
rv = false;
}
});
return rv;
}
}
export interface SmoInstrumentMeasure {
measureIndex: number,
instrument: SmoInstrumentParams;
}
/**
* constructor params for {@link SmoStaffHairpin}
* @category SmoParameters
*/
export interface SmoStaffHairpinParams {
xOffsetLeft: number,
xOffsetRight: number,
yOffset: number,
height: number,
position: number,
hairpinType: number,
startSelector: SmoSelector,
endSelector: SmoSelector
}
/**
* Also called crescendo etc.
* @category SmoModifier
*/
export class SmoStaffHairpin extends StaffModifierBase {
static get editableAttributes() {
return ['xOffsetLeft', 'xOffsetRight', 'yOffset', 'height'];
}
static get defaults(): SmoStaffHairpinParams {
return JSON.parse(JSON.stringify({
xOffsetLeft: -2,
xOffsetRight: 0,
yOffset: -50,
height: 10,
position: SmoStaffHairpin.positions.BELOW,
hairpinType: SmoStaffHairpin.types.CRESCENDO,
startSelector: SmoSelector.default,
endSelector: SmoSelector.default
}));
}
static get positions() {
// matches VF.modifier
return {
LEFT: 1,
RIGHT: 2,
ABOVE: 3,
BELOW: 4,
};
}
static get types() {
return {
CRESCENDO: 1,
DECRESCENDO: 2
};
}
static get attributes() {
return ['position', 'startSelector', 'endSelector', 'xOffsetLeft',
'xOffsetRight', 'yOffset', 'hairpinType', 'height'];
}
xOffsetLeft: number = -2;
xOffsetRight: number = 0;
yOffset: number = -50;
height: number = 10;
position: number = SmoStaffHairpin.positions.BELOW;
hairpinType: number = SmoStaffHairpin.types.CRESCENDO;
startSelector: SmoSelector = SmoSelector.default;
endSelector: SmoSelector = SmoSelector.default;
serialize() {
const params: any = {};
smoSerialize.serializedMergeNonDefault(SmoStaffHairpin.defaults, SmoStaffHairpin.attributes, this, params);
params.ctor = 'SmoStaffHairpin';
return params;
}
constructor(params: SmoStaffHairpinParams) {
super('SmoStaffHairpin');
Vex.Merge(this, SmoStaffHairpin.defaults);
smoSerialize.filteredMerge(SmoStaffHairpin.attributes, params, this);
// If start/end selector on same note, make sure the hairpin extends
if (SmoSelector.eq(this.startSelector, this.endSelector)) {
if (this.xOffsetRight === SmoStaffHairpin.defaults.xOffsetRight
&& this.xOffsetLeft === SmoStaffHairpin.defaults.xOffsetLeft) {
this.xOffsetLeft = -10;
this.xOffsetRight = 10;
}
}
if (!this.attrs) {
this.attrs = {
id: VF.Element.newID(),
type: 'SmoStaffHairpin'
};
}
}
}
/**
* constructor params for {@link SmoStaffHairpin}
* @category SmoParameters
*/
export interface SmoStaffTextBracketParams {
line: number,
position: number,
text: string,
superscript: string,
startSelector: SmoSelector,
endSelector: SmoSelector
}
export type SmoTextBracketStringType = 'text' | 'superscript';
export const SmoTextBracketStringTypes: SmoTextBracketStringType[] = ['text', 'superscript'];
export type SmoTextBracketNumberType = 'line' | 'position';
export const SmoTextBracketNumberTypes: SmoTextBracketNumberType[] = ['line', 'position'];
/**
* Text like 8va, rit. that is bracketed on a system
* @category SmoModifier
*/
export class SmoStaffTextBracket extends StaffModifierBase {
static RITARD = 'ritard';
static ACCEL = 'accelerando';
static CRESCENDO = 'crescendo';
static DIMENUENDO = 'diminuendo';
static OCTAVEUP = '8va';
static OCTAVEDOWN = '8vb';
static OCTAVEUP2 = '15va';
static OCTAVE2DOWN = '15vb';
static get defaults(): SmoStaffTextBracketParams {
return JSON.parse(JSON.stringify({
line: 1,
position: SmoStaffTextBracket.positions.BOTTOM,
text: '',
superscript: '',
startSelector: SmoSelector.default,
endSelector: SmoSelector.default
}));
}
static get positions() {
// matches VF.modifier
return {
TOP: 1,
BOTTOM: -1
};
}
static get attributes() {
return ['startSelector', 'endSelector', 'line', 'position', 'text', 'superscript'];
}
position: number = SmoStaffTextBracket.positions.BOTTOM;
text: string = '';
superscript: string = '';
line: number = 1;
startSelector: SmoSelector = SmoSelector.default;
endSelector: SmoSelector = SmoSelector.default;
serialize() {
const params: any = {};
smoSerialize.serializedMergeNonDefault(SmoStaffTextBracket.defaults, SmoStaffTextBracket.attributes, this, params);
params.ctor = 'SmoStaffTextBracket';
return params;
}
constructor(params: SmoStaffTextBracketParams) {
super('SmoStaffTextBracket');
smoSerialize.serializedMerge(SmoStaffTextBracket.attributes, SmoStaffTextBracket.defaults, this);
smoSerialize.serializedMerge(SmoStaffTextBracket.attributes, params, this);
this.startSelector = JSON.parse(JSON.stringify(params.startSelector));
this.endSelector = JSON.parse(JSON.stringify(params.endSelector));
if (!this.attrs) {
this.attrs = {
id: VF.Element.newID(),
type: 'SmoStaffTextBracket'
};
}
}
}
export type SlurNumberParam = 'spacing' | 'thickness' | 'xOffset' | 'yOffset' | 'position' |
'position_end' | 'cp1x' | 'cp1y' | 'cp2x' | 'cp2y';
export const SlurNumberParams: SlurNumberParam[] = ['spacing', 'thickness', 'xOffset', 'yOffset', 'position',
'position_end', 'cp1x', 'cp1y', 'cp2x', 'cp2y'];
/**
* parameters for a slur
* @param spacing between note and curve
* @param thickness thickness of the line
* @param xOffset in pixels
* @param yOffset in pixels
* @param position top or bottom of the chord we are attached to
* @param position_end top or bottom of the chord we are attached to
* @param invert turns that frown upside down
* @param cp1x bz control point
* @param cp1y bz control point
* @param cp2x bz control point
* @param cp2y bz control point
* @param startSelector the start note we are attached to
* @param endSelector the end note we are attached to
* @category SmoParameters
*/
export interface SmoSlurParams {
spacing: number,
thickness: number,
xOffset: number,
yOffset: number,
position: number,
position_end: number,
orientation: number,
invert: boolean,
cp1x: number,
cp1y: number,
cp2x: number,
cp2y: number,
startSelector: SmoSelector,
endSelector: SmoSelector
}
/**
* Defines a slur
* @category SmoModifier
*/
export class SmoSlur extends StaffModifierBase {
static get defaults(): SmoSlurParams {
return JSON.parse(JSON.stringify({
spacing: 2,
thickness: 2,
xOffset: 5,
yOffset: 0,
position: SmoSlur.positions.TOP,
position_end: SmoSlur.positions.TOP,
orientation: SmoSlur.orientations.AUTO,
invert: false,
cp1x: 0,
cp1y: 15,
cp2x: 0,
cp2y: 15,
startSelector: SmoSelector.default,
endSelector: SmoSelector.default
}));
}
// matches VF curve
static get positions() {
return {
HEAD: 1,
TOP: 2,
ABOVE: 3,
BELOW: 4,
AUTO: 5
};
}
static get orientations() {
return {
AUTO: 0,
UP: 1,
DOWN: 2
};
}
static get parameterArray() {
return ['startSelector', 'endSelector', 'spacing', 'xOffset', 'yOffset', 'position', 'position_end', 'invert',
'orientation', 'cp1x', 'cp1y', 'cp2x', 'cp2y', 'thickness', 'pitchesStart', 'pitchesEnd'];
}
spacing: number = 2;
thickness: number = 2;
xOffset: number = -5;
yOffset: number = 10;
position: number = SmoSlur.positions.TOP;
position_end: number = SmoSlur.positions.TOP;
orientation: number = SmoSlur.orientations.AUTO;
invert: boolean = false;
cp1x: number = 0;
cp1y: number = 15;
cp2x: number = 0;
cp2y: number = 15;
startSelector: SmoSelector = SmoSelector.default;
endSelector: SmoSelector = SmoSelector.default;
serialize(): any {
const params: any = {};
smoSerialize.serializedMergeNonDefault(SmoSlur.defaults,
SmoSlur.parameterArray, this, params);
params.ctor = 'SmoSlur';
return params;
}
get controlPoints(): SvgPoint[] {
const ar: SvgPoint[] = [{
x: this.cp1x,
y: this.cp1y
}, {
x: this.cp2x,
y: this.cp2y
}];
return ar;
}
constructor(params: SmoSlurParams) {
super('SmoSlur');
smoSerialize.serializedMerge(SmoSlur.parameterArray, SmoSlur.defaults, this);
smoSerialize.serializedMerge(SmoSlur.parameterArray, params, this);
this.startSelector = params.startSelector;
this.endSelector = params.endSelector;
// Fix some earlier serialization error.
if (!this.attrs) {
this.attrs = {
id: VF.Element.newID(),
type: 'SmoSlur'
};
}
}
}
/**
* Map pitch indices of the tie line
*/
export interface TieLine {
from: number,
to: number
}
/**
* Constructor parameters for a tie.
* @category SmoParameters
*/
export interface SmoTieParams {
tie_spacing: number,
cp1: number,
cp2: number,
first_x_shift: number,
last_x_shift: number,
y_shift: number,
lines: TieLine[],
startSelector: SmoSelector | null,
endSelector: SmoSelector | null
}
/**
* Like slur, but multiple pitches.
* @category SmoModifier
*/
export class SmoTie extends StaffModifierBase {
invert: boolean = false;
cp1: number = 8;
cp2: number = 12;
first_x_shift: number = 0;
last_x_shift: number = 0;
y_shift: number = 7;
tie_spacing: number = 0;
lines: TieLine[] = [];
startSelector: SmoSelector = SmoSelector.default;
endSelector: SmoSelector = SmoSelector.default;
static get defaults(): SmoTieParams {
return JSON.parse(JSON.stringify({
invert: false,
cp1: 8,
cp2: 12,
y_shift: 7,
first_x_shift: 0,
last_x_shift: 0,
lines: [],
startSelector: SmoSelector.default,
endSelector: SmoSelector.default
}));
}
static get parameterArray() {
return ['startSelector', 'endSelector', 'invert', 'lines', 'y_shift', 'tie_spacing', 'cp1', 'cp2', 'first_x_shift', 'last_x_shift'];
}
static get vexParameters() {
return ['cp1', 'cp2', 'first_x_shift', 'last_x_shift'];
}
static isTie(modifier: SmoTie | SmoModifierBase): modifier is SmoTie {
return modifier.ctor === 'SmoTie';
}
static createLines(fromNote: SmoNote, toNote: SmoNote): TieLine[] {
const maxPitches = Math.max(fromNote.pitches.length, toNote.pitches.length);
let i = 0;
const lines: TieLine[] = [];
// By default, just tie all the pitches to all the other pitches in order
for (i = 0; i < maxPitches; ++i) {
const from = i < fromNote.pitches.length ? i : fromNote.pitches.length - 1;
const to = i < toNote.pitches.length ? i : toNote.pitches.length - 1;
lines.push({ from, to });
}
return lines;
}
get vexOptions() {
const rv: any = {};
rv.direction = this.invert ? VF.Stem.DOWN : VF.Stem.UP;
SmoTie.vexParameters.forEach((p) => {
rv[p] = (this as any)[p];
});
return rv;
}
serialize() {
const params: any = {};
smoSerialize.serializedMergeNonDefault(SmoTie.defaults,
SmoTie.parameterArray, this, params);
params.ctor = 'SmoTie';
return params;
}
// ### checkLines
// If the note chords have changed, the lines may no longer be valid so update them
checkLines(fromNote: SmoNote, toNote: SmoNote) {
const maxTo = this.lines.map((ll) => ll.to).reduce((a, b) => a > b ? a : b);
const maxFrom = this.lines.map((ll) => ll.from).reduce((a, b) => a > b ? a : b);
if (maxTo < toNote.pitches.length && maxFrom < fromNote.pitches.length) {
return;
}
this.lines = SmoTie.createLines(fromNote, toNote);
}
constructor(params: SmoTieParams) {
super('SmoTie');
smoSerialize.serializedMerge(SmoTie.parameterArray, SmoTie.defaults, this);
smoSerialize.serializedMerge(SmoTie.parameterArray, params, this);
if (!this.attrs) {
this.attrs = {
id: VF.Element.newID(),
type: 'SmoTie'
};
}
}
}