-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
250 lines (216 loc) · 6.96 KB
/
main.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
// Copyright (C) 2023 euwbah
//
// This file is part of Xen Tuner.
//
// Xen Tuner 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.
//
// Xen Tuner 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 Xen Tuner. If not, see <http://www.gnu.org/licenses/>.
/**
* @file Simple code for generating/downloading tuning config as JSON and
* testing the tuning config parser.
*/
var Lookup = ImportLookup();
window.testTuning = `
// A subset of 5-limit JI notation using HEJI notation
// A4 is tuned to 440 Hz.
//
// Tuning space comprises:
// 3 flats to 3 sharps
// 3 comma downs to 3 comma ups
C4: 440*16/27 // Indirectly tune A4 by tuning C4 to 16/27 of A4 = 440
// Nominals are a chain of pure fifths from F to B
0 9/8 81/64 4/3 3/2 27/16 243/128 2/1
// Here we declare two accidental chains
// 3-limit accidentals from bbb to #x
bbb bb b (2187/2048) # x #x
// 5-limit acc from -3 to +3
\\\\.\\\\.\\\\ \\\\.\\\\ \\\\ (81/80) / /./ /././
// Notice that we need to escape backslashes. "\\" instead of "\"
// Here we declare ligatures between the 1st and 2nd chain
// In HEJI, syntonic commas can merge with sharp/flat/natural
// accidentals to form ligatured symbols.
lig(1,2)
-2 -3 bbv3 // doubleflat and 3 downs combine into a double flat with 3 down arrows HEJI symbol
-2 -2 bbv2
-2 -1 bbv
-2 1 bb^
-2 2 bb^2
-2 3 bb^3
-1 -3 bv3
-1 -2 bv2
-1 -1 bv
-1 1 b^
-1 2 b^2
-1 3 b^3
1 -3 #v3
1 -2 #v2
1 -1 #v
1 1 #^
1 2 #^2
1 3 #^3
2 -3 xv3
2 -2 xv2
2 -1 xv
2 1 x^
2 2 x^2
2 3 x^3
// Here we declare 4 auxiliary up/down operations.
// These will be accessible as the aux1, aux2, aux3, aux4 up/down operations
// respectively. You can lookup/modify the keyboard shortcuts to these
// operations in the "xen tuner.qml" file.
aux(0) // aux1 will modify nominals only, without modifying accidentals
aux(1) // aux2 will modify flats/sharps only, without modifying nominals or other accs
aux(2) // aux3 will modify syntonic comas only
aux(0,1) // aux4 will modify both nominals and flats/sharps.
// Now we declare secondary accidentals & ASCII text representations
// E.g. If you attach the fingering 'bbbbb' on to a note,
// the plugin will convert & render it into a triple-flat symbol
// and a double-flat symbol bbb.bb.
//
// The triple-flat will match as degree -3 of the sharps/flats chain
// and the double-flat will match as a secondary accidental.
sec()
'bbb' bbb Math.pow(2187/2048,-3) // converts fingering 'bbb' into triple-flat symbol
'bb' bb Math.pow(2187/2048,-2)
'b' b 2048/2187
'###' #x Math.pow(2187/2048,3)
'#x' #x Math.pow(2187/2048,3)
'x#' #x Math.pow(2187/2048,3)
'##' x Math.pow(2187/2048,2)
'x' x Math.pow(2187/2048,2)
'#' # 2187/2048
'/' / 81/80
'\\\\' \\\\ 80/81
`;
function download(content, filename, contentType)
{
if(!contentType) contentType = 'application/octet-stream';
var a = document.createElement('a');
var blob = new Blob([content], { 'type': contentType });
a.href = window.URL.createObjectURL(blob);
a.download = filename;
a.click();
}
function debugTC() {
var tuningConfigStr = document.getElementById('tcinput').value.trim();
var tuningConfig = parseTuningConfig(tuningConfigStr);
if (!tuningConfig) {
document.getElementById('errormsg').innerText =
'Error parsing tuning config! See browser console for details.';
return null;
}
document.getElementById('errormsg').innerText = '';
var tuningConfigJSON = JSON.stringify(tuningConfig);
// save generated tuning config JSON to `tc` global variable
// to enable browser console debugging
window.tc = tuningConfig;
console.log('Access the tuning config object via the `tc` variable:')
console.debug(tc)
return tuningConfigJSON;
}
/**
*
* @param {string} hash XenNote hash
* @returns List of all enharmonics of given hash
*/
function enharmonics(hash) {
if (!tc || !tc.enharmonics[hash]) return;
let curr = tc.enharmonics[hash];
let enharmonics = [hash];
while (curr != hash) {
enharmonics.push(curr);
curr = tc.enharmonics[curr];
}
return enharmonics;
}
function generateTuningConfigJSON() {
var tuningConfigJSON = debugTC();
if (tuningConfigJSON == null) return;
download(tuningConfigJSON, 'tuningconfig.json', 'text/plain');
}
/**
* Create a fake MuseScore Note QObject for testing purposes.
*
* @param {string} noteName A to G
* @param {number} octave (octave resets at C)
* @param {string|number} accidental SymCode or Text Code
* @param {string} symbols SymCodes or Text Codes separated by dot .
* @param {number} tick tick position of Segment that note is attached to.
*/
function testNewNote(noteName, octave, accidental, symbols, tick=0) {
var noteName = noteName.toLowerCase();
var accidental = accidental;
var noteNameTpcLookup = {
'f': -8,
'c': -7,
'g': -6,
'd': -5,
'a': -4,
'e': -3,
'b': -2,
};
var accidentalTpcLookup = {
8: 0,
7: 7, // bb
6: 14, // b
1: 21, // default is None
5: 28, // #
4: 35, // x
3: 42, // x#
};
if (Lookup.TEXT_TO_CODE[accidental]) {
accidental = Lookup.TEXT_TO_CODE[accidental];
}
if (isNaN(parseInt(accidental))) {
accidental = 1; // no accidental.
}
var pitch = 69 + (octave - 4) * 12 + Lookup.LETTERS_TO_SEMITONES[noteName];
if (accidental >= 6 && accidental <= 8) {
pitch -= accidental - 5;
} else if (accidental >= 3 && accidental <= 5) {
pitch += 6 - accidental;
}
var tpc = noteNameTpcLookup[noteName];
if (accidentalTpcLookup[accidental]) {
tpc += accidentalTpcLookup[accidental];
}
var elements;
if (symbols.trim().length == 0) {
elements = [];
} else {
elements = symbols.split('.').map(function(x) {
var symCode = x;
if (Lookup.TEXT_TO_CODE[x]) {
symCode = Lookup.TEXT_TO_CODE[x];
}
return {
symbol: Lookup.CODE_TO_LABELS[symCode][0] || 'NoSym'
}
});
}
return {
pitch: pitch,
tpc: tpc,
accidental: Lookup.CODE_TO_LABELS[accidental][0] || 'NONE',
elements: elements,
parent: {
parent: {
tick: tick
}
},
// supposed to represent stave line this note appears on
// just put some fluff number, doesn't really matter.
// as long as the same nominals are consistently on the
// same lines, its fine.
line: - (noteNameTpcLookup[noteName] + 6 + 7 * octave),
}
}