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