-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMuseScore.ts
More file actions
198 lines (184 loc) · 7.64 KB
/
MuseScore.ts
File metadata and controls
198 lines (184 loc) · 7.64 KB
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
import { notUndefined, pick, sum } from './utils';
type DurationType =
| 'whole'
| 'half'
| '1/2'
| 'quarter'
| '1/4'
| 'eighth'
| '3/4'
| '1/6'
| '1/8'
| '3/8'
| '1/12'
| '16th'
| '1/16'
| '32nd'
| '1/32'
| '64th'
| '1/64';
export const durationTypeToSec = (bpm: number, durationType: DurationType) => {
const beatSecond = 4 * 60 / bpm;
if (durationType === 'whole') return beatSecond;
if (durationType === 'half') return beatSecond / 2;
if (durationType === '1/2') return beatSecond / 2;
if (durationType === 'quarter') return beatSecond / 4;
if (durationType === '1/4') return beatSecond / 4;
if (durationType === 'eighth') return beatSecond / 8;
if (durationType === '3/4') return beatSecond * 3 / 4;
if (durationType === '1/6') return beatSecond / 6;
if (durationType === '1/8') return beatSecond / 8;
if (durationType === '3/8') return beatSecond * 3 / 8;
if (durationType === '1/12') return beatSecond / 12;
if (durationType === '16th') return beatSecond / 16;
if (durationType === '1/16') return beatSecond / 16;
if (durationType === '32nd') return beatSecond / 32;
if (durationType === '1/32') return beatSecond / 32;
if (durationType === '64th') return beatSecond / 64;
if (durationType === '1/64') return beatSecond / 64;
console.warn(`Unknown durationType: ${durationType}`);
return 0;
};
const pitchNumberToHz = (pitch: number) => 440 * Math.pow(2, (pitch - 69) / 12);
const dotsToMultiple = (dots: number) => sum(
Array.from({ length: dots + 1 }, (_, i) => (1 / 2) ** i)
);
type PlayCommand = {
type: 'play';
hz: number;
second: number;
};
type PauseCommand = {
type: 'pause';
second: number;
};
type BpmChangeCommand = {
type: 'bpmChange';
bpm: number;
};
type Command = PlayCommand | PauseCommand | BpmChangeCommand;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const parseMuseScoreScoreIntoPartCommands = (score: any): Command[][] => {
const staffs = pick(score, 'Staff');
const allCommands = staffs.map(staff => {
const elements = pick(staff, 'Measure').map(measure => pick(measure, 'voice')).flat().flat();
const commands = elements.map(element => {
if (element.Tempo) {
const bpm = pick(
pick(element.Tempo, 'tempo')[0], '#text'
)[0] * 60;
return {
type: 'bpmChange' as const,
bpm,
};
}
if (element.Chord) {
const durationType = pick(
pick(element.Chord, 'durationType')[0], '#text'
)[0];
const dotsElement = pick(element.Chord, 'dots');
const dots = dotsElement.length > 0 ? pick(dotsElement[0], '#text')[0] : 0;
const note = pick(element.Chord, 'Note')[0];
const pitch = pick(
pick(note, 'pitch')[0], '#text'
)[0];
const duration = durationTypeToSec(1, durationType) * dotsToMultiple(dots);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const tie = note.map((e: any) =>
e.Spanner && e[':@']['@_type'] === 'Tie'
? e.Spanner
: undefined
).filter(notUndefined)[0];
if (tie) {
if (pick(tie, 'next')[0]) {
// tie の前半なので、後半のぶんまで鳴らす (duration を増やす)
const next = pick(tie, 'next')[0];
const location = pick(next, 'location')[0];
const measures = pick(
pick(location, 'measures')[0] || [], '#text'
)[0];
const fractions = pick(
pick(location, 'fractions')[0] || [], '#text'
)[0];
const nextDuration = ((ms: number | undefined, fr: string | undefined) => {
if (ms === 1) {
if (fr === '-1/2') return durationTypeToSec(1, '1/2');
if (fr === '-3/4') return durationTypeToSec(1, '1/4');
if (fr === '-5/6') return durationTypeToSec(1, '1/6');
if (fr === '-7/8') return durationTypeToSec(1, '1/8');
if (fr === '-11/12') return durationTypeToSec(1, '1/12');
if (fr === undefined) return durationTypeToSec(1, 'whole');
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (ms === undefined) return durationTypeToSec(1, fr as any);
console.error('Unknown fractions for "next" tie: ', ms, fr);
return 0;
})(measures, fractions);
return [
{
type: 'play' as const,
hz: pitchNumberToHz(pitch),
second: duration + nextDuration,
},
{
type: 'pause' as const,
second: duration + nextDuration,
},
];
}
if (pick(tie, 'prev')[0]) {
// tie の後半なので、音を鳴らさない
return [];
}
} else {
return [
{
type: 'play' as const,
hz: pitchNumberToHz(pitch),
second: duration,
},
{
type: 'pause' as const,
second: duration,
},
];
}
}
if (element.Rest) {
const durationType = pick(
pick(element.Rest, 'durationType')[0], '#text'
)[0];
if (durationType === 'measure') {
const durationText = pick(
pick(element.Rest, 'duration')[0], '#text'
)[0];
if (durationText === '4/4') {
return {
type: 'pause' as const,
second: durationTypeToSec(1, 'whole'),
};
} else if (durationText === '2/4') {
return {
type: 'pause' as const,
second: durationTypeToSec(1, 'half'),
};
} else {
console.error('Unknown durationText for "while" rest: ', durationText);
return {
type: 'pause' as const,
second: durationTypeToSec(1, 'whole'),
};
}
} else {
const duration = durationTypeToSec(1, durationType);
return {
type: 'pause' as const,
second: duration,
};
}
}
}).flat().filter(notUndefined);
return commands;
});
return allCommands;
};