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