-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
160 lines (118 loc) · 6.24 KB
/
core.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
/*========================================================================
Pitch and Rhythm Transformer
https://github.com/Ash-86/Pitch-and-Rhythm-Transformer
Copyright (C)2023 Ashraf El Droubi (Ash-86)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
=========================================================================*/
function applyTransform(roll){
var cursor = curScore.newCursor();
/////// Get Selection //////////////////////////
cursor.rewind(2); // go to the end of the selection
var endTick = cursor.tick;
// if (endTick == 0) { // dealing with some bug when selecting to end.
// var endTick = score.lastSegment.tick + 1;
// }
var endStaff = cursor.staffIdx +1;
var endTrack = endStaff * 4;
//start
cursor.rewind(1); // go to the beginning of the selection
var startSegTick= curScore.selection.startSegment.tick;
var startTick = cursor.tick;
var startStaff = cursor.staffIdx;
var startTrack = startStaff * 4;
cursor.rewind(1); // beginning of selection
///////////////////////////////////////////////////
curScore.startCmd()
var onlyPitches=getArrays()
rollChord(onlyPitches, roll)
curScore.selection.selectRange(startTick, endTick, startStaff, endStaff);
curScore.endCmd()
/////////// Get arrays: Pitches, onlyPitches, Rhythm (durations)///////
function getArrays(){
var onlyPitches=[]///without rests
while (cursor.segment != null && cursor.tick < endTick) {
var chord=[]
for(var track=startTrack; track<endTrack; track++){
cursor.track=track
if( !cursor.element) continue
var el=cursor.element
if (el.type == Element.CHORD) {
for (var n in el.notes){
var chordNote ={
pitch: el.notes[n].pitch,
tpc: el.notes[n].tpc,
tpc1: el.notes[n].tpc1,
tpc2: el.notes[n].tpc2
}
chord.push(chordNote)
}
}
track++
}
onlyPitches.push(chord)
cursor.track=startTrack
cursor.next();
}
cursor.rewind(1)
return onlyPitches
}
/////////////////////////////////////////////////////////////
function rollChord(Pitches,roll){
var i=0
while (cursor.segment != null && cursor.tick < endTick) {
var C=[]
Pitches[i].sort(function(a, b){return a.pitch%12 - b.pitch%12}); //sorting up
for (var m=0; m<Pitches[i].length;m++){
Pitches[i][m].pitch%=12
if(!C.some(function(x){return x.pitch==Pitches[i][m].pitch})){ ////discard note is pitch already exists
C.push(Pitches[i][m])
}
}
///map scale of chord notes
var CLength= C.length
for (var k=1; k<12; k++){
for (var m=0; m<CLength; m++){
var n= JSON.parse(JSON.stringify(C[m])); // clone C[m] object without shared reference
n.pitch+=12*k
C.push(n)
}
}
for(var track=startTrack; track<endTrack; track++){
cursor.track=track
if( !cursor.element) continue
var el=cursor.element
if (el.type == Element.CHORD) {
if (roll=="up"){
for (var n=el.notes.length-1; n>=0; n--){
var idx = C.findIndex(function(obj){return obj.pitch == el.notes[n].pitch});
el.notes[n].pitch= C[idx+1].pitch ///change lowest note pitch and tpc
el.notes[n].tpc1= C[idx+1].tpc1
el.notes[n].tpc2= C[idx+1].tpc2
}
}
if (roll=="down"){
for (var n=0; n<el.notes.length; n++){
var idx = C.findIndex(function(obj){return obj.pitch == el.notes[n].pitch});
el.notes[n].pitch= C[idx-1].pitch ///change lowest note pitch and tpc
el.notes[n].tpc1= C[idx-1].tpc1
el.notes[n].tpc2= C[idx-1].tpc2
}
}
}
track++
}
i++
cursor.track=startTrack
cursor.next()
}
} /// end rollCHord
}///end transform