nicholas@815: /** nicholas@815: * ape.js nicholas@815: * Create the APE interface nicholas@815: */ nicholas@815: nicholas@815: nicholas@815: // Once this is loaded and parsed, begin execution nicholas@815: loadInterface(); nicholas@815: nicholas@815: function loadInterface() { nicholas@815: nicholas@815: // Get the dimensions of the screen available to the page nicholas@815: var width = window.innerWidth; nicholas@815: var height = window.innerHeight; nicholas@815: nicholas@815: // The injection point into the HTML page nicholas@815: interfaceContext.insertPoint = document.getElementById("topLevelBody"); nicholas@815: var testContent = document.createElement('div'); nicholas@815: nicholas@815: testContent.id = 'testContent'; nicholas@815: nicholas@815: nicholas@815: // Create APE specific metric functions nicholas@815: audioEngineContext.metric.initialiseTest = function() nicholas@815: { nicholas@815: }; nicholas@815: nicholas@815: audioEngineContext.metric.sliderMoved = function() nicholas@815: { nicholas@815: var id = this.data; nicholas@815: this.data = -1; nicholas@815: var position = convSliderPosToRate(id); nicholas@815: console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id nicholas@815: if (audioEngineContext.timer.testStarted) nicholas@815: { nicholas@815: audioEngineContext.audioObjects[id].metric.moved(time,position); nicholas@815: } nicholas@815: }; nicholas@815: nicholas@815: audioEngineContext.metric.sliderPlayed = function(id) nicholas@815: { nicholas@815: var time = audioEngineContext.timer.getTestTime(); nicholas@815: if (audioEngineContext.timer.testStarted) nicholas@815: { nicholas@815: if (this.lastClicked >= 0) nicholas@815: { nicholas@815: audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); nicholas@815: } nicholas@815: this.lastClicked = id; nicholas@815: audioEngineContext.audioObjects[id].metric.listening(time); nicholas@815: } nicholas@815: console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id nicholas@815: }; nicholas@815: nicholas@815: // Bindings for interfaceContext nicholas@815: Interface.prototype.checkAllPlayed = function() nicholas@815: { nicholas@815: hasBeenPlayed = audioEngineContext.checkAllPlayed(); nicholas@815: if (hasBeenPlayed.length > 0) // if a fragment has not been played yet nicholas@815: { nicholas@815: str = ""; nicholas@815: if (hasBeenPlayed.length > 1) { nicholas@815: for (var i=0; i 1) { nicholas@815: var str = ""; nicholas@815: for (var i=0; i 1) { nicholas@815: var str = ""; nicholas@815: for (var i=0; i maxRanking) { maxRanking = ranking;} nicholas@815: } nicholas@815: } nicholas@815: if (minRanking > minScale || maxRanking < maxScale) { nicholas@815: alert('Please use the full width of the scale'); nicholas@815: return false; nicholas@815: } else { nicholas@815: return true; nicholas@815: } nicholas@815: }; nicholas@815: nicholas@815: // Bindings for audioObjects nicholas@815: nicholas@815: // Create the top div for the Title element nicholas@815: var titleAttr = specification.title; nicholas@815: var title = document.createElement('div'); nicholas@815: title.className = "title"; nicholas@815: title.align = "center"; nicholas@815: var titleSpan = document.createElement('span'); nicholas@815: nicholas@815: // Set title to that defined in XML, else set to default nicholas@815: if (titleAttr != undefined) { nicholas@815: titleSpan.textContent = titleAttr; nicholas@815: } else { nicholas@815: titleSpan.textContent = 'Listening test'; nicholas@815: } nicholas@815: // Insert the titleSpan element into the title div element. nicholas@815: title.appendChild(titleSpan); nicholas@815: nicholas@815: var pagetitle = document.createElement('div'); nicholas@815: pagetitle.className = "pageTitle"; nicholas@815: pagetitle.align = "center"; nicholas@815: var titleSpan = document.createElement('span'); nicholas@815: titleSpan.id = "pageTitle"; nicholas@815: pagetitle.appendChild(titleSpan); nicholas@815: nicholas@815: // Create Interface buttons! nicholas@815: var interfaceButtons = document.createElement('div'); nicholas@815: interfaceButtons.id = 'interface-buttons'; nicholas@815: nicholas@815: // Create playback start/stop points nicholas@815: var playback = document.createElement("button"); nicholas@815: playback.innerHTML = 'Stop'; nicholas@815: playback.id = 'playback-button'; nicholas@815: // onclick function. Check if it is playing or not, call the correct function in the nicholas@815: // audioEngine, change the button text to reflect the next state. nicholas@815: playback.onclick = function() { nicholas@815: if (audioEngineContext.status == 1) { nicholas@815: audioEngineContext.stop(); nicholas@815: this.innerHTML = 'Stop'; nicholas@815: var time = audioEngineContext.timer.getTestTime(); nicholas@815: console.log('Stopped at ' + time); // DEBUG/SAFETY nicholas@815: } nicholas@815: }; nicholas@815: // Create Submit (save) button nicholas@815: var submit = document.createElement("button"); nicholas@815: submit.innerHTML = 'Submit'; nicholas@815: submit.onclick = buttonSubmitClick; nicholas@815: submit.id = 'submit-button'; nicholas@815: // Append the interface buttons into the interfaceButtons object. nicholas@815: interfaceButtons.appendChild(playback); nicholas@815: interfaceButtons.appendChild(submit); nicholas@815: nicholas@815: // Now create the slider and HTML5 canvas boxes nicholas@815: nicholas@815: // Create the div box to center align nicholas@815: var sliderBox = document.createElement('div'); nicholas@815: sliderBox.className = 'sliderCanvasDiv'; nicholas@815: sliderBox.id = 'sliderCanvasHolder'; nicholas@815: nicholas@815: // Create the slider box to hold the slider elements nicholas@815: var canvas = document.createElement('div'); nicholas@815: canvas.id = 'slider'; nicholas@815: canvas.align = "left"; nicholas@815: canvas.addEventListener('dragover',function(event){ nicholas@815: event.preventDefault(); nicholas@815: event.dataTransfer.effectAllowed = 'none'; nicholas@815: event.dataTransfer.dropEffect = 'copy'; nicholas@815: return false; nicholas@815: },false); nicholas@815: var sliderMargin = document.createAttribute('marginsize'); nicholas@815: sliderMargin.nodeValue = 42; // Set default margins to 42px either side nicholas@815: // Must have a known EXACT width, as this is used later to determine the ratings nicholas@815: var w = (Number(sliderMargin.nodeValue)+8)*2; nicholas@815: canvas.style.width = width - w +"px"; nicholas@815: canvas.style.marginLeft = sliderMargin.nodeValue +'px'; nicholas@815: canvas.setAttributeNode(sliderMargin); nicholas@815: sliderBox.appendChild(canvas); nicholas@815: nicholas@815: // Create the div to hold any scale objects nicholas@815: var scale = document.createElement('div'); nicholas@815: scale.className = 'sliderScale'; nicholas@815: scale.id = 'sliderScaleHolder'; nicholas@815: scale.align = 'left'; nicholas@815: sliderBox.appendChild(scale); nicholas@815: nicholas@815: // Global parent for the comment boxes on the page nicholas@815: var feedbackHolder = document.createElement('div'); nicholas@815: feedbackHolder.id = 'feedbackHolder'; nicholas@815: nicholas@815: testContent.style.zIndex = 1; nicholas@815: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema nicholas@815: nicholas@815: // Inject into HTML nicholas@815: testContent.appendChild(title); // Insert the title nicholas@815: testContent.appendChild(pagetitle); nicholas@815: testContent.appendChild(interfaceButtons); nicholas@815: testContent.appendChild(sliderBox); nicholas@815: testContent.appendChild(feedbackHolder); nicholas@815: interfaceContext.insertPoint.appendChild(testContent); nicholas@815: nicholas@815: // Load the full interface nicholas@815: testState.initialise(); nicholas@815: testState.advanceState(); nicholas@815: nicholas@815: } nicholas@815: nicholas@815: function loadTest(audioHolderObject) nicholas@815: { nicholas@815: nicholas@815: // Reset audioEngineContext.Metric globals for new test nicholas@815: audioEngineContext.newTestPage(); nicholas@815: nicholas@815: var id = audioHolderObject.id; nicholas@815: nicholas@815: var feedbackHolder = document.getElementById('feedbackHolder'); nicholas@815: var canvas = document.getElementById('slider'); nicholas@815: feedbackHolder.innerHTML = null; nicholas@815: canvas.innerHTML = null; nicholas@815: nicholas@815: var interfaceObj = audioHolderObject.interfaces; nicholas@815: for (var k=0; k'; nicholas@815: var inject = document.getElementById('interface-buttons'); nicholas@815: inject.appendChild(pagecountHolder); nicholas@815: } nicholas@815: } nicholas@815: } nicholas@815: // Setup question title nicholas@815: nicholas@815: var commentBoxPrefix = "Comment on track"; nicholas@815: if (interfaceObj.length != 0) { nicholas@815: interfaceObj = interfaceObj[0]; nicholas@815: var titleNode = interfaceObj.title; nicholas@815: if (titleNode != undefined) nicholas@815: { nicholas@815: document.getElementById('pageTitle').textContent = titleNode; nicholas@815: } nicholas@815: var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2); nicholas@815: var offset = Number(document.getElementById('slider').attributes['marginsize'].value); nicholas@815: var scale = document.getElementById('sliderScaleHolder'); nicholas@815: scale.innerHTML = null; nicholas@815: $(interfaceObj.scale).each(function(index,scaleObj){ nicholas@815: var value = document.createAttribute('value'); nicholas@815: var position = Number(scaleObj[0])*0.01; nicholas@815: value.nodeValue = position; nicholas@815: var pixelPosition = (position*positionScale)+offset; nicholas@815: var scaleDOM = document.createElement('span'); nicholas@815: scaleDOM.textContent = scaleObj[1]; nicholas@815: scale.appendChild(scaleDOM); nicholas@815: scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px'; nicholas@815: scaleDOM.setAttributeNode(value); nicholas@815: }); nicholas@815: nicholas@815: if (interfaceObj.commentBoxPrefix != undefined) { nicholas@815: commentBoxPrefix = interfaceObj.commentBoxPrefix; nicholas@815: } nicholas@815: } nicholas@815: nicholas@815: /// CHECK FOR SAMPLE RATE COMPATIBILITY nicholas@815: if (audioHolderObject.sampleRate != undefined) { nicholas@815: if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) { nicholas@815: var errStr = 'Sample rates do not match! Requested '+Number(audioHolderObject.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; nicholas@815: alert(errStr); nicholas@815: return; nicholas@815: } nicholas@815: } nicholas@815: nicholas@815: var commentShow = audioHolderObject.elementComments; nicholas@815: nicholas@815: var loopPlayback = audioHolderObject.loop; nicholas@815: nicholas@815: audioEngineContext.loopPlayback = loopPlayback; nicholas@815: // Create AudioEngine bindings for playback nicholas@815: audioEngineContext.selectedTrack = function(id) { nicholas@815: console.log('Deprecated'); nicholas@815: }; nicholas@815: nicholas@815: currentTestHolder = document.createElement('audioHolder'); nicholas@815: currentTestHolder.id = audioHolderObject.id; nicholas@815: currentTestHolder.repeatCount = audioHolderObject.repeatCount; nicholas@815: nicholas@815: // Delete any previous audioObjects associated with the audioEngine nicholas@815: audioEngineContext.audioObjects = []; nicholas@815: interfaceContext.deleteCommentBoxes(); nicholas@815: interfaceContext.deleteCommentQuestions(); nicholas@815: nicholas@815: // Find all the audioElements from the audioHolder nicholas@815: $(audioHolderObject.audioElements).each(function(index,element){ nicholas@815: // Find URL of track nicholas@815: // In this jQuery loop, variable 'this' holds the current audioElement. nicholas@815: nicholas@815: // Now load each audio sample. First create the new track by passing the full URL nicholas@815: var trackURL = audioHolderObject.hostURL + element.url; nicholas@815: var audioObject = audioEngineContext.newTrack(element); nicholas@815: nicholas@815: var node = interfaceContext.createCommentBox(audioObject); nicholas@815: nicholas@815: // Create a slider per track nicholas@815: audioObject.interfaceDOM = new sliderObject(audioObject); nicholas@815: nicholas@815: // Distribute it randomnly nicholas@815: var w = window.innerWidth - (offset+8)*2; nicholas@815: w = Math.random()*w; nicholas@815: w = Math.floor(w+(offset+8)); nicholas@815: audioObject.interfaceDOM.trackSliderObj.style.left = w+'px'; nicholas@815: nicholas@815: canvas.appendChild(audioObject.interfaceDOM.trackSliderObj); nicholas@815: audioObject.metric.initialised(convSliderPosToRate(audioObject.interfaceDOM.trackSliderObj)); nicholas@815: nicholas@815: }); nicholas@815: if (commentShow) { nicholas@815: interfaceContext.showCommentBoxes(feedbackHolder,true); nicholas@815: } nicholas@815: nicholas@815: $(audioHolderObject.commentQuestions).each(function(index,element) { nicholas@815: var node = interfaceContext.createCommentQuestion(element); nicholas@815: feedbackHolder.appendChild(node.holder); nicholas@815: }); nicholas@815: nicholas@815: // Construct outside reference; nicholas@815: if (audioHolderObject.outsideReference != null) { nicholas@815: var outsideReferenceHolder = document.createElement('div'); nicholas@815: outsideReferenceHolder.id = 'outside-reference'; nicholas@815: outsideReferenceHolderspan = document.createElement('span'); nicholas@815: outsideReferenceHolderspan.textContent = 'Reference'; nicholas@815: outsideReferenceHolder.appendChild(outsideReferenceHolderspan); nicholas@815: nicholas@815: var audioObject = audioEngineContext.newTrack(audioHolderObject.outsideReference); nicholas@815: nicholas@815: outsideReferenceHolder.onclick = function(event) nicholas@815: { nicholas@815: audioEngineContext.play(audioEngineContext.audioObjects.length-1); nicholas@815: $('.track-slider').removeClass('track-slider-playing'); nicholas@815: $('.comment-div').removeClass('comment-box-playing'); nicholas@815: if (event.currentTarget.nodeName == 'DIV') { nicholas@815: $(event.currentTarget).addClass('track-slider-playing'); nicholas@815: } else { nicholas@815: $(event.currentTarget.parentElement).addClass('track-slider-playing'); nicholas@815: } nicholas@815: }; nicholas@815: nicholas@815: document.getElementById('interface-buttons').appendChild(outsideReferenceHolder); nicholas@815: } nicholas@815: nicholas@815: nicholas@815: //testWaitIndicator(); nicholas@815: } nicholas@815: nicholas@815: function sliderObject(audioObject) { nicholas@815: // Create a new slider object; nicholas@815: this.parent = audioObject; nicholas@815: this.trackSliderObj = document.createElement('div'); nicholas@815: this.trackSliderObj.className = 'track-slider track-slider-disabled'; nicholas@815: this.trackSliderObj.id = 'track-slider-'+audioObject.id; nicholas@815: nicholas@815: this.trackSliderObj.setAttribute('trackIndex',audioObject.id); nicholas@815: this.trackSliderObj.innerHTML = ''+audioObject.id+''; nicholas@815: this.trackSliderObj.draggable = true; nicholas@815: this.trackSliderObj.ondragend = dragEnd; nicholas@815: nicholas@815: this.trackSliderObj.ondragstart = function(event){ nicholas@815: event.dataTransfer.setData('Text',null); nicholas@815: }; nicholas@815: nicholas@815: this.trackSliderObj.ondrop = function(event) nicholas@815: { nicholas@815: if(event.stopPropagation) {event.stopPropagation();} nicholas@815: return false; nicholas@815: }; nicholas@815: nicholas@815: // Onclick, switch playback to that track nicholas@815: this.trackSliderObj.onclick = function(event) { nicholas@815: // Cannot continue to issue play command until audioObjects reported as ready! nicholas@815: // Get the track ID from the object ID nicholas@815: var element; nicholas@815: if (event.currentTarget.nodeName == "SPAN") { nicholas@815: element = event.currentTarget.parentNode; nicholas@815: } else { nicholas@815: element = event.currentTarget; nicholas@815: } nicholas@815: var id = Number(element.attributes['trackIndex'].value); nicholas@815: //audioEngineContext.metric.sliderPlayed(id); nicholas@815: audioEngineContext.play(id); nicholas@815: // Currently playing track red, rest green nicholas@815: nicholas@815: //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000"; nicholas@815: $('.track-slider').removeClass('track-slider-playing'); nicholas@815: $(element).addClass('track-slider-playing'); nicholas@815: $('.comment-div').removeClass('comment-box-playing'); nicholas@815: $('#comment-div-'+id).addClass('comment-box-playing'); nicholas@815: var outsideReference = document.getElementById('outside-reference'); nicholas@815: if (outsideReference != undefined) nicholas@815: $(outsideReference).removeClass('track-slider-playing'); nicholas@815: }; nicholas@815: nicholas@815: this.enable = function() { nicholas@815: if (this.parent.state == 1) nicholas@815: { nicholas@815: $(this.trackSliderObj).removeClass('track-slider-disabled'); nicholas@815: } nicholas@815: }; nicholas@815: nicholas@815: this.exportXMLDOM = function(audioObject) { nicholas@815: // Called by the audioObject holding this element. Must be present nicholas@815: var node = document.createElement('value'); nicholas@815: node.textContent = convSliderPosToRate(this.trackSliderObj); nicholas@815: return node; nicholas@815: }; nicholas@815: this.getValue = function() { nicholas@815: return convSliderPosToRate(this.trackSliderObj); nicholas@815: }; nicholas@815: } nicholas@815: nicholas@815: function dragEnd(ev) { nicholas@815: // Function call when a div has been dropped nicholas@815: var slider = document.getElementById('slider'); nicholas@815: var marginSize = Number(slider.attributes['marginsize'].value); nicholas@815: var w = slider.style.width; nicholas@815: w = Number(w.substr(0,w.length-2)); nicholas@815: var x = ev.clientX; nicholas@815: nicholas@815: //x += Math.abs(window.screenX); nicholas@815: x = x % window.outerWidth; nicholas@815: nicholas@815: if (x >= marginSize && x < w+marginSize) { nicholas@815: this.style.left = (x)+'px'; nicholas@815: } else { nicholas@815: if (x