<!--
Translate MusicXML <http://www.musicxml.org> to Nightingale <http://www.ngale.com/> Notelist
Since Notelist is a limited subset of MusicXML, much will be lost in translation

Geoff Chirgwin <geoff at seiso dot com> 

Version 0.3 July 11, 2007
   rewrote determination of measureOffset, now a single Xpath expression has replaced 3 templates (1 recursive, all totalling 100 lines of markup)
   now get measure beat counts by notes, thereby replacing three templates (1 recursive, weight in at 100 lines of markup) with a single xpath expression

Version 0.2 July 2, 2007
  changes:
    upgrade to/confirm support of MusicXML 2.0 
    upgrade xsl version from 1.0 to 2.0, simple version port, no real changes or optimization
    clean up comparison operators (e.g. 'ge' instead of '&gt;='
    improve type casting of variables used in comparisons
    fix bug in chord-not comparison where xpath expression returned multiple nodes
    test with Saxon v 8.9.0.3, confirmed on test file output is identical
    add support for unpitched notes
    handle cross-staff chords in a way that Nightingale is happy with (Nightingale doesn't support cross-staff stemmed/beamed chords) by adding another voice

Version 0.1 May 2004 
    initial version

TODO: 
improve performance on larger scores: lessen recursion, optimize using new features of xsl 2.0, specifically
  -measureOffset
  -orderedNote
encoding of diacritical characters
improve cross-staff support (consecutive cross-staff-beamed chords, as in first few measures of DebuMandSample example)
account for <backup> when determining start times

-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     version="2.0" exclude-result-prefixes="#all">
     
  <!--other envodings iso-8859-1 or MacRoman  MacTEC Mac ??-->
  <xsl:output method="text" version="1.0" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>
      
  <xsl:strip-space elements="*"/>
  <xsl:template match="text()|@*"/>
  <xsl:template match="/">
    <xsl:apply-templates select="score-timewise"/>
  </xsl:template>
  
  <!--output header line and score meta-data -->
  <xsl:template match="/score-timewise">
  <xsl:text>%%Notelist-V2</xsl:text>
    <xsl:text> file='</xsl:text>
    <xsl:value-of select="work/work-title" />
    <xsl:text>'</xsl:text>
    <xsl:apply-templates select="part-list" />
    <xsl:apply-templates select="work" />
    <xsl:apply-templates select="identification" />
    <xsl:apply-templates select="measure" />
  </xsl:template>
  
  <!--output work and identification as unattached text elements -->
  <xsl:template match="work">
    <xsl:if test="work-title">
      <xsl:text>A v=-2 npt=-2 stf=-2 S0 '</xsl:text>
      <xsl:value-of select="work-title" />
      <xsl:text>'</xsl:text>
      <xsl:text>&#10;</xsl:text>      
    </xsl:if>
    <xsl:if test="work-number">
      <xsl:text>A v=-2 npt=-2 stf=-2 S0 '</xsl:text>
      <xsl:value-of select="work-number" />
      <xsl:text>'</xsl:text>
      <xsl:text>&#10;</xsl:text>      
    </xsl:if>
  </xsl:template>

  <xsl:template match="identification">
    <xsl:for-each select="creator">
      <xsl:text>A v=-2 npt=-2 stf=-2 S0 '</xsl:text>
      <xsl:value-of select="." />
      <xsl:text>'</xsl:text>
      <xsl:text>&#10;</xsl:text>      
    </xsl:for-each>
    <xsl:if test="rights">
      <xsl:text>A v=-2 npt=-2 stf=-2 S0 '</xsl:text>
      <xsl:value-of select="rights" />
      <xsl:text>'</xsl:text>
      <xsl:text>&#10;</xsl:text>      
    </xsl:if>
  </xsl:template>

  <xsl:template match="part-list">
    <xsl:text> partstaves=</xsl:text>
    <xsl:for-each select="score-part">    
      <xsl:variable name="partID" select="@id" />
      <xsl:variable name="numStaves" select="xs:integer(/score-timewise/measure[1]/part[@id=$partID]/attributes/staves)" />
      <xsl:choose>
        <xsl:when test="$numStaves ge 1 ">
          <xsl:value-of select="/score-timewise/measure[1]/part[@id=$partID]/attributes/staves" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>1</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text> </xsl:text>
    </xsl:for-each>
    <xsl:text>0 startmeas=1</xsl:text>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

     
  <xsl:template match="measure">
    <xsl:apply-templates select="part/attributes/clef" />
    <xsl:apply-templates select="part/attributes/time" />
    <xsl:apply-templates select="part/attributes/key" />

    <xsl:apply-templates select="part/note/notations/dynamics" />
    
    <xsl:variable name="thisMeasure" select="count(preceding::measure)" />
     
    <!-- normalize to greatest divisions in document, which will be the highest possible resolution -->
    <xsl:variable name="thisGreatestDivision">
      <xsl:call-template name="greatestDivisions" />
    </xsl:variable>
    
    <!-- count up note values to this point instead, ignoring anacruses, and normalizing to greatest number of divisions in entire score -->
    <xsl:variable name="thisMeasureStart" select="(sum(preceding-sibling::measure/part[1]/note[voice=1 or not(voice)]/duration)) div /score-timewise/measure[1]/part[1]/attributes[1]/divisions[1] * $thisGreatestDivision"/>

    <!-- this allows us to process measure-free musicxml files;  if measureEnd is set to 0,
        all notes will be processed infinitely without checking the measure bounds -->    
    <xsl:variable name="thisMeasureEnd">
      <xsl:choose>
        <xsl:when test="count(../measure) gt 1">
           <xsl:value-of select="(sum(preceding-sibling::measure/part[1]/note[voice=1 or not(voice)]/duration) + sum(part[1]/note[voice=1 or not(voice)]/duration)) div /score-timewise/measure[1]/part[1]/attributes[1]/divisions[1]"/>
        </xsl:when>
        <xsl:otherwise>0</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:apply-templates select="part/direction" />
    <xsl:call-template name="orderedNote">
      <xsl:with-param name="offsetCounter" select="$thisMeasureStart" as="xs:double"/>
      <xsl:with-param name="measureStart" select="$thisMeasureStart" as="xs:double"/>
      <xsl:with-param name="measureEnd" select="$thisMeasureEnd * $thisGreatestDivision"  as="xs:double"/>
    </xsl:call-template>
    
    <xsl:if test="$thisMeasure ge 0 and count(../measure) gt 1">
      <xsl:call-template name="outputBarline">
        <xsl:with-param name="barlineT" select="$thisMeasureEnd * 480" />
      </xsl:call-template>
    </xsl:if>

  </xsl:template>  
 
 
  <xsl:template name="outputBarline">
    <xsl:param name="barlineT">0</xsl:param>

    <xsl:text>/</xsl:text>
    <xsl:text> t=</xsl:text>
    <xsl:value-of select="$barlineT" />
    <xsl:text> type=</xsl:text>
    
    <xsl:choose>
      <!-- if we have a <barline> at the end of this measure or the beginning of the next -->
      <xsl:when test="part[1]/barline/@location='right' or following-sibling::measure[1]/part[1]/barline/@location='left'">

        <xsl:variable name="thisBarStyle" select="part[1]/barline[@location='right']/bar-style" />
        <xsl:variable name="thisBarRepeat" select="part[1]/barline[@location='right']/repeat/@direction" />
        <xsl:variable name="nextBarStyle" select="following-sibling::measure[1]/part[1]/barline[@location='left']/bar-style" />
        <xsl:variable name="nextBarRepeat" select="following-sibling::measure[1]/part[1]/barline[@location='left']/repeat/@direction" />
        <!--get barline style from part 1 (barline are the same across all parts/staves in notelists -->
        <xsl:choose>
          <!-- only have one style of repeat barline in notelist, so default to repeat, rather than light/heavy style of double bar -->
          <!--  normal bar line -->
          <xsl:when test="($thisBarStyle='regular' or $nextBarStyle='regular') and not($thisBarRepeat) and not($nextBarRepeat)">1</xsl:when>
          <!--  double bar line -->  
          <xsl:when test="($thisBarStyle='light-light' or $nextBarStyle='light-light') and not($thisBarRepeat) and not($nextBarRepeat)">2</xsl:when>
          <!--  final double bar line -->
          <xsl:when test="($thisBarStyle='light-heavy' or $nextBarStyle='light-heavy') and not($thisBarRepeat) and not($nextBarRepeat)">3</xsl:when>
          <!--  heavy double bar line -->
          <xsl:when test="($thisBarStyle='heavy-heavy' or $nextBarStyle='heavy-heavy') and not($thisBarRepeat) and not($nextBarRepeat)">4</xsl:when>
          <!--  repeat-left double bar line (||:) <bar-style>?-->
          <xsl:when test="not($thisBarRepeat) and $nextBarRepeat='forward'">5</xsl:when>
          <!--  repeat-right double bar line (:||)  <bar-style>?-->
          <xsl:when test="$thisBarRepeat='backward' and not($nextBarRepeat)">6</xsl:when>
          <!--  repeat-both double bar line (:||:)  <bar-style>? -->
          <xsl:when test="$thisBarRepeat='backward' and $nextBarRepeat='forward'">7</xsl:when>
          <!--default to normal style (1) -->
          <xsl:otherwise>1</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <!--default to normal style (1) -->
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>

    <xsl:text>&#10;</xsl:text>            
  </xsl:template>
           
  <xsl:template match="part/attributes/clef">

    <xsl:variable name="partNum" select="count(../../preceding-sibling::*) + 1" />
    <xsl:variable name="staffNum" select="@number" />
    <xsl:text>C stf=</xsl:text>
    
    <xsl:choose>
      <xsl:when test="$partNum and $staffNum">
        <xsl:value-of select="$partNum + $staffNum - 1"/> 
      </xsl:when>
      <xsl:when test="$partNum and not($staffNum)">
        <xsl:value-of select="$partNum"/> 
      </xsl:when>      
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
   
    <xsl:text> type=</xsl:text>
    <xsl:choose>
      <!-- 1  treble clef with 8va sign above -->
      <xsl:when test='sign="G" and line=2 and clef-octave-change=1'>1</xsl:when>
      <!-- 2  French violin clef (treble clef on bottom line) -->
      <xsl:when test='sign="G" and line=1 and not(clef-octave-change)'>2</xsl:when>
      <!-- 3  treble clef -->
      <xsl:when test='sign="G" and line=2 and not(clef-octave-change)'>3</xsl:when>
      <!-- 4  soprano clef (C clef on bottom line) -->
      <xsl:when test='sign="C" and line=1 and not(clef-octave-change)'>4</xsl:when>
      <!-- 5  mezzo-soprano clef (C clef on 4th line from top) -->
      <xsl:when test='sign="C" and line=2 and not(clef-octave-chnge)'>5</xsl:when>
      <!-- 6  alto clef -->
      <xsl:when test='sign="C" and line=3 and not(clef-octave-change)'>6</xsl:when>
      <!-- 7  treble-tenor clef (treble clef with 8va sign) -->
      <xsl:when test='sign="G" and line=2 and clef-octave-change=-1'>7</xsl:when>
      <!-- 8  tenor clef (C clef on 2nd line from top) -->
      <xsl:when test='sign="C" and line=4 and not(clef-octave-change)'>8</xsl:when>
      <!-- 9  baritone clef (C clef on top line) -->
      <xsl:when test='sign="C" and line=5 and not(clef-octave-change)'>9</xsl:when>
      <!--10  bass clef -->
      <xsl:when test='sign="F" and line=4 and not(clef-octave-change)'>10</xsl:when>
      <!--11  bass clef with 8va sign below -->
      <xsl:when test='sign="F" and line=4 and clef-octave-change=-1'>11</xsl:when>
      <!--12  percussion clef -->
      <xsl:when test='sign="Percussion" and line=3 and not(clef-octave-change)'>12</xsl:when>
      <!-- default to treble clef -->
      <xsl:otherwise>1</xsl:otherwise>            
    </xsl:choose>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="part/attributes/time">
    <xsl:variable name="partID" select="../../@id" />    
    <!--need to ouput a time signature for each staff for notelist -->
    <xsl:variable name="thisStaves">
      <xsl:choose>
        <xsl:when test="../staves">
          <xsl:value-of select="../staves" />
        </xsl:when>
        <xsl:when test="/score-timewise/measure[1]/part[@id=$partID]/attributes/staves">
          <xsl:value-of select="/score-timewise/measure[1]/part[@id=$partID]/attributes/staves" />
        </xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:call-template name="outputTimeSignature">
      <xsl:with-param name="staffCount" select="$thisStaves" />
    </xsl:call-template>
  </xsl:template>

   
  <xsl:template name="outputTimeSignature">
    <xsl:param name="staffCount">1</xsl:param>
    <xsl:variable name="partNum" select="count(../../preceding-sibling::*) + 1" />

    <xsl:text>T stf=</xsl:text>
<!--    <xsl:value-of select="count(../../preceding-sibling::*) + $staffCount" />    -->
    <xsl:choose>
      <xsl:when test="$partNum and xs:integer($staffCount) ge 1 and ($partNum + $staffCount) ge 2">
        <xsl:value-of select="$partNum + $staffCount - 1"/> 
      </xsl:when>
      <xsl:when test="$partNum ge 1 and xs:integer($staffCount) lt 1">
        <xsl:value-of select="$partNum"/> 
      </xsl:when>      
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
    
    <xsl:text> num=</xsl:text>
    <xsl:value-of select="beats" />
    <xsl:text> denom=</xsl:text>
    <xsl:value-of select="beat-type" />
    <xsl:text>&#10;</xsl:text>
    
    <!-- recurse for additional staves-->
    <xsl:if test="xs:integer($staffCount) gt 1">
      <xsl:call-template name="outputTimeSignature">
        <xsl:with-param name="staffCount" select="$staffCount - 1" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>


  <xsl:template match="part/attributes/key">
    <xsl:variable name="partID" select="../../@id" />    
    <xsl:variable name="thisStaves">
      <xsl:choose>
        <xsl:when test="../staves">
          <xsl:value-of select="../staves" />
        </xsl:when>
        <xsl:when test="/score-timewise/measure[1]/part[@id=$partID]/attributes/staves">
          <xsl:value-of select="/score-timewise/measure[1]/part[@id=$partID]/attributes/staves" />
        </xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <!--need to ouput a keysignature for each staff for notelist -->
    <xsl:call-template name="outputKeySignature">
      <xsl:with-param name="staffCount" select="$thisStaves" />
    </xsl:call-template>
  </xsl:template>
   
  <xsl:template name="outputKeySignature">
    <xsl:param name="staffCount">1</xsl:param>
    <xsl:variable name="partNum" select="count(../../preceding-sibling::*) + 1" />

    <xsl:text>K stf=</xsl:text>
    <xsl:choose>
      <xsl:when test="$partNum and xs:integer($staffCount) ge 1 and ($partNum + $staffCount) ge 2">
        <xsl:value-of select="$partNum + $staffCount - 1"/> 
      </xsl:when>
      <xsl:when test="$partNum ge 1 and  xs:integer($staffCount) lt 1">
        <xsl:value-of select="$partNum"/> 
      </xsl:when>      
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
    
    <xsl:text> KS=</xsl:text>
    <xsl:variable name="fifths" select="xs:integer(fifths)"/>
    <xsl:choose>
      <xsl:when test="$fifths le 0">
        <xsl:value-of select="$fifths * -1" />           
        <xsl:text> b</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$fifths" />         
        <xsl:text> #</xsl:text>
      </xsl:otherwise>      
    </xsl:choose>   
    <xsl:text>&#10;</xsl:text>
    <xsl:if test="xs:integer($staffCount) gt 1">
      <xsl:call-template name="outputKeySignature">
        <xsl:with-param name="staffCount" select="$staffCount - 1" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="processNote">
    <xsl:param name="notelistT">0</xsl:param>
    <xsl:param name="maxTime">1</xsl:param>
    
    <xsl:variable name="partNum" select="count(../preceding-sibling::*) + 1" />
  
    <xsl:variable name="thisPdur">
      <xsl:call-template name="outputPdur" />
    </xsl:variable>

    <!--make sure current note's duration does not extend beyond end of current measure 
        otherwise output nothing; missing note is better than broken notelist-->    
    <xsl:if test="($notelistT + $thisPdur lt $maxTime) or $maxTime eq 0" >
      <xsl:choose>
        <xsl:when test="./rest">
          <xsl:text>R</xsl:text>
        </xsl:when>
        <xsl:when test="./grace">
          <xsl:text>G</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>N</xsl:text>
        </xsl:otherwise>   
      </xsl:choose>
      <!-- time offset placeholder-->
      <xsl:text> t=</xsl:text> 
      <xsl:value-of select="$notelistT" />

      <!-- voice -->
      <xsl:apply-templates select="voice"/> 
      <xsl:if test="not(voice)">
        <xsl:text> v=1</xsl:text>
      </xsl:if>
          
      <!-- staff/part -->
      <xsl:text> npt=</xsl:text>
      <xsl:choose>
        <xsl:when test="$partNum">
          <xsl:value-of select="$partNum"/> 
        </xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
        <!-- staff number begin incrementing again from 1 with each part in MusicXML, 
        but in notelist they increment through the system, across all parts -->
      <xsl:text> stf=</xsl:text>
      <xsl:choose>
        <xsl:when test="$partNum and staff">
          <xsl:value-of select="$partNum + staff - 1"/> 
        </xsl:when>
        <xsl:when test="$partNum and not(staff)">
          <xsl:value-of select="$partNum"/> 
        </xsl:when>
        <xsl:when test="not($partNum) and staff">
          <xsl:value-of select="staff"/> 
        </xsl:when>
        <xsl:otherwise>1</xsl:otherwise>
      </xsl:choose>
        
      <xsl:choose>
        <!-- just in case we don't have type, output dur= and dots= based on duration -->
        <xsl:when test="not(type)">
          <xsl:call-template name="outputDur" />
        </xsl:when>
        <!-- othewise output normally based on <type> and <dots> -->
        <xsl:otherwise>
          <!-- duration type -->
          <xsl:apply-templates select="type"/> 
        
          <!-- dots -->
          <xsl:variable name="dots" select="count(dot)"/>
          <xsl:text> dots=</xsl:text>
          <xsl:value-of select="$dots"/>     
        </xsl:otherwise>
      </xsl:choose>
      <!-- the rest of these apply only to notes, not rests -->
      <xsl:if test="not(./rest)">
        <!-- pitch numeric value (MIDI)-->
        <xsl:apply-templates select="pitch"/> 

        <!-- no pitch -->
        <xsl:apply-templates select="unpitched"/> 

        <!-- duration numeric value        -->
        <!-- if we don't have duration (grace notes), we still need pDur -->
        <!-- template outputPdur now figures this out -->
        <xsl:text> pDur=</xsl:text>
        <xsl:call-template name="outputPdur" />
        <!-- velocity (MIDI) placeholder-->
        <xsl:text> vel=75</xsl:text>      
      </xsl:if>
      <!-- additional appearance placeholders -->
      <xsl:call-template name="appearanceDots"/>
  
      <xsl:call-template name="noteheadAppearance" />
      
      <!-- gracenotes don't have mods= attribute in notelist -->
      <xsl:if test="not(grace)">
        <xsl:apply-templates select="notations[not(tuplet) and not(dynamics)]" />
      </xsl:if>
      
      <xsl:text>&#10;</xsl:text>
    </xsl:if>
  </xsl:template>
  

  <xsl:template match="pitch">
    <xsl:text> nn=</xsl:text>
    
    <xsl:choose>
      <xsl:when test="alter">
        <xsl:call-template name="translatePitch">
          <xsl:with-param name="noteStep" select="step"/>
          <xsl:with-param name="noteOctave" select="octave"/>
          <xsl:with-param name="noteAlter" select="alter"/>
        </xsl:call-template> 
    
        <!-- accidentals -->
        <xsl:apply-templates select="alter" />
      </xsl:when>    
      <!-- no alter tag, so we know it's a natural -->
      <xsl:otherwise>
        <xsl:call-template name="translatePitch">
          <xsl:with-param name="noteStep" select="step"/>
          <xsl:with-param name="noteOctave" select="octave"/>
          <xsl:with-param name="noteAlter" select="0"/>
        </xsl:call-template> 
        <xsl:variable name="thisStep" select="step"/>
        <xsl:variable name="thisOctave" select="xs:integer(octave)" />
        <xsl:variable name="thisStaff" select="xs:integer(../staff)" />
        <xsl:variable name="keyFifths" as="xs:integer">
          <xsl:call-template name="getKeyFifths">
            <xsl:with-param name="thisPartNum" select="count(../preceding-sibling::*) + 1" />
            <xsl:with-param name="thisMeasureNum" select="count(../../preceding::measure)"/>
          </xsl:call-template>
        </xsl:variable>
 
        <xsl:choose>
          <!-- output explicit natural the immediately preceding note in this measure has an alter value -->  
          <!-- or if the natural contradicts the current key (and this is the first note and not preceded by an alter) -->          
          <xsl:when test="((($thisStep eq 'B' and ($keyFifths le -1 or $keyFifths ge 7)) or ($thisStep eq 'E' and ($keyFifths le -2 or $keyFifths ge 6)) or ($thisStep eq 'A' and ($keyFifths le -3 or $keyFifths ge 5)) or ($thisStep eq 'D' and ($keyFifths le -4 or $keyFifths ge 4)) or ($thisStep eq 'G' and ($keyFifths le -5 or $keyFifths ge 3)) or ($thisStep eq 'C' and ($keyFifths le -6 or $keyFifths ge 2)) or ($thisStep eq 'F' and ($keyFifths le -7 or $keyFifths ge 1))) and (($thisStaff and ../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter) or (not($thisStaff) and ../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter) or ((($thisStaff and count(../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave]) eq 0) or (not($thisStaff) and count(../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave]) eq 0))))) or (($thisStaff and ../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter) or (not($thisStaff) and ../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter))"> acc=3 eAcc=3</xsl:when> 

          <!-- otherwise notelist still needs explicit effective naturals -->          
          <xsl:otherwise> acc=0 eAcc=3</xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template match="unpitched">
    <xsl:text> nn=</xsl:text>
      <xsl:call-template name="translatePitch">
        <xsl:with-param name="noteStep" select="display-step"/>
        <xsl:with-param name="noteOctave" select="display-octave"/>
        <xsl:with-param name="noteAlter" select="0"/>
      </xsl:call-template> 

    <xsl:text> acc=0 eAcc=3</xsl:text>
  </xsl:template>

  <!-- outputs numeric duration; called from <note> context
      useful for making sure current note does not extend beyond end of measure, which results in a broken notelist-->
  <xsl:template name="outputPdur">
    <xsl:variable name="partID" select="../@id" />
    <xsl:variable name="durDivisions" select="/score-timewise/measure[1]/part[@id=$partID]/attributes/divisions" />
    <xsl:choose>
      <!-- unpitched, so set duration to 0 -->
      <xsl:when test="duration and unpitched">0</xsl:when>
      <xsl:when test="duration">
        <xsl:value-of select="duration div $durDivisions * 480 * .95" />
      </xsl:when>
      <!--<duration> doesn't exist (probably a gracenote), so determine duration from <type> -->
      <xsl:otherwise>
        <xsl:variable name="thisBaseDur" >
          <xsl:call-template name="translateDurationNum">
            <xsl:with-param name="thisDuration" select="type"/>
          </xsl:call-template>
        </xsl:variable>
    
        <xsl:variable name="numDuration">
          <xsl:call-template name="addDots">
            <xsl:with-param name="currentDur" select="$thisBaseDur" />
            <xsl:with-param name="dotDuration" select="$thisBaseDur" />      
            <xsl:with-param name="dotsCounter" select="count(dots)" />
          </xsl:call-template>    
        </xsl:variable>
    
        <!-- factor in tuplet, if necessary (it's unlikely for a gracenote to be in a tuplet, though) -->
        <xsl:variable name="thisTupletFactor">
          <xsl:choose>
            <xsl:when test="time-modification/actual-notes and time-modification/normal-notes">
              <xsl:value-of select="time-modification/actual-notes div time-modification/normal-notes" />
            </xsl:when>
            <xsl:otherwise>1</xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
          
        <xsl:value-of select="floor($numDuration * $thisTupletFactor * 480 * .95)" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  
  <xsl:template name="appearanceDots">

<!--    1  +  main note (carrying the stem) in a chord (preceding-sibling or following-sibling::part/note[not(rest)]/chord/)
    1  -  other note in a chord (part/note[not(rest)]/chord/)
    -->
    <!-- chords only apply to notes, not rests -->
    <xsl:text> </xsl:text>
    <xsl:choose>
      <xsl:when test="not(./rest)">
      <xsl:variable name="thisVoice" as="xs:integer">
        <xsl:choose>
          <xsl:when test="voice">
            <xsl:value-of select="xs:integer(voice)"/> 
          </xsl:when>
          <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:variable name="thisStaff" as="xs:integer">
        <xsl:choose>
          <xsl:when test="staff">
            <xsl:value-of select="xs:integer(staff)"/> 
          </xsl:when>
          <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:variable name="thisDuration">
        <xsl:value-of select="duration"/> 
      </xsl:variable>

      <xsl:variable name="thisType" select="type" />      
        <xsl:choose>
          <!--if we have a staff and a voice -->
          <xsl:when test="$thisVoice ge 1 and $thisStaff ge 1">
            <xsl:choose>
<!--
              <xsl:when test="not(chord) and following-sibling::note[1 and chord and staff=$thisStaff and voice=$thisVoice]/type eq $thisType">+</xsl:when>
              <xsl:when test="chord and preceding-sibling::note[1 and not(chord) and staff=$thisStaff and voice=$thisVoice]/type=$thisType">-</xsl:when>
              -->
              
              <xsl:when test="not(chord) and count(following-sibling::note[1 and chord and staff=$thisStaff and voice=$thisVoice and type=$thisType]) gt 0">+</xsl:when>
              <xsl:when test="chord and count(preceding-sibling::note[1 and not(chord) and staff=$thisStaff and voice=$thisVoice and type=$thisType]) gt 0">-</xsl:when>
              <!-- handle chords that started out as cross-staff -->
              <xsl:when test="following-sibling::note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType][1] or preceding-sibling::note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType][1]">
                <xsl:choose>
                  <xsl:when test="following-sibling::note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff!=$thisStaff]">+</xsl:when>
                  <xsl:when test="preceding-sibling::note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff!=$thisStaff]">-</xsl:when>
                  <xsl:when test="following-sibling::note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff=$thisStaff] or preceding-sibling::note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff=$thisStaff]">+</xsl:when>
                  <xsl:when test="following-sibling::note[not (chord) and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff=$thisStaff] or preceding-sibling::note[not (chord) and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff=$thisStaff]">-</xsl:when>
                </xsl:choose>
              </xsl:when>
              <xsl:otherwise>.</xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          
          <!--have a voice, but no staff-->
          <xsl:when test="$thisVoice ge 1 and $thisStaff lt 1">
            <xsl:choose>
              <xsl:when test="not(chord) and following-sibling::note[1 and chord and voice=$thisVoice]/type=$thisType">+</xsl:when>     
              <xsl:when test="chord and preceding-sibling::note[1 and not(chord) and voice=$thisVoice]/type=$thisType">-</xsl:when>
              <xsl:otherwise>.</xsl:otherwise>
            </xsl:choose>
          </xsl:when>

          <!--if we have a staff, but no voice -->
          <xsl:when test="$thisStaff ge 1 and $thisVoice lt 1">
            <xsl:choose>
              <xsl:when test="not(chord) and following-sibling::note[1 and chord and staff=$thisStaff]/type=$thisType" >+</xsl:when>     
              <xsl:when test="chord and preceding-sibling::note[1 and not(chord) and staff=$thisStaff]/type=$thisType">-</xsl:when>
              <xsl:otherwise>.</xsl:otherwise>
            </xsl:choose>
          </xsl:when>

                    
          <!--otherwise, assume the document doesn't use voice or staff -->
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="not(chord) and following-sibling::note[1 and chord]/type=$thisType" >+</xsl:when>     
              <xsl:when test="chord and preceding-sibling::note[1 and not(chord)]/type=$thisType">-</xsl:when>
              <xsl:otherwise>.</xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>.</xsl:otherwise>
    </xsl:choose>
    
    <!--   2*  )  tied to the preceding note (notations/tied[@type='stop']) -->
    <xsl:choose>
      <xsl:when test="notations/tied[@type='stop']">)</xsl:when>
      <xsl:otherwise>.</xsl:otherwise>
    </xsl:choose>
    <!--   3*  (  tied to the following grace (notations/tied[@type='start']) -->
    <xsl:choose>
      <xsl:when test="notations/tied[@type='start']">(</xsl:when>
      <xsl:otherwise>.</xsl:otherwise>
    </xsl:choose>    
    <!--   4*  >  slurred to the preceding note or grace note (notations/slur[@type='stop']) -->
    <xsl:choose>
      <xsl:when test="notations/slur[@type='stop']">&gt;</xsl:when>
      <xsl:otherwise>.</xsl:otherwise>
    </xsl:choose>    
    <!--   5*  <  slurred to the following note or grace note (notations/slur[@type='start']) -->
    <xsl:choose>
      <xsl:when test="notations/slur[@type='start']">&lt;</xsl:when>
      <xsl:otherwise>.</xsl:otherwise>
    </xsl:choose>                
    <!--  6*  T  note is a member of a tuplet group (note/time-modification) -->
    <xsl:choose>
      <xsl:when test="time-modification">T</xsl:when>
      <xsl:otherwise>.</xsl:otherwise>
    </xsl:choose> 
    
  </xsl:template>
  
  
  <xsl:template name="noteheadAppearance">
    <xsl:text> appear=</xsl:text>
    <xsl:choose>
      <xsl:when test='notehead="none"'>0</xsl:when>
      <xsl:when test='notehead="x" and notehead/@filled="no"'>2</xsl:when>
      <xsl:when test='notehead="diamond" and notehead/@filled="no"'>3</xsl:when>
      <xsl:when test='notehead="square" and notehead/@filled="yes"'>4</xsl:when>
      <xsl:when test='notehead="square" and notehead/@filled="no"'>5</xsl:when>
      <xsl:when test='notehead="diamond" and notehead/@filled="yes"'>6</xsl:when>
      <xsl:when test='notehead="diamond" and not(notehead/@filled)'>7</xsl:when>
      <xsl:when test='notehead="slash" and not(notehead/@filled)'>9</xsl:when>
      <!--
      <xsl:when test='notehead="none"'>10</xsl:when>
      -->
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
 
     <!-- additional notations go in notelist mod field -->
  <xsl:template match="notations[not(tuplet) and not(dynamics)]">
    <xsl:if test="not(rest) and technical or fermata or ornaments or articulations or technical">
      <xsl:variable name="theseMods">
        <xsl:if test="xs:integer(technical/fingering) eq 1">1,</xsl:if>
        <xsl:if test="xs:integer(technical/fingering) eq 2">2,</xsl:if>
        <xsl:if test="xs:integer(technical/fingering) eq 3">3,</xsl:if>
        <xsl:if test="xs:integer(technical/fingering) eq 4">4,</xsl:if>
        <xsl:if test="xs:integer(technical/fingering) eq 5">5,</xsl:if>
        <xsl:if test="fermata">10,</xsl:if>
        <xsl:if test="ornaments/trill-mark">11,</xsl:if>
        <xsl:if test="articulations/accent">12,</xsl:if>
        <xsl:if test="articulations/strong-accent">13,</xsl:if>
        <xsl:if test="articulations/staccato">14,</xsl:if>
        <xsl:if test="articulations/tenuto">15,</xsl:if>
        <xsl:if test="articulations/strong-accent">30,</xsl:if>
        <xsl:if test="ornaments/mordent">17,</xsl:if>
        <xsl:if test="ornaments/inverted-mordent">18,</xsl:if>
        <xsl:if test="ornaments/turn">19,</xsl:if>
        <xsl:if test="technical/harmonic">21,</xsl:if>
        <xsl:if test="technical/up-bow">22,</xsl:if>
        <xsl:if test="technical/down-bow">23,</xsl:if>
        <xsl:if test="ornaments/inverted-mordent">31,</xsl:if>
      </xsl:variable>
    
      <!-- remove trailing comma while outputting mods-->
      <xsl:variable name="trimmedMods" select="substring($theseMods,0,string-length($theseMods))" />
      
      <!-- make sure our mods= isn't empty, because Nightingale doesn't like that -->
      <xsl:if test="string-length($trimmedMods) gt 0 and $trimmedMods ne ' '">
        <xsl:text> mods=</xsl:text>
        <xsl:value-of select="$trimmedMods" />
      </xsl:if>
    </xsl:if>
  </xsl:template>
 
  <!-- tuplets -->
  
  <xsl:template match="notations/tuplet[@type='start']">
  
  <xsl:text>P v=</xsl:text>
    <xsl:choose>
      <xsl:when test="../../voice">
        <xsl:value-of select="../../voice" /> 
      </xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
    
    <xsl:text> npt=</xsl:text>
    <xsl:value-of select="count(../../../preceding-sibling::*) + 1" />

    <xsl:text> num=</xsl:text>
    <xsl:value-of select="../../time-modification/actual-notes" />
    
    <xsl:text> denom=</xsl:text>
    <xsl:value-of select="../../time-modification/normal-notes" />
          
    <xsl:text> appear=</xsl:text>  
  
    <!--   1  numerator  1  0 -->
    <xsl:choose>
      <xsl:when test="@show-number='normal' or @show-number='none'">0</xsl:when>
      <!--default to one since this is the most standard -->
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>

    <!-- 2  denominator  1  0 -->    
    <xsl:choose>
      <xsl:when test="@show-number='normal' or @show-number='both'">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>      
    </xsl:choose>
  
    <!-- 3  tuplet bracket  1  0 -->
    <xsl:choose>
      <xsl:when test="@bracket='yes'">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>      
    </xsl:choose>
    
    <xsl:text>&#10;</xsl:text>  
  
  </xsl:template>
   
  
  <!--output dur based on duration, when type is not available 
      these seems to happen with some rests, but this will cover this instance
      for any note-like element (note, gracenote, rest)-->
  <xsl:template name="outputDur">
    <xsl:variable name="partID" select="../@id" />
    <xsl:variable name="durDivisions" select="/score-timewise/measure[1]/part[@id=$partID]/attributes/divisions" />
    <xsl:text> dur=</xsl:text>
    <xsl:variable name="thisNumDur" select="duration div $durDivisions" /> 

      <!--map and output normalized xml numeric duration values to notelist duration keys 
          and set base duration (numeric value not including dots)--> 
      <xsl:choose>
        <xsl:when test="$thisNumDur ge 8">
          <xsl:text>1 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="8" />
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$thisNumDur ge 4">
          <xsl:text>2 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="4" />
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$thisNumDur ge 2">
          <xsl:text>3 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="2" />
          </xsl:call-template>
        </xsl:when>        
        <xsl:when test="$thisNumDur ge 1">
          <xsl:text>4 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="1" />
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$thisNumDur ge 0.5">
          <xsl:text>5 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="0.5" />
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$thisNumDur ge 0.25">
          <xsl:text>6 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="0.25" />
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$thisNumDur ge 0.125">
          <xsl:text>7 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="0.125" />
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$thisNumDur ge 0.0625">
          <xsl:text>8 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="0.0625" />
          </xsl:call-template>
        </xsl:when>
        <xsl:when test="$thisNumDur ge 0.03125">
          <xsl:text>9 dots=</xsl:text>
          <xsl:call-template name="determineDots">
            <xsl:with-param name="currentDur" select="$thisNumDur" />
            <xsl:with-param name="baseDur" select="0.03125" />
          </xsl:call-template>
        </xsl:when>
<!--        <xsl:otherwise>
          <xsl:variable name="thisBaseDur" select="0" />
        </xsl:otherwise>
-->        
      </xsl:choose>            
  </xsl:template>


  <!-- recursive template to determine the number of dots from the remainder of a duration -->
  <xsl:template name="determineDots">
    <xsl:param name="currentDur" />
    <xsl:param name="baseDur" />
    <xsl:param name="currentDotVal"><xsl:value-of select="$baseDur div 2" /></xsl:param>    
    <xsl:param name="numDots">1</xsl:param>

    <xsl:choose>
      <!--check for dot-free durations -->
      <xsl:when test="$currentDur - $baseDur le 0">0</xsl:when>
      <!--recurse -->
      <xsl:when test="$currentDur - $currentDotVal gt $baseDur">
        <xsl:call-template name="determineDots">
          <xsl:with-param name="currentDur" select="$currentDur - $currentDotVal" />
          <xsl:with-param name="baseDur" select="$baseDur" />
          <xsl:with-param name="currentDotVal" select="$currentDotVal div 2" />
          <xsl:with-param name="numDots" select="$numDots + 1" />
        </xsl:call-template>
      </xsl:when>
      <!--we're done; output dots -->
      <xsl:otherwise>
        <xsl:value-of select="$numDots" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="addDots" >
    <xsl:param name="dotDuration">0</xsl:param>
    <xsl:param name="currentDur">0</xsl:param>
    <xsl:param name="dotsCounter">0</xsl:param>
    <!--recurse -->
    <xsl:choose>
      <xsl:when test="$dotsCounter gt 0">
        <xsl:call-template name="addDots">
          <xsl:with-param name="dotDuration" select="$dotDuration div 2" />        
          <xsl:with-param name="currentDur" select="$currentDur + $dotDuration div 2" />
          <xsl:with-param name="dotsCounter" select="$dotsCounter - 1" />
        </xsl:call-template>
      </xsl:when>
      <!--output final value -->
      <xsl:otherwise>
        <xsl:value-of select="$currentDur" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
    
  <!-- output accidentals -->
  <xsl:template match="alter">
  
  <xsl:variable name="keyFifths" as="xs:integer">
    <xsl:call-template name="getKeyFifths">
      <xsl:with-param name="thisPartNum" select="count(../../preceding-sibling::*) + 1" />
      <xsl:with-param name="thisMeasureNum" select="count(../../../preceding::measure)"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:variable name="thisStaff" select="xs:integer(../../staff)" />
  <xsl:variable name="thisStep" select="../step" />
  <xsl:variable name="thisOctave" select="xs:integer(../octave)" />
        
      
  <!-- accidental, default to 0 -->  
    <xsl:text> acc=</xsl:text>
    <xsl:choose>
      
      <!-- double flat -->
      <xsl:when test='. ="-2"'>1</xsl:when>
        <!-- flat -->
      <xsl:when test='. ="-1"'>
        <xsl:choose>
          <!-- if accidental is in the key and is not immediately preceded by a note of this type and octave in this measure with a different accidental, or is preceded by a note of this type and octave in this measure with the same accidental, output no accidental -->        
          <xsl:when test="((($thisStep eq 'B' and $keyFifths le -1) or ($thisStep eq 'E' and $keyFifths le -2) or ($thisStep eq 'A' and $keyFifths le -3) or ($thisStep eq 'D' and $keyFifths le -4) or ($thisStep eq 'G' and $keyFifths le -5) or ($thisStep eq 'C' and $keyFifths le -6) or ($thisStep eq 'F' and $keyFifths le -7)) and (not(($thisStaff and ../../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter ne .) or (not($thisStaff) and ../../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter ne .) or not(($thisStaff and ../../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch[not(alter)])) or not((not($thisStaff) and ../../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch[not(alter)]))) or ($thisStaff and count(../../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave]) eq 0) or (not($thisStaff) and count(../../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave]) eq 0))) or (($thisStaff and ../../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter eq .) or (not($thisStaff) and ../../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter eq .))">0</xsl:when>          
          <xsl:otherwise>2</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <!-- natural; this should never happen: instead of <alter>0</alter>, there should be no alter tag in the document at all -->
      <xsl:when test='. ="0"'>3</xsl:when>
  
      <!-- sharp -->
      <xsl:when test='. ="1"'>
        <xsl:choose>
          <!-- if accidental is in the key and is not immediately preceded by a note of this type and octave in this measure with a different accidental, or is preceded by a note of this type and octave in this measure with the same accidental, output no accidental -->        
          <xsl:when test="((($thisStep eq 'F' and $keyFifths ge 1) or ($thisStep eq 'C' and $keyFifths ge 2) or ($thisStep eq 'G' and $keyFifths ge 3) or ($thisStep eq 'D' and $keyFifths ge 4) or ($thisStep eq 'A' and $keyFifths ge 5) or ($thisStep eq 'E' and $keyFifths ge 6) or ($thisStep eq 'B' and $keyFifths ge 7)) and (not(($thisStaff and ../../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter ne .) or (not($thisStaff) and ../../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter ne .) or not(($thisStaff and ../../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch[not(alter)])) or not((not($thisStaff) and ../../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch[not(alter)]))) or ($thisStaff and count(../../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave]) eq 0) or (not($thisStaff) and count(../../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave]) eq 0))) or (($thisStaff and ../../preceding-sibling::note[staff=$thisStaff and pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter eq .) or (not($thisStaff) and ../../preceding-sibling::note[pitch/step=$thisStep and pitch/octave=$thisOctave][1]/pitch/alter eq .))">0</xsl:when>
          <xsl:otherwise>4</xsl:otherwise>
        </xsl:choose>
      </xsl:when>
            
      <!-- double sharp -->
      <xsl:when test='. ="2"'>5</xsl:when>
      
      <xsl:otherwise>0</xsl:otherwise>        
    </xsl:choose>
      
    <!-- effective accidental, default to 3 -->  
    <xsl:text> eAcc=</xsl:text>
    <xsl:choose>
      <xsl:when test='. ="-2"'>1</xsl:when>
      <xsl:when test='. ="-1"'>2</xsl:when>
      <xsl:when test='. ="0"'>3</xsl:when>
      <xsl:when test='. ="1"'>4</xsl:when>
      <xsl:when test='. ="2"'>5</xsl:when>
      <xsl:otherwise>3</xsl:otherwise>     
    </xsl:choose>
  </xsl:template>


  <xsl:template name="getKeyFifths">
    <xsl:param name="thisPartNum">1</xsl:param>
    <xsl:param name="thisMeasureNum">1</xsl:param>
    <xsl:choose>
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/part[$thisPartNum]/attributes/key/fifths">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/part[$thisPartNum]/attributes/key/fifths" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure[1]/part[$thisPartNum]/attributes/key/fifths">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure[1]/part[$thisPartNum]/attributes/key/fifths" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[1]/part[$thisPartNum]/attributes/key/fifths">
        <xsl:value-of select="/score-timewise/measure[1]/part[$thisPartNum]/attributes/key/fifths" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[1]/part[1]/attributes/key/fifths">
        <xsl:value-of select="/score-timewise/measure[1]/part[1]/attributes/key/fifths" />
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
<!--
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure/part[$thisPartNum]/attributes/key/fifths[1]">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure/part[$thisPartNum]/attributes/key/fifths[1]" />
      </xsl:when>
-->  

  <xsl:template match="voice">
    <xsl:variable name="thisVoice">
      <xsl:value-of select="."/> 
    </xsl:variable>

    <xsl:variable name="thisStaff">
      <xsl:value-of select="../staff"/> 
    </xsl:variable>

    <xsl:variable name="thisType">
      <xsl:value-of select="../type"/> 
    </xsl:variable>

    <xsl:variable name="thisDuration">
      <xsl:value-of select="../duration"/> 
    </xsl:variable>
    
    <xsl:variable name="partNum">
      <xsl:value-of select="count(../../preceding-sibling::*) + 1"/> 
    </xsl:variable>

<!--    fuck<xsl:value-of select="../../note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff!=$thisStaff]"/>stain-->

  
    <!-- prevent cross-staff chords, which nightingale can't handle -->
    <xsl:choose>     
      <xsl:when test="../preceding-sibling::note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff!=$thisStaff][1] or ../following-sibling::note[chord and voice=$thisVoice and duration=$thisDuration and type=$thisType and staff!=$thisStaff][1]">
        <xsl:variable name="notelistStaffNum">
          <xsl:choose>
            <xsl:when test="$partNum and $thisStaff">
              <xsl:value-of select="$partNum + $thisStaff - 1"/> 
            </xsl:when>
            <xsl:when test="$partNum and not($thisStaff)">
              <xsl:value-of select="$partNum"/> 
            </xsl:when>
            <xsl:when test="not($partNum) and $thisStaff">
              <xsl:value-of select="$thisStaff"/> 
            </xsl:when>
            <xsl:otherwise>1</xsl:otherwise>
          </xsl:choose>
        </xsl:variable>    
        <xsl:text> v=</xsl:text>
        <!-- add staff number to voice to make it a unique voice number-->
        <xsl:value-of select=". + $notelistStaffNum"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text> v=</xsl:text>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- should use duration for this ?-->
  <xsl:template match="type">
    <xsl:text> dur=</xsl:text>
    <xsl:call-template name="translateDuration">
      <xsl:with-param name="thisDuration" select="."/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="direction">    
    <xsl:apply-templates select="direction-type/words[not(../../sound[@tempo])]"/>
    <xsl:apply-templates select="direction-type/wedge[not(@type='stop')]"/>
<!--    <xsl:apply-templates select="direction-type/dynamics"/> -->
    <xsl:apply-templates select="sound[@tempo]" />
  </xsl:template>
  
  <xsl:template match="lyric">
    <xsl:text>A v=</xsl:text>
    <xsl:choose>
      <xsl:when test="../voice">
        <xsl:value-of select="../voice" /> 
      </xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
    
    <xsl:variable name="partNum" select="count(../../preceding-sibling::*) + 1" />
    <xsl:text> npt=</xsl:text>
    <xsl:value-of select="$partNum"/> 
    <xsl:text> stf=</xsl:text>

    <xsl:choose>
      <xsl:when test="$partNum and ../staff">
        <xsl:value-of select="$partNum + ../staff - 1"/> 
      </xsl:when>
      <xsl:when test="$partNum and not(../staff)">
        <xsl:value-of select="$partNum"/> 
      </xsl:when>      
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
    
    <xsl:text> L </xsl:text>
    <!--           <syllabic>single</syllabic> -->
    <xsl:text>'</xsl:text>
    <xsl:value-of select="text" />
    <xsl:text>'</xsl:text>       
    <xsl:text>&#10;</xsl:text>   
  </xsl:template>

  <xsl:template match="direction-type/words[not(../../sound[@tempo])]">
    <xsl:variable name="partNum" select="count(../../../preceding-sibling::*) + 1" />
    <xsl:text>A v=1 npt=</xsl:text>
    <xsl:value-of select="$partNum"/> 
    <xsl:text> stf=</xsl:text>
    
    <xsl:choose>
      <xsl:when test="$partNum and ../../staff">
        <xsl:value-of select="$partNum + ../../staff - 1"/> 
      </xsl:when>
      <xsl:when test="$partNum and not(../../staff)">
        <xsl:value-of select="$partNum"/> 
      </xsl:when>      
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
    
    <xsl:text> S </xsl:text>
    <xsl:text>'</xsl:text>
    <xsl:value-of select="." />
    <xsl:text>'</xsl:text>    
    <xsl:text>&#10;</xsl:text>   
  </xsl:template>

   <xsl:template match="direction-type/wedge[not(@type='stop')]">
<!-- type="diminuendo" spread="0" /> -->

    <xsl:variable name="partNum" select="count(../../../preceding-sibling::*) + 1" />

    <xsl:text>D stf=</xsl:text>
        
    <xsl:choose>
      <xsl:when test="$partNum and ../../staff">
        <xsl:value-of select="$partNum + ../../staff - 1"/> 
      </xsl:when>
      <xsl:when test="$partNum and not(../../staff)">
        <xsl:value-of select="$partNum"/> 
      </xsl:when>      
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
 
    <xsl:text> dType=</xsl:text>
    <xsl:choose>
      <xsl:when test="@type='diminuendo'">22</xsl:when>
      <xsl:when test="@type='crescendo'">23</xsl:when>
    </xsl:choose>
    <xsl:text>&#10;</xsl:text>
   </xsl:template>

  <!--this still isn't called correctly -->
  <xsl:template match="notations/dynamics">
    <xsl:variable name="partNum" select="count(../../../preceding-sibling::*) + 1" />

    <xsl:text>D stf=</xsl:text>
        
    <xsl:choose>
      <xsl:when test="$partNum and ../../staff">
        <xsl:value-of select="$partNum + ../../staff - 1"/> 
      </xsl:when>
      <xsl:when test="$partNum and not(../../staff)">
        <xsl:value-of select="$partNum"/> 
      </xsl:when>      
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>

    <xsl:text> dType=</xsl:text>
    <xsl:choose>
      <xsl:when test="pppp">1</xsl:when>
      <xsl:when test="ppp">2</xsl:when>
      <xsl:when test="pp">3</xsl:when>
      <xsl:when test="p">4</xsl:when>
      <xsl:when test="mp">5</xsl:when>
      <xsl:when test="mf">6</xsl:when>
      <xsl:when test="f">7</xsl:when>
      <xsl:when test="ff">8</xsl:when>
      <xsl:when test="fff">9</xsl:when>
      <xsl:when test="ffff">10</xsl:when>
<!--
    <xsl:when test="piu piano">11</xsl:when>
    <xsl:when test="meno piano">12</xsl:when>
    <xsl:when test="meno forte">13</xsl:when>
    <xsl:when test="piu forte">14</xsl:when>
-->
      <xsl:when test="sf">15</xsl:when>
      <xsl:when test="fz">16</xsl:when>
      <xsl:when test="sfz">17</xsl:when>
      <xsl:when test="rf">18</xsl:when>
      <xsl:when test="rfz">19</xsl:when>
      <xsl:when test="fp">20</xsl:when>
      <xsl:when test="sfp">21</xsl:when>
      <xsl:otherwise>6</xsl:otherwise>
    </xsl:choose>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

  <xsl:template match="sound[@tempo]">
    <xsl:text>M stf=</xsl:text>
    <xsl:variable name="thisStaff" select="count(../../preceding-sibling::*) + ../../staff"/>
    <xsl:choose>
      <xsl:when test="$thisStaff ge 1">
        <xsl:value-of select="$thisStaff" />
      </xsl:when>
      <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
    <xsl:text> '</xsl:text>
    <xsl:value-of select="../direction-type/words" />
    <xsl:text>' q=</xsl:text>
    <xsl:value-of select="@tempo" />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>  
  
  <xsl:template name="translateDuration">
    <xsl:param name="thisDuration" />
    <xsl:choose>
      <xsl:when test='$thisDuration="full"'>1</xsl:when>
      <xsl:when test='$thisDuration="whole"'>2</xsl:when>
      <xsl:when test='$thisDuration="half"'>3</xsl:when>
      <xsl:when test='$thisDuration="quarter"'>4</xsl:when>
      <xsl:when test='$thisDuration="eighth"'>5</xsl:when>
      <xsl:when test='$thisDuration="16th"'>6</xsl:when>
      <xsl:when test='$thisDuration="32nd"'>7</xsl:when>
      <xsl:when test='$thisDuration="64th"'>8</xsl:when>
      <xsl:when test='$thisDuration="128th"'>9</xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="translateDurationNum">
    <xsl:param name="thisDuration" />
    <xsl:choose>
      <xsl:when test='$thisDuration="full"'>8</xsl:when>
      <xsl:when test='$thisDuration="whole"'>4</xsl:when>
      <xsl:when test='$thisDuration="half"'>2</xsl:when>
      <xsl:when test='$thisDuration="quarter"'>1</xsl:when>
      <xsl:when test='$thisDuration="eighth"'>0.5</xsl:when>
      <xsl:when test='$thisDuration="16th"'>0.25</xsl:when>
      <xsl:when test='$thisDuration="32nd"'>0.125</xsl:when>
      <xsl:when test='$thisDuration="64th"'>0.0625</xsl:when>
      <xsl:when test='$thisDuration="128th"'>0.03125</xsl:when>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="translatePitch">
    <xsl:param name="noteStep" />
    <xsl:param name="noteOctave" />
    <xsl:param name="noteAlter"/> 
    <xsl:variable name="stepAlter" select="$noteAlter + $noteOctave * 12 + 12" />
    <xsl:choose>
      <xsl:when test='$noteStep="C"'>
        <xsl:value-of select="$stepAlter" />
      </xsl:when>      
      <xsl:when test='$noteStep="D"'>
        <xsl:value-of select="$stepAlter + 2" />          
      </xsl:when>      
      <xsl:when test='$noteStep="E"'>
        <xsl:value-of select="$stepAlter + 4" />          
      </xsl:when>      
      <xsl:when test='$noteStep="F"'>
        <xsl:value-of select="$stepAlter + 5" />      
      </xsl:when>
      <xsl:when test='$noteStep="G"'>
        <xsl:value-of select="$stepAlter + 7" />
      </xsl:when>
      <xsl:when test='$noteStep="A"'>
        <xsl:value-of select="$stepAlter + 9" />
      </xsl:when>
      <xsl:when test='$noteStep="B"'>
        <xsl:value-of select="$stepAlter + 11" />   
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  
    
  <!--Notelist requires all events to be in sequential order; 
      this template outputs ordered notes for a given measure -->
  <xsl:template name="orderedNote">
    <xsl:param name="offsetCounter">0</xsl:param>
    <!--<xsl:param name="thisMeasure">1</xsl:param> -->
    <xsl:param name="measureStart">0</xsl:param>
    <xsl:param name="measureEnd">1</xsl:param>    

    <xsl:variable name="thisGreatestDivision">
      <xsl:call-template name="greatestDivisions" />
    </xsl:variable>
  
    <!--need to force tuplet to be the first item at this time .. following-sibling?-->
    <!--so, look ahead to remaining notes in measure, to see if there are tuplets at the current time position -->
    <xsl:for-each select ="part/note[notations/tuplet]">
      <xsl:variable name="tuplPartID" select="../@id" />
            
      <!-- calculate total duration of all notes preceding current note in current measure -->
      <xsl:variable name="tuplMeasureTimePos" as="xs:decimal">
        <xsl:call-template name="getTimePosition">
          <xsl:with-param name="durDivisions" select="/score-timewise/measure[1]/part[@id=$tuplPartID]/attributes/divisions"/>  
          <xsl:with-param name="voiceNum" select="voice" />
          <xsl:with-param name="staffNum" select="staff" />
        </xsl:call-template>
      </xsl:variable>
      
      <!-- if the look-ahead tuplet is at the current time, we need to output it before any notes at this time -->
      <!-- also need to make sure it's not output a second time -->
      <xsl:if test="($offsetCounter eq $measureStart + round($tuplMeasureTimePos * $thisGreatestDivision) and $tuplMeasureTimePos ge 0) or $measureEnd eq 0">
        <xsl:apply-templates select="notations/tuplet[@type='start']" />
      </xsl:if>
    </xsl:for-each>

  <!--need to process all graces notes at current time position first -->
  <xsl:for-each select="part/note[grace]">
   
    <xsl:variable name="gracePartID" select="../@id" />
      <xsl:variable name="graceMeasureTimePos" as="xs:decimal">
        <xsl:call-template name="getTimePosition">
        <xsl:with-param name="durDivisions" select="/score-timewise/measure[1]/part[@id=$gracePartID]/attributes/divisions"/>  
        <xsl:with-param name="voiceNum" select="voice" />
        <xsl:with-param name="staffNum" select="staff" />
      </xsl:call-template>
    </xsl:variable>
      
    <!-- only output notes at current time position (this is how we order times for notelist output); 
    make sure we don't have any notes start at a point beyond the end of the current measure
    these may get lost, but it's better than a broken notelist -->     
    <xsl:if test="($offsetCounter eq $measureStart + round($graceMeasureTimePos * $thisGreatestDivision) and $graceMeasureTimePos ge 0 and $graceMeasureTimePos lt $measureEnd - 1) or $measureEnd eq 0">
     
      <!-- can a grace note have a lyric? -->
      <xsl:apply-templates select="lyric" />
      <xsl:apply-templates select="notations/dynamics" />
    
      <!-- output current note; all time values are normalized to notelist's divisions (480 per quarter note)-->
      <xsl:call-template name="processNote" >
        <xsl:with-param name="notelistT" select="round((($measureStart div $thisGreatestDivision) + $graceMeasureTimePos) * 480)" />
        <xsl:with-param name="maxTime" select="$measureEnd div $thisGreatestDivision * 480" />        
      </xsl:call-template>
    
    
    </xsl:if>
  </xsl:for-each>
  
<!-- order notes by time and output one measure at a time -->

   <xsl:for-each select="part/note[not(grace)]">
   
      <xsl:variable name="partID" select="../@id" />
      <!-- calculate total duration of all notes preceding current note in current measure -->

      <xsl:variable name="thisMeasureTimePos" as="xs:decimal">
        <xsl:call-template name="getTimePosition">
          <xsl:with-param name="durDivisions" select="/score-timewise/measure[1]/part[@id=$partID]/attributes/divisions"/>  
          <xsl:with-param name="voiceNum" select="voice" />
          <xsl:with-param name="staffNum" select="staff" />
        </xsl:call-template>
      </xsl:variable>
                  

      <!--add up previous measure's offset, all notes up to current position in current measure, subtract
      <backup> duration for multiple voices ?? -->
    
         <!-- only output notes at current time position (this is how we order times for notelist output); 
              make sure we don't have any notes start at a point beyond the end of the current measure
              these may get lost, but it's better than a broken notelist -->     
      <xsl:if test="($offsetCounter eq $measureStart + round($thisMeasureTimePos * $thisGreatestDivision) and $thisMeasureTimePos ge 0.0 and $thisMeasureTimePos lt $measureEnd - 1) or $measureEnd eq 0">
      
      
        <!--need to force tuplet to be the first item at this time .. following-sibling?-->

        <xsl:apply-templates select="lyric" />
        
        <!-- output current note; all time values are normalized to notelist's divisions (480 per quarter note)-->
        <xsl:call-template name="processNote" >
          <xsl:with-param name="notelistT" select="round((($measureStart div $thisGreatestDivision) + $thisMeasureTimePos) * 480)" />
          <xsl:with-param name="maxTime" select="$measureEnd div $thisGreatestDivision * 480" />        
        </xsl:call-template>
        
      </xsl:if>
    </xsl:for-each>
    
      <xsl:if test="$offsetCounter lt $measureEnd">      
      <xsl:call-template name="orderedNote">
        <!-- check every integer in time, these have been normalized to 
        the resolution of the greatest divisions in the document -->
        <xsl:with-param name="offsetCounter" select="$offsetCounter + 1" />
        <xsl:with-param name="measureStart" select="$measureStart" />
        <xsl:with-param name="measureEnd" select="$measureEnd" />        
      </xsl:call-template>
    </xsl:if>
      
  </xsl:template>


  <!--template to determine the greatest divisions (highest resolution) -->
  <xsl:template name="greatestDivisions">
    <xsl:variable name="lastDivision" select="count(/score-timewise/measure/part/attributes/divisions)" />
    <xsl:for-each select="/score-timewise/measure/part/attributes/divisions">
      <xsl:sort data-type="number" order="ascending" /> 
      <xsl:if test="position() eq $lastDivision"><xsl:value-of select="." /></xsl:if>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template name="getTimePosition">
    <xsl:param name="staffNum">0</xsl:param>
    <xsl:param name="voiceNum">0</xsl:param>
    <xsl:param name="durDivisions">1</xsl:param>
  
      <xsl:choose>
        <xsl:when test="chord">
          <xsl:choose>
            <xsl:when test="voice">
              <xsl:value-of select ="(sum(./preceding-sibling::note[voice=$voiceNum and not(chord)]/duration) - ./preceding-sibling::note[1]/duration) div $durDivisions" />
            </xsl:when>
            <xsl:when test="not(voice)">          
              <xsl:value-of select ="(sum(./preceding-sibling::note[not(chord)]/duration) - ./preceding-sibling::note[1]/duration) div $durDivisions" /> 
            </xsl:when>
            <!--otherwise, no preceding notes in measure -->
            <xsl:otherwise>0</xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:choose>
            <xsl:when test="voice">
              <xsl:value-of select ="sum(./preceding-sibling::note[voice=$voiceNum and not(chord)]/duration) div $durDivisions" />
            </xsl:when>
            <xsl:when test="not(voice)">          
              <xsl:value-of select ="sum(./preceding-sibling::note[not(chord)]/duration) div $durDivisions" />                
            </xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose> 
  </xsl:template>
  
</xsl:stylesheet>  




<!--
  These templates have been replaced in version 0.3 with a simpler, tidier xpath expression for deterimining measure start position,
  but are being left here, just in case, until 0.4.
  
  <- number of beats to the current measure ->
  <xsl:template name="measureOffset">
    <xsl:param name="endMeasure">0</xsl:param>
    <xsl:param name="currentMeasure">1</xsl:param>
    <xsl:param name="totalOffset">0</xsl:param>
      
    <- get divisions, beat, and beat-type for current measure, based on part[1]; for security, may want to
         get these from other parts, if part 1 is unavailable ->
    <xsl:variable name="part1Divisions" select="/score-timewise/measure[1]/part[1]/attributes/divisions" />

    <xsl:variable name="part1Beats">
      <xsl:call-template name="getBeats">
        <xsl:with-param name="thisPartNum" select="1" />
        <xsl:with-param name="thisMeasureNum" select="$currentMeasure" />        
      </xsl:call-template>
    </xsl:variable>

    <xsl:variable name="part1BeatType">
      <xsl:call-template name="getBeatType">
        <xsl:with-param name="thisPartNum" select="1" />
        <xsl:with-param name="thisMeasureNum" select="$currentMeasure" />        
      </xsl:call-template>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$endMeasure lt 1">0</xsl:when>
      <xsl:when test="xs:integer($currentMeasure) ge xs:integer($endMeasure)">
        <xsl:value-of select="($totalOffset + $part1Beats div $part1BeatType) * 4" />
      </xsl:when>
      <xsl:otherwise>
      <- recurse ->
        <xsl:call-template name="measureOffset">
          <xsl:with-param name="endMeasure" select="$endMeasure" />
          <xsl:with-param name="currentMeasure" select="$currentMeasure + 1" />
          <xsl:with-param name="totalOffset" select="$totalOffset + ($part1Beats div $part1BeatType)"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="getBeats">
    <xsl:param name="thisPartNum">1</xsl:param>
    <xsl:param name="thisMeasureNum">1</xsl:param>
    <xsl:choose>
      <xsl:when test="/score-timewise/measure[1]/part[$thisPartNum]/attributes/time/beats">
        <xsl:value-of select="/score-timewise/measure[1]/part[$thisPartNum]/attributes/time/beats" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[1]/part[1]/attributes/time/beats">
        <xsl:value-of select="/score-timewise/measure[1]/part[1]/attributes/time/beats" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure[1]/part[$thisPartNum]/attributes/time/beats">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure[1]/part[$thisPartNum]/attributes/time/beats" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure/part[$thisPartNum]/attributes/time/beats[1]">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure/part[$thisPartNum]/attributes/time/beats[1]" />
      </xsl:when>
      <xsl:otherwise>4</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
<-
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/part[$thisPartNum]/attributes/time/beats">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/part[$thisPartNum]/attributes/time/beats" />
      </xsl:when>

->    
  <xsl:template name="getBeatType">
    <xsl:param name="thisPartNum">1</xsl:param>
    <xsl:param name="thisMeasureNum">1</xsl:param>
    <xsl:choose>
      <xsl:when test="/score-timewise/measure[1]/part[1]/attributes/time/beat-type">
        <xsl:value-of select="/score-timewise/measure[1]/part[1]/attributes/time/beat-type" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure[1]/part[$thisPartNum]/attributes/time/beat-type">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure[1]/part[$thisPartNum]/attributes/time/beat-type" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure/part[$thisPartNum]/attributes/time/beat-type[1]">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/preceding-sibling::measure/part[$thisPartNum]/attributes/time/beat-type[1]" />
      </xsl:when>
      <xsl:when test="/score-timewise/measure[1]/part[$thisPartNum]/attributes/time/beat-type">
        <xsl:value-of select="/score-timewise/measure[1]/part[$thisPartNum]/attributes/time/beat-type" />
      </xsl:when>
      <xsl:otherwise>4</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

<-
      <xsl:when test="/score-timewise/measure[$thisMeasureNum]/part[$thisPartNum]/attributes/time/beat-type">
        <xsl:value-of select="/score-timewise/measure[$thisMeasureNum]/part[$thisPartNum]/attributes/time/beat-type" />
      </xsl:when>

-->      
