\documentclass[a4paper,twoside,openany,10pt,swedish]{book}
%\documentclass[a4paper,twoside,openany,10pt,swedish,draft]{book}

\usepackage[swedish]{babel}
\usepackage[utf8]{inputenc}
\selectlanguage{swedish}
%\usepackage{bookman}
\usepackage{graphicx}
%\usepackage[T1]{tipa}
\usepackage{array}
%\usepackage{mathabx}
%\usepackage{dvgloss}
\usepackage{relsize}
\usepackage{yfonts}
\usepackage{url}
\usepackage{ifthen}
%\usepackage{textfit}
%\usepackage{dcolumn}
%\usepackage{tabularx}
%\usepackage{textcomp}
%\usepackage{calc}
%\usepackage{fancyvrb}

\usepackage{multicol}
\setlength{\columnseprule}{0.2pt}

%\newcommand{\betweenLilyPondSystem}[1]{\\[5mm]}
%\newcommand{\TI}[0]{\textprimstress}
%\newcommand{\TB}[0]{\begin{tabular}{@{}p{43mm}p{52mm}p{51mm}@{}}}

%\usepackage{fancyvrb}
%\RecustomVerbatimEnvironment{Verbatim}{Verbatim}{xleftmargin=5mm,xrightmargin=5mm,frame=single}
%\DefineVerbatimEnvironment{verbatim}{Verbatim}{xleftmargin=4mm}

\usepackage{verbatim}
\newlength\myverbindent 
\setlength\myverbindent{5mm} % change this to change indentation
\makeatletter
\def\verbatim@processline{\hspace{\myverbindent}\the\verbatim@line\par}
\makeatother
\newenvironment{code}{\footnotesize\verbatim}{\endverbatim\normalsize}

\newcommand{\asdf}[1]{\texttt{#1}}
%\usepackage{listings}

\usepackage{fancyhdr}
%\renewcommand{\sectionmark}[1]{\markboth{#1}}
\rhead[\leftmark]{\rightmark}
\chead[\thepage]{\thepage}
\lhead[\rightmark]{\leftmark}
\lfoot[]{}
\cfoot[]{}
\rfoot[]{}
\renewcommand{\headrulewidth}{0pt}
\raggedbottom
%\fancypagestyle{plain}{\fancyhead{}} % get rid of headers on plain pages

\newcommand{\antiqua}[1]{{\fontencoding{T1}\fontfamily{zaq}\selectfont #1}}

\newcommand{\person}[1]{\textbf{#1}}
\newcommand{\LYIND}{\noindent}
\newcommand{\NEWP}{\pagestyle{plain}\makebox[1mm]{}\newpage\pagestyle{fancy}}
%\newcommand{\LYIND}{\hspace{10mm}}
\newcommand{\ind}{\makebox[3mm]{}}
\newcommand{\I}{\makebox[1ex]{}}
\newcommand{\AST}{\raisebox{.2ex}{$\ast$}}
\newcommand{\life}[2]{(#1---#2)}
\newcommand{\Life}[2]{(\AST #1, \dag #2)}
\newcommand{\Exp}[1]{\raisebox{.7ex}{\relsize{-2}#1}}
\newcommand{\DT}[6]{%
  \makebox[27.5ex][l]{#1 #2\Exp{#3}--#4\Exp{#5} #6}
}
\newcommand{\citat}[1]{
\begin{quote}
  \begin{multicols}{2}
    \relsize{-1}
    #1
  \end{multicols}
\end{quote}
}
\newcommand{\rtext}[2]{
  \noindent%
  #1:\\[0.3ex]
  \makebox[1em]{ }
  \begin{minipage}[t]{150mm}
    #2
  \end{minipage}
  \vspace{-1ex}
}

\begin{document}
 --- Midi to lilypond

\newpage
\section*{introduction}

On the web there are many scores available as pdfs.
Usually thoose scoreds are sufficient but one problem is that pdfs are
hard to edit, eg. how do you transpose the score up one tone, or
how do get parts from a partiture ?
 Most of the time thoose pdfs are accompanied with a midi file and
you can convert the midi file to lilypond code and then you can edit
as you wish.
 The midi file format is described in [1] or by registering an follow
the links in [2]. The RP-017 and RP-026 recommendations are also of
interest.
 For an example of the conversion lets take [3] and [4]. As you can see
in the pdf, the tenor in choir 1 have an d'' in measure 25 and you might
want to lower the pitch to help them.

\subsection*{midi2ly}

Along with lilypond there is a program midi2ly which can do the 
conversion:

\begin{code}
$ midi2ly stabat-palestrina-e.mid 
LY output to 'stabat-palestrina-e-midi.ly'...
\end{code}
%$

\subsubsection*{first problem: lyric char set}

Unfortunately this score contains non-utf8 characters in the lyrics:

\begin{code}
$ lilypond --ps stabat-palestrina-e-midi.ly 
GNU LilyPond 2.19.16
Processing `stabat-palestrina-e-midi.ly'
Parsing...
stabat-palestrina-e-midi.ly:69:5: warning: non-UTF-8 input
  "p
    ?"2 "nas " 
...
\end{code}
%$

which is measure 36 in the soprano and should read \verb|pœ -- nas| (i.e. with
a ligature oe) in lilypond code. The error message above tells us the
line number (65), so we can what char codes are used:

\begin{code}
$ sed -ne '69p' stabat-palestrina-e-midi.ly | od -ta -tx1
0000000  sp  sp   "   p  fs   "   2  sp   "   n   a   s  sp   "  sp  nl
         20  20  22  70  9c  22  32  20  22  6e  61  73  20  22  20  0a
0000020
\end{code}
%$

Here we see that the code \verb|0x9c| is used.
Guessing that this is cp1252 and verifying with [5], 
I can convert it to utf-8.
But since cp1252 has \verb|\r\n| line endings, I must change the line
endings with unix2dos (at least on Unix systems), before I recode the file:

\begin{code}
$ unix2dos stabat-palestrina-e-midi.ly 
unix2dos: converting file stabat-palestrina-e-midi.ly to DOS format ...
$ recode cp1252..u8 < stabat-palestrina-e-midi.ly > test.ly
\end{code}

\subsubsection*{second problem, combining syllables into words}

``Recommended Practice (RP-017) SMF Lyric Meta Event Definition''
(in [2] under RP-017) states that

\begin{itemize}
\item Each syllable is an individual Lyric Meta Event
\item Use Space (ASCII 20 hex) as a Delimiter at the end of every word
\end{itemize}

or to sum up, the midi lyrics:
\begin{code}
 'Sta' 'bat ' 'Ma' 'ter ' 'do' 'lo' 'ro' 'sa ' 
\end{code}

should in lilypond code become
\begin{code}
 Sta -- bat Ma -- ter do -- lo -- ro -- sa
\end{code}

Instead, midi2ly produces

\begin{code}
  "Sta"2 "bat " 
  | % 2
  "Ma" "ter "4 "do"2 "lo"4 "ro"2 
  | % 4
  "sa "4*15 "dum "4*0/480 ""2 "pen"4 "de" "bat "4*0/480 ""2 "Fi" 
\end{code}

i.e. it misses the " -- " between syllables.

\subsubsection*{third problem, strange output}

the produced lilypond file doesn't compile cleanly and it produces a
strange output file

\begin{code}
$ lilypond --ps test.ly 
GNU LilyPond 2.19.16
Processing `test.ly'
Parsing...
Interpreting music...
test.ly:1467:5: warning: Two simultaneous lyric events, junking this one
  " 
    "2*7 "jux"2 
test.ly:1149:7: warning: Previous lyric event here
  "Sta
      "2 "bat " 
test.ly:1892:5: warning: Two simultaneous lyric events, junking this one
[...]
test.ly:1648:3: warning: adding note head to incompatible stem (type = 1/4)
  
  r2*7 a''4*959/480 r4*1/480 
test.ly:1648:3: warning: maybe input should specify polyphonic voices
  
  r2*7 a''4*959/480 r4*1/480 
test.ly:2043:3: warning: adding note head to incompatible stem (type = 1/4)
[...]
\end{code}
%$
 -----------------------

[1] http://www.music.mcgill.ca/~ich/classes/mumt306/midiformat.pdf
[2] https://www.midi.org/specifications/item/standard-midi-files-smf
[3] http://www.johnkilpatrick.co.uk/music/misc/stabat-palestrina-e.pdf
[4] http://www.johnkilpatrick.co.uk/music/misc/stabat-palestrina-e.mid
[5] https://en.wikipedia.org/wiki/Windows-1252

\end{document}
