% !TEX TS-program = LuaLaTeXmk

\documentclass[11pt]{article}
\usepackage{calc}
\usepackage{fontspec}
\usepackage[lmargin=.75in,rmargin=.75in,tmargin=1in,bmargin=1in]{geometry}
\usepackage{titling}
\usepackage{array, booktabs,tabularx}
\usepackage{enumitem}
\usepackage{fancyvrb}
\usepackage{listings}
\usepackage{url}
\usepackage[sf,small]{titlesec}
\usepackage[section]{placeins}
\setmonofont{Inconsolatazi4}
\usepackage{recorder-fingering}
\usepackage{parskip}
\usepackage{musixtex}
\input musixlyr
% musixtex and biblatex clash
\let\mxaddspace\addspace
\let\addspace\relax
\AtBeginEnvironment{music}{\let\addspace\mxaddspace}
\usepackage[style=authoryear]{biblatex}
\addbibresource{recorder-fingering.bib}
\usetikzlibrary{tikzmark}
\usetikzlibrary{positioning}
\usepackage[colorlinks=true,citecolor=black]{hyperref}

\NewDocumentCommand{\addf}{m}{\tikz[remember picture]{\node[overlay,above=of pic cs:#1]{\Soprano{#1}};}}
\DefineShortVerb{\|}
\title{The \texttt{recorder-fingering} package}
\author{Alan Munn\\amunn@msu.edu}
\date{Version 1.3\\July 17, 2023}
  
\lstset{%
    basicstyle=\ttfamily\small,
    commentstyle=\itshape\ttfamily\small,
    showspaces=false,
    showstringspaces=false,
    breaklines=true,
    breakautoindent=true,
    language=TeX,
    frame=tl,
}
  
\newcommand*{\pkg}[1]{\texttt{#1}}
\newcolumntype{t}[1]{>{\ttfamily}#1}
\newcolumntype{T}{>{\ttfamily}c}
\newcommand*{\bs}{\textbackslash}
\setlength{\droptitle}{-1in}
\renewcommand{\abstractname}{\sffamily Abstract}



\begin{document}
\maketitle
\thispagestyle{empty}
\begin{abstract}{\noindent
The |recorder-fingering| package provides support for generating fingering diagrams for baroque fingering recorders and the tin whistle. Standard fingerings are provided for recorders in both C and F, and the tin whistle in D, along with methods to create and display alternate fingerings for trills, etc. 
}
\end{abstract}


\section{Display commands}
This package provides five recorder commands (|\Sopranino|, |\Soprano|, |\Alto|, |\Tenor|, and |\Bass|) and one tin whistle command (|\Whistle|).\footnote{Because \pkg{musixtex} defines commands \pkg{\bs alto} and \pkg{\bs bass} the fingering display commands are capitalized.}  which will produce a fingering diagram for any note in the playable range of that recorder. Additionally, a set of trill fingering commands are provided to make producing trill fingerings simpler. At present, there are no differences between the three F recorders (sopranino, alto, and bass) and the two C recorders (soprano and tenor), so the different command names are simply there for convenience. A set of dedicated trill fingering display commands is also provided.   These are described in section \ref{trill-fingerings}.


\begin{table}[htpb]
\centering
\begin{tabularx}{.8\textwidth}{t{r}X}
\toprule
\bs Sopranino [<parameters>]\{<note>\} & Display a fingering diagram for note in F\\
\bs Soprano [<parameters>]\{<note>\} & Display a fingering diagram for note in C\\
\bs Alto [<parameters>]\{<note>\} & Display a fingering diagram for note in F\\
\bs Tenor [<parameters>]\{<note>\} & Display a fingering diagram for note in C\\
\bs Bass [<parameters>]\{<note>\} & Display a fingering diagram for note in F\\
\bs Whistle [<parameters>]\{<note>\} & Display a tin whistle  diagram (in D) \\
\bottomrule
\end{tabularx}
\caption{Display commands}
\end{table}
\subsection{Note ranges and notation\label{Notes}}
The notes range from C–Eb for recorders in C (|\Soprano| and |\Tenor|) or F–Ab for recorders in F (|\Sopranino| and |\Alto|), and D-d' for the tin whistle. 

Notes are noted as C-B (first octave) c-b (second octave) and c'–eb' (third octave) with an equivalent scheme starting at F for the recorders in F, and starting at D for the tin whistle.

Enharmonic notes are available for any of the flat/sharp notes (i.e., Db and C\#, etc. are both defined by default.)
\subsection{Display parameters}
Fingering diagrams are customizable either globally using |\fingeringSetup{}| or locally as an optional argument passed to any individual command.  

\begin{table}[htpb]
\centering
\begin{tabularx}{.9\textwidth}{t{l}X}
\toprule
thumboffset = <true/false> & Display the thumb hole offset or not (default = \pkg{true})\\
width = <length> & Width of the diagram (default = \pkg{20pt}). Height adjusts proportionally\\
LHRHdivider = <true/false> & Display a line separating the left and right hand holes (default = \pkg{true})\\
LHRHspace = <length> & Space between the left hand and right hand holds (default = \pkg{10pt})\\
trilldir = <L/R/LR> & trill arrows uniformly to the Left or Right, or top 3 holes L, bottom 4 holes R\\
Lpointer = <left trill pointer> & changes the left pointer for trills\\
Rpointer = <right trill pointer> & changes the right pointer for trills\\
trill = \{<list of holes>\} & add a trill marker at each hole position in the list (local use only)\\
\bottomrule
\end{tabularx}
\caption{Display options}
\end{table}

The |width| parameter sets the width of the diagram; it defaults to |20pt| for the default thumb offset diagrams. If you set |thumboffset=false| you will probably want to choose a smaller value e.g. |10pt|.  Since the tin whistle has no thumb position, I recommend setting the |width| parameter for tin whistle diagrams to |7pt|.

The |trill| parameter sets an arrow pointing to a hole (numbered 1-7 from the top hole). This parameter is only in effect when using the display commands. It will place an arrow on the side specified by |trilldir| at a single hole or comma separated list of holes. e.g. |trill=3| sets an arrow at hole 3, |trill={6,7}| sets arrows at holes 6 and 7.

The trill arrow direction can be specified using the |trilldir| parameter. Setting it to |L| places all arrows on the left, setting it to |R| places all arrows on the right. Setting it to |LR| places arrows to the left on holes 1-3 and on the right on holes 4-7.

The |Lpointer| and |Rpointer| parameters set the left and right trill pointers. The default values are |Lpointer={$\rightarrow$}| and |Rpointer={$\leftarrow$}|.

You can set the parameters individually for any particular display command, but most likely you'll want global settings. To set the parameters globally use the command |\fingeringSetup|

e.g. |\fingeringSetup{width=12pt,thumboffset=false}| makes all diagrams use an inline thumb hole and 12pt width.
\begin{table}[htpb]
\centering
\begin{tabular}{TTT}
\bs Alto\{c\} & \bs Soprano[width=10pt,thumboffset=false]\{f\} & \bs Alto[trill=3]\{C\}\\
\\
\Alto{c} & \Soprano[width=10pt,thumboffset=false]{f} & \Alto[trill=3]{C}
\end{tabular}
\caption{Example fingering commands}
\end{table}
\section{Producing new fingerings}

There are four commands to add new fingerings to the predefined list, or in fact, to change the existing defaults.
\subsection{Note names}
Although the basic predefined fingerings use the note naming schema outlined in section \ref{Notes}, the note names themselves are arbitrary. This allows you to add trill fingerings or alternate fingerings with meaningful names, e.g. |ftrill| or |altF| are possible names for new fingerings.
\subsection{Fingering vectors\label{fingering-vectors}}
Fingerings are specified using an 8 (and possibly 9) element comma separated list (7 for the tin whistle\footnote{Since I don’t play the tin whistle, I don’t know if there are any esoteric fingerings similar to those in the recorder that use the knee to close the bell hole. But in case they do exist, it’s possible to specify a 7th hole for D fingerings.}). For the recorders, each position in the list denotes a hole starting from the thumb (position 0) to the bell (position 8). For the tin whistle, each position in the list denotes a hole. Each hole position is indicated by a hole state according to the following scheme:

\begin{table}[htpb]
\centering
\begin{tabularx}{.8\textwidth}{t{r}X}
\toprule
0 &  open hole\\
1 &  closed hole (positions 0-6); or single closed hole (recorder positions 6,7)\\
2 &  double closed hole (recorder positions 6,7 only)\\
t &  half hole (thumb)\\
h &  half hole (positions 1-6)\\
\bottomrule
\end{tabularx}
\caption{Hole states}
\end{table}

\subsection{New fingering commands}

Three commands |\NewFfingering|, |\NewCfingering|, and |\NewDfingering| are designed to add a single fingering. They check whether the name for the fingering is already used and produce an error if so. To overwrite an existing fingering, use |\NewFfingering*|, |\NewCfingering*| or |\NewDfingering*|.

The fourth command is designed to enter a set of new fingerings at once. It will override any existing note names. It takes two arguments, a key, (F, C, or D) and a key value list consisting of note names plus fingering vectors. 

\begin{table}[htpb]
\centering
\begin{tabularx}{.8\textwidth}{X}
\toprule
\texttt{\bs NewFfingering\{<notename>\}\{<fingering vector>\}} \\\rule{3em}{0pt}Add a new note and fingering to the F recorder set\\
\texttt{\bs NewCfingering\{<notename>\}\{<fingering vector>\}} \\\rule{3em}{0pt} Add a new note and fingering to the C recorder set\\
\texttt{\bs NewDfingering\{<notename>\}\{<fingering vector>\}} \\\rule{3em}{0pt} Add a new note and fingering to the tin whistle set\\
\texttt{\bs AddFingerings\{<key>\}\{<note = \{<fingering vector>\}>\}} \\\rule{3em}{0pt} Add a set of new fingerings to the F, C or D instrument set\\
\bottomrule
\end{tabularx}
\caption{Commands for adding new fingerings}
\end{table}

\section{Trill fingerings}
Because trill fingerings are likely to be displayed with, rather than without, their trill annotations, a separate set of trill fingering macros is provided so that trill fingerings can be stored along with their trill annotations and produced without needing to add the trill annotation manually. 

\begin{table}[htpb]
\centering
\begin{tabularx}{.8\textwidth}{X}
\toprule
\texttt{\bs NewFtrill\{<notename>\}\{<fingering vector>\}\{<trill vector>\}} \\\rule{3em}{0pt}Add a new trill and fingering to the F recorder set\\
\texttt{\bs NewCtrill\{<notename>\}\{<fingering vector>\}\{<trill vector>\}} \\\rule{3em}{0pt} Add a new trill and fingering to the C recorder set\\
\texttt{\bs NewDtrill\{<notename>\}\{<fingering vector>\}\{<trill vector>\}} \\\rule{3em}{0pt} Add a new trill and fingering to the tin whistle set\\
\texttt{\bs AddTrills\{<key>\}\{<note = \{\{<fingering vector>\}\},\{<trill vector>\}\}>\}} \\\rule{3em}{0pt} Add a set of new trills to the F, C or D instrument set\\
\bottomrule
\end{tabularx}
\caption{Commands for adding new trill fingerings}
\end{table}

The three commands |\NewFtrill|, |\NewCtrill|, and |\NewDtrill| take two arguments: a fingering vector, and a trill vector. The fingering vector is the same as described in section \ref{fingering-vectors}. The trill vector indicates which holes will be annotated with trill arrows when displayed using one of the dedicated trill display macros. 

The fourth command takes two arguments: a key (F,C, or D) and a key value set of ordered pairs of vectors, a fingering vector and a trill vector.  

\subsection{Displaying trill fingerings\label{trill-fingerings}}
Fingerings that are created using the trill fingering macros can be displayed along with their associated trill annotations using the commands |\Ftrill|, |\Ctrill|, and |\Dtrill|.

\begin{table}[htpb]
\centering
\begin{tabularx}{.8\textwidth}{t{r}X}
\toprule
\bs Ftrill [<parameters>]\{<note>\} & Display a trill diagram for note in F\\
\bs Ctrill [<parameters>]\{<note>\} & Display a trill diagram for note in C\\
\bs Dtrill [<parameters>]\{<note>\} & Display a tin whistle trill diagram (in D) \\
\bottomrule
\end{tabularx}
\caption{Trill display commands}
\end{table}

Note that the package does not come with any pre-coded trill fingerings, so these commands will not work unless you have added trill fingerings yourself. 

\clearpage
\section{New fingering examples}
Here are some examples of how to add new fingerings and trills.

\AddFingerings{F}{
altD = {1,0,1,1,1,1,0,0},
altE = {1,0,1,1,0,0,0,0},
altAb = {t,1,1,1,1,1,1,0}
}
\AddTrills{F}{
BbA = {{0,1,1,1,1,1,0,0},{5}},
C#D ={{1,1,1,0,1,1,1,0},{4,5}}
}
\NewCfingering{alte}{t,1,1,1,1,0,2,2}
\NewCtrill{ef#}{t,1,1,1,0,1,0,0}{4}
\NewCtrill{gab}{t,1,1,0,1,0,2,0}{6}
	
\begin{lstlisting}
\AddFingerings{F}{
altD = {1,0,1,1,1,1,0,0},
altE = {1,0,1,1,0,0,0,0},
altAb = {t,1,1,1,1,1,1,0}
}
\Alto{altD}\quad
\Alto{altE}\quad
\Alto{altAb}
\end{lstlisting}

\Alto{altD}\quad
\Alto{altE}\quad
\Alto{altAb}

\begin{lstlisting}
\AddTrills{F}{
BbA = {{0,1,1,1,1,1,0,0},{5}},
C#D ={{1,1,1,0,1,1,1,0},{4,5}}
}
\Ftrill{BbA}\quad
\Ftrill{C#D}\quad
\end{lstlisting}

\Ftrill{BbA}\quad
\Ftrill{C#D}\quad

\begin{lstlisting}
\NewCfingering{alte}{t,1,1,1,1,0,2,2}
\NewCtrill{ef#}{t,1,1,1,0,1,0,0}{4}
\NewCtrill{gab}{t,1,1,0,1,0,2,0}{6}
\Soprano{alte}\quad
\Ctrill{ef#}\quad
\Ctrill{gab}
\end{lstlisting}
\Soprano{alte}\quad
\Ctrill{ef#}\quad
\Ctrill{gab}
\clearpage

\section{Version History}
Version 0.5 of this package (the initial version) was in response to a \href{https://tex.stackexchange.com/q/674847/2693}{TeX.se question} and used a substantially different set of user commands. The current version (1.x) is more flexible and has a more useable user interface. Version 1.1 added support for trill annotation, and added a separation between the left and right hand holes.  Version 1.2 added support for the tin whistle. Version 1.3 added dedicated trill fingering commands.  Bug reports and feature requests are welcome at the \href{https://github.com/amunn/recorder-fingering/issues}{GitHub bug tracker}. 
\section{Acknowledgements}
This is my first attempt to write a package using |expl3|. Thanks to Jonathan P. Spratte for suggesting how to split fingering vectors and the various LaTeX development team members (David Carlisle, Ulrike Fischer, Phelype Olenik and Joseph Wright) who have answered random |expl3| questions in the TeX.se chat. Thanks to Bob Tennent, whose suggestions resulted in version 1.1.  Thanks also to all the TeX.se users who have both asked and answered questions on the site. Your questions encourage new packages like this one and your answers help make the code better. Thanks to Charles-Albert Lehalle for the suggestion to add the tin whistle. 
\section{Sources}
The core fingerings for recorders are taken from \textcite{Wollitz1981}. The core fingerings for the tin whistle are taken from \textcite{GreyLarsenWhistleFingerings}. The sample recorder trill fingerings are from \textcite{BlockisRecorderFingerings}.

\printbibliography[heading=none]
\clearpage
\section{Samples} The charts below shows all of the defaults fingerings for the C recorders and the tin whistle.\footnote{Since the top row of the table represents the input name, \pkg{b} and  \pkg{\#} are used instead the proper symbols \flat\ and \sharp.}

\section*{Soprano recorder in C  fingering chart}
\begin{tabular}{cccccccccccc}
\toprule
C & C\# & D & Eb & E & F & F\# & G & Ab & A & Bb & B\\ 
\midrule
\Soprano{C} & 
\Soprano{C#} & 
\Soprano{D} & 
\Soprano{Eb} & 
\Soprano{E} &
\Soprano{F} &
\Soprano{F#} &
\Soprano{G} &
\Soprano{Ab} &
\Soprano{A} &
\Soprano{Bb} &
\Soprano{B}\\
\bottomrule
\end{tabular}

\bigskip
\begin{tabular}{cccccccccccc}
\toprule
c & c\# & d & eb & e & f & f\# & g & ab & a & bb & b\\ 
\midrule
\Soprano{c} &
\Soprano{db} &
\Soprano{d} &
\Soprano{eb} &
\Soprano{e} &
\Soprano{f} &
\Soprano{gb} &
\Soprano{g} &
\Soprano{ab} &
\Soprano{a} &
\Soprano{bb} &
\Soprano{b}\\
\bottomrule
\end{tabular}

\bigskip
\begin{minipage}[t]{.66\linewidth}
\fingeringSetup{width=7pt}
\section*{Tin whistle in D fingering chart}
\begin{tabular}{*{12}c}
\toprule
D & D\# & E & F & F\# & G & G\# & A & A\# & B & C & C\#\\ 
\midrule
\Whistle{D} & 
\Whistle{D#} & 
\Whistle{E} &
\Whistle{F} &
\Whistle{F#} &
\Whistle{G} &
\Whistle{G#} &
\Whistle{A} &
\Whistle{A#} &
\Whistle{B} &
\Whistle{C} & 
\Whistle{C#}\\
\bottomrule
\end{tabular}

\bigskip
\begin{tabular}{*{13}c}
\toprule
d & d\# & e & f & f\# & g & g\# & a & a\# & b & c & c\# & d' \\ 
\midrule
\Whistle{d} &
\Whistle{d#} &
\Whistle{e} &
\Whistle{f} &
\Whistle{f#} &
\Whistle{g} &
\Whistle{g#} &
\Whistle{a} &
\Whistle{a#} &
\Whistle{b} &
\Whistle{c} &
\Whistle{c#} &
\Whistle{d'} \\
\bottomrule
\end{tabular}
\end{minipage}
\fingeringSetup{width=10pt,thumboffset=false}
\hfill
\begin{minipage}[t]{.33\linewidth}
\section*{\raggedright Example diagrams  with \texttt{thumboffset=false}}
\begin{tabular}{cccc}
\toprule
c' & c\#' & d' & eb'\\
\midrule
\Soprano{c'} &
\Soprano{db'} &
 &
\Soprano{eb'}\\
\bottomrule
\end{tabular}
\end{minipage}
\clearpage
\section{Using in conjunction with \texttt{musixtex}}
\fingeringSetup{thumboffset=true,width=12pt}
It's simple to add fingerings on top of musical excerpts created with |musixtex| using the |tikzmark| library to place the fingerings.  Here's a sample of what you can do, with the code below.

\subsection*{C Maj scale Soprano/Tenor recorder fingerings}
\vspace{\heightof{\Soprano{C}}+\baselineskip}
\begin{music}
\setlyrics{scale}{C D E F G A B C}
\lyrraise{1}{b-2ex}
\assignlyrics{1}{scale}
\startextract
\NOTEs
\tikzmark{C}\wh{c}\tikzmark{D}\wh{d}\tikzmark{E}\wh{e}\tikzmark{F}\wh{f}
\tikzmark{G}\wh{g}\tikzmark{A}\wh{h}\tikzmark{B}\wh{i}\tikzmark{c}\wh{j}
\en
\zendextract
\end{music}
\addf{C}\addf{D}\addf{E}\addf{E}\addf{F}\addf{G}\addf{A}\addf{B}\addf{c}
\begin{lstlisting}
%\usetikzlibrary{tikzmark} % in document premable
%\usetikzlibrary{positioning} % in document preamble
%\usepackage{calc} % in document preamble
% Helper command to place each fingering
\NewDocumentCommand{\addf}{m}{\tikz[remember picture]{\node[overlay,above=of pic cs:#1]{\Soprano{#1}};}}
% add vertical space for the height of the diagram 
\vspace{\heightof{\Soprano{C}}+\baselineskip}
\begin{music}
\setlyrics{scale}{C D E F G A B C}
\lyrraise{1}{b-2ex}
\assignlyrics{1}{scale}
\startextract
\NOTEs
% add \tikzmark for before each note
\tikzmark{C}\wh{c}\tikzmark{D}\wh{d}\tikzmark{E}\wh{e}\tikzmark{F}\wh{f}
\tikzmark{G}\wh{g}\tikzmark{A}\wh{h}\tikzmark{B}\wh{i}\tikzmark{c}\wh{j}
\en
\zendextract
\end{music}
% use the helper function to add the fingerings (requires two compilations)
\addf{C}\addf{D}\addf{E}\addf{E}\addf{F}\addf{G}\addf{A}\addf{B}\addf{c}
\end{lstlisting}
\end{document} 
