-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeToTempo-4.4x.qml
More file actions
610 lines (511 loc) · 23.2 KB
/
Copy pathTimeToTempo-4.4x.qml
File metadata and controls
610 lines (511 loc) · 23.2 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
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
//====================================================================================================
// TimeToTempo Plugin for Musescore
// Credit to all the help I got from other developers and the Musescore devs while making this plugin.
// This plugin is free software. You can redistribute it and/or modify it.
//====================================================================================================
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts 1.1
import MuseScore
MuseScore {
version: "1.0.1"
title: "TimeToTempo 4.4x"
description: "Place a tempo marking on the selected note to match the duration desired"
pluginType: "dialog"
categoryCode: "composing-arranging-tools"
requiresScore: true
id: 'pluginId'
width: 314
height: 240
onRun: {
console.log("Plugin loaded...");
}
function calculateTempo() {
// Get currently seelected element
var currentElement = curScore.selection.elements[0];
if (currentElement == null) {
showMessage("Warning", "Please select a note head...");
return;
}
var startTimeValues;
var endTimeValues;
var durationTimeValues;
var startTime;
var endTime;
if (durationTimeInput.text == "" || durationTimeInput.text == null) {
// Read input times
startTime = startingTimeValue.text;
endTime = endingTimeValue.text;
// Check if the starting time and the ending time are filled
if (startTime == "" || endTime == "") {
var msg = (startTime == "") ? "Start time is empty..." : "End time is empty...";
showMessage("Error", msg);
return;
}
// Check if the starting and ending time are the same
if (startTime == endTime) {
showMessage("Error", "Start time and end time are the same...");
return;
}
// Parse starting time values and check if they are correctly formatted
startTimeValues = parseTimeToArray(startTime);
if (startTimeValues.length == 0) {
return;
}
// Parse ending time values and check if they are correctly formatted
endTimeValues = parseTimeToArray(endTime);
if (endTimeValues.length == 0) {
return;
}
// Parse duration time values and check it is negative
durationTimeValues = calculateDuration(startTimeValues, endTimeValues);
if (durationTimeValues.length == 0) {
return;
}
} else {
// Parse starting time values and check if they are correctly formatted
durationTimeValues = parseTimeToArray(durationTimeInput.text);
if (durationTimeValues.length == 0) {
return;
}
}
var currentNoteDurationObj;
var currentNoteDuration;
// Handle note duration if the note is part of a tuple or not
if (currentElement.parent.tuplet) {
console.log("TUPLE PARENT PARENT: " + currentElement.parent.tuplet);
var noteTuplet = currentElement.parent.tuplet;
var actualNotesTuplet = noteTuplet.actualNotes;
currentNoteDurationObj = noteTuplet.duration;
currentNoteDuration = (currentNoteDurationObj.denominator * actualNotesTuplet) / currentNoteDurationObj.numerator;
} else {
currentNoteDurationObj = getChordRest(currentElement).duration;
currentNoteDuration = currentNoteDurationObj.denominator / currentNoteDurationObj.numerator;
}
// Calculate the BPM
var divisorForBeats = parseFloat(currentNoteDuration / 4);
var durationInMilliseconds = calculateMilliseconds(durationTimeValues);
var bpm = calculateBPM(currentNoteDuration, durationInMilliseconds);
// Round the bpm to an integer or 2 d.p.
bpm = (setInteger.checked) ? Math.round(bpm / 60) : (bpm / 60).toFixed(2);
// Create a new TEMPO_TEXT and apply the BPM
applyTempo(bpm);
}
function applyTempo(bpm) {
// Get current note tick
var currentNoteTick = getTick(curScore.selection.elements[0]);
// Make a new cursor to manipulate
var cursor = curScore.newCursor();
cursor.rewindToTick(0);
while(!cursor.element.staff.is(curScore.selection.elements[0].staff)) {
cursor.staffIdx++
}
cursor.track = curScore.selection.elements[0].track;
cursor.rewindToTick(currentNoteTick);
curScore.startCmd();
// Create a new TEMPO_TEXT element
var tempoElement = newElement(Element.TEMPO_TEXT);
// Set text
tempoElement.text = '\uECA5' + ' = ' + bpm;
tempoElement.visible = visible;
// Add the element to the score
cursor.add(tempoElement);
// This has to be done after the element is added to the score for some reason
tempoElement.tempo = parseFloat(bpm/60);
tempoElement.tempoFollowText = true;
// End the command
curScore.endCmd();
// TO BE DONE
//=====================================================
// Gets the last note that is tied to this current one
//var tiedNotes = note.lastTiedNote;
//console.log("All tied notes: ", tiedNotes);
//=====================================================
// Set the ending time as the new starting time
startingTimeValue.text = endingTimeValue.text;
endingTimeValue.text = "";
durationTimeInput.text = "";
// Go to the next note
selectPrevNext(true);
}
function parseTimeToArray(timeString) {
// Split the time string at ':' and '.'
var timeParts = timeString.split(/[:.]/);
// Make sure that there are only 4 parts
if (timeParts.length !== 4) {
showMessage("Error", "Invalid time format. Expected HH:MM:SS.FFF");
return [];
}
// Regex for the digits 0-9
var numberRegex = /^[0-9]+$/;
try {
// Make sure that only digits are present otherwise spit out an error
for (var i = 0; i < timeParts.length; i++) {
if (!numberRegex.test(timeParts[i])) {
showMessage("Error", "Invalid characters in time input...");
return [];
}
}
// Parse integers in Base-10
var hours = parseInt(timeParts[0], 10);
var minutes = parseInt(timeParts[1], 10);
var seconds = parseInt(timeParts[2], 10);
var milliseconds = parseInt(timeParts[3], 10);
// Return the time parts as an array of integers
return [hours, minutes, seconds, milliseconds];
} catch (e) {
// Show an error message if anything goes wrong for whatever reason
showMessage("Error", "Error parsing time...");
return [];
}
}
function calculateDuration(startValues, endValues) {
// Calculate the duration in milliseconds
var durationMilliseconds = calculateMilliseconds(endValues) - calculateMilliseconds(startValues);
// Make sure duration is a positive time span
if (durationMilliseconds < 0) {
showMessage("Error", "End time is earlier than start time...");
return [];
}
// Convert the duration back into hours, minutes, seconds, and milliseconds
var hours = Math.floor(durationMilliseconds / (3600 * 1000));
var minutes = Math.floor((durationMilliseconds % (3600 * 1000)) / (60 * 1000));
var seconds = Math.floor((durationMilliseconds % (60 * 1000)) / 1000);
var milliseconds = durationMilliseconds % 1000;
// Return the duration as [hours, minutes, seconds, milliseconds]
return [hours, minutes, seconds, milliseconds];
}
// Calculate the milliseconds of a given time
function calculateMilliseconds(values) {
var milliseconds = (values[0] * 3600 + values[1] * 60 + values[2]) * 1000 + values[3];
return milliseconds;
}
function calculateBPM(noteType, timeDur) {
var divisorForBeats = parseFloat(noteType) / 4;
// Time duration for each beat (in milliseconds)
if (divisorForBeats < 1) {
// If note type is bigger than 1 beat
timeDur = timeDur / (1 / divisorForBeats);
} else {
// If note type is smaller than 1 beat
timeDur = timeDur * divisorForBeats;
}
// Calculate BPM
// note: timeDur is in milliseconds
// note: 60 / (timeDur / 60000)
// note: 60 seconds is 60000ms
var bpm = 3600000 / timeDur;
return bpm;
}
//====================================================================
// FOR BELOW CODE: CREDIT GOES TO "xiaomigros" (discord) FOR THIS CODE
// Allows identical treatment/parenthood of notes/chords/rests
function getChordRest(element) {
switch (element.type) {
case Element.NOTE:
return element.parent;
default:
return element;
}
}
// Returns the segment of a given note/chord/rest
function getSegment(element) {
return getChordRest(element).parent;
}
// Returns the tick of a given note/chord/rest/tuplet
function getTick(element) {
return element.type == Element.TUPLET ? getTick(element.elements[0]) : getSegment(element).tick;
}
// Returns readable duration values from a note/chord/rest/tuplet
function getDuration(element) {
return {numerator: getChordRest(element).duration.numerator, denominator: getChordRest(element).duration.denominator}
}
// Returns an element that the cursor can be set to select
function getSelectableElement(element) {
switch (element.type) {
case Element.CHORD:
return element.notes[0];
default:
return element;
}
}
// Select the previous or next note
function selectPrevNext(next) {
var c = curScore.newCursor();
c.rewindToTick(0);
// locate staff & voice of selected element
while(!c.element.staff.is(curScore.selection.elements[0].staff)) {
c.staffIdx++;
}
c.track = curScore.selection.elements[0].track;
// move cursor to position of selected element
c.rewindToTick(getTick(curScore.selection.elements[0]));
// move forwards or backwards
if (next) {
c.next();
} else {
c.prev();
}
// select the new element
curScore.selection.select(getSelectableElement(c.element));
}
// FOR ABOVE CODE: CREDIT GOES TO "xiaomigros" (discord) FOR THIS CODE
//====================================================================
// GUI
Rectangle {
id: ctrlRectangle
property alias msgDiag: msgDiag
property alias pluginGUI: pluginGUI
property alias startingTimeValue: startingTimeValue
property alias endingTimeValue: endingTimeValue
property alias durationTimeInput: durationTimeInput
property alias setInteger: setInteger
property alias calculateButton: calculateButton
property alias rewindBtn: rewindBtn
property alias skipBtn: skipBtn
width: 360
height: 240
color: "#e6ebe0"
// Error handling popup template
MessageDialog {
id: msgDiag
title: "Title"
text: "Text"
visible: false
onAccepted: {
//msgDiag.visible = false;
msgDiag.close();
}
}
// The grid layout for items
GridLayout {
id: pluginGUI
anchors.fill: parent
anchors.margins: 10
columns: 3
focus: true
// Starting time
Label {
text: "Starting time:"
font.pixelSize: 18
font.bold: true
color: "#ED6A5A"
MouseArea{
anchors.fill: parent
onClicked: {
startingTimeValue.text = "";
startingTimeValue.paste();
}
}
}
TextField {
id: startingTimeValue
Layout.columnSpan: 2
placeholderText: "<font color=\"#7F7F7F\">HH:MM:SS.FFF</font>"
font.pixelSize: 12
font.bold: true
background: Rectangle {
y: 1
radius: 4
implicitWidth: 140
implicitHeight: 24
border.color: "#5CA4A9"
border.width: 2
}
Component.onCompleted: {
startingTimeValue.color = "#7F7F7F";
}
}
// Ending time
Label {
text: "Ending time:"
font.pixelSize: 18
font.bold: true
color: "#ED6A5A"
MouseArea{
anchors.fill: parent
onClicked: {
endingTimeValue.text = "";
endingTimeValue.paste();
if (startingTimeValue.text != "" || startingTimeValue.text != null) {
calculateTempo();
}
}
}
}
TextField {
id: endingTimeValue
Layout.columnSpan: 2
placeholderText: "<font color=\"#7F7F7F\">HH:MM:SS.FFF</font>"
font.pixelSize: 12
font.bold: true
background: Rectangle {
y: 1
radius: 4
implicitWidth: 140
implicitHeight: 24
border.color: "#5CA4A9"
border.width: 2
}
Component.onCompleted: {
endingTimeValue.color = "#7F7F7F";
}
}
// Duration time
Label {
text: "Duration time:"
font.pixelSize: 18
font.bold: true
color: "#ED6A5A"
MouseArea{
anchors.fill: parent
onClicked: {
durationTimeInput.text = "";
durationTimeInput.paste();
calculateTempo();
}
}
}
TextField {
id: durationTimeInput
Layout.columnSpan: 2
placeholderText: "<font color=\"#7F7F7F\">HH:MM:SS.FFF</font>"
font.pixelSize: 12
font.bold: true
background: Rectangle {
y: 1
radius: 4
implicitWidth: 140
implicitHeight: 24
border.color: "#5CA4A9"
border.width: 2
}
Component.onCompleted: {
durationTimeInput.color = "#7F7F7F";
}
}
// Integer BPM
Label {
text: "Set as integer:"
font.pixelSize: 18
font.bold: true
color: "#ED6A5A"
}
CheckBox {
id: setInteger
indicator: Rectangle {
y: 8
implicitWidth: 16
implicitHeight: 16
radius: 4
border.color: "#5CA4A9"
border.width: 2
Rectangle {
visible: setInteger.checked
color: "#ED6A5A"
radius: 2
anchors.margins: 4
anchors.fill: parent
}
}
}
// Calculate
Button {
id: calculateButton
Layout.columnSpan: 3
contentItem: Text {
text: "Calculate"
font.pixelSize: 14
font.bold: true
color: "#F4F1BB"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
x: 20
implicitWidth: 251
implicitHeight: 30
color: "#5CA4A9"
radius: 4
border.color: "#F4F1BB"
border.width: 2
}
Component.onCompleted: {
calculateButton.contentItem.x = 24;
}
onClicked: {
// Calculate the tempo based on the input times
calculateTempo();
}
}
// Previous note
Button {
id: rewindBtn
Layout.columnSpan: 1
contentItem: Text {
text: "Previous note"
font.pixelSize: 12
font.bold: true
color: "#F4F1BB"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
x: 20
implicitWidth: 100
implicitHeight: 30
color: "#5CA4A9"
radius: 4
border.color: "#F4F1BB"
border.width: 2
}
Component.onCompleted: {
rewindBtn.contentItem.x = 24;
}
onClicked: {
// Rewind and select the previous note
curScore.startCmd();
selectPrevNext(false);
curScore.endCmd();
}
}
// Next note
Button {
id: skipBtn
Layout.columnSpan: 1
contentItem: Text {
text: "Next note"
font.pixelSize: 12
font.bold: true
color: "#F4F1BB"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
background: Rectangle {
x: 20
implicitWidth: 100
implicitHeight: 30
color: "#5CA4A9"
radius: 4
border.color: "#F4F1BB"
border.width: 2
}
Component.onCompleted: {
skipBtn.contentItem.x = 24;
}
onClicked: {
// Skip and select the next note
curScore.startCmd();
selectPrevNext(true);
curScore.endCmd();
}
}
}
}
//Show the message dialogue with the desired title, text and icon
function showMessage(title, text) {
msgDiag.title = title;
msgDiag.text = text;
msgDiag.visible = true;
}
}