forked from renard/guitar-fretboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
guitar-fretboard.sty
526 lines (456 loc) · 12.7 KB
/
guitar-fretboard.sty
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
%%
%% Copyright © 2018 by Sébastien Gross <seb -at- chezwam -dot- org>
%% This program is free software. It comes without any warranty, to the extent
%% permitted by applicable law. You can redistribute it and/or modify it under
%% the terms of the Do What The Fuck You Want To Public License, Version 2, as
%% published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
%% details.
%% This file may be distributed and/or modified under the conditions of
%% the LaTeX Project Public License, either version 1.3c of this license
%% or (at your option) any later version. The latest version of this
%% license is in:
%% http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2006/05/20 or later.
\def\fbfileversion{1.1}
\def\fbfiledate{2018/11/11}
\ProvidesPackage{guitar-fretboard}[\fbfiledate\space (v\fbfileversion)]
\NeedsTeXFormat{LaTeX2e}
\newif\iffb@split
%% Include all required packages
\RequirePackage{guitar-fretboard.colors}
\RequirePackage{tikz}
\usetikzlibrary{shapes,positioning,calc,shapes.multipart,backgrounds}
\RequirePackage{ifthen}
\RequirePackage{xspace}
\RequirePackage{fontspec}
\RequirePackage{etoolbox}
\RequirePackage{guitar-fretboard.notes}
%\RequirePackage{guitar-fretboard.notes}
%% \RequirePackage{guitar-fretboard.pitches}
\RequirePackage{guitar-fretboard.scales}
\RequirePackage{xparse,expl3}
\makeatletter
\def\notefont{\sffamily\bfseries}
\def\fretfont{\sffamily}
\def\stringfont{\sffamily\bfseries}
\def\titlefont{\sffamily}
\def\intervalfont{\sffamily}
\pgfkeys{/tikz/.cd,
% Execute style
@es/.style = {#1},
% Execute macro
@em/.style = {@es/.expand once=#1}
}
\tikzset{
fretboard/.style = {
yscale=-1,
xscale=2,
background rectangle/.style={
fill=white,
inner sep = 0pt,
outer sep=0pt,
},
show background rectangle,
},
%
fret/.style = { line width = 2pt, },
fret numbers/.style = {
font=\fretfont,
},
fret numbers position/.style = {shift={(0,0.75)}},
%
string tuning/.style = {
rotate=90,
anchor=center,
align=center,
font=\stringfont\Large,
},
string tuning position/.style = {
shift = {(-0.75, 1)},
},
%
string/.style = { },
%
overlay/.style = {
color=white,
opacity=0.85,
line width = 0pt,
outer sep=0pt,
},
title/.style = {
anchor=south,
align=center,
font=\titlefont\Huge
},
title position/.style = {
shift = {(0, -0.75)},
},
%
note/.style = {
circle,
inner sep = 0.2em,
draw,
align=center,
minimum size = 2.3em,
fill = red!20,
font=\notefont\Large
},
note split/.style = {
circle split,
inner sep = 0.1em,
font=\notefont\normalsize,
},
note shade/.style = {
fill = white,
opacity=0.85,
text = black!30,
draw=black!20,
},
note highlight/.style = {
draw=bg5,
%% double=bg5,
%% double distance=2pt,
line width=2pt,
},
}
%% https://en.wikipedia.org/wiki/List_of_guitar_tunings
\pgfkeys{
/fb/tunings/guitar/.cd,
standard/.initial = {4, 9, 2, 7, 11, 4},
dadgad/.initial = {2, 9, 2, 7, 9, 2},
drop d/.initial = {2, 9, 2, 7, 11, 4},
double drop d/.initial = {2, 9, 2, 7, 11, 2},
open g/.initial = { 2, 7, 2, 7, 11, 2},
7 string/.initial = {11, 4, 9, 2, 7, 11, 4},
}
\pgfkeys{
/fb/tunings/ukulele/.cd,
standard/.initial = {7, 0, 4, 9},
}
\pgfkeys{
/fb/tunings/bass/.cd,
standard/.initial = {4, 9, 2, 7},
5 string/.initial = {11, 4, 9, 2, 7},
5 string tenor/.initial = { 4, 9, 2, 7, 0},
6 string/.initial = {11, 4, 9, 2, 7, 0},
}
\newcommand{\FBtuning}[1]{
\pgfkeysvalueof{/fb/tunings/#1}
}
%% Should fret numbers be displayed?
\newif\iffb@fret@numbers
\newif\iffb@show@tuning
\newif\iffb@split
\newif\iffb@transpose@pitch
\newif\iffb@chord
\newif\iffb@left@handed
\pgfkeys{
/fb/.cd,
%
tuning/.initial = \FBtuning{guitar/standard},
%
frets min/.initial = 0,
frets max/.initial = 12,
frets before/.initial = 0,
frets after/.initial = 0,
frets offset/.initial = 0.5,
%
fret numbers/.is if = fb@fret@numbers,
fret numbers = false,
%
string width/.initial = 0.5pt,
string factor/.initial = 0.75pt,
%
show tuning/.is if = fb@show@tuning,
show tuning = true,
%
split/.is if = fb@split,
split = false,
%
title/.initial = {},
%
transpose/.initial = 0,
transpose pitch/.is if = fb@transpose@pitch,
transpose pitch = false,
%
chord/.is if = fb@chord,
chord = false,
%
scale/.initial = 1,
%
left handed/.is if = fb@left@handed,
left handed = false,
}
% Store a the list of strings in a conviniant way for \foreach
\def\fb@strings{}
% Since there is no simple way to get the size of an array, store the number
% of strings.
\def\fb@strings@count{}
%% Pitch order used to when showing the string names
\def\@pitches{"C","CS","D","Eb","E","F","FS","G","GS","A","Bb","B"}
\newcommand{\@transposepitch}[2]{%
\pgfmathparse{array({\@pitches},
Mod(\pgfkeysvalueof{/fb/notes/#1/semitones}+#2,12))}
\let\newpitch=\pgfmathresult%
}%
%% Computes the length of given array.
%
% #1 - macro to store #2 length
% #2 - The array
%
% Taken from https://tex.stackexchange.com/questions/66121
\def\@getlen#1#2{%
\pgfmathsetmacro{\lenarray}{0}%
\foreach \i in #2{%
\pgfmathtruncatemacro{\lenarray}{\lenarray+1}%
\global\let\lenarray\lenarray%
}%
\let#1=\lenarray%
}%
\newenvironment{fretboard}[1][]{%
\pgfkeys{/fb/.cd, #1}%
\edef\fb@strings{\pgfkeysvalueof{/fb/tuning}}%
\@getlen{\fb@strings@count}{\fb@strings}%
%% Compute the canvas size on x axis.
\pgfmathtruncatemacro{\fb@canvas@x@min}{%
\pgfkeysvalueof{/fb/frets min} - \pgfkeysvalueof{/fb/frets before}}%
\pgfmathtruncatemacro{\fb@canvas@x@max}{%
\pgfkeysvalueof{/fb/frets max} + \pgfkeysvalueof{/fb/frets after}}%
%% Add a +1 value for conviniance.
\pgfmathtruncatemacro{\fb@canvas@x@max@plusone}{\fb@canvas@x@max+1}%
\iffb@chord%
\tikzset{%
fretboard/.append style = {
rotate=90,
yscale=1/2,
xscale=1.2,
},
string tuning/.append style = {
rotate = -90,
shift = {(0, 0.15)},
},
title position/.append style = {
shift = {(-0.75, 0.75)},
},
}%
\fi%
\iffb@left@handed%
\iffb@chord
\tikzset{%
fretboard/.append style = {
yscale=-1,
xscale=1,
},
}
\else
\tikzset{%
fretboard/.append style = {
yscale=1,
xscale=-1,
},
string tuning/.append style = {
rotate = 180,
},
}
\fi
\fi%
\begin{adjustbox}{scale={\pgfkeysvalueof{/fb/scale}}}%
\begin{tikzpicture}[fretboard]%
% Draw the frets and put fret number
\foreach \fret [count=\@ifret from \fb@canvas@x@min]
in {\fb@canvas@x@min,...,\fb@canvas@x@max@plusone}{%
%% Frets are offset from the regular grid
\pgfmathparse{\fret-\pgfkeysvalueof{/fb/frets offset}}
\let\@fret@x=\pgfmathresult
\draw [fret] (\@fret@x,1) -- (\@fret@x,\fb@strings@count);
% Do not draw last fret number
\iffb@fret@numbers{%
\ifnum\fret < \fb@canvas@x@max@plusone{%
\begin{scope}[fret numbers position]
\node[fret numbers] at (\@ifret,\fb@strings@count) {\@ifret};
\end{scope}
}\fi
}\fi
}
% Draw the strings
\foreach \is in {1,...,\fb@strings@count} {
\draw[string,
line width=\pgfkeysvalueof{/fb/string width} +
\is*\pgfkeysvalueof{/fb/string factor}
]
(\fb@canvas@x@min-\pgfkeysvalueof{/fb/frets offset}, \is) --
(\fb@canvas@x@max@plusone-\pgfkeysvalueof{/fb/frets offset}, \is);
}
}{%
% Draw the opacity mask
\ifnum\pgfkeysvalueof{/fb/frets before}>0
\fill [overlay]
(\fb@canvas@x@min-\pgfkeysvalueof{/fb/frets offset}-0.07,0)
rectangle
(\pgfkeysvalueof{/fb/frets min}-\pgfkeysvalueof{/fb/frets offset}-0.07,
\fb@strings@count+1);
\fi
\ifnum\pgfkeysvalueof{/fb/frets after}>0
\fill [overlay]
(\fb@canvas@x@max+\pgfkeysvalueof{/fb/frets offset}+0.07,0)
rectangle
(\pgfkeysvalueof{/fb/frets max}+\pgfkeysvalueof{/fb/frets offset}+0.07,
\fb@strings@count+1);
\fi
%% Add a title
\pgfkeysgetvalue{/fb/title}{\@@fb@title}
\expandafter\notblank\expandafter{\@@fb@title}{%
\begin{scope}[title position]
\iffb@chord
\node (l) at (\fb@canvas@x@min-\pgfkeysvalueof{/fb/frets offset}, 1) {};
\node (r) at (\fb@canvas@x@min-\pgfkeysvalueof{/fb/frets offset}, \fb@strings@count) {};
\else
\node[] (l) at (\fb@canvas@x@min, 1) {};
\node[] (r) at (\fb@canvas@x@max, 1) {};
\fi
\node[title] at ($(l)!0.5!(r)$)%
{\pgfkeysvalueof{/fb/title}};
\end{scope}
}%
% Show string names
\iffb@show@tuning
\begin{scope}[string tuning position]%
\foreach \s [count = \is ] in \fb@strings {%
\node[string tuning] at (\fb@canvas@x@min, \fb@strings@count-\is) {%
\pgfmathparse{array({\@pitches}, Mod(\s,12))}%
\expandafter\csname p\pgfmathresult\endcsname%
};
}
\end{scope}
\fi
\end{tikzpicture}
\end{adjustbox}
}%
%% fb environment ends here
\newif\iffb@note@has@reversed
\newcommand{\FBnote}[2][]{
\begin{scope}
\pgfkeys{/fb/notes/#2/.cd, #1}
%% \notblank requires a macro and not a command result
\pgfkeysgetvalue{/fb/notes/#2/reversed}{\@note@reversed@text}
%% Do we need to transpose the note?
\let\newpitch=#2
\iffb@transpose@pitch
\@transposepitch{#2}{\pgfkeysvalueof{/fb/transpose}}
\pgfkeysgetvalue{/fb/notes/\newpitch/reversed}{\@note@reversed@text}
\fi
% Check if the note has an inversion
\fb@note@has@reversedfalse%
\expandafter\notblank\expandafter{\@note@reversed@text}{%
\iffb@split
\fb@note@has@reversedtrue%
\fi
}
% Check if the note is limited to a specific string or not
\edef\note@limit{\FBgetNote{#2}{limit}}
\@getlen{\note@limit@count}{\note@limit}
\ifnum\note@limit@count >0%
\edef\@strings@limit{\FBgetNote{#2}{limit}}
\else
\edef\@strings@limit{1,...,\fb@strings@count}
\fi
%% Setup note style before looping over strings and octaves
\@setup@note@style[#1]{#2}
%% Compute note placement:
%% X = semitone + transpose - string + octave
%
% First compute static parts:
\pgfmathtruncatemacro{\@base@semitones}{
%% Add note semitones from C
\FBgetNote{#2}{semitones}
%% Finaly apply a transposition if needed.
+\pgfkeysvalueof{/fb/transpose}
}
%% \str is the string index
\foreach \str in \@strings@limit {
%% Compute note placement:
%
% Compute the string part.
\pgfmathtruncatemacro{\@base@semitones@string}{
\@base@semitones
%% Substract the string tuning (Convert string tuning to C).
-array({\fb@strings},\fb@strings@count-\str)
}
% Compute all octaves
\foreach \oct in { -24, -12, 0, 12, 24 } {
%% Compute note position
%
% Add the octave part
\pgfmathtruncatemacro{\@x}{
\@base@semitones@string
%% Add (of remove) octave to cover the whole fretboard
+\oct
}
\pgfmathparse{(\@x>=\fb@canvas@x@min) && (\@x<=\fb@canvas@x@max)?int(1):int(0)}
%% If the note if within our fret range
\ifnum\pgfmathresult>0{%
\iffb@transpose@pitch
\@placenote[#1]{\newpitch}{\@x}{\str}
\else
\@placenote[#1]{#2}{\@x}{\str}
\fi
}\fi
}
}
\end{scope}
}
\newcommand{\@setup@note@style}[2][]{
%% initialize note style
\tikzset{
note/.append style = {/fb/notes/#2/style},
}
\iftrue\csname fi\endcsname\csname iffb@note@#2@highlight\endcsname%
\tikzset{note/.append style = {
note highlight,
/fb/notes/#2/highlight style}}
\fi%
\iffb@note@has@reversed
\tikzset{note/.append style = {note split}}
\fi
}
%% Generic low level command to place a note.
%
% #1 note style override
% #2 The note to place (as defined with newnote)
% #3 X
% #4 Y
%
%% place the note. Since the split is determined by style, we always place
% You might want use \placenote instead
\newcommand{\@placenote}[4][]{
\node[note] at (#3, #4){%
\FBgetNote{#2}{tex small}%
\iffb@note@has@reversed%
\nodepart[]{lower}\FBgetNote{\@note@reversed@text}{tex small}%
\fi%
};%
%% If the node should be shaded
%% https://tex.stackexchange.com/questions/46377
\iftrue\csname fi\endcsname\csname iffb@note@#2@shade\endcsname{%
\node[note, note shade] at (#3, #4) {
\FBgetNote{#2}{tex small}%
\iffb@note@has@reversed%
\nodepart[]{lower}\FBgetNote{\@note@reversed@text}{tex small}%
\fi%
};
}\fi
}
%% Generic high level command to place a note.
%
% #1 note style override
% #2 The note to place (as defined with newnote)
% #3 X
% #4 Y
%
\newcommand{\FBnoteAt}[4][]{
\pgfkeys{/fb/notes/#2/.cd, #1}
\@setup@note@style[#1]{#2}
\@placenote[#1]{#2}{#3}{#4}
}
\makeatother