-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfretboard.asy
544 lines (411 loc) · 9.88 KB
/
fretboard.asy
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
unitsize(20pt);
real fretSize = 1;
real stringSpacing = .6;
defaultpen(linecap(2));
/*
* Globals
* -------------------------------------------------------------------------- *
*/
int numFrets = 4;
int capo = 0;
int[] majorscale = {
0, // 1
2, // 2
4, // 3
5, // 4
7, // 5
9, // 6
11 // 7
};
/*
* Notes
*/
int C = 0;
int D = 2;
int E = 4;
int F = 5;
int G = 7;
int A = 9;
int B = 11;
/*
* Default string set
*/
int strings[] = {
E,
A,
D,
G,
B,
E
};
/*
* Default pen set
*/
pen[] defaultScalePens = {
red, // 1
currentpen, // 2
heavygreen, // 3
currentpen, // 4
lightolive, // 5
currentpen, // 6
currentpen // 7
};
/*
* Calculated Globals
* -------------------------------------------------------------------------- *
*/
real noteSize = .4 * stringSpacing;
void setStrings(int[] strs) {
strings = strs;
}
int[] getStrings() {
return strings;
}
int getStringsNumber() {
return getStrings().length;
}
real getFingerboardHeight() {
return (getStringsNumber() - 1) * stringSpacing;
};
/*
* Private methods
* -------------------------------------------------------------------------- *
*/
/**
* Contains scale's degree number and alteration
*/
struct DegreeInfo {
int degree;
int alteration;
};
/**
* Returns DegreeInfo from string token
*/
DegreeInfo getDegreeInfoFromToken(string token="") {
write("token: " + token);
int degree = (int) substr(token, 0, 1);
string alterationToken = substr(token, 1, 1);
int alteration = 0;
if (alterationToken == "b") {
alteration = -1;
}
if (alterationToken == "#") {
alteration = +1;
}
DegreeInfo degreeInfo;
degreeInfo.degree = degree;
degreeInfo.alteration = alteration;
return degreeInfo;
}
/**
* Returns an array of DegreeInfo's from scale string
*/
DegreeInfo[] getDegreeInfosFromScale(string scale = "") {
string[] tokens = split(scale, " ");
DegreeInfo[] degreeInfos = {};
for (int i = 0; i < tokens.length; ++i) {
string token = tokens[i];
DegreeInfo degreeInfo = getDegreeInfoFromToken(token);
degreeInfos.push(degreeInfo);
}
return degreeInfos;
}
/**
* Returns an array of pitches from scale string
*/
int[] getPitchesFromScale(string scale = "") {
string[] tokens = split(scale, " ");
int[] pitches = {};
for (int i = 0; i < tokens.length; ++i) {
string token = tokens[i];
int degree = (int) substr(token, 0, 1);
if (degree > 7) {
degree = degree - 7;
}
string alterationToken = substr(token, 1, 1);
int alteration = 0;
if (alterationToken == "b") {
alteration = -1;
}
if (alterationToken == "#") {
alteration = +1;
}
int pitch = majorscale[degree - 1] + alteration;
write(pitch);
pitches.push(pitch);
}
return pitches;
}
/**
* Returns nicely formatted LaTeX string with alteration for scale degree token
*/
string getFormattedDegreeFromToken(string token = "") {
DegreeInfo degreeInfo = getDegreeInfoFromToken(token);
write(degreeInfo.degree);
write(degreeInfo.alteration);
string degree = (string) degreeInfo.degree;
string alteration = "";
if (degreeInfo.alteration == 1) {
alteration = "$\sharp$";
}
if (degreeInfo.alteration == -1) {
alteration = "$\flat$";
}
return alteration + degree;
}
/**
* Draws title
*/
void drawTitle(string title = "") {
label("\Large " + title, (getFingerboardHeight() / 2, 0), 7N);
return;
}
/**
* Returns token for given pitch and key
*/
string getTokenForPitchInKey(int pitch = 0, int key = 0) {
write(format("pitch: %i", pitch));
if (pitch < key) {
pitch = pitch + 12;
}
pitch = pitch - key;
for (int i = 0; i < majorscale.length; ++i) {
if (pitch > majorscale[i]) continue;
if (pitch == majorscale[i]) return (string) (i + 1);
if (pitch < majorscale[i]) {
return ((string) (i + 1)) + "b";
};
}
return "7";
}
/**
* Returns pen for given scale degree acording to `pens` pen set
*/
pen getPenForDegree(
int degree = 1,
pen[] pens = defaultScalePens
) {
if (degree < pens.length + 1) {
return pens[degree -1];
}
return currentpen;
}
/*
* Public Methods
* -------------------------------------------------------------------------- *
*/
/**
* Draws fingerboard
*/
void drawFingerboard(string title = "", bool dots = false) {
real fingerboardHeight = (getStringsNumber() - 1) * stringSpacing;
drawTitle(title);
/* Draw invisible lines for nice margin */
draw((-1, -numFrets * fretSize)--(-1, 0), invisible);
draw((fingerboardHeight + 1, -numFrets * fretSize)--(fingerboardHeight + 1,0), invisible);
draw((- 1, -numFrets * fretSize - 1)--(fingerboardHeight + 1, -numFrets * fretSize - 1), invisible);
draw((- 1, 3)--(fingerboardHeight + 1, 3), invisible);
for (int i = 0; i < numFrets + 1; ++i) {
real fretPos = - i * fretSize;
draw((0, fretPos)--(fingerboardHeight, fretPos));
int currentFret = i + capo;
/* Draw dots */
if (dots) {
if (
i > 0 &&
currentFret % 2 > 0 && currentFret > 1 &&
currentFret % 11 != 0 && currentFret % 13 != 0
) {
fill(shift(-.6, fretPos + .5 * fretSize) * scale(.1) * unitcircle, gray);
}
if (i > 0 && currentFret == 12) {
fill(shift(-.6, fretPos + .35 * fretSize) * scale(.1) * unitcircle, gray);
fill(shift(-.6, fretPos + .65 * fretSize) * scale(.1) * unitcircle, gray);
}
}
/* draw fret numbers */
// label("\tiny " + (string) (capo + i), (-.1, - i * fretSize), 2W, lightgray);
}
for (int i = 0; i < getStringsNumber(); ++i) {
real stringPos = i * stringSpacing;
draw((stringPos, 0)--(stringPos, - numFrets * fretSize));
}
if (capo == 0) {
draw((0,0)--(fingerboardHeight, 0), linewidth(1.5) + linecap(1));
}
return;
}
/**
* Returns canvas position of a note
*/
pair getNotePosition(
int str = 1,
int fret = 1
) {
str = str - 1;
pair notePosition = (
str * stringSpacing,
- fret * fretSize + fretSize / 2
);
return notePosition;
}
/**
* Draws single note
*/
void drawNote(
int str = 1,
int fret = -1,
string token = "",
int finger = 0,
pen bg = defaultpen,
pen border = black,
pen fg = white
) {
assert(
str < getStrings().length + 1 && str > 0,
format("Wrong string number (%i)", str) +
format(" when trying to put note at fret %i", fret)
);
assert(
fret > -2 && fret < numFrets + 1,
format("Wrong fret (%i)", fret)
);
assert(
finger > -2 && finger < 5,
format("Wrong finger (%i)", finger)
);
pair notePosition = getNotePosition(str, fret);
str = str - 1;
int fretNum = 0;
if (fret == -1) {
fretNum = 0;
} else {
fretNum = fret;
}
pen notePen = defaultpen;
if (fret == -1) {
label("$\times$", (str * stringSpacing, fretSize / 2));
return;
}
if (fretNum == 0) {
if (fg == white) {
fg = defaultpen;
}
draw(
shift(str * stringSpacing, fretSize / 2)
* scale(noteSize)
* unitcircle,
bg
);
label(
"\tiny " + getFormattedDegreeFromToken(token),
(str * stringSpacing, fretSize / 2),
fg
);
return;
}
filldraw(
shift(notePosition)
* scale(noteSize)
* unitcircle,
bg,
border
);
label(
"\tiny " + getFormattedDegreeFromToken(token),
notePosition,
fg
);
if (finger < 1) return;
label(
"\tiny " + (string) finger,
notePosition + 1.8 * noteSize * SE ,
gray
);
return;
}
/**
* Draws fingering of a note
*/
void drawFingering(int str = 1, int fret = 0, int finger = 0) {
pair notePosition = getNotePosition(str, fret);
if (finger < 1) return;
label(
"\tiny " + (string) finger,
notePosition + 1.8 * noteSize * SE ,
gray
);
}
/**
* Draws notes according to tab string
*/
void drawChord(
string tab = "x x x x x x",
string fingering="",
int root=0
) {
string[] notes = split(tab, " ");
string[] fingerings = split(fingering, " ");
for (int i = 0; i < notes.length; ++i) {
int string = i + 1;
string note = notes[i];
if (note == "-") continue;
int finger = 0;
int tuning = getStrings()[i];
int pitch = -1;
int fret = -1;
string token = "";
pen bg = currentpen;
if (note != "x") {
fret = (int) note;
pitch = (tuning + fret + capo) % 12;
token = getTokenForPitchInKey(pitch, root);
DegreeInfo degreeInfo = getDegreeInfoFromToken(token);
bg = getPenForDegree(degreeInfo.degree);
}
if (fingerings.length > i && note != "x" && fingerings[i] != "-") {
finger = (int) fingerings[i];
}
drawNote(string, fret, token, finger, bg);
}
return;
}
/**
* Draws notes of a scale, represented as array of semitones
*/
void drawScaleOnString(
int root = 0,
string scale = "1 2 3 4 5 6 7",
int string=1,
int tuning = 0,
pen[] pens = defaultScalePens
) {
int[] pitches = getPitchesFromScale(scale);
string[] tokens = split(scale, " ");
DegreeInfo[] degreeInfos = getDegreeInfosFromScale(scale);
int fret = 0;
int degree = 0;
int offset = -tuning - 12 + root - capo;
while (fret < numFrets) {
fret = pitches[degree] + offset;
int scaleDegree = degreeInfos[degree].degree;
string token = tokens[degree];
pen bg = getPenForDegree(scaleDegree, pens);
if (fret > -1 && fret < numFrets + 1) {
if (!(fret == 0 && capo > 0)) {
drawNote(string, fret, token, bg);
}
}
++degree;
if (degree > pitches.length - 1) {
degree = 0;
offset = fret + 12 - pitches[pitches.length - 1];
}
}
}
void drawScale(int root=0, string scale ="1 2 3 4 5 6 7", pen[] pens = defaultScalePens) {
for (int i = 0; i < getStringsNumber(); ++i) {
drawScaleOnString(root, scale, i + 1, strings[i], pens);
}
}
/* -------------------------------------------------------------------------- */