This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ExportJSON.plgx
433 lines (367 loc) · 15.6 KB
/
ExportJSON.plgx
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
{
Initialize "(){
//
// This Sibelius plugin writes a Sibelius score as a JSON file
//
// It basically copies the internal object model in Sibelius to
// a JSON approximation. There is *no* specification, but it
// should be relatively easy to see what this file does.
//
AddToPluginsMenu('Export JSON','Run');
}"
DEBUG "true"
Run "() {
score = Sibelius.ActiveScore;
if (DEBUG) {
path = '/Users/hans/Desktop/test.sib_json';
WriteScoreToPath(score, path);
} else {
file = Sibelius.SelectFileToSave(
'Export JSON',
score.FileName,
null,
'json',
'TEXT',
'JSON document');
if (file != null) {
path = file.NameWithExt;
WriteScoreToPath(score, path);
}
}
}"
WriteScoreToPath "(score, path) {
progress = CreateProgressCallback();
BeginProgress(score);
json = ScoreToJSON(score, progress);
MidProgress(score);
text = ShowJSON(json) & '\n\n';
Sibelius.CreateTextFile(path);
Sibelius.AppendTextFile(path, text, false);
EndProgress(score);
}"
CreateProgressCallback "() {
progress = CreateDictionary();
progress.SetMethod('Apply', Self, 'SetProgress');
return progress;
}"
BeginProgress "(score) {
Sibelius.CreateProgressDialog('Exporting JSON...', 0, score.SystemStaff.BarCount);
}"
MidProgress "(score) {
Sibelius.UpdateProgressDialog(score.SystemStaff.BarCount, 'Writing file...');
}"
EndProgress "(score) {
Sibelius.DestroyProgressDialog();
}"
SetProgress "(_, staffName, bar, numBars) {
// StaffName -> Bar -> NumBars -> ()
// Updates the global progress dialog with the current
// translation state.
Sibelius.UpdateProgressDialog(bar, 'Processing staff: ' & staffName);
}"
ScoreToJSON "(score, progress) {
// Score -> (StaffName -> Bar -> NumBars -> ()) -> ()
// Converts the given score to a JSON representation
// The progress callback is invoked repeatedly and may be used to report
// the current state of the translation to the user.
score2 = CreateJSONObject();
score2['title'] = ToJSONString(score.Title);
score2['composer'] = ToJSONString(score.Composer);
score2['information'] = ToJSONString(score.OtherInformation);
score2['transposing'] = ToJSONBool(score.TransposingScore);
score2['staffHeight'] = score.StaffHeight;
temp = CreateJSONArray();
ExportSystemStaff(temp, score.SystemStaff, progress);
score2['systemStaff'] = temp[0];
score2['staves'] = CreateJSONArray();
for each staff in score {
ExportStaff(score2.staves, staff, progress);
}
return score2;
}"
ExportSystemStaff "(dest, staff, progress) {
// JSONScore -> Staff -> ()
// Converts and sets the system staff in the given score
staff2 = CreateJSONObject();
staff2['bars'] = CreateJSONArray();
for each bar in staff {
// trace(' Exporting bar' & bar.BarNumber & ' out of ' & staff.BarCount & ' in ' & staff.InstrumentName);
if (progress != null) {
progress.Apply('System staff', bar.BarNumber, staff.BarCount);
}
ExportBar(staff2.bars, bar);
}
PushJSONArray(dest, staff2);
}"
ExportStaff "(dest, staff, progress) {
// [JSONStaff] -> Staff -> ()
// Appends the given staff to the given destination
staff2 = CreateJSONObject();
staff2['name'] = ToJSONString(staff.InstrumentName);
staff2['shortName'] = ToJSONString(staff.ShortInstrumentName);
staff2['bars'] = CreateJSONArray();
for each bar in staff {
// trace(' Exporting bar' & bar.BarNumber & ' out of ' & staff.BarCount & ' in ' & staff.InstrumentName);
if (progress != null) {
progress.Apply(staff.InstrumentName, bar.BarNumber, staff.BarCount);
}
ExportBar(staff2.bars, bar);
}
PushJSONArray(dest, staff2);
}"
ExportBar "(dest, bar) {
// [JSONBar] -> Bar -> ()
// Appends the given bar to the given destination
bar2 = CreateJSONObject();
bar2['number'] = bar.BarNumber;
bar2['elements'] = CreateJSONArray();
for each object in bar {
ExportBarObject(bar2.elements, object);
}
PushJSONArray(dest, bar2);
}"
ExportBarObject "(dest, object) {
// [JSONBarObject] -> BarObject -> ()
// Appends the given bar object to the given destination
object2 = CreateJSONObject();
object2['type'] = ToJSONString('unknown: ' & object.Type);
if (object.Type = 'NoteRest') {
object2['type'] = ToJSONString('chord');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['duration'] = object.Duration;
object2['doubleTremolos'] = object.Position;
object2['singleTremolos'] = object.SingleTremolos; // -1 for Z
object2['acciaccatura'] = ToJSONBool(object.IsAcciaccatura);
object2['appoggiatura'] = ToJSONBool(object.IsAppoggiatura);
object2['articulations'] = CreateJSONArray();
ExportArticulations(object2.articulations, object);
object2['notes'] = CreateJSONArray();
for each note in object {
ExportNote(object2.notes, note);
}
}
if (object.Type = 'Text') {
object2['type'] = ToJSONString('text');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['text'] = ToJSONString(object.Text);
if (object.StyleId = 'text.staff.expression') {
object2['style'] = ToJSONString('expression');
}
if (object.StyleId = 'text.staff.technique') {
object2['style'] = ToJSONString('technique');
}
}
if (object.Type = 'Clef') {
object2['type'] = ToJSONString('clef');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['style'] = ToJSONString(object.StyleId);
}
if (object.Type = 'Slur') {
object2['type'] = ToJSONString('slur');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['duration'] = object.Duration;
object2['style'] = ToJSONString(object.StyleId);
}
if (object.Type = 'CrescendoLine') {
object2['type'] = ToJSONString('cresc');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['duration'] = object.Duration;
object2['style'] = ToJSONString(object.StyleId);
}
if (object.Type = 'DiminuendoLine') {
object2['type'] = ToJSONString('dim');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['duration'] = object.Duration;
object2['style'] = ToJSONString(object.StyleId);
}
if (object.Type = 'KeySignature') {
object2['type'] = ToJSONString('key');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['major'] = ToJSONBool(object.Major);
object2['sharps'] = object.Sharps;
object2['isOpen'] = ToJSONBool(false);
}
if (object.Type = 'TimeSignature') {
object2['type'] = ToJSONString('time');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['value'] = CreateJSONArray();
object2.value[0] = object.Numerator;
object2.value[1] = object.Denominator;
if (object.Text = CommonTimeString) {
object2['common'] = ToJSONBool(true);
} else {
object2['common'] = ToJSONBool(false);
}
if (object.Text = AllaBreveTimeString) {
object2['allaBreve'] = ToJSONBool(true);
} else {
object2['allaBreve'] = ToJSONBool(false);
}
}
if (object.Type = 'BarLine') {
object2['type'] = ToJSONString('barLine');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
// TODO
}
if (object.Type = 'Tuplet') {
// Note: this is an annotation that the given voice is tupleted
// from this point on.
object2['type'] = ToJSONString('tuplet');
object2['voice'] = object.VoiceNumber;
object2['position'] = object.Position;
object2['duration'] = object.Duration;
object2['playedDuration'] = object.PlayedDuration; // TODO What is the difference?
object2['value'] = CreateJSONArray();
object2.value[0] = object.Left;
object2.value[1] = object.Right;
}
// Bar rests are simply empty element arrays
if (object.Type != 'BarRest') {
PushJSONArray(dest, object2);
}
}"
ExportArticulations "(dest, chord) {
// [JSONArticulation] -> Chord -> ()
// Appends the articulations of the given chord to the given destination
if (chord.GetArticulation(DownBowArtic)) {
PushJSONArray(dest, ToJSONString('upBow'));
}
if (chord.GetArticulation(UpBowArtic)) {
PushJSONArray(dest, ToJSONString('downBow'));
}
if (chord.GetArticulation(PlusArtic)) {
PushJSONArray(dest, ToJSONString('plus'));
}
if (chord.GetArticulation(HarmonicArtic)) {
PushJSONArray(dest, ToJSONString('harmonic'));
}
if (chord.GetArticulation(MarcatoArtic)) {
PushJSONArray(dest, ToJSONString('marcato'));
}
if (chord.GetArticulation(AccentArtic)) {
PushJSONArray(dest, ToJSONString('accent'));
}
if (chord.GetArticulation(TenutoArtic)) {
PushJSONArray(dest, ToJSONString('tenuto'));
}
if (chord.GetArticulation(WedgeArtic)) {
PushJSONArray(dest, ToJSONString('wedge'));
}
if (chord.GetArticulation(StaccatissimoArtic)) {
PushJSONArray(dest, ToJSONString('staccatissimo'));
}
if (chord.GetArticulation(StaccatoArtic)) {
PushJSONArray(dest, ToJSONString('staccato'));
}
}"
ExportNote "(dest, note) {
// [JSONNote] -> Note -> ()
// Appends a note to the given destination
note2 = CreateJSONObject();
note2['pitch'] = note.Pitch;
note2['accidental'] = note.Accidental;
note2['diatonicPitch'] = note.DiatonicPitch;
note2['tied'] = ToJSONBool(note.Tied);
note2['style'] = note.NoteStyle; // Number identifier by note style name, 0 for normal
PushJSONArray(dest, note2);
}"
CreateJSONObject "() {
// () -> JSON
// Creates an empty JSON object
x = CreateDictionary();
x._property:__type__ = 'object';
return x;
}"
CreateJSONArray "() {
// () -> JSON
// Creates an empty JSON array
x = CreateDictionary();
x._property:__type__ = 'array';
return x;
}"
PushJSONArray "(obj, x) {
// JSON -> Value -> ()
// Pushes an element to a JSON array
len = obj._propertyNames.Length - 1; // Ignore the __type__ field
obj['' & len] = x;
}"
IsJSONObject "(obj) {
// JSON -> Bool
return (obj._property:__type__ = 'object');
}"
IsJSONArray "(obj) {
// JSON -> Bool
return (obj._property:__type__ = 'array');
}"
ToJSONString "(x) {
// String -> JSON
return Chr(34) & x & Chr(34);
}"
ToJSONBool "(x) {
// Bool -> JSON
if (x = 0) {
return 'false';
} else {
return 'true';
}
}"
ShowJSON "(obj) {
// JSON -> String
str = '';
if (not IsObject(obj)) {
str = str & obj;
} else {
if (IsJSONObject(obj)) {
str = str & '{';
fst = true;
for i = 0 to obj._propertyNames.Length {
k = obj._propertyNames[i];
v = obj[k];
if (k != '__type__') {
if (not fst) {
str = str & ', ';
} else {
fst = false;
}
str = str & ShowJSON(ToJSONString(k));
str = str & ': ';
str = str & ShowJSON(v);
}
}
str = str & '}';
}
else {
if (IsJSONArray(obj)) {
str = str & '[';
fst = true;
for i = 0 to (obj._propertyNames.Length - 1) { // Ignore __type__
k = '' & i;
v = obj[k];
if (true) {
if (not fst) {
str = str & ', ';
} else {
fst = false;
}
str = str & ShowJSON(v);
}
}
str = str & ']';
}
else {
StopPlugin('ShowJSON: Could not convert object.');
}
}
}
return str;
}"
}