\def\MAPS{\textsc{maps}}
\def\Cite#1{[\cite{#1}]}
\setlength{\unitlength}{1mm}
\def\musictex{Music\TeX{}}
\def\midi2tex{MIDI2\TeX{}}
\def\mtex{M\TeX{}}
\title{\midi2tex, a \musictex\ tool}
\author[Hans J.P. Kuykens]{Hans J.P. Kuykens\\\texttt{kuykens@amc.uva.nl}}

\begin{article}
\begin{multicols}{2}
\begin{quote}
\itshape This article was first published in Dutch in the NTG journal
\MAPS{} in 1994. We thank the author and \MAPS{} for allowing us to
print it here.
\end{quote}
\bibliographystyle{plain}
\begin{center}
\setlength{\unitlength}{1mm}
\begin{picture}(60.00,29.00)
\put(4.75,10.75){\line(1,0){50.00}}
\put(54.75,13.75){\line(-1,0){50.00}}
\put(4.75,16.75){\line(1,0){50.00}}
\put(4.75,7.75){\framebox(50.00,12.00)}
\put(9.75,19.75){\line(0,-1){12.00}}
\put(14.75,7.75){\line(0,1){12.00}}
\put(19.75,19.75){\line(0,-1){12.00}}
\put(24.75,7.75){\line(0,1){2.00}}
\put(24.75,11.75){\line(0,1){5.00}}
\put(29.75,15.75){\line(0,1){3.00}}
\put(33.75,11.75){\line(0,1){7.00}}
\put(38.75,15.75){\line(0,-1){6.00}}
\put(44.75,11.75){\line(0,1){6.00}}
\put(50.75,14.75){\line(0,1){6.00}}
\put(44.75,17.75){\line(2,1){6.00}}
\put(50.75,18.75){\line(-2,-1){2.00}}
\put(5.75,17.75){\rule{4.00\unitlength}{1.00\unitlength}}
\put(5.75,9.75){\rule{15.00\unitlength}{1.00\unitlength}}
\put(14.75,17.75){\rule{6.00\unitlength}{1.00\unitlength}}
\put(18.75,14.75){\framebox(3.00,1.00)}
\put(19.75,11.75){\framebox(6.00,1.00)}
\put(25.75,16.75){\framebox(3.00,1.00)}
\put(27.00,15.00){\oval(2.00,1.00)}
\put(23.50,9.25){\oval(0.75,0.50)}
\put(28.75,10.25){\oval(2.00,1.25)}
\put(32.75,12.00){\oval(2.25,1.75)}
\put(49.75,14.50){\circle*{2.00}}
\put(43.75,11.50){\circle*{2.00}}
\put(39.75,16.25){\circle*{2.00}}
\put(21.75,19.75){\line(0,1){5.00}}
\put(21.75,7.75){\line(0,-1){5.00}}
\put(6.75,21.75){\makebox(0.00,0.00)[bl]{MIDI}}
\put(35.75,21.75){\makebox(0.00,0.00)[bl]{Music\TeX}}
\put(21.75,24.75){\line(2,-1){10.25}}
\put(22.00,2.75){\line(2,1){9.75}}
\end{picture}
\end{center}
\section{Introduction}

I must have been eight or nine years old. The NINT museum of technology
in Amsterdam was my favourite because visitors were allowed to touch
everything. The steering console of a trolley car was exciting because
it made exactly the same rumbling sound when you used the throttle wheel.
There were wheels to turn and knobs to push. My grandfather and I spent
many hours strolling and playing in this museum. During one of our
visits we did a guided tour through the computer department. In a
separate room stood a giant, growling and buzzing monster. This beast
still lived on punch cards. One of the more amusing demonstration
programs controlled a line printer. By varying the typing speed it
was possible to generate a tune. Prehistoric computer music, of course.
However, this made such an impression on me\ldots\
A computer that could sing, wow! 

This example of computer music is of course nothing compared to the
abilities that modern computers presently offer to the musician.
Nowadays, the computer is applied in different ways in music.
One application is {\em sequencing}: simply recording and playing tunes.
The computer functions as a sort of merry-go-round organ. The organ-book
is replaced by a computer file that stores the musical information.
In this application the computer normally does not produce the sounds.

One step further the computer {\em is} responsible for the generation of
the sound waves. In this case one speaks of {\em sampling}. Sound is digitized
and recorded or played by the computer. If digitized sounds are mixed
with the objective to create new sounds one speaks of {\em synthesis}.
In the academic field computers are also used for {\em analysis} of the
structure of music, mostly classical. How does Bach do it? Are there
tricks or can we recognize typical patterns? Can we learn those tricks
and teach a computer to automatically generate a similar composition?
Thus, current computer technology can help the musician significantly
with composition or performance of his musical ideas.

The computer has also proved to be a valuable tool for the generation of
graphical output. This led to a revolution in the tools and methods
used in the graphical industry.  Nowadays, generation of sheet music
can also be performed with a computer. Clear-cut programs are available,
sometimes included with a sequencer program, for the development of
sheet music. \TeX\ has proved to be a perfect tool for typesetting of
text. However, this software package has more to offer and it appeared
to be suitable for the layout of sheet music as well.

\section{\musictex}

As  \TeX\ became popular, it seemed that it might be suitable
for ordering other things than text. Andrea Steinbach and Angelika
Schofer \Cite{Steinbach} were the first who attempted to combine \TeX\
and music. They generated the necessary font files and a first set of
macro files for the creation of sheet music using \TeX: \mtex.
The drawback of their system was that only a limited number of parallel
staffs could be generated. This must have been the motivation for Daniel
Taupin, a physicist from France, to enhance this system.
He based his work on the code of Steinbach and Schofer and developed a set of
macro files that broke through the barriers of \mtex. He named this package
\musictex\ \Cite{Taupin}. Also Steinbach and Schofer continued to work
on \mtex\ and also this system has found its way to many users.
However, the enhanced flexibility of \musictex\ has led to a wider acceptance
of this system. In recent years it has evolved into an extensive music
notation package that allows not only to code traditional sheet music but
also more exotic forms.

\section{What is wrong with it?}

When I first \TeX ed a \musictex\ file I was thrilled by the excellent
quality of the output. Undoubtedly suitable for publications.
\musictex, being built on
\TeX,  uses ASCII input files. These files store the codes that
represent the musical information and here, to my opinion, lies the
bottleneck of the system.

The computer keyboard is very suitable for the generation of a source
text file for \TeX. If one types a `{\tt K}' in the input this character
will also appear in the output. With sheet music this is quite a different
story as there are no musical notes on the computer keyboard. In \musictex\
this problem is solved by coding the notes with \TeX\ commands.
A note is characterized by two parameters: pitch and length. In sheet
music pitch is represented by the vertical position of a note on a staff.
The length of the note is specified by the type of symbol.
For example, a semibreve is represented by an open oval without a stick.
Coding a single note in \musictex\ thus requires two identifiers.
This is solved by using a command that represents the length of the note
(the type of symbol) and an additional parameter to specify pitch
(the vertical position of the symbol on the staff).
As an example the scale of C is coded. The first note is a semibreve
and each subsequent note is half as long as the previous one. \\
~\\
\verb@\wh c \hu d \qu e \cu f \ccu g \cccu h \ccccu i@\\
~\\
this produces the following score :\\

\noindent\epsfig{figure=music1.eps}

Of course to code a more serious music piece we need more tools:
i.e., multiple staffs, different keys, incidentals, ornaments.
To show that \musictex\ is also suitable to code more complex
musical pieces an example of the \musictex\ manual is copied here
(Brahm's Intermezzo Op.~117,1, coded by Miguel Filgueiras).

\noindent\epsfig{figure=music2.eps}

\noindent This was coded as:
{\small
 \begin{verbatim}
\begin{music}
\def\nbinstruments{1}\relax
\def\freqbarno{9999}% no bar numbers
\nbporteesi=2\relax
\cleftoksi={6000}\generalsignature{-3}% 3 flats
\debutextrait
\normal
\off{1em}\temps\Notes\larpeggio{E}5\zq{EI}\qu{N}%
\charnote0{\smallclefdesol}\relax
\nextstaff\qsk\ibl0e{-1}\zq{eg}\qb0l\zq{d}\qb0k
\enotes\cleftoksi={0000}\saveclefs\Notes\tbu0
\zq{ce}\qh0j\relax\nextstaff\zq{sn}\cl{l}\enotes
\Notes\zq{be}\qu{i}\nextstaff\zq{sn}\ql{l}\enotes
\cleftoksi={6000}\changeclefs\Notes\zq{E}\cu{I}%
\relax\nextstaff\ibbu1h{-1}\zq{ae}\qh1h\tbu1
\zq{N}\qh1g\enotes\finextrait
\end{music}
\end{verbatim}
}

Coding a piece of music for \musictex\ thus involves editing an
ASCII file with this type of commands. No-one (musician or
      programmer) is going to take this in `at a glance'; the file is
      simply crammed with \TeX{} commands; as a result, coding
with \musictex\ is a laborious and time consuming
task. That's what's wrong with it.

\section{Other musical file formats, MIDI}
The computer has proved to be a powerful and versatile tool for sequencing.
Many software packages are now available on most personal computers.
Initially programmers of such software did not bother about standards
of music files. You just had to stick to their product. Many different
file formats occurred that were not exchangeable.

Along with the development of computers a growing interest occurred in
electronic instruments. A tidal wave of keyboards and synthesizers was
poured out over the market. The need occurred to connect instruments with
each other and to computers to allow a full-size orchestra sound with only a
few boxes of electronics.
Different manufacturers' instruments did
      not understand each other.  Control of different companies'
      sound modules was impossible or hardly possible.

This problem was soon recognized and about ten years ago musicians
and  manufacturers of electronic instruments formed a group 
to define some standards. MIDI was the result: Musical Instrument
Digital Interface. Initially a communication protocol was developed that
allowed electronic instruments to communicate with each other. MIDI was
adopted by all companies and nowadays most keyboards and synthesizers
are equipped with a MIDI interface.

Now that they were busy organizing standards this group also decided to
tackle the problem of the many musical file formats. They developed a
standard file format: the MIDI file.
This file allows the exchange of musical pieces created with different programs.

There is no musical software company that is foolish enough to
exclude support of this file format.
Most sequencers now have the ability to import such MIDI files.

\section{Development of \midi2tex}
So, there I was. On the one hand a beautiful music notation package,
\musictex, needing very complicated ASCII input. On the other hand MIDI
files that could easily be recorded using a keyboard and sequencing program
but lacking the ability to print nice sheet music.
The link seemed so obvious. Surely, I thought, someone has
already solved this problem with a program which translates MIDI files
into \musictex\ source code. I scanned the Internet but found nothing. There
seemed to be no other alternative than to grab a chair, sit at my
keyboard and code it myself.  The first action was to get my hands on a good
book about MIDI. I came home with: `MIDI programmer's handbook' by Steven De
Furia and John Scacciaferro \Cite{defuria}, an excellent book which
explains the architecture in detail and contains many programming
examples.  I intended to develop just a small utility that would only
translate the notes in the MIDI file into their \musictex\ equivalents. I
started programming on my Atari~ST in Pascal. However, while developing the
code the number of design specs grew. In the long term it evolved into a
program that created a ready-to-compile \musictex\ source file from a standard
MIDI file. Command line parameters allowed altering specific options of the
program.

I realized I had ended up with a piece of software that a lot of other
computer music minded people would enjoy as well. A shame if it would die a
lonely death in my computer. So, I decided to release the program as
shareware in Cyberspace. In August 1992 I released version P 1.0. Initially,
I distributed the package by email but when the number of requests exceeded
one hundred (so people really {\em were\/} interested!) I decided to place the
package on an FTP server. Of course many bug-reports followed that
have led to further updates of the package.

\section{Portability}
The first versions of the program were only available for Atari~ST and MS-DOS
machines. Many requests reached me to port the code to other machines as well.
However, Pascal is very poorly supported on other computer systems.
If a Pascal compiler was available at all, it appeared that my
source code was not compatible with that compiler.
The programming language C is much more
suitable for porting code to other computers due to standardization (ANSI-C).
Many people inquired about the availability of C source code.
That did not exist. At that moment the source had already grown to several
thousand lines of code. My knowledge about C was minimal. So, translating the
whole bunch into C by myself was not a realistic option. Finally, I learned
that programs existed that could do a major part of the translation process
automatically. Steve Chamberlain in the U.S. than poured all the Pascal
source files into the P2C (Pascal to C) program. The resulting C source files
than had been lying for a few months on a dusty shelf on the hard disk of
my computer until I received a small note from Ad Verbruggen, who
works at DIMES, Delft University. He worked with a Macintosh, was familiar with
C and most important he had the energy and spare time to teach the crippled
C code to run. In concert with each other we worked out the C code until
it functioned correctly on both the Mac and the PC. This version C~1.0 was
released in the winter 1993--94. Immediately messages reached us that the
program also compiled successfully on other systems. The goal was reached:
\midi2tex\ was now available on most computer systems.

\section{The program's features}
Currently two versions are available: the C-version~1.3 of which only the
source is available and P~1.3; a Pascal version. Before the release of
version C~1.0 I could not resist to implement some new ideas in the Pascal
version. In Pascal I program twice as fast as in C, for that reason the
Pascal version was not yet put on a side track.\\
~\\
Version C~1.3 offers the following features :
\begin{description}
\item[\underline{S}kip tracks]
Suppress output of specified tracks in the MIDI file.
\item[comb\underline{I}ne tracks]
Two or more staffs are combined as one instrument ( for example left
and right hand of a piano piece).
\item[\underline{B}ass clef]
The specified staffs are coded using the bass clef.
\item[\underline{A}1..4]
The specified staffs are coded using the alto key (1..4).
\item[\underline{O}rder]
Reorder the tracks in the MIDI file to the staffs.
\item[\underline{Q}uantize]
Quantization the start time and length of the notes in the MIDI file.
\item[\underline{K}ey sign]
Specify the number of flats or sharps with which the piece is to be coded.
\end{description}

In addition, some layout parameters can be altered. Version P~1.3 offers
the ability to automatically merge a text file into the generated code.
\midi2tex\ is not a magic tool; it was developed simply
to automate translation of the notes in a
MIDI file into \musictex. In many cases it is still necessary to fine tune
the resulting source code by hand. For a more extensive description of
the program the reader should to read the manual that is included
in the software package \Cite{kuykens}.

\section{Availability}
\musictex\ is freeware and available on the Internet through anonymous FTP
from the CTAN archives.
Both executables and C source code are available.
The package will soon also be distributed on a number of shareware
CD-ROMS including the 4\TeX\ CD-ROM from NTG. \musictex\ has a
discussion group on the Internet where information, problems and solutions
are exchanged through email. Registration for this discussion group can be
done by sending a request to {\tt mutex-request.stolaf.edu}.

\section{The future}
The development of \midi2tex\ was performed on a fairly irregular basis.
It is expected that this trend will continue. To further enhance portability
to other systems some minor bugs will be removed. Due to the limited
demands on the design the performance of the package is not optimal.
Vertical alignment of notes is not always correct. Solving this problem
is not so trivial and together with Ad Verbruggen and Dave Benson, a
professor in mathematics in the USA, we are currently trying to
find better algorithms.

The first versions of \musictex\ did not support the coding of triplets.
As a result, this option was also not included in \midi2tex.
In a future release the option to merge a text file  will also be implemented
in the C version.

There is a lot of work still to be done. With the ongoing development of
\musictex\ new features become available: coding of percussion, guitar chords,
etc.

\section{An example}
This article would not be complete without an example of the output of
\midi2tex\ and \musictex, in Figure \ref{midi1}.
In consideration of the available space only
one page of \musictex\ source code is given below.

\begin{thebibliography}{1}

\bibitem{defuria}
S.~De~Furia and J.~Scacciaferro.
\newblock {\em MIDI programmer's handbook}.
\newblock M \& T Publishing, Inc., 1989.

\bibitem{kuykens}
H.J.P. Kuykens.
\newblock {\em \midi2tex, a MIDI to \musictex\ translation program}.
\newblock Dapperstraat 273$^{c}$, 1093 BS Amsterdam, 1993.
\newblock Email: \texttt{kuykens@amc.uva.nl}

\bibitem{Steinbach}
A.~Steinbach and A.~Schofer.
\newblock M{\TeX}.
\newblock Master's thesis, Rheinische Friedrich-Wilhelms Universit{\"a}t, Bonn,
  Germany, 1987,1988.

\bibitem{Taupin}
Daniel Taupin.
\newblock {\em Music\TeX, Using \TeX~ to write polyphonic or instrumental
  music}.
\newblock Laboratoire de Physique ds Solides, Orsay, France, June 1992.
\newblock (Version 4.52, (latest version is 5.07)).

\end{thebibliography}

\end{multicols}

\begin{figure*}
\centering
\noindent\epsfig{figure=music3.eps,width=\textwidth}
\caption{\midi2tex\ and \musictex\ output}\label{midi1}
\end{figure*}

{\small
\begin{verbatim}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%    MIDI2TeX C-version V 1.3 translation
%           of MIDI file :
%          VIVALDI.MID
%
%   Written by Hans Kuykens, Ad Verbruggen
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\normal
\hsize 160mm \vsize 240mm
\musicsize=16
\elemskip=6pt%
\def\nbinstruments{3}
\generalsignature{0}\relax
\def\thename{Vivaldi}
\medskip\centerline{\moyen \thename}
\rightline{translation by MIDI2TeX}
\rightline{by H.J.P. Kuykens}
\generalmeter{\meterfrac{4}{4}}%
\cleftoksi{{6}{0}{0}{0}}
\debutmorceau
%measure 1
\NOtes\qu {_I}&\zqp d\qup f&\ibbl1h{0}\qb1{_i}\qb1h\enotes\relax
\NOtes&&\qb1i\tbl1\qb1j\enotes\relax
\NOtes\ibu0H{0}\qh0H&&\ibl2k{0}\qb2k\enotes\relax
\NOtes\tbu0\qh0G&\zq g\cu {_i}&\tbl2\qb2{^k}\enotes\relax
\temps\NOtes\qu F&\zq h\ql j&\ql m\enotes\relax
\NOtes\ds &\ds &\qs \ibbl3m{0}\qb3m\enotes\relax
\NOtes\cl M&\zq h\cl j&\qb3n\tbl3\qb3o\enotes\relax
%measure 2
\barre
\NOtes\ql {_b}&\zq k\ql {_i}&\ibbl3m{0}\qb3{_p}\qb3o\enotes\relax
\NOtes&&\qb3n\tbl3\qb3m\enotes\relax
\NOtes\qu I&\zq f\ibu2i{-1}\qh2i&\ibbl4i{0}\qb4{^k}\qb4{=k}\enotes\relax
\NOtes&\zq d\tbu2\qh2f&\qb4j\tbl4\qb4i\enotes\relax
\NOtes\ibl0{^K}{0}\qb0M&\zh c\hu f&\ibu5h{0}\qh5h\enotes\relax
\NOtes\tbl0\qb0{^K}&&\tbu5\qh5g\enotes\relax
\NOtes\ibu1{=K}{0}\qh1{=K}&&\qu f\enotes\relax
\NOtes\tbu1\qh1J&&\enotes\relax
%measure 3
\alaligne
\end{verbatim}
}
\end{article}
