\makeatletter
\newcommand{\TeXmuse}{\TeX{\itshape\kern-.1em m\kern-.05em use}}
%Declarations
\newtoks\@temptoks
\newtoks\quant@toks
\newtoks\curr@note
\newtoks\this@note
\newtoks\to@write
\newtoks\extracted@line
\newtoks\@plainnote
\newtoks\@atnote
\newtoks\@@atnote
\newwrite\written@tm@file
\newwrite\instrument@file
\newread\read@tm@file
\@tempswafalse % (tempswa = repeat@test)
\newif\if@tempswb \@tempswbtrue % (tempswb = first@file)
\newif\ifnot@written\not@writtentrue
\newif\ifbeam@sw  % Whether the note has to be added to a beam
\newif\ifblock@done       % Whether the last block is over (to catch pre-barline material)
\newcount\@octave
\newcount\@key
\newcount\block@period
\newcount\instrument@number
\newcount\@no@of@instr      %   No. of instruments included in the system
\newcount\extracted@number
\newcount\current@quantum\current@quantum=0\relax
\newcount\quantum@skip\quantum@skip=0\relax
\newcount\char@no
\newcount\block@no
\newcount\file@no
\newcount\s@factor
\newcount\@restshift\@restshift0
\newcount\@tempcntd
\newcount\@hookels
\newcount\@@break
\newcount\@firstmf
\newcount\FontSplit     % Number of characters per font
\newcount\beam@no %Number of beam lines in the beam (modified by \e@n, etc.)
\chardef\@ther=12
\chardef\@i=1
\chardef\@ii=2
\chardef\@iii=3
\chardef\@iv=4 
\chardef\@v=5
\chardef\@vi=6
\chardef\@vii=7
\chardef\@viii=8
\chardef\@ix=9
\chardef\@z=0
\newenvironment{texmuse}{\bgroup%
    \catcode`\[=13 \catcode`\]=13
    \catcode`\|=13
    \catcode`A=13 \lccode`A=`A
    \catcode`B=13 \lccode`B=`B
    \catcode`C=13 \lccode`C=`C
    \catcode`D=13 \lccode`D=`D
    \catcode`E=13 \lccode`E=`E
    \catcode`F=13 \lccode`F=`F
    \catcode`G=13 \lccode`G=`G
    \catcode`R=13 \lccode`R=`R
    \catcode`\!=\@ther \lccode`\!=`\1 \catcode`1=\active
    \catcode`\?=\@ther \lccode`\?=`\2 \catcode`2=\active 
    \catcode`\_=\@ther \lccode`\_=`\3 \catcode`3=\active 
    \catcode`\$=\@ther \lccode`\$=`\4 \catcode`4=\active
    \catcode`\/=\@ther \lccode`\/=`\5 \catcode`5=\active
    \catcode`\^=\@ther \lccode`\^=`\6 \catcode`6=\active
    \catcode`\&=\@ther \lccode`\&=`\7 \catcode`7=\active
    \catcode`\*=\@ther \lccode`\*=`\8 \catcode`8=\active
    }{\egroup}
%Newinstrument
\newcommand\newinstrument[2][\trebleclef]{%
    \expandafter\newtoks\csname #2@toks\endcsname%
    \expandafter\def\csname #2\endcsname{\global\csname #2@toks\endcsname}%
    \bgroup
        \edef\trebleclef{\gdef\expandafter\noexpand\csname #2@clef\endcsname{0}}%
        \edef\bassclef{\gdef\expandafter\noexpand\csname #2@clef\endcsname{6}}%
        #1
    \egroup}
    %
%Constant definitions
\def\quant@note#1{\let\@let@token=#1\@quant@note}
\def\@quant@note{\let\next\@let@token
    \ifx\@let@token-\let\next\quant@note
    \else\ifx\@let@token+\let\next\quant@note
    \else\ifx\@let@token.\let\next\quant@dot
    \else\ifx=\@let@token\let\next\@@quant@note
    \else\def\next{\@@quant@note\@let@token}%
    \fi\fi\fi\fi\next}
\def\quant@dot#1{\let\@let@token=#1\@quant@dot}
\def\@quant@dot{\let\next\@let@token
    \ifx\@let@token.\let\next\@quant@dot            % Not sure it works like this
    \else\ifx=\@let@token\let\next\@@quant@dot
    \else\def\next{\@@quant@dot\@let@token}%
    \fi\fi\next}
\def\@@quant@dot{\global\quant@toks\expandafter{\the\quant@toks\quant@@dot\@note\quant@@undot}}
\def\@@quant@note{\global\quant@toks\expandafter{\the\quant@toks\@note}}
\def\quant@sixtif{\quantum@skip4\relax}
\def\quant@thirt{\quantum@skip8\relax}
\def\quant@sixt{\quantum@skip16\relax}
\def\quant@eighth{\quantum@skip32\relax}
\def\quant@quarter{\quantum@skip64\relax}
\def\quant@half{\quantum@skip128\relax}
\def\quant@whole{\quantum@skip256\relax}
\def\quant@double{\quantum@skip512\relax}
\def\quant@@dot{\multiply\quantum@skip3\divide\quantum@skip2\relax}
\def\quant@@undot{\multiply\quantum@skip2\divide\quantum@skip3\relax}
\def\quant@barline{\global\quant@toks\expandafter{\the\quant@toks\no@note0\current@quantum0\relax}}
%\@note
\def\@note{%
    \if@tempswa\else\read\read@tm@file to \matrix@line
        \expandafter\extract@from@line\matrix@line
    \fi
    \@tempswafalse
    \def\next{\global\advance\current@quantum\quantum@skip\relax}%
    \ifnum\block@period>\current@quantum\relax
        \ifnum\extracted@number=\current@quantum\relax
            \edef\to@write{\the\extracted@number:\the\extracted@line,%
                1.}%
        \else\ifnum\extracted@number>\current@quantum\relax
            \@tempswatrue
            \@tempcnta=\instrument@number\relax
            \edef\to@write{\the\current@quantum:\noexpand\@gobble}%
            \loop\ifnum\@tempcnta>1\relax\edef\to@write{\to@write,0}%
                \advance\@tempcnta-1\relax\repeat
            \edef\to@write{\to@write,1.}%
        \else% (extracted@number<current@quantum)
            \let\next\@note
            \edef\to@write{\the\extracted@number:%
                \the\extracted@line,0.}%
        \fi\fi
        \immediate\write\written@tm@file{\to@write}%
    \else
        \def\next{\@tempswatrue\no@note0\current@quantum0\relax\@note}%
    \fi
    \next}
\def\no@note#1{%
    \if@tempswa\else\read\read@tm@file to \matrix@line
        \expandafter\extract@from@line\matrix@line
    \fi
    \@tempswafalse
    \let\@next\relax
    \ifnum\extracted@number=\current@quantum\relax
        \edef\to@write{\the\extracted@number:\the\extracted@line,%
            #1.}%
    \else\ifnum\extracted@number>\current@quantum\relax
        \@tempswatrue
        \@tempcnta=\instrument@number\relax
        \edef\to@write{\the\current@quantum:\noexpand\@gobble}%
        \loop\ifnum\@tempcnta>1\relax\edef\to@write{\to@write,0}%
            \advance\@tempcnta-1\relax\repeat
        \edef\to@write{\to@write,#1.}%
    \else% (extracted@number<current@quantum)
        \def\@next{\no@note{#1}}%
        \edef\to@write{\the\extracted@number:%
            \the\extracted@line,0.}%
    \fi\fi
    \immediate\write\written@tm@file{\to@write}%
    \@next}
\def\@stem{\immediate\write\written@tm@file{\space\space\space regular_stem;}}
\def\@rest#1{\immediate\write\written@tm@file{\space\space\space add_rest(\the\beam@no,#1);}}
\begin{texmuse}
\lowercase{%
%Octave handling
    \global\def\rangefrom#!{\numbers@as@numbers\relax\@tempcnta=4\relax
        \expandafter\@rangefrom\noexpand#!}%
    \global\def\@rangefrom#!#?{%
        \global\@tempcntb=#?\relax\global\@tempcnta=#?\relax\global\advance\@tempcnta1\relax\egroup
        \expandafter\@@la\the\@tempcnta\relax
        \ifx#!A\expandafter\@@la\the\@tempcntb\else
        \ifx#!B\expandafter\@@ti\the\@tempcntb\else
        \ifx#!C\expandafter\@@do\the\@tempcntb\else
        \ifx#!D\expandafter\@@re\the\@tempcntb\else
        \ifx#!E\expandafter\@@mi\the\@tempcntb\else
        \ifx#!F\expandafter\@@fa\the\@tempcntb\else
        \expandafter\@@sol\the\@tempcntb\relax
        \fi\fi\fi\fi\fi\fi
        }
    \global\def\@@sol#!{\global\defG{\@octave#!\def\@pitch{g}\@tempswafalse\@@octave}}%
    \global\def\@@fa#!{\@@sol#!\global\defF{\@octave#!\def\@pitch{f}\@tempswafalse\@@octave}}%
    \global\def\@@mi#!{\@@fa#!\global\defE{\@octave#!\def\@pitch{e}\@tempswafalse\@@octave}}%
    \global\def\@@re#!{\@@mi#!\global\defD{\@octave#!\def\@pitch{d}\@tempswafalse\@@octave}}%
    \global\def\@@do#!{\@@re#!\global\defC{\@octave#!\def\@pitch{c}\@tempswafalse\@@octave}}%
    \global\def\@@ti#!{\@@do#!\global\defB{\@octave#!\def\@pitch{b}\@tempswafalse\@@octave}}%
    \global\def\@@la#!{\@@ti#!\global\defA{\@octave#!\def\@pitch{a}\@tempswafalse\@@octave}}}
\gdef\numbers@as@numbers{\bgroup% Esto también tiene que ser un loop
    \def1{\@i}\def2{\@ii}\def3{\@iii}\def4{\@iv}\def5{\@v}\def6{\@vi}\def7{\@vii}\def8{\@viii}}
%Accidentals
\gdef\#{\open@block\this@note\expandafter{\the\this@note\string\@sharp}}%
\gdef\b{\open@block\this@note\expandafter{\the\this@note\string\@flat}}%
\gdef\n{\open@block\this@note\expandafter{\the\this@note\string\@natural}}%
%Beams and rhythmic notation
    \gdef\rhythm@not{\stem@kind}%
    \gdef[{\@open@beam}\gdef]{\@close@beam}%
    \gdef\@open@beam{%
        \open@block
        \edef\current@block{\current@block\relax\string\next@note\string\open@beam}%
        \def\rhythm@not{\string\add@tobeam}%
        \not@writtenfalse}%
    \gdef\@close@beam{\edef\current@block{\current@block\string\close@beam\relax}%
        \def\rhythm@not{\stem@kind}}%
%%notes
\lowercase{%
    \gdef\@add@note#!#?{%
        \open@block
        \ifnot@written% When \@open@beam has been called, it already added \next@note
            \edef\current@block{\current@block\relax\string\next@note}%
        \fi
        \edef\current@block{\current@block
            \@value\the\this@note\the\@atnote\the\@@atnote\string\add@note{#!#?}\rhythm@not}%
        \@atnote{}%
        \@@atnote{}%
        \this@note\@plainnote
        \not@writtentrue
        \advance\current@quantum\quantum@skip\relax
        \block@donefalse}%
    \global\defR{\@ifnextchar-{\advance\@restshift-!\relax\@firstoftwoR}\@@rest}%
    \global\def\@@rest{\@ifnextchar+{\advance\@restshift!\relax\@firstoftwo\@@rest}\@@@rest}
    \gdef|{\def\@barline{!0}\current@quantum0\relax}%
    }%
\gdef\@@@rest{%
    \open@block
    \def\rhythm@not{\string\@rest{\the\@restshift}}%
    \ifnot@written%
        \edef\current@block{\current@block\relax\string\next@note}%
    \fi
    \edef\current@block{\current@block%
        \@value\the\this@note\the\@@atnote\rhythm@not}%
    \this@note\@plainnote
    \@@atnote{}%
    \not@writtentrue
    \@restshift0\relax
    \def\rhythm@not{\stem@kind}%
    \advance\current@quantum\quantum@skip\relax
    \block@donefalse}%
%clefs
\gdef\trebleclef{\this@note\expandafter{\the\this@note\string\@clef0}}
\gdef\bassclef{\this@note\expandafter{\the\this@note\string\@clef\string6}}
%blocks
\gdef\open@block{%
    \ifnum\block@period>\current@quantum\else\current@quantum0\relax\fi
    \ifnum\current@quantum=0\relax
        \end@block
    \fi}
\gdef\end@block{%
    \ifblock@done\else
        \immediate\write\instrument@file{%
            \current@block\string\end@of@block}%
        \immediate\write\instrument@file\expandafter{%
            \the\this@note\string\@bar@line{\@barline}\string\relax}%
        \def\current@block{\@gobble}%
        \this@note\@plainnote
        \let\@barline\barline@default
    \fi
    \block@donetrue}
%bars
%rhythmic values
    \global\def2{\quant@thirt\global\quant@toks\expandafter{\the\quant@toks\quant@thirt}%
        \def\@value{\string\t@n}\def\stem@kind{\string\@stem\string\@flag}}%
    \global\def3{\quant@sixt\global\quant@toks\expandafter{\the\quant@toks\quant@sixt}%
        \def\@value{\string\s@n}\def\stem@kind{\string\@stem\string\@flag}}%
    \global\def4{\quant@eighth\global\quant@toks\expandafter{\the\quant@toks\quant@eighth}%
        \def\@value{\string\e@n}\def\stem@kind{\string\@stem\string\@flag}}%
    \global\def5{\quant@quarter\global\quant@toks\expandafter{\the\quant@toks\quant@quarter}%
        \def\@value{\string\q@n}\def\stem@kind{\string\@stem}}%
    \global\def6{\quant@half\global\quant@toks\expandafter{\the\quant@toks\quant@half}%
        \def\@value{\string\h@n}\def\stem@kind{\string\@stem}}%
    \global\def7{\quant@whole\global\quant@toks\expandafter{\the\quant@toks\quant@whole}%
        \def\@value{\string\w@n}\def\stem@kind{\string\relax}}%
    \global\def8{\quant@double\global\quant@toks\expandafter{\the\quant@toks\quant@double}%
        \def\@value{\string\d@n}\def\stem@kind{\string\relax}}%
%The actual reading of the user's input for quantization (redefinition of musical commands)
\lowercase{%
    \global\def\key#!{\numbers@as@numbers\global\@key=#!\relax\egroup}
    \global\def\meter#!#?{\numbers@as@numbers\global\block@period?/^\relax
        \global\divide\block@period by #?\relax  %If any argument is a two-digit number, this does not yet work
        \global\multiply\block@period by #!\relax\egroup}
    \global\def\extr@quant@toks#!{%
        \begin{texmuse}%Meaning of commands for quantization purposes. 
            \let[\relax\let]\relax
            \let|\quant@barline
            \letA\quant@note\letB\quant@note\letC\quant@note\letD\quant@note
            \letE\quant@note\letF\quant@note\letG\quant@note\letR\quant@note
            \let\rangefrom\@gobble
            \let\#\relax\let\n\relax\let\b\relax
            \expandafter\the#!%
            \global\quant@toks\expandafter{\the\quant@toks\quant@instr@end\relax}%
        \end{texmuse}}}%
\end{texmuse}
\def\@@octave#1{\let\next\@@octave
    \ifx#1-\advance\@octave-1\else\ifx#1+\advance\@octave1\relax
    \else\if@tempswa\else\@add@note{\@pitch}{\the\@octave}\@tempswatrue\fi
        \ifx=#1\add@tie{\expandafter\@pitch\the\@octave}\let\next\relax
        \else\ifx#1.\add@dot{\@pitch}{\the\@octave}%
        \else\let\next#1%
        \fi\fi
    \fi\fi\next}
%Quantization functions
\def\quant@instr@end{\let\next\quant@instr@end
    \if@tempswa\else\read\read@tm@file to \matrix@line
        \expandafter\extract@from@line\matrix@line
    \fi
    \ifnum\extracted@number=9999\relax 
        \immediate\write\written@tm@file{\matrix@line}%
        \immediate\closeout\written@tm@file\immediate\closein\read@tm@file\current@quantum0\relax
        \let\next\relax
    \else
        \edef\to@write{\the\extracted@number:%
            \the\extracted@line,0.}%
        \immediate\write\written@tm@file{\to@write}%
        \@tempswafalse
    \fi\next}
%Character construction functions
\def\read@blocks#1{\@for\curr@instr:=#1\do
        {\expandafter\read\csname\curr@instr @file\endcsname to \@tempcs
        \csname\curr@instr @blocktoks\endcsname\expandafter{\@tempcs}%
        \expandafter\extract@note\csname\curr@instr @blocktoks\endcsname}}
\def\open@beam{\expandafter\let\csname\curr@instr @beam\endcsname\@gobble}
\def\close@beam{\immediate\write\written@tm@file\expandafter{\space\space\space
    beam(\csname\curr@instr @beam\endcsname);}%
    \beam@swfalse}
\def\t@n{\beam@no3\relax\def\notehead@kind{add_noteheads}}
\def\s@n{\beam@no2\relax\def\notehead@kind{add_noteheads}}
\def\e@n{\beam@no1\relax\def\notehead@kind{add_noteheads}}
\def\q@n{\beam@no0\relax\def\notehead@kind{add_noteheads}}
\def\h@n{\beam@no-1\relax\def\notehead@kind{add_halfheads}}
\def\w@n{\beam@no-2\relax\def\notehead@kind{add_wholeheads}}
\def\d@n{\beam@no-3\relax\def\notehead@kind{add_doubleheads}}
\def\@flag{\immediate\write\written@tm@file{\space\space\space add_flag(\the\beam@no);}}
\def\@sharp#1\add@note#2{#1\relax\add@note{#2}\immediate\write\written@tm@file{\space\space\space add_sharp(#2);}}
\def\@flat#1\add@note#2{#1\relax\add@note{#2}\immediate\write\written@tm@file{\space\space\space add_flat(#2);}}
\def\@natural#1\add@note#2{#1\relax\add@note{#2}\immediate\write\written@tm@file{\space\space\space add_natural(#2);}}
\def\add@note#1{\def\@@note{#1}%
    \immediate\write\written@tm@file{\space\space\space\notehead@kind(\@@note);}}
\def\add@dot#1#2{{\divide\quantum@skip2\relax
        \global\advance\current@quantum\quantum@skip}%
        \edef\current@block{\current@block
    \the\this@note\string\@dot{#1#2}}%
    \not@writtentrue}%
\def\add@tie#1{\expandafter\expandafter\expandafter\add@@tie#1}
\def\add@@tie#1#2{%
    \edef\current@block{\current@block
    \the\this@note\string\@tie{#1#2}}%
    \not@writtentrue
    {\@temptoks{\string\close@tie{#1#2}}\global\@@atnote\expandafter{\the\@temptoks}}%
    }%
\def\@tie#1{\immediate\write\written@tm@file{\space\space\space add_tie(#1);}}
\def\close@tie#1{\immediate\write\written@tm@file{\space\space\space close_tie(#1);}}
\def\@dot#1{\immediate\write\written@tm@file{\space\space\space add_dot(#1);}}
\def\@clef#1{\expandafter\def\csname\curr@instr @clef\endcsname{#1}%
    \immediate\write\written@tm@file{\space\space\space clef_(#1);}}
\def\add@tobeam{\expandafter\edef\csname\curr@instr @beam\endcsname{%
    \csname\curr@instr @beam\endcsname,\the\char@no,\the\beam@no,1,\@@note}} % 1 is for just one note-head
\def\init@char{%
    \immediate\write\written@tm@file{new_char(\the\s@factor);}}
\def\extract@note#1{\expandafter\extract@@note\the#1\relax
    #1\expandafter{\the\@temptoks}}
\def\extract@@note#1\next@note#2\end@of@block{\curr@note{#1}\@temptoks{#2\next@note\end@of@block}}
%Auxiliary functions
\def\get@instr#1,#2,. {\def\curr@instr{#1}\edef\temp@instr@list{#2\noexpand\@gobble}}
\def\open@tm@files{\if@tempswb\immediate\openout\written@tm@file=\jobname.tm1\relax
        \immediate\openin\read@tm@file=\jobname.tm2\relax
    \else\immediate\openout\written@tm@file=\jobname.tm2\relax
        \immediate\openin\read@tm@file=\jobname.tm1\relax
    \fi}
\def\extract@from@line#1:#2. {\extracted@number=#1\relax\extracted@line{#2}}
\def\@gobbleline#1.{\relax}
\def\strip@spaces#1{%
    \@temptoks#1%
    \@temptoks\expandafter{\the\@temptoks{\relax} }%
    \quant@toks{}%
    \@tempswatrue
    \loop\if@tempswa
        \expandafter\strip@@spaces\the\@temptoks{\relax} \@nil
    \repeat
    #1\quant@toks}
\def\strip@@spaces#1 #2#3\@nil{\quant@toks\expandafter{\the\quant@toks#1}%
    \ifx#2\relax\@tempswafalse\fi
    \@temptoks{#2#3}}
%Music Boxes
\def\music#1{%
    \def\@desiredwidth{\the\textwidth}%
    \def\@break{1}\@@break\@break
    \def\@afterblock{\ }%
    \def\@lastbreak{numeric desired_width}%
    \def\last@barline{20}%
    \def\instr@list{#1}%
    \def\block@chars{\@gobble}%
    \def\music@font@splits{\@gobble}%
    \@no@of@instr0\relax
    \instrument@number0\relax
    \@quantization
    \mf@files
    \@compose
    \global\@firstmf\file@no\global\advance\@firstmf1\relax}
\def\excerpt#1{%
    \def\@desiredwidth{desired_width}%
    \def\@break{0}\@@break\@break
    \def\@afterblock{\relax}%
    \def\@lastbreak{\@gobble}%
    \def\last@barline{0}%
    \def\instr@list{#1}%
    \def\block@chars{\@gobble}%
    \def\music@font@splits{\@gobble}%
    \@no@of@instr0\relax
    \instrument@number0\relax
    \@quantization
    \mf@files
    \@compose
    \global\@firstmf\file@no\global\advance\@firstmf1\relax}
\def\@quantization{%
    %First the quantization
    \init@quantization
    \@for\curr@instr:=\instr@list\do
        {\expandafter\strip@spaces\csname\curr@instr @toks\endcsname%
        \advance\@no@of@instr1\relax
        \quant@toks{}\current@quantum0\relax
        \extr@quant@toks{\csname\curr@instr @toks\endcsname}%
        \@tempswafalse
        \advance\instrument@number1\relax
        \if@tempswb\@tempswbfalse\else\@tempswbtrue\fi\open@tm@files
        \the\quant@toks
    %Then, the writing of the auxiliary files for each instrument
        \this@note\@plainnote
        \def\current@block{\@gobble}%
        \current@quantum0\relax
        \block@donetrue
        \immediate\openout\instrument@file=\curr@instr.tms\relax
        \expandafter\the\csname\curr@instr @toks\endcsname
        \ifnum\current@quantum=0%       Which means there was '|' at the end
            \end@block
        \else
            \immediate\write\instrument@file{\current@block\string\end@of@block}%
        \fi
        \immediate\closeout\instrument@file}%
        \count@chars}
\def\mf@files{%
    \open@matrix
    \open@tms
    \char@no0
    \block@no0
    \newtoks\previous@line
    \@tempswatrue
    \file@no\@firstmf
    \mf@@headers
    \let\music@@font@splits\music@font@splits%
    \extracted@number0\relax
    \read\read@tm@file to \matrix@line
    \expandafter\extract@from@line\matrix@line
    \loop\ifnum\extracted@number<9999\relax
        \expandafter\check@split\music@font@splits,1000\@nil
        \read@blocks{\instr@list}%
        \let\next\build@block
        \build@block
    \repeat}
\def\build@block{%
    \edef\temp@instr@list{\instr@list}%
    \previous@line\expandafter{\the\extracted@line}%
    \@tempcnta\extracted@number\relax
    \read\read@tm@file to \matrix@line
    \expandafter\extract@from@line\matrix@line
    \ifnum\extracted@number=0\relax
        \let\next\finish@block
    \else
        \advance\char@no1\relax
        \ifnum\extracted@number=9999\relax
            \ifnum\@tempcnta=\block@period
                \def\next{\finish@block\finish@music}%
            \else
                \def\next{\last@block\finish@music}%
            \fi
            \extracted@number\block@period\relax
        \fi
        \find@sf
        \init@char
        \@for\matrix@number:=\the\previous@line\do
            {\expandafter\get@instr\temp@instr@list,,. 
            \ifnum\matrix@number>0\relax
                \global\block@donefalse
                \immediate\write\written@tm@file{\curr@instr;}%
                \expandafter\extract@note\csname\curr@instr @blocktoks\endcsname
                \the\curr@note
            \fi}%
        \immediate\write\written@tm@file{end_of_char;}%
    \fi
    \next
    }
\def\finish@block{%
    \advance\char@no1\relax
    \immediate\write\written@tm@file{new_char(-least_spaced);}%
    \@for\curr@instr:=\instr@list\do
        {\expandafter\read\csname\curr@instr @file\endcsname to \@tempcs
        \immediate\write\written@tm@file{\curr@instr;}%
        \@tempcs}%
    \immediate\write\written@tm@file{end_of_char;}%
    \ifcase\@@break            %\@@break is set in the instruments' tokens
        \or\immediate\write\written@tm@file{end_of_block;}% 1 default for \music: breakable block
            \advance\block@no1\relax
        \or\immediate\write\written@tm@file{make_line(next_char);}% 2 is forced break
    \fi
    \@@break\@break\relax}
\def\last@block{%
    \advance\char@no1\relax
    \immediate\write\written@tm@file{new_char(-least_spaced);}%
    \@for\curr@instr:=\instr@list\do
        {\immediate\write\written@tm@file{\curr@instr;}%
        \immediate\write\written@tm@file{\space\space\space barline_(\last@barline);}}%
    \immediate\write\written@tm@file{end_of_char;}}    
\def\finish@music{%
    \extracted@number9999\relax
    \immediate\write\written@tm@file{\@lastbreak; end_of_block; make_line(next_char);}%
    \immediate\write\written@tm@file{showstats; end.}%
    \immediate\closeout\written@tm@file}
\def\@compose{%
    \file@no\@firstmf
    \block@no-1\relax
    {%
        \char@no0\relax
        \@tempcnta0\relax
        \font\@musicfont=\jobname\the\@firstmf\relax
        \@for\@tempcsa:=\block@chars\do
            {\advance\block@no1\relax
            \@musicfont
            \@whilenum\char@no<\@tempcsa\do
                {\advance\char@no1\relax
                \message{\the\char@no}\mbox{\char\the\char@no}%
                }%
            \message{|}\@afterblock %
            \expandafter\check@@split\music@@font@splits,0\@nil
            }%
    }}
\def\@bar@line#1{\immediate\write\written@tm@file{\space\space\space barline_(#1);}}

\def\find@sf{%
    \bgroup
        \advance\extracted@number-\@tempcnta
        \global\s@factor\extracted@number
    \egroup}
\def\count@chars{%
    \open@matrix
    \extracted@number0\relax
    \char@no0\relax
    \@tempcnta-1\relax           % The number of blocks
    \@tempcntb0\relax           % The number of the last character in the last block
    \loop\ifnum\extracted@number<9999\relax
        \read\read@tm@file to \matrix@line
        \expandafter\extract@from@line\matrix@line
        \ifnum\char@no>\FontSplit\relax
            \edef\music@font@splits{\music@font@splits,\the\@tempcnta}%
            \advance\char@no-\@tempcntb\relax
        \fi
        \ifnum\extracted@number=0\relax     % To fix all this with the new model.
            \advance\@tempcnta1\relax
            \@tempcntb\char@no\relax
            \edef\block@chars{\block@chars,\the\char@no}%
        \fi
        \advance\char@no1\relax
    \repeat
    \edef\block@chars{\block@chars,\the\char@no}%
    \edef\music@font@splits{\music@font@splits,1000}%
    \immediate\closein\read@tm@file}
\def\check@split#1,#2\@nil{%
    \ifnum\block@no=#1\relax          %This block was the last to write into the current file
        \char@no0\relax
        \close@@line
        \global\advance\file@no1\relax
        \mf@@headers
        \def\music@font@splits{#2}%
    \fi}    
\def\check@@split#1,#2\@nil{%
    \ifnum\block@no=#1%
        \char@no0\relax
        \global\advance\file@no1\relax
        \font\@musicfont=\jobname\the\file@no
        \def\music@@font@splits{#2}%
        \@musicfont\newline
    \fi}
\def\mf@@headers{%
    \immediate\openout\written@tm@file=\jobname\the\file@no.mf%
    \immediate\write\written@tm@file{mode_setup;}%
    \immediate\write\written@tm@file{input txmfont;}%
    \immediate\write\written@tm@file{input texmuse;}%
    \immediate\write\written@tm@file{instr_no(\the\@no@of@instr);}%
    \@tempcnta0\relax
    \immediate\write\written@tm@file{key_ := \the\@key;}%
    \immediate\write\written@tm@file{desired_width := \@desiredwidth/pt;}%
    \@tempcnta0\relax
    \@for\curr@instr:=\instr@list\do
        {\advance\@tempcnta1\relax
        \immediate\write\written@tm@file{staff_\the\@tempcnta="-1,0,1,2,3";}%
        \immediate\write\written@tm@file{bar_type\the\@tempcnta=\barline@default;}%
        \immediate\write\written@tm@file{instr_clef\the\@tempcnta :=
            \csname\curr@instr @clef\endcsname;}%
        \immediate\write\written@tm@file{def \curr@instr\space =}%
        \immediate\write\written@tm@file{\space\space\space curr_instr:="\curr@instr;";}%
        \immediate\write\written@tm@file{\space\space\space note_[index][instr_]:=currentpicture;}%
        \immediate\write\written@tm@file{\space\space\space instr_:=\the\@tempcnta;}%
        \immediate\write\written@tm@file{\space\space\space currentpicture:=note_[index][instr_];}%
        \immediate\write\written@tm@file{\space\space\space enddef;}}%
    \@tempswatrue}
\def\close@@line{%
    \immediate\write\written@tm@file{make_line(next_char); showstats; end.}%
    \immediate\closeout\written@tm@file}
%Initializations
\def\init@quantization{\@tempswbtrue
    \immediate\openout\written@tm@file=\jobname.tm1\relax%
    \immediate\write\written@tm@file{9999: 0.}%
    \immediate\closeout\written@tm@file\relax}%
\def\open@matrix{\immediate\openin\read@tm@file=\jobname.tm\ifodd\instrument@number 2\else1\fi\relax}
\def\open@tms{%
    \@for\curr@instr:=\instr@list\do
        {\expandafter\newread\csname\curr@instr @file\endcsname
        \immediate\openin\csname\curr@instr @file\endcsname=\curr@instr.tms%
        \expandafter\newtoks\csname\curr@instr @blocktoks\endcsname}}
\@plainnote{}
\@atnote{}
\@hookels0
\@firstmf1\relax
\FontSplit=180\relax
\def\barline@default{10}
\let\@barline\barline@default

\makeatother 

