File: ags-simple.xsl

package info (click to toggle)
gsequencer 8.2.9-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 74,272 kB
  • sloc: ansic: 1,195,333; xml: 31,048; cpp: 9,749; sh: 5,798; makefile: 4,024; perl: 536; sed: 16; python: 11
file content (225 lines) | stat: -rw-r--r-- 8,061 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?xml version="1.0"?>
<!DOCTYPE xsl[
<!ENTITY version "0.7.129">
<!ENTITY buildid "XSLT">
<!ENTITY audiochannels "1">
<!ENTITY defaultsf2 ""> <!-- /usr/share/sounds/sf2/FluidR3_GM.sf2 -->
<!ENTITY preset ""> <!-- "Yamaha Grand Piano" -->
<!ENTITY instrument ""> <!-- "Yamaha Grand Piano" -->
]>

<!-- Copyright (C) 2005-2018 Joël Krähemann -->
<!-- Copying and distribution of this file, with or without modification, -->
<!-- are permitted in any medium without royalty provided the copyright -->
<!-- notice and this notice are preserved.  This file is offered as-is, -->
<!-- without any warranty. -->


<xsl:stylesheet version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
  <xsl:strip-space elements="*"/>
  
  <!-- ticks to tact -->
  <xsl:template name="ticks-to-tact">
    <xsl:param name="current-tick"/>

    <xsl:variable name="division" select="//midi-header/@division"/>
    <xsl:variable name="tempo" select="//midi-tracks/midi-track[1]/midi-message[@event='tempo-number']/@tempo"/>
    <xsl:variable name="secs">60.0</xsl:variable>
    <xsl:variable name="microsecs">1000000.0</xsl:variable>

    <xsl:variable name="bpm">
      <xsl:call-template name="midi-read-bpm"/>
    </xsl:variable>

    <xsl:if test="$division &gt; 0">
      
      <xsl:value-of select="16.0 * $bpm div $secs * $current-tick * $tempo div $division div $microsecs"/>
    </xsl:if>

    <xsl:if test="not($division &gt; 0)">
      <xsl:variable name="smtpe-format">floor($division div 256)</xsl:variable>
      <xsl:variable name="smtpe-resoulution">floor($division div 256) * 256</xsl:variable>

      <xsl:value-of select="16.0 * $bpm div $secs * $current-tick div $smtpe-format div $smtpe-resolution div $microsecs"/>
    </xsl:if>
  </xsl:template>

  
  <!-- midi read bpm -->
  <xsl:template name="midi-read-bpm">
    <xsl:variable name="ticks" select="//midi-header/@division"/>
    <xsl:variable name="division" select="//midi-header/@division"/>
    <xsl:variable name="tempo" select="//midi-tracks/midi-track[1]/midi-message[@event='tempo-number']/@tempo"/>
    <xsl:variable name="secs">60.0</xsl:variable>
    <xsl:variable name="microsecs">1000000.0</xsl:variable>
    
    <xsl:if test="$division &gt; 0">

      <xsl:value-of select="$secs div $ticks div $tempo * $division * $microsecs"/>

    </xsl:if>

    <xsl:if test="not($division &gt; 0)">
      <xsl:variable name="smtpe-format">floor($division div 256)</xsl:variable>
      <xsl:variable name="smtpe-resoulution">floor($division div 256) * 256</xsl:variable>

      <xsl:value-of select="$secs div $ticks * $smtpe-format * $smtpe-resolution * $microsecs"/>

    </xsl:if>
  </xsl:template>
  
  <!-- note node -->
  <xsl:template match="midi-message">
    <xsl:param name="parent-sequence"/>
    <xsl:param name="parent-instrument"/>

    <xsl:if test="current()/@event='note-on'">

      <xsl:variable name="sequence" select="../midi-message[boolean(@sequence-name)]/@sequence-name"/>
      <xsl:variable name="instrument" select="../midi-message[boolean(@instrument-name)]/@instrument-name"/>
      <xsl:variable name="current-tick"><xsl:value-of select="./@delta-time"/></xsl:variable>

      <xsl:if test="$parent-sequence=$sequence and $parent-instrument=$instrument">
	<ags-sf-note>
	  
	  <xsl:attribute name="id">note-<xsl:value-of select="position()"/></xsl:attribute>
	  <xsl:attribute name="flags">0</xsl:attribute>

	  <xsl:variable name="y" select="./@note"/>
	  <xsl:attribute name="y"><xsl:value-of select="$y"/></xsl:attribute>
	  
	  <xsl:variable name="x0">
	    <xsl:call-template name="ticks-to-tact">
	      <xsl:with-param name="current-tick" select="$current-tick"/>
	    </xsl:call-template>
	  </xsl:variable>
	  <xsl:attribute name="x0"><xsl:value-of select="round($x0)"/></xsl:attribute>
	  
	  <xsl:if test="following-sibling::node()[@event='note-off' and @note='$y']">
	    <xsl:variable name="next-tick"><xsl:value-of select="following-sibling::node()[@event='note-off' and @note='$y']/@delta-time"/></xsl:variable>

	    <xsl:variable name="x1">
	      <xsl:call-template name="ticks-to-tact">
		<xsl:with-param name="current-tick" select="$next-tick"/>
	      </xsl:call-template>
	    </xsl:variable>
	    
	    <xsl:attribute name="x1">
	      <xsl:value-of select="floor($x1)"/>
	    </xsl:attribute>
	  </xsl:if>
	  
	  <xsl:if test="not(following-sibling::ags-sf-note[@event='note-off' and @note='$y'])">
	    <xsl:variable name="add-one" select="1"/>
	    <xsl:attribute name="x1"><xsl:value-of select="round($x0) + $add-one"/></xsl:attribute>
	  </xsl:if>
	  
	</ags-sf-note>
      </xsl:if>
      
    </xsl:if>
    
  </xsl:template>

  <!-- notation node -->
  <xsl:template name="add-notation">
    <xsl:param name="sequence"/>
    <xsl:param name="instrument"/>
    <xsl:param name="num" select="1"/>
    <xsl:param name="count"/>

    <ags-sf-notation>
      <xsl:attribute name="id">notation-<xsl:value-of select="position()"/></xsl:attribute>
      <xsl:attribute name="flags">0</xsl:attribute>
      <xsl:attribute name="audio-channel"><xsl:value-of select="$num - 1"/></xsl:attribute>
      <xsl:attribute name="base-note">C</xsl:attribute>

      <!-- add note -->
      <xsl:apply-templates select="/midi/midi-tracks/midi-track/midi-message">
	<xsl:with-param name="parent-sequence" select="$sequence"/>
	<xsl:with-param name="parent-instrument" select="$instrument"/>
      </xsl:apply-templates>

    </ags-sf-notation>
    
    <xsl:if test="$num &lt; $count">
      <xsl:call-template name="add-notation">
	<xsl:with-param name="num" select="$num + 1"/>
	<xsl:with-param name="count" select="$count"/>
      </xsl:call-template>
    </xsl:if>
    
  </xsl:template>
  
  <!-- machine node -->
  <xsl:template name="add-machine">
    <xsl:param name="sequence"/>
    <xsl:param name="instrument"/>

    <ags-sf-machine>
      <xsl:attribute name="id">machine-<xsl:value-of select="position()"/></xsl:attribute>
      <xsl:attribute name="type">AgsFFPlayer</xsl:attribute>
      <xsl:attribute name="name"><xsl:value-of select="$sequence"/> - <xsl:value-of select="$instrument"/></xsl:attribute>
      <xsl:attribute name="channels">&audiochannels;</xsl:attribute>
      <xsl:attribute name="output-pads">1</xsl:attribute>
      <xsl:attribute name="input-pads">128</xsl:attribute>

      <!-- add notation -->
      <ags-sf-notation-list>
	<xsl:call-template name="add-notation">
	  <xsl:with-param name="count" select="&audiochannels;"/>
	  <xsl:with-param name="sequence" select="$sequence"/>
	  <xsl:with-param name="instrument" select="$instrument"/>
	</xsl:call-template>
	
      </ags-sf-notation-list>
    </ags-sf-machine>

  </xsl:template>

  <!-- midi track -->
  <xsl:template match="/midi/midi-tracks/midi-track">
    <xsl:if test="not(midi-message[@event='time-signature'])">
      <xsl:variable name="sequence" select="midi-message[boolean(@sequence-name)]/@sequence-name"/>
      <xsl:variable name="instrument" select="midi-message[boolean(@instrument-name)]/@instrument-name"/>

      <xsl:variable name="previous" select="preceding-sibling::node()"/>

      <xsl:if test="not($previous/midi-message/@sequence-name=$sequence and $previous/midi-message/@instrument-name=$instrument)">

	<!-- add machine -->
	<xsl:call-template name="add-machine">
	  <xsl:with-param name="sequence" select="$sequence"/>
	  <xsl:with-param name="instrument" select="$instrument"/>
	</xsl:call-template>
      </xsl:if>	
    </xsl:if>
  </xsl:template>

  <xsl:template match="midi-header">    
    <xsl:variable name="bpm">
      <xsl:call-template name="midi-read-bpm"/>
    </xsl:variable>
  </xsl:template>

  <!-- root node -->
  <xsl:template match="/">    
    <ags-simple-file>
      <ags-sf-window>
	<ags-sf-machine-list>

	  <!-- add machine -->
	  <xsl:apply-templates select="/midi/midi-tracks/midi-track"/>
    	  
	</ags-sf-machine-list>

	<ags-sf-navigation/>
      </ags-sf-window>
    </ags-simple-file>

  </xsl:template>

</xsl:stylesheet>