-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmicrotune.js
463 lines (413 loc) · 15.7 KB
/
microtune.js
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
//=============================================================================
// MuseScore
// Music Score Editor
//
// " M I C R O T U N E " plugin
//
// Manages and applies micro-tonal tunings.
// Version 1.0 - Date 14Aug2014
//
// By Maurizio M. Gavioli, 2010.
// By Joachim Schmitz, 2012-2014.
//
// MuseScore: Copyright (C)2008 Werner Schweer and others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================
// Global vars
// The g_presets array contains the data for each preset.
// Each item of this array is itself an array with 27 items:
// items [0] to [26] correspond to the 27 accidentals of MuseScore
// (accidentals 0 - 5 are no micro-intervals (none, #, b, ##, bb and natural),
// accidentals 25 (Sori) and 26 (Koron) are hardcoded to 50 resp. -50)
// item ["Name"] is a human-readable name of the preset.
var g_presets = [];
var g_numOfPresets = 0;
var g_defaultPreset = [0, 0, 0, 0, 0, 0, -50, 0, -150, -50, 0, -150, 50, 0, 0, 150, -50, -150, 0, 50, -50, 0, 150, 50, 0, 50, -50];
var g_szDefaultPresetName = "default";
var g_form;
var g_bDirty = false;
var g_szExportPath;
var g_szImportPath;
var g_szOrgName = "MusE"
var g_szAppName = "pluginMicrotune";
var g_szPathsSect = "Paths";
var g_szExportPathKey = g_szPathsSect + "/ExportPath";
var g_szImportPathKey = g_szPathsSect + "/ImportPath";
var g_szPresetsSect = "Presets";
var g_szCurrPresetKey = g_szPresetsSect + "/CurrentPreset";
var g_szNumOfPresetsKey = g_szPresetsSect + "/NumOfPresets";
var g_szPresetKey = g_szPresetsSect + "/Preset";
var g_szPresetNameKey = g_szPresetsSect + "/PresetName";
//---------------------------------------------------------
// init()
// this function will be called on startup of mscore
//---------------------------------------------------------
function init()
{
}
//-------------------------------------------------------------------
// run()
// this function will be called when activating the plugin menu entry
//-------------------------------------------------------------------
function run()
{ // create the UI
var loader = new QUiLoader(null);
var dir = new QDir("" + pluginPath + "/res");
loader.setWorkingDirectory(dir);
var file = new QFile(pluginPath + "/microtune.ui");
file.open(QIODevice.OpenMode(QIODevice.ReadOnly, QIODevice.Text));
g_form = loader.load(file, null);
g_form.comboPresets["currentIndexChanged(int)"].connect(setValuesFromPreset);
g_form.pushUpdate.clicked.connect(updatePreset);
g_form.pushRename.clicked.connect(renamePreset);
g_form.pushAdd.clicked.connect(addPreset);
g_form.pushDelete.clicked.connect(deletePreset);
g_form.pushExport.clicked.connect(exportPresets);
g_form.pushImport.clicked.connect(importPresets);
g_form.pushOk.clicked.connect(applyValues);
g_form.pushCancel.clicked.connect(dlgDone);
g_form.addTo.toggled.connect(toggleAdd);
// load configuration and update dlg controls
loadIni(null, true);
setValuesFromPreset(g_form.comboPresets.currentIndex);
g_form.show(); // show the dlg
}
//---------------------------------------------------------
// applyValues()
// called when user presses the "Apply" button
// applies the values currently in the dlg to the score
// or the selection
//---------------------------------------------------------
function applyValues()
{ // no score open (MuseScore 2.0+, can't happen earlier)
if (typeof curScore === 'undefined')
return
var idx = g_form.comboPresets.currentIndex;
var preset = g_presets[idx];
// for each note of each chord of each part of each staff
var cursor = new Cursor(curScore);
cursor.goToSelectionStart();
var startStaff = cursor.staff;
cursor.goToSelectionEnd();
var endStaff = cursor.staff;
var endTick = cursor.tick(); // if no selection, go to end of score
if (cursor.eos()) // if no selection
{ startStaff = 0; // start with 1st staff
endStaff = curScore.staves; // and end with last
}
curScore.startUndo();
for (var staff = startStaff; staff < endStaff; ++staff)
{ for (var voice = 0; voice < 4; voice++)
{ cursor.goToSelectionStart();
cursor.staff = staff;
cursor.voice = voice;
// if no selection, start at beginning of score
if (cursor.eos())
cursor.rewind();
while (cursor.tick() < endTick)
{ if (cursor.isChord())
{ for (var chordnote = 0; chordnote < cursor.chord().notes; chordnote++)
{ var note = cursor.chord().note(chordnote);
idx = note.userAccidental;
if (idx < preset.length)
if (g_Form.addTo.checked)
note.tuning += preset[idx];
else
note.tuning = preset[idx];
}
}
cursor.next();
}
}
}
curScore.endUndo();
saveIni(null, true); // save data
g_form.accept();
}
function toggleAdd(checked)
{ g_form.addTo.checked = checked;
}
function dlgDone()
{ saveIni(null, true); // save data
g_form.reject();
}
//---------------------------------------------------------
// updatePreset()
// called when user presses the "Update" button: updates the preset
// currently selected in the combo with the values in the dlg.
//---------------------------------------------------------
function updatePreset()
{ //get selected preset and pick the right item in g_presets array
var preset = g_presets[g_form.comboPresets.currentIndex];
for (var step=6; step < 25; step++)
preset[step] = parseInt( g_form["e"+step].text );
g_bDirty = true;
}
//---------------------------------------------------------
// renamePreset()
// called when user presses the "Rename" button
// Allows to rename the currently selected preset.
//---------------------------------------------------------
function renamePreset()
{ //get selected preset and pick the right item in g_presets array
var idx = g_form.comboPresets.currentIndex;
var preset = g_presets[idx];
// ask the user for a new name of the preset
var name = QInputDialog.getText(g_form, "New preset name",
"Enter a new name for the preset:", QLineEdit.Normal,
preset["Name"], 0);
// if returned string is not empty, update preset name
if (name != null)
{ preset["Name"] = name; // update name in internal data
g_form.comboPresets.setItemText(idx, preset["Name"]);
g_bDirty = true;
}
}
//---------------------------------------------------------
// addPreset()
// called when user presses the "Add" button
// Adds a new presets with the values currently in the dlg.
//---------------------------------------------------------
function addPreset()
{ var idx;
var preset = [];
// init the new preset to the values currently in the dlg
for (idx=0; idx < 6; idx++) // none, #, b, ##, bb and natural
preset[idx] = 0;
for (idx=6; idx < 25; idx++)
preset[idx] = parseInt(g_form["e"+idx].text);
preset[25] = 50; // Sori
preset[26] = -50; // Koron
// ask the user for a name of the new preset
preset["Name"] = QInputDialog.getText(g_form, "New preset",
"Enter a name for the new preset:", QLineEdit.Normal,
"[new preset]", 0);
// if returned string is not empty, add the new preset
if (preset["Name"] != null)
{ g_presets[g_numOfPresets] = preset; // add to internal data
g_numOfPresets++;
g_form.comboPresets.addItem(preset["Name"]);// add to combo box
g_form.comboPresets.setCurrentIndex(g_numOfPresets-1);
g_bDirty = true;
}
}
//---------------------------------------------------------
// deletePreset()
// called when user presses the "Delete" button:
// removes the selected preset from the combo box and from the internal data.
// Data are saved.
//---------------------------------------------------------
function deletePreset()
{ // get selected temperament and ask the user for a confirmation
var idx = g_form.comboPresets.currentIndex;
// using custom buttons to show "Yes" / "No" does not seem to work!
if (QMessageBox.question(g_form, "Delete preset",
"\"" + g_presets[idx]["Name"] +
"\" will be deleted permanently\nProceed? (press ESC to abort)") != QMessageBox.Ok)
return;
// remove item from combo box
g_form.comboPresets.removeItem(idx);
// remove item from internal data, shifting all 'next' presets 'down' one slot
for (var preset = idx; preset < g_numOfPresets-1; preset++)
g_presets[preset] = g_presets[preset+1];
g_presets.pop(); // remove last temperament
g_numOfPresets--;
g_bDirty = true;
}
//---------------------------------------------------------
// exportPresets() / importPresets()
// called when user presses the "Export" / "Import" button:
// exports the presets in / imports presets from an external file.
//---------------------------------------------------------
function exportPresets()
{ // open a file selection dlg
var fName = QFileDialog.getSaveFileName(g_form, "Select a data file to create",
g_szExportPath, "DAT file (*.dat)", 0);
if (fName == null)
return;
saveIni(fName, false); // save data, but not the preferences
// store last export path
var idx = fName.lastIndexOf("/");
if (idx != -1)
g_szExportPath = fName.substring(0, idx);
g_bDirty = true;
}
function importPresets()
{
// open a file selection dlg
var fName = QFileDialog.getOpenFileName(g_form, "Select data file to import",
g_szImportPath, "DAT file (*.dat)", 0);
if (fName == null)
return;
loadIni(fName, false); // load data, but not the preferences
// store last import path
idx = fName.lastIndexOf("/");
if (idx != -1)
g_szImportPath = fName.substring(0, idx);
g_bDirty = true;
}
//---------------------------------------------------------
// setValuesFromPreset(nPresetIdx)
// sets the dlg data values from the values of nIdx-th preset.
//---------------------------------------------------------
function setValuesFromPreset(nIdx)
{ var preset = g_presets[nIdx];
// set each value of the preset
for (var step = 6; step < 25; step++)
g_form["e"+step].setText("" + preset[step]);
g_bDirty = true;
}
//---------------------------------------------------------
// presetExists(preset)
// checks the preset already exists in the global preset array: only data
// are checked, name is ignored.
//---------------------------------------------------------
function presetExists(preset)
{ bExists = false;
for (var nIdx=0; nIdx < g_numOfPresets; nIdx++)
{ var bSame = true;
for (var nStep=0; nStep < 27; nStep++)
{ if (preset[nStep] != g_presets[nIdx][nStep])
{ bSame = false; // at least this item is different
break;
}
}
if (bSame) // if complete match
return true; // return preset exists
}
return false;
}
//---------------------------------------------------------
// loadIni(fName, bPrefs)
// loads an INI-like file. The file can be either a settings file
// (if bPrefs == true) or a data file (if bPrefs == false).
// In the first case, the file is looked for in a standard location (fName is
// ignored), and both preferences and preset data are loaded. If no preset
// data are found, a default preset is generated.
// In the second case, the fName parameter is used to locate the file and
// only preset data are loaded and duplicate presets are identified and
// skipped.
// In both cases, preset data are added to the internl g_preset array and
// to the "Presets" combo box.
// Parameters:
// fName: the file to load from, if bSettings == false
// bPrefs: if true, full config is loaded | if false, only presets
//---------------------------------------------------------
function loadIni(fName, bPrefs)
{ var currPreset, idx, idx2, numOfPresets, preset, settings, step;
// if preferences, look in AppData and load config entries
if (bPrefs)
{ settings = new QSettings(QSettings.IniFormat, QSettings.UserScope,
g_szOrgName, g_szAppName, null);
currPreset = settings.value(g_szCurrPresetKey, 0);
g_szExportPath = settings.value(g_szExportPathKey, pluginPath);
g_szImportPath = settings.value(g_szImportPathKey, pluginPath);
}
else
settings = new QSettings(fName, QSettings.IniFormat);
// presets: get number of presets and each preset in turn
numOfPresets = parseInt(settings.value(g_szNumOfPresetsKey, 0));
for (idx=idx2=0; idx < numOfPresets; idx++)
{ preset = new Array();
preset = settings.value(g_szPresetKey+idx, g_defaultPreset);
if ( preset.length < 26 ) // old preset
{
preset[25] = 50 // Sori
preset[26] = -50 // Koron
}
preset["Name"] = settings.value(g_szPresetNameKey+idx, g_szDefaultPresetName);
// on import, check the preset does not already exists
if (!bPrefs && presetExists(preset) )
{ if (QMessageBox.question(g_form, "Preset exists",
"Import preset \"" + preset["Name"] +
"\" is the same as existing preset \"" + g_presets[idx]["Name"] +
"\"\nSkip it? (press ESC to import it anyway)") == QMessageBox.Ok)
continue;
}
// add to internal data and to combo box
g_presets[idx2+g_numOfPresets] = preset;
g_form.comboPresets.addItem(preset["Name"]);
idx2++;
}
numOfPresets = idx2;
if (bPrefs)
{ g_bDirty = false;
g_numOfPresets = numOfPresets;
// if no preset, create a default one
if (g_numOfPresets < 1)
{ g_numOfPresets = 0;
preset = [];
for (step = 0; step < g_defaultPreset.length; step++)
preset[step] = g_defaultPreset[step];
preset["Name"] = g_szDefaultPresetName;
g_presets[0] = preset; // add to internal data
g_numOfPresets = 1;
g_form.comboPresets.addItem(preset["Name"]);// add to combo box
g_form.comboPresets.setCurrentIndex(0);
g_bDirty = true;
}
g_form.comboPresets.setCurrentIndex(currPreset);
}
else
{ g_numOfPresets += numOfPresets;
}
}
//---------------------------------------------------------
// saveIni(fName, bPrefs)
// saves an INI-like file. The file can be either a settings file
// (if bPrefs == true) or a data file (if bPrefs == false).
// In the first case, the file is created in a standard location (fName is
// ignored), and both preferences and preset data are stored.
// In the second case, the fName parameter is used to locate the file and
// only preset data are stored.
// Parameters:
// fName: the file to store into, if bSettings == false
// bPrefs: if true, full config is stored | if false, only presets
//---------------------------------------------------------
function saveIni(fName, bPrefs)
{ var settings;
// if preferences, look in AppData and store config entries
if (bPrefs)
{ if (!g_bDirty) // if settings are not dirty, do nothing
return;
settings = new QSettings(QSettings.IniFormat, QSettings.UserScope,
g_szOrgName, g_szAppName, null);
settings.setValue(g_szCurrPresetKey, g_form.comboPresets.currentIndex);
settings.setValue(g_szExportPathKey, g_szExportPath);
settings.setValue(g_szImportPathKey, g_szImportPath);
}
else
settings = new QSettings(fName, QSettings.IniFormat);
// presets: ensure no left-over remains from deleted presets and store number
settings.remove(g_szPresetsSect);
settings.setValue(g_szNumOfPresetsKey, g_numOfPresets);
// save each preset
for (var preset=0; preset < g_numOfPresets; preset++)
{ settings.setValue(g_szPresetKey+preset, g_presets[preset]);
settings.setValue(g_szPresetNameKey+preset, g_presets[preset]["Name"]);
}
settings.sync(); // flush file
if(bPrefs)
g_bDirty = false;
}
//---------------------------------------------------------
// menu: defines were the function will be placed
// in the MuseScore menu structure
//---------------------------------------------------------
var mscorePlugin =
{ menu: 'Plugins.Microtonal tunings',
init: init,
run: run
};
mscorePlugin;