nicholas@2: /** nicholas@2: * ape.js nicholas@2: * Create the APE interface nicholas@2: */ nicholas@2: n@656: nicholas@2: // Once this is loaded and parsed, begin execution n@911: loadInterface(); nicholas@2: n@911: function loadInterface() { nicholas@2: n@701: // Get the dimensions of the screen available to the page nicholas@2: var width = window.innerWidth; nicholas@2: var height = window.innerHeight; nicholas@2: nicholas@2: // The injection point into the HTML page n@912: interfaceContext.insertPoint = document.getElementById("topLevelBody"); n@706: var testContent = document.createElement('div'); nicholas@934: n@706: testContent.id = 'testContent'; n@676: nicholas@887: // Bindings for interfaceContext n@805: interfaceContext.checkAllPlayed = function() nicholas@887: { nicholas@887: hasBeenPlayed = audioEngineContext.checkAllPlayed(); nicholas@887: if (hasBeenPlayed.length > 0) // if a fragment has not been played yet nicholas@887: { nicholas@887: str = ""; nicholas@887: if (hasBeenPlayed.length > 1) { nicholas@887: for (var i=0; i 1) { nicholas@887: var str = ""; nicholas@887: for (var i=0; i maxRanking) n@788: { n@788: maxRanking = ranking; n@776: } n@776: } n@788: if (minRanking > minScale || maxRanking < maxScale) n@788: { n@788: state = false; n@788: str += 'On axis "'+this.interfaceSliders[i].interfaceObject.title+'" you have not used the full width of the scale. '; nicholas@1039: } nicholas@1039: } n@788: if (state != true) n@788: { n@788: alert(str); n@788: console.log(str); nicholas@1039: } n@788: return state; nicholas@1039: }; nicholas@1039: n@832: Interface.prototype.objectSelected = null; n@832: Interface.prototype.objectMoved = false; n@832: Interface.prototype.selectObject = function(object) n@832: { n@832: if (this.objectSelected == null) n@832: { n@832: this.objectSelected = object; n@832: this.objectMoved = false; n@832: } n@832: }; n@832: Interface.prototype.moveObject = function() n@832: { n@832: if (this.objectMoved == false) n@832: { n@832: this.objectMoved = true; n@832: } n@832: }; n@832: Interface.prototype.releaseObject = function() n@832: { n@832: this.objectSelected = null; n@832: this.objectMoved = false; n@832: }; n@832: Interface.prototype.getSelectedObject = function() n@832: { n@832: return this.objectSelected; n@832: }; n@832: Interface.prototype.hasSelectedObjectMoved = function() n@832: { n@832: return this.objectMoved; n@832: }; n@832: nicholas@763: // Bindings for slider interfaces nicholas@763: Interface.prototype.interfaceSliders = []; nicholas@763: n@907: // Bindings for audioObjects n@907: nicholas@2: // Create the top div for the Title element n@911: var titleAttr = specification.title; nicholas@2: var title = document.createElement('div'); nicholas@2: title.className = "title"; nicholas@2: title.align = "center"; nicholas@2: var titleSpan = document.createElement('span'); nicholas@2: nicholas@2: // Set title to that defined in XML, else set to default nicholas@2: if (titleAttr != undefined) { n@911: titleSpan.textContent = titleAttr; nicholas@2: } else { n@911: titleSpan.textContent = 'Listening test'; nicholas@2: } nicholas@2: // Insert the titleSpan element into the title div element. nicholas@2: title.appendChild(titleSpan); nicholas@2: nicholas@7: // Create Interface buttons! nicholas@7: var interfaceButtons = document.createElement('div'); nicholas@7: interfaceButtons.id = 'interface-buttons'; nicholas@7: nicholas@7: // Create playback start/stop points nicholas@7: var playback = document.createElement("button"); BrechtDeMan@939: playback.innerHTML = 'Stop'; n@673: playback.id = 'playback-button'; n@701: // onclick function. Check if it is playing or not, call the correct function in the n@701: // audioEngine, change the button text to reflect the next state. nicholas@7: playback.onclick = function() { BrechtDeMan@939: if (audioEngineContext.status == 1) { BrechtDeMan@939: audioEngineContext.stop(); n@709: this.innerHTML = 'Stop'; BrechtDeMan@936: var time = audioEngineContext.timer.getTestTime(); BrechtDeMan@936: console.log('Stopped at ' + time); // DEBUG/SAFETY nicholas@7: } n@701: }; nicholas@7: // Create Submit (save) button nicholas@7: var submit = document.createElement("button"); n@709: submit.innerHTML = 'Submit'; n@667: submit.onclick = buttonSubmitClick; n@673: submit.id = 'submit-button'; n@701: // Append the interface buttons into the interfaceButtons object. nicholas@7: interfaceButtons.appendChild(playback); nicholas@7: interfaceButtons.appendChild(submit); nicholas@7: n@776: var sliderHolder = document.createElement("div"); n@776: sliderHolder.id = "slider-holder"; nicholas@2: n@671: n@701: // Global parent for the comment boxes on the page nicholas@3: var feedbackHolder = document.createElement('div'); n@658: feedbackHolder.id = 'feedbackHolder'; n@656: n@662: testContent.style.zIndex = 1; n@912: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema n@656: n@662: // Inject into HTML n@662: testContent.appendChild(title); // Insert the title n@662: testContent.appendChild(interfaceButtons); n@776: testContent.appendChild(sliderHolder); n@662: testContent.appendChild(feedbackHolder); n@912: interfaceContext.insertPoint.appendChild(testContent); n@656: n@660: // Load the full interface nicholas@964: testState.initialise(); nicholas@964: testState.advanceState(); n@663: n@656: } n@656: n@911: function loadTest(audioHolderObject) n@658: { n@776: var width = window.innerWidth; n@776: var height = window.innerHeight; n@911: var id = audioHolderObject.id; n@658: nicholas@763: interfaceContext.interfaceSliders = []; nicholas@763: n@658: var feedbackHolder = document.getElementById('feedbackHolder'); n@776: var sliderHolder = document.getElementById('slider-holder'); n@658: feedbackHolder.innerHTML = null; n@776: sliderHolder.innerHTML = null; n@767: n@749: // Delete outside reference n@749: var outsideReferenceHolder = document.getElementById('outside-reference'); n@749: if (outsideReferenceHolder != null) { n@749: document.getElementById('interface-buttons').removeChild(outsideReferenceHolder); n@749: } n@671: nicholas@1055: var interfaceObj = audioHolderObject.interfaces; nicholas@1055: for (var k=0; k'; nicholas@867: var inject = document.getElementById('interface-buttons'); nicholas@867: inject.appendChild(pagecountHolder); nicholas@1055: } nicholas@1055: } nicholas@1055: } nicholas@1055: BrechtDeMan@810: var commentBoxPrefix = "Comment on fragment"; n@669: n@911: var commentShow = audioHolderObject.elementComments; nicholas@689: n@911: var loopPlayback = audioHolderObject.loop; n@911: n@670: currentTestHolder = document.createElement('audioHolder'); n@911: currentTestHolder.id = audioHolderObject.id; n@911: currentTestHolder.repeatCount = audioHolderObject.repeatCount; n@670: n@701: // Find all the audioElements from the audioHolder n@911: $(audioHolderObject.audioElements).each(function(index,element){ nicholas@7: // Find URL of track n@701: // In this jQuery loop, variable 'this' holds the current audioElement. n@701: n@804: // Check if an outside reference n@804: if (index == audioHolderObject.outsideReference) n@804: { n@804: return; n@804: } n@804: n@701: // Now load each audio sample. First create the new track by passing the full URL n@911: var trackURL = audioHolderObject.hostURL + element.url; n@912: var audioObject = audioEngineContext.newTrack(element); nicholas@689: n@1029: var node = interfaceContext.createCommentBox(audioObject); n@701: nicholas@5: // Create a slider per track n@776: audioObject.interfaceDOM = new sliderObject(audioObject,interfaceObj); n@785: audioObject.metric.initialPosition = convSliderPosToRate(audioObject.interfaceDOM.trackSliderObjects[0]); n@773: if (audioObject.state == 1) n@773: { n@773: audioObject.interfaceDOM.enable(); n@773: } BrechtDeMan@940: n@700: }); n@831: $('.track-slider').mousedown(function(event) { n@832: interfaceContext.selectObject($(this)[0]); n@830: }); n@812: $('.track-slider').on('touchstart',null,function(event) { n@812: interfaceContext.selectObject($(this)[0]); n@812: }); n@830: n@831: $('.track-slider').mousemove(function(event) { n@831: event.preventDefault(); n@831: }); n@831: n@776: $('.slider').mousemove(function(event) { n@830: event.preventDefault(); n@832: var obj = interfaceContext.getSelectedObject(); n@832: if (obj == null) {return;} n@832: $(obj).css("left",event.clientX + "px"); n@832: interfaceContext.moveObject(); n@832: }); n@812: n@812: $('.slider').on('touchmove',null,function(event) { n@812: event.preventDefault(); n@812: var obj = interfaceContext.getSelectedObject(); n@812: if (obj == null) {return;} n@812: var move = event.originalEvent.targetTouches[0].clientX - 6; n@812: $(obj).css("left",move + "px"); n@812: interfaceContext.moveObject(); n@812: }); n@832: n@832: $(document).mouseup(function(event){ n@832: event.preventDefault(); n@832: var obj = interfaceContext.getSelectedObject(); n@832: if (obj == null) {return;} nicholas@764: var interfaceID = obj.parentElement.getAttribute("interfaceid"); nicholas@764: var trackID = obj.getAttribute("trackindex"); n@832: if (interfaceContext.hasSelectedObjectMoved() == true) n@830: { n@832: var l = $(obj).css("left"); n@832: var id = obj.getAttribute('trackIndex'); n@832: var time = audioEngineContext.timer.getTestTime(); n@832: var rate = convSliderPosToRate(obj); n@832: audioEngineContext.audioObjects[id].metric.moved(time,rate); nicholas@764: interfaceContext.interfaceSliders[interfaceID].metrics[trackID].moved(time,rate); n@832: console.log("slider "+id+" moved to "+rate+' ('+time+')'); n@832: } else { n@832: var id = Number(obj.attributes['trackIndex'].value); n@832: //audioEngineContext.metric.sliderPlayed(id); n@832: audioEngineContext.play(id); n@832: // Currently playing track red, rest green n@832: n@832: $('.track-slider').removeClass('track-slider-playing'); n@776: var name = ".track-slider-"+obj.getAttribute("trackindex"); n@776: $(name).addClass('track-slider-playing'); n@832: $('.comment-div').removeClass('comment-box-playing'); n@832: $('#comment-div-'+id).addClass('comment-box-playing'); n@832: var outsideReference = document.getElementById('outside-reference'); n@832: if (outsideReference != undefined) n@832: $(outsideReference).removeClass('track-slider-playing'); n@830: } n@832: interfaceContext.releaseObject(); n@830: }); n@830: n@812: $('.slider').on('touchend',null,function(event){ n@812: var obj = interfaceContext.getSelectedObject(); n@812: if (obj == null) {return;} n@812: var interfaceID = obj.parentElement.getAttribute("interfaceid"); n@812: var trackID = obj.getAttribute("trackindex"); n@812: if (interfaceContext.hasSelectedObjectMoved() == true) n@812: { n@812: var l = $(obj).css("left"); n@812: var id = obj.getAttribute('trackIndex'); n@812: var time = audioEngineContext.timer.getTestTime(); n@812: var rate = convSliderPosToRate(obj); n@812: audioEngineContext.audioObjects[id].metric.moved(time,rate); n@812: interfaceContext.interfaceSliders[interfaceID].metrics[trackID].moved(time,rate); n@812: console.log("slider "+id+" moved to "+rate+' ('+time+')'); n@812: } n@812: interfaceContext.releaseObject(); n@812: }); n@812: n@830: n@912: if (commentShow) { n@912: interfaceContext.showCommentBoxes(feedbackHolder,true); n@912: } nicholas@3: n@911: $(audioHolderObject.commentQuestions).each(function(index,element) { n@1026: var node = interfaceContext.createCommentQuestion(element); n@1026: feedbackHolder.appendChild(node.holder); nicholas@688: }); n@1007: nicholas@1044: // Construct outside reference; nicholas@1044: if (audioHolderObject.outsideReference != null) { nicholas@1044: var outsideReferenceHolder = document.createElement('div'); nicholas@1044: outsideReferenceHolder.id = 'outside-reference'; n@783: outsideReferenceHolder.className = 'outside-reference'; nicholas@1044: outsideReferenceHolderspan = document.createElement('span'); nicholas@1044: outsideReferenceHolderspan.textContent = 'Reference'; nicholas@1044: outsideReferenceHolder.appendChild(outsideReferenceHolderspan); nicholas@1044: n@804: var audioObject = audioEngineContext.newTrack(audioHolderObject.audioElements[audioHolderObject.outsideReference]); nicholas@1044: n@852: outsideReferenceHolder.onclick = function(event) nicholas@1044: { nicholas@1044: audioEngineContext.play(audioEngineContext.audioObjects.length-1); nicholas@1044: $('.track-slider').removeClass('track-slider-playing'); nicholas@1044: $('.comment-div').removeClass('comment-box-playing'); n@852: if (event.currentTarget.nodeName == 'DIV') { n@852: $(event.currentTarget).addClass('track-slider-playing'); nicholas@1044: } else { n@852: $(event.currentTarget.parentElement).addClass('track-slider-playing'); nicholas@1044: } nicholas@1044: }; nicholas@1044: nicholas@1044: document.getElementById('interface-buttons').appendChild(outsideReferenceHolder); nicholas@1044: } nicholas@1044: n@1007: nicholas@864: //testWaitIndicator(); n@663: } n@663: nicholas@763: function interfaceSliderHolder(interfaceObject) nicholas@763: { nicholas@763: this.sliders = []; nicholas@764: this.metrics = []; nicholas@763: this.id = document.getElementsByClassName("sliderCanvasDiv").length; nicholas@763: this.name = interfaceObject.name; nicholas@763: this.interfaceObject = interfaceObject; nicholas@763: this.sliderDOM = document.createElement('div'); nicholas@763: this.sliderDOM.className = 'sliderCanvasDiv'; nicholas@763: this.sliderDOM.id = 'sliderCanvasHolder-'+this.id; nicholas@763: nicholas@763: var pagetitle = document.createElement('div'); nicholas@763: pagetitle.className = "pageTitle"; nicholas@763: pagetitle.align = "center"; nicholas@763: var titleSpan = document.createElement('span'); nicholas@763: titleSpan.id = "pageTitle-"+this.id; nicholas@763: if (interfaceObject.title != undefined && typeof interfaceObject.title == "string") nicholas@763: { nicholas@763: titleSpan.textContent = interfaceObject.title; nicholas@755: } else { nicholas@755: titleSpan.textContent = "Axis "+String(this.id+1); nicholas@763: } nicholas@763: pagetitle.appendChild(titleSpan); nicholas@763: this.sliderDOM.appendChild(pagetitle); nicholas@763: nicholas@763: // Create the slider box to hold the slider elements nicholas@763: this.canvas = document.createElement('div'); nicholas@763: if (this.name != undefined) nicholas@763: this.canvas.id = 'slider-'+this.name; nicholas@763: else nicholas@763: this.canvas.id = 'slider-'+this.id; nicholas@764: this.canvas.setAttribute("interfaceid",this.id); nicholas@763: this.canvas.className = 'slider'; nicholas@763: this.canvas.align = "left"; nicholas@763: this.canvas.addEventListener('dragover',function(event){ nicholas@763: event.preventDefault(); nicholas@763: event.dataTransfer.effectAllowed = 'none'; nicholas@763: event.dataTransfer.dropEffect = 'copy'; nicholas@763: return false; nicholas@763: },false); nicholas@763: var sliderMargin = document.createAttribute('marginsize'); nicholas@763: sliderMargin.nodeValue = 42; // Set default margins to 42px either side nicholas@763: // Must have a known EXACT width, as this is used later to determine the ratings nicholas@763: var w = (Number(sliderMargin.nodeValue)+8)*2; nicholas@763: this.canvas.style.width = window.innerWidth - w +"px"; nicholas@763: this.canvas.style.marginLeft = sliderMargin.nodeValue +'px'; nicholas@763: this.canvas.setAttributeNode(sliderMargin); nicholas@763: this.sliderDOM.appendChild(this.canvas); nicholas@763: nicholas@763: // Create the div to hold any scale objects nicholas@763: this.scale = document.createElement('div'); nicholas@763: this.scale.className = 'sliderScale'; nicholas@763: this.scale.id = 'sliderScaleHolder-'+this.id; nicholas@763: this.scale.align = 'left'; nicholas@763: this.sliderDOM.appendChild(this.scale); nicholas@763: var positionScale = this.canvas.style.width.substr(0,this.canvas.style.width.length-2); nicholas@763: var offset = Number(this.canvas.attributes['marginsize'].value); nicholas@763: for (var index=0; index'; nicholas@763: if (this.name != undefined) { nicholas@763: trackObj.setAttribute('interface-name',this.name); n@776: } else { nicholas@763: trackObj.setAttribute('interface-name',this.id); n@776: } nicholas@763: var offset = Number(this.canvas.attributes['marginsize'].value); n@776: // Distribute it randomnly n@776: var w = window.innerWidth - (offset+8)*2; n@776: w = Math.random()*w; n@776: w = Math.floor(w+(offset+8)); n@776: trackObj.style.left = w+'px'; nicholas@763: this.canvas.appendChild(trackObj); nicholas@763: this.sliders.push(trackObj); nicholas@764: this.metrics.push(new metricTracker(this)); nicholas@764: this.metrics[this.metrics.length-1].initialPosition = convSliderPosToRate(trackObj); nicholas@763: return trackObj; nicholas@763: }; nicholas@763: nicholas@763: this.resize = function(event) nicholas@763: { nicholas@763: var holdValues = []; nicholas@763: for (var index = 0; index < this.sliders.length; index++) nicholas@763: { nicholas@763: holdValues.push(convSliderPosToRate(this.sliders[index])); nicholas@763: } nicholas@763: var width = event.target.innerWidth; nicholas@763: var sliderDiv = this.canvas; nicholas@763: var sliderScaleDiv = this.scale; nicholas@763: var marginsize = Number(sliderDiv.attributes['marginsize'].value); nicholas@763: var w = (marginsize+8)*2; nicholas@763: sliderDiv.style.width = width - w + 'px'; nicholas@763: var width = width - w; nicholas@763: // Move sliders into new position nicholas@763: for (var index = 0; index < this.sliders.length; index++) nicholas@763: { nicholas@763: var pos = holdValues[index]; nicholas@763: var pix = pos * width; nicholas@763: this.sliders[index].style.left = pix+marginsize+'px'; nicholas@763: } nicholas@763: nicholas@763: // Move scale labels nicholas@763: for (var index = 0; index < this.scale.children.length; index++) nicholas@763: { nicholas@763: var scaleObj = this.scale.children[index]; nicholas@763: var position = Number(scaleObj.attributes['value'].value); nicholas@763: var pixelPosition = (position*width)+marginsize; nicholas@763: scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px'; nicholas@763: } n@783: }; nicholas@763: } nicholas@763: nicholas@763: function sliderObject(audioObject,interfaceObjects) { n@913: // Create a new slider object; n@913: this.parent = audioObject; nicholas@763: this.trackSliderObjects = []; nicholas@763: for (var i=0; i saves nicholas@764: // Get the current information in store (remember to appendChild your data to it) n@785: if (interfaceContext.interfaceSliders.length == 1) n@674: { n@785: // If there is only one axis, there only needs to be one metric return n@785: return; n@674: } nicholas@764: var audioelements = store.getElementsByTagName("audioelement"); nicholas@764: for (var i=0; i