From 86d1f7cdc0a8a0d4a06c37fe5715db9312730a0c Mon Sep 17 00:00:00 2001
From: Richard Lewis <richard.lewis@gold.ac.uk>
Date: Tue, 15 Apr 2014 17:37:25 +0100
Subject: [PATCH] Altered open2an transform to calculate start times using new
 Musicbrainz alignment data

---
 templates/opera2an.xslt | 78 ++++++++++++++++++++++-------------------
 1 file changed, 41 insertions(+), 37 deletions(-)

diff --git a/templates/opera2an.xslt b/templates/opera2an.xslt
index a2a73f0..8d3da28 100644
--- a/templates/opera2an.xslt
+++ b/templates/opera2an.xslt
@@ -1,9 +1,8 @@
 <?xml version="1.0" encoding="utf-8" ?>
 
-<!-- Transform TEI-P5 to Akoma Ntoso (SayIt format) -->
-<!-- v0.9 -->
+<!-- Transform TEI-P5 libretto to Akoma Ntoso (SayIt format) -->
 <!-- by JMG & RL -->
-<!-- Feb 25 2014 -->
+<!-- 2014-04-15 -->
 
 <xsl:stylesheet
     version="1.0"
@@ -18,6 +17,8 @@
 <xsl:param name="START-TIME" />
 <xsl:param name="PERFORMANCE" />
 
+<xsl:variable name="start" select="date:add(date:date($START-TIME), date:duration((date:hour-in-day($START-TIME) * 3600) + (date:minute-in-hour($START-TIME) * 60) + date:second-in-minute($START-TIME)))" />
+
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 
 <xsl:key name="singers" match="//tei:singer" use="@who" />
@@ -68,20 +69,15 @@
 </xsl:template>
 
 <xsl:template name="sum-preceding-performance-time">
-  <!-- FIXME The startTimes that you calculate here are actually out
-       by one cue: you're actually logging the startTime *including*
-       the time that elapses during the track. -->
-  <xsl:param name="ptr" />
+  <xsl:param name="from-when" />
   <xsl:param name="acc" select="number(0)" />
 
   <xsl:choose>
-    <xsl:when test="$ptr/preceding::tei:ptr[contains(@cRef,$PERFORMANCE)]">
-      <xsl:variable name="audio-cue-id"><xsl:value-of select="substring-after($ptr/@target,'#')" /></xsl:variable>
-      <xsl:variable name="track" select="document('../der-ring-des-nibelungen/audio-cues.xml')//cue[@id=$audio-cue-id]" />
-    
+    <xsl:when test="$from-when/@since">
+      <xsl:variable name="since-id"><xsl:value-of select="substring-after($from-when/@since,'#')" /></xsl:variable>
       <xsl:call-template name="sum-preceding-performance-time">
-        <xsl:with-param name="ptr" select="$ptr/preceding::tei:ptr[contains(@cRef,$PERFORMANCE)][1]" />
-        <xsl:with-param name="acc" select="number($acc) + number($track/seconds)" />
+        <xsl:with-param name="from-when" select="//tei:when[@xml:id=$since-id]" />
+        <xsl:with-param name="acc" select="number($acc) + number($from-when/@interval)" />
       </xsl:call-template>
     </xsl:when>
     <xsl:otherwise>
@@ -92,18 +88,22 @@
 
 <xsl:template match="tei:sg[not(tei:stage)]">
   <xsl:choose>
-    <xsl:when test=".//tei:ptr[contains(@cRef,$PERFORMANCE)]">
-      <xsl:variable name="audio-cue" select=".//tei:ptr[contains(@cRef,$PERFORMANCE)][1]" />
-      <!--
-      <xsl:variable name="audio-cue-id"><xsl:value-of select="substring-after($audio-cue/@target,'#')" /></xsl:variable>
-      <xsl:variable name="track" select="document('../der-ring-des-nibelungen/audio-cues.xml')//cue[@id=$audio-cue-id]" />
-      <xsl:message>[no stage] Found track: <xsl:value-of select="$track/performance" />: <xsl:value-of select="$track/tracksort" /></xsl:message>
-      -->
-      <xsl:variable name="elapsed-time"><xsl:call-template name="sum-preceding-performance-time"><xsl:with-param name="ptr" select="$audio-cue" /></xsl:call-template></xsl:variable>
-      <xsl:variable name="start-time"><xsl:value-of select="date:add(date:date($START-TIME), date:duration($elapsed-time))" /></xsl:variable>
-      <!--
-      <xsl:message>[no stage] Elapsed time: "<xsl:value-of select="$elapsed-time" />"; start-time: "<xsl:value-of select="$start-time" />"</xsl:message>
-      -->
+    <xsl:when test="tei:anchor[@type='audio-cue' and contains(@xml:id,$PERFORMANCE)]">
+      <xsl:variable
+          name="audio-cue"
+          select="tei:anchor[@type='audio-cue' and contains(@xml:id,$PERFORMANCE)][1]" />
+      <xsl:variable name="audio-cue-id">
+        <xsl:value-of select="$audio-cue/@xml:id" />
+      </xsl:variable>
+      <xsl:variable name="elapsed-time">
+        <xsl:call-template name="sum-preceding-performance-time">
+          <xsl:with-param name="from-when" select="//tei:when[@xml:id=concat('when-',substring-after($audio-cue/@xml:id,'cue-'))]" />
+        </xsl:call-template>
+      </xsl:variable>
+      <xsl:variable name="start-time">
+        <xsl:value-of select="date:add($start, date:duration($elapsed-time))" />
+      </xsl:variable>
+
       <speech by="#{translate(tei:singer[1]/@who,' ','-')}" startTime="{$start-time}">
         <xsl:apply-templates/>
       </speech>
@@ -126,18 +126,22 @@
 
 <xsl:template match="tei:sg[tei:stage]">
   <xsl:choose>
-    <xsl:when test=".//tei:ptr[contains(@cRef,$PERFORMANCE)]">
-      <xsl:variable name="audio-cue" select=".//tei:ptr[contains(@cRef,$PERFORMANCE)][1]" />
-      <!--
-      <xsl:variable name="audio-cue-id"><xsl:value-of select="substring-after($audio-cue/@target,'#')" /></xsl:variable>
-      <xsl:variable name="track" select="document('../der-ring-des-nibelungen/audio-cues.xml')//cue[@id=$audio-cue-id]" />
-      <xsl:message>[stage] Found track: <xsl:value-of select="$track/performance" />: <xsl:value-of select="$track/tracksort" /></xsl:message>
-      -->
-      <xsl:variable name="elapsed-time"><xsl:call-template name="sum-preceding-performance-time"><xsl:with-param name="ptr" select="$audio-cue" /></xsl:call-template></xsl:variable>
-      <xsl:variable name="start-time"><xsl:value-of select="date:add(date:date($START-TIME), date:duration($elapsed-time))" /></xsl:variable>
-      <!--
-      <xsl:message>[stage] Elapsed time: "<xsl:value-of select="$elapsed-time" />"; start-time: "<xsl:value-of select="$start-time" />"</xsl:message>
-      -->
+    <xsl:when test="tei:anchor[@type='audio-cue' and contains(@xml:id,$PERFORMANCE)]">
+      <xsl:variable
+          name="audio-cue"
+          select="tei:anchor[@type='audio-cue' and contains(@xml:id,$PERFORMANCE)][1]" />
+      <xsl:variable name="audio-cue-id">
+        <xsl:value-of select="$audio-cue/@xml:id" />
+      </xsl:variable>
+      <xsl:variable name="elapsed-time">
+        <xsl:call-template name="sum-preceding-performance-time">
+          <xsl:with-param name="from-when" select="//tei:when[@xml:id=concat('when-',substring-after($audio-cue/@xml:id,'cue-'))]" />
+        </xsl:call-template>
+      </xsl:variable>
+      <xsl:variable name="start-time">
+        <xsl:value-of select="date:add($start, date:duration($elapsed-time))" />
+      </xsl:variable>
+
       <xsl:for-each select="*[generate-id() = generate-id(key('stageKey', concat(generate-id(..), '|', generate-id(preceding-sibling::tei:stage[1])))[1])]">
         <xsl:apply-templates select="preceding-sibling::tei:stage[1]" />
         <xsl:element name="speech">
-- 
GitLab