% Song.sty --- Anton de Wet (adw@chopin.rau.ac.za, adw@obsidian.co.za)
% Last modified : 14 September 1995
% Style sheet to type transposable chords with song lyrics
%
%    Copyright (C) 1994,1995  <Anton P.C. de Wet>
%
%    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 2 of the License, or
%    (at your option) any later version.
%
%    This code 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.
%
%
% A quick introduction to song.sty
%
% This package was originally written for latex 2.09. It works well with
% LaTeX 2e, but doesn't conform to the 2e package standards (yet). This
% might change sometime in the future.
%
% I also use a4 paper and this style assumes this and uses a large
% portion of the page. Feel free to modify this for your needs.
%
% 
% 1) Use ``\usepackage{song}'', it  works well with \twocolumn
% 
% 2) Define key of song at start using eg. \Cmaj  \Fshmaj \Amin
%    \Bflmin etc.
%
% 3) Chords are typed by prepending \c before a roman numeral (i -- vii)
%    and (possibly) appending a modifier eg. m for minor, ds for
%    dominant seven, n for nine etc. Type in chords right before the
%    position you want it above the lyrics eg: 
%
%    \Gmaj
%    \ci My Love in \cv your e\civ bony eyes.2
%
%    Won't put a space in ebony, but put a C chord right above the b.
%    To write sequences of chord without lyrics, the chords must be
%    separated with a ``chord space'' \csp so for eg. \ci \csp \cv
%    \csp \ci would typeset an instumental perfect cadence.
%    Another way of doing this would be to use \notei\ \notevi\ \notei\
%    Which would put it at the same height as noram text. Please note,
%    the \note sequences do not support modifiers. i.e. type \noteiii m
%    for a minr third chord and not \noteiim
%
% 4) The predefined chord modifiers aren't exhausitve, but defining
%    one is simple. The command \chordgroup takes two operands. The
%    first is the modifier string, the second the symbols to be
%    typeset when this modifier is encountered eg. \chordgroup{th}{13}
%    will define th to be the modifier that adds 13 to a chord. So
%    you'd be able to use \ciiith in E maj to get the chord G#13.
%    Currently the following modifiers are predefined:
%
%       minor           : m    -> m
%       diminished      : dim  -> dim
%       augmented       : aug  -> aug
%       dom. seven      : ds   -> 7
%       suspended       : sus  -> sus
%       seventh susp.   : ssus -> 7sus
%       major seventh   : majs -> maj7
%       minor seventh   : ms   -> m7
%       nine            : n    -> 9
%       minor add 6     : msix -> m6
%
%    Which are basically the ones I've used since the last major
%    rewite of song.sty If you think I should use                                
%
% 5) Base notes can be added using the notation eg. \ci\bn{\noteiii}
%    to get the output C/E in the key of C maj. A note foreign to the
%    key can be specified using eg. \up\noteiv would get you F# in C maj
%    and \down\noteii would get you C#.
%
% 6) A general procedure for putting text above lyrics is provided in
%    two functions: \c (overwriting the cedilla in an anglo centric
%    way) and \varc. This can be used to typeset a F# chord in C maj:
%    \c{\up\noteiv}.
%
% 7) Modulation between keys is handled with the commands \modup and
%    \moddown, each taking two operands. The first is the number of half
%    tones to modulate (up or down respectively) and the second is either
%    an f or s indicating whether the new key contains sharps or flats.
%    So to go from C to G maj you'd use \modup{5}{s}
%    Keep in mind that TeX scope rules still apply so an isolated
%    modulation can be surrounded by braces and the modulation will
%    revert at the close of the braces i.e.
%
%    \Cmaj This is in C {\modup{5}{s} And this in G} and back to C.
% 
% 8) Typing the verses, end each line with a \= and the end of the
%    verse with a \nv (for new verse). This is supposed to constrain
%    page breaks during verses. This is not always successful but the
%    commands should improve with later versions.
%    
% 9) As a typing aid, i've included a command \setchorus that takes as
%    argument the (you guessed it) the chorus of a song. After
%    defining the chorus, it can be repeated by just typing \chorus
%    The chorus is printed in boldface (\bf).
%
% 10)I also define \songtitle to typeset its one argument in \large\bf
%
% Please feel free to email me with bugs, suggestions and questions
% at: adw@chopin.rau.ac.za or adw@obsidian.co.za
%
%
% setup page size to use much of an a4
%    **** ToDo: in Latex2e make normalpage an option
\pagestyle{empty}
\oddsidemargin 0in
\evensidemargin 0in
\topmargin -0.5in
\headheight 0cm
\headsep 0cm
\textheight 10.5in
\textwidth 6in
\columnsep 1.5cm
\parindent 0cm
\parskip 0cm
\raggedbottom

% Expect to use chord symbols --- increase distance between lines
\renewcommand{\baselinestretch}{2}

% In an attempt to use the LaTeX command existance checking, declare
% all commands using \newcommand before \def'ining any command

% basic commands
  % abbreviations
    \newcommand{\sh}{$\sharp$}
    \newcommand{\fl}{$\flat$}

  % the basic command to put objects above lines of text

    % first some holding registers 
    \newsavebox{\chord}
    \newsavebox{\tempchord}
    \newdimen\chordwidth
    \newdimen{\tempchordwidth}
 
    % The main chord setting command --- NB no spaces
    \newcommand{\putchord}[1]{%
      \savebox{\chord}[0pt][l]{#1}%
      \savebox{\tempchord}{#1}%
      \chordwidth=\wd\tempchord
      \raisebox{1em}{\usebox{\chord}}}

    % Command to typeset base notes of chords
    \newcommand{\bn}[1]{\tempchordwidth=\chordwidth 
      \hspace{\chordwidth}\putchord{/#1}\hspace{-\tempchordwidth}}

    % ***** ToDo: make this work nicely with \csp

    % Spacing between chords without words must be explicit
    % \csp stands for Chord space
    \newcommand{\csp}{\hspace{\chordwidth} }       

  % primitive commands for use in documents
    \renewcommand{\c}[1]{\putchord{#1}}   % simply specifying the chords 
                                          % assumes no use of cedilla accent
    \newcommand{\varc}[1]{\putchord{#1}}  % simply specifying the
                                          % chords (old version)

% Internal representation of notes in key

  % provide the basic commands

  \newcommand{\notei}{}
  \newcommand{\noteii}{}
  \newcommand{\noteiii}{}
  \newcommand{\noteiv}{}
  \newcommand{\notev}{}
  \newcommand{\notevi}{}
  \newcommand{\notevii}{}
  \newcommand{\wholeinc}{}
  \newcommand{\halfinc}{}


% Two note incrementation routines for keys with sharps and keys with
% flats with compare routine
% **** ToDo: sort out the modulation bugs in keys with many(5+) sharps
%            and flats 

  % String compare command that ignores \sh and \fl
  % The Result is stored in \ifsresult and a success flag in (neg)\ifsflag
    \newcommand{\ifs}{}
    \newcommand{\ifsresult}{}
    \newcommand{\testone}{}
    \newcommand{\testtwo}{}
    \newif\ifsflag
    \def\ifs#1#2#3{\ifsflag{\def\sh{P}\def\fl{M}%
        \xdef\testone{#1}\xdef\testtwo{#2}}%
        \ifx\testone\testtwo\def\ifsresult{#3}\sflagfalse\fi\fi}

  \newcommand{\incsharpnote}[1]{%
  \sflagtrue
  \ifsflag\ifs{#1}{C}{C\sh}\fi
  \ifsflag\ifs{#1}{C\sh}{D}\fi
  \ifsflag\ifs{#1}{D}{D\sh}\fi
  \ifsflag\ifs{#1}{D\sh}{E}\fi
  \ifsflag\ifs{#1}{E}{F}\fi
  \ifsflag\ifs{#1}{F}{F\sh}\fi
  \ifsflag\ifs{#1}{F\sh}{G}\fi
  \ifsflag\ifs{#1}{G}{G\sh}\fi
  \ifsflag\ifs{#1}{G\sh}{A}\fi
  \ifsflag\ifs{#1}{A}{A\sh}\fi
  \ifsflag\ifs{#1}{A\sh}{B}\fi
  \ifsflag\ifs{#1}{B}{C}\fi
  \ifsflag\ifs{#1}{D\fl}{D}\fi
  \ifsflag\ifs{#1}{E\fl}{E}\fi
  \ifsflag\ifs{#1}{G\fl}{G}\fi
  \ifsflag\ifs{#1}{A\fl}{A}\fi
  \ifsflag\ifs{#1}{B\fl}{B}\fi
  \ifsflag\def\ifsresult{Unknown Chord}\fi}

  \newcommand{\incflatnote}[1]{%
  \sflagtrue
  \ifsflag\ifs{#1}{C}{D\fl}\fi
  \ifsflag\ifs{#1}{D\fl}{D}\fi
  \ifsflag\ifs{#1}{D}{E\fl}\fi
  \ifsflag\ifs{#1}{E\fl}{E}\fi
  \ifsflag\ifs{#1}{E}{F}\fi
  \ifsflag\ifs{#1}{F}{G\fl}\fi
  \ifsflag\ifs{#1}{G\fl}{G}\fi
  \ifsflag\ifs{#1}{G}{A\fl}\fi
  \ifsflag\ifs{#1}{A\fl}{A}\fi
  \ifsflag\ifs{#1}{A}{B\fl}\fi
  \ifsflag\ifs{#1}{B\fl}{B}\fi
  \ifsflag\ifs{#1}{B}{C}\fi
  \ifsflag\ifs{#1}{C\sh}{D}\fi
  \ifsflag\ifs{#1}{D\sh}{E}\fi
  \ifsflag\ifs{#1}{F\sh}{G}\fi
  \ifsflag\ifs{#1}{G\sh}{A}\fi
  \ifsflag\ifs{#1}{A\sh}{B}\fi
  \ifsflag\def\ifsresult{Unknown Chord}\fi%
  }

% Using these definitions closer to music terms can be made: 
%   \wholeinc and \halfinc 
%   the definitions change according to the specific key type
%   the result is stored in \incresult

\newcommand{\incresult}{}
\newcommand{\wholeresult}{}
\newcommand{\sharpkey}{%
\def\halfinc##1{\incsharpnote{##1}\let\incresult=\ifsresult}%
\def\wholeinc##1{\incsharpnote{##1}\let\wholeresult=\ifsresult%
\incsharpnote{\wholeresult}\let\incresult=\ifsresult}}

\newcommand{\flatkey}{%
\def\halfinc##1{\incflatnote{##1}\let\incresult=\ifsresult}%
\def\wholeinc##1{\incflatnote{##1}\let\wholeresult=\ifsresult%
\incflatnote{\wholeresult}\let\incresult=\ifsresult}}

% For typesetting porposes provide \up and \down to nudge notes up or
% down one tone
\newcommand{\down}{}
\newcommand{\up}{}
\def\down#1{\typeout{!!!!! Down not yet implemented}}
\def\up#1{\halfinc{#1}\incresult}

% Then the definitions of a Major and a minor key:
% keep last type in \keytype
\newcommand{\makemajor}[1]{\def\keytype{maj}%
  \def\notevii{#1}\let\notei=\notevii 
  \wholeinc{\notei  }\let\noteii =\incresult
  \wholeinc{\noteii }\let\noteiii=\incresult
  \halfinc {\noteiii}\let\noteiv =\incresult
  \wholeinc{\noteiv }\let\notev  =\incresult
  \wholeinc{\notev  }\let\notevi =\incresult
  \wholeinc{\notevi }\let\notevii=\incresult}

\newcommand{\Cmaj}{\sharpkey\makemajor{C}}
\newcommand{\Gmaj}{\sharpkey\makemajor{G}}
\newcommand{\Dmaj}{\sharpkey\makemajor{D}}
\newcommand{\Amaj}{\sharpkey\makemajor{A}}
\newcommand{\Emaj}{\sharpkey\makemajor{E}}
\newcommand{\Bmaj}{\sharpkey\makemajor{B}}
\newcommand{\Fshmaj}{\sharpkey\makemajor{F\sh}}%****** note vii wrong
\newcommand{\Cshmaj}{\sharpkey\makemajor{C\sh}}%****** note iii and vii wrong

\newcommand{\Fmaj}{\flatkey\makemajor{F}}
\newcommand{\Bflmaj}{\flatkey\makemajor{B\fl}}
\newcommand{\Eflmaj}{\flatkey\makemajor{E\fl}}
\newcommand{\Aflmaj}{\flatkey\makemajor{A\fl}}
\newcommand{\Dflmaj}{\flatkey\makemajor{D\fl}}
\newcommand{\Gflmaj}{\flatkey\makemajor{G\fl}}%******* note iv wrong

\newcommand{\makeminor}[1]{\def\keytype{min}%
  \def\notevii{#1}\let\notei=\notevii 
  \wholeinc{\notei  }\let\noteii =\incresult
  \halfinc {\noteii }\let\noteiii=\incresult
  \wholeinc{\noteiii}\let\noteiv =\incresult
  \wholeinc{\noteiv }\let\notev  =\incresult
  \halfinc {\notev  }\let\notevi =\incresult
  \wholeinc{\notevi }\let\notevii=\incresult % inc 7th note another half 
}% \halfinc {\notevii}\let\notevii=\incresult} % ***** Check correct use of vii in minor

\newcommand{\Amin}{\sharpkey\makeminor{A}}
\newcommand{\Emin}{\sharpkey\makeminor{E}}
\newcommand{\Bmin}{\sharpkey\makeminor{B}}
\newcommand{\Fshmin}{\sharpkey\makeminor{F\sh}}
\newcommand{\Cshmin}{\sharpkey\makeminor{C\sh}}
\newcommand{\Gshmin}{\sharpkey\makeminor{G\sh}}
\newcommand{\Dshmin}{\sharpkey\makeminor{D\sh}} % ***** wrong notes
\newcommand{\Ashmin}{\sharpkey\makeminor{A\sh}} % ***** wrong notes

\newcommand{\Dmin}{\flatkey\makeminor{D}}
\newcommand{\Gmin}{\flatkey\makeminor{G}}
\newcommand{\Cmin}{\flatkey\makeminor{C}}
\newcommand{\Fmin}{\flatkey\makeminor{F}}
\newcommand{\Bflmin}{\flatkey\makeminor{B\fl}}
\newcommand{\Eflmin}{\flatkey\makeminor{E\fl}} % wrong notes


  % possible chord extensions
    \newcommand{\minor}{m}
    \newcommand{\suspended}{sus}
    \newcommand{\augmented}{aug}
    \newcommand{\diminished}{dim}
    \newcommand{\dominantseven}{7}
    \newcommand{\majorseven}{maj7}
    \newcommand{\minorseven}{m7}
    \newcommand{\nine}{9}

% define chordmaker with 3 arguments
%   1: roman numeral <r>
%   2: cs append string <css>
%   3: chord append string <app>
% This creates a control sequence \c<r><css>
%  with definition \putchord{\note<r> <app>}
%  Uses \tempcs and \tempnote and \putcschord

\newcommand{\chordmaker}{}
\newcommand{\tempcs}{}
\newcommand{\putcschord}{}

\def\putcschord#1#2{\putchord{\csname#1\endcsname#2}}
\def\chordmaker#1#2#3{{\edef\tempcs{\csname c\romannumeral#1#2\endcsname}
  \xdef\tempnote{note\romannumeral#1}
  \let\putcschord=\relax        
  \expandafter\xdef\tempcs{\putcschord{\tempnote}{#3}}}}

% define \chordgroup with 2 arguments
%  1: cs append string <css>
%  2: chord append string <app>
% This creates seven control sequences 
%    \c<n><css> = \putchord{\note<n><app>}
% Uses \chordcounter

\newcommand{\chordcounter}{}
\newcommand{\chordgroup}{}
\newcount\chordcounter
\def\chordgroup#1#2{\chordcounter=0
  \loop\ifnum\chordcounter<7 \advance\chordcounter by1
    \chordmaker\chordcounter{#1}{#2}\repeat}

% Make relevant chord groups

\chordgroup{}{}
\chordgroup{m}{m}
\chordgroup{dim}{dim}
\chordgroup{aug}{aug}
\chordgroup{ds}{7}
\chordgroup{sus}{sus}
\chordgroup{ssus}{7sus}
\chordgroup{majs}{maj7}
\chordgroup{ms}{m7}
\chordgroup{n}{9}
\chordgroup{msix}{m6}


% Modulation
%
% Define commands
% \modup{<nr>}{f|s}
% \moddown{<nr>}{f|s}
%   <nr> = number of half tones
%   f|s  = indicates if new key is flat or sharp key
% Uses \modcounter, \moddowncounter \modresult and \modkey

\newcommand{\modup}{}
\newcommand{\moddown}{}
\newcommand{\modcounter}{}
\newcommand{\moddowncounter}{}
\newcommand{\modresult}{}
\newcommand{\modkey}{}

\newcount\modcounter
\def\modup#1#2{\ifx#2s\sharpkey\fi\ifx#2f\flatkey\fi
      \modcounter=0
      \let\modresult=\notei
      \loop\ifnum\modcounter<#1 \advance\modcounter by1
        \halfinc{\modresult}\let\modresult=\incresult\repeat
      {\def\sh{sh}\def\fl{fl}%
       \xdef\modkey{\modresult\keytype}}
      \csname\modkey\endcsname}

\newcount\moddowncounter
\def\moddown#1#2{\moddowncounter=12\advance\moddowncounter by-#1
   \modup{\moddowncounter}{#2}}
     
  
  

% Other typographic commands

\newcommand{\songtitle}[1]{{\large\bf#1}\par\vspace{2ex}}
\newcommand{\nv}{\pagebreak[3]\newline}
\renewcommand{\=}{\hfill\\*}
\newcommand{\twocol}{\twocolumn}
\newcommand{\chorus}{}
\newcommand{\setchorus}[1]{{\bf#1}\renewcommand{\chorus}{{\bf#1}}}


