wavesurfer
Version:
Interactive navigable audio visualization using Web Audio and Canvas
3 lines • 4.42 kB
JavaScript
/*! wavesurfer.js 1.3.4 (Sat, 25 Feb 2017 22:02:05 GMT)
* https://github.com/katspaugh/wavesurfer.js
* @license CC-BY-3.0 */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.ELAN={Types:{ALIGNABLE_ANNOTATION:"ALIGNABLE_ANNOTATION",REF_ANNOTATION:"REF_ANNOTATION"},init:function(a){if(this.data=null,this.params=a,this.container="string"==typeof a.container?document.querySelector(a.container):a.container,!this.container)throw Error("No container for ELAN");this.bindClick(),a.url&&this.load(a.url)},load:function(a){var b=this;this.loadXML(a,function(a){b.data=b.parseElan(a),b.render(),b.fireEvent("ready",b.data)})},loadXML:function(a,b){var c=new XMLHttpRequest;c.open("GET",a,!0),c.responseType="document",c.send(),c.addEventListener("load",function(a){b&&b(a.target.responseXML)})},parseElan:function(a){var b=Array.prototype.forEach,c=Array.prototype.map,d={media:{},timeOrder:{},tiers:[],annotations:{},alignableAnnotations:[]},e=a.querySelector("HEADER"),f="milliseconds"==e.getAttribute("TIME_UNITS"),g=e.querySelector("MEDIA_DESCRIPTOR");d.media.url=g.getAttribute("MEDIA_URL"),d.media.type=g.getAttribute("MIME_TYPE");var h=a.querySelectorAll("TIME_ORDER TIME_SLOT"),i={};return b.call(h,function(a){var b=parseFloat(a.getAttribute("TIME_VALUE"));f&&(b=Math.round(100*b)/1e5),i[a.getAttribute("TIME_SLOT_ID")]=b}),d.tiers=c.call(a.querySelectorAll("TIER"),function(a){return{id:a.getAttribute("TIER_ID"),linguisticTypeRef:a.getAttribute("LINGUISTIC_TYPE_REF"),defaultLocale:a.getAttribute("DEFAULT_LOCALE"),annotations:c.call(a.querySelectorAll("REF_ANNOTATION, ALIGNABLE_ANNOTATION"),function(a){var b={type:a.nodeName,id:a.getAttribute("ANNOTATION_ID"),ref:a.getAttribute("ANNOTATION_REF"),value:a.querySelector("ANNOTATION_VALUE").textContent.trim()};return this.Types.ALIGNABLE_ANNOTATION==b.type&&(b.start=i[a.getAttribute("TIME_SLOT_REF1")],b.end=i[a.getAttribute("TIME_SLOT_REF2")],d.alignableAnnotations.push(b)),d.annotations[b.id]=b,b},this)}},this),d.tiers.forEach(function(a){a.annotations.forEach(function(a){null!=a.ref&&(a.reference=d.annotations[a.ref])},this)},this),d.alignableAnnotations.sort(function(a,b){var c=a.start-b.start;return 0==c&&(c=b.end-a.end),c}),d.length=d.alignableAnnotations.length,d},render:function(){var a=this.data.tiers;this.params.tiers&&(a=a.filter(function(a){return a.id in this.params.tiers},this));var b={},c={};a.forEach(function(a,d){a.annotations.forEach(function(a){a.reference&&a.reference.type==this.Types.ALIGNABLE_ANNOTATION&&(a.reference.id in b||(b[a.ref]={}),b[a.ref][d]=a,c[d]=!0)},this)},this),c=Object.keys(c).sort(),this.renderedAlignable=this.data.alignableAnnotations.filter(function(a){return b[a.id]});var d=document.createElement("table");d.className="wavesurfer-annotations";var e=document.createElement("thead"),f=document.createElement("tr");e.appendChild(f),d.appendChild(e);var g=document.createElement("th");g.textContent="Time",g.className="wavesurfer-time",f.appendChild(g),c.forEach(function(b){var c=a[b],d=document.createElement("th");d.className="wavesurfer-tier-"+c.id,d.textContent=c.id,d.style.width=this.params.tiers[c.id],f.appendChild(d)},this);var h=document.createElement("tbody");d.appendChild(h),this.renderedAlignable.forEach(function(d){var e=document.createElement("tr");e.id="wavesurfer-alignable-"+d.id,h.appendChild(e);var f=document.createElement("td");f.className="wavesurfer-time",f.textContent=d.start.toFixed(1)+"–"+d.end.toFixed(1),e.appendChild(f);var g=b[d.id];c.forEach(function(b){var c=a[b],f=document.createElement("td"),h=g[b];h&&(f.id="wavesurfer-annotation-"+h.id,f.dataset.ref=d.id,f.dataset.start=d.start,f.dataset.end=d.end,f.textContent=h.value),f.className="wavesurfer-tier-"+c.id,e.appendChild(f)},this)},this),this.container.innerHTML="",this.container.appendChild(d)},bindClick:function(){var a=this;this.container.addEventListener("click",function(b){var c=b.target.dataset.ref;if(null!=c){var d=a.data.annotations[c];d&&a.fireEvent("select",d.start,d.end)}})},getRenderedAnnotation:function(a){var b;return this.renderedAlignable.some(function(c){return c.start<=a&&c.end>=a&&(b=c,!0)}),b},getAnnotationNode:function(a){return document.getElementById("wavesurfer-alignable-"+a.id)}},a.util.extend(a.ELAN,a.Observer)});