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