nicholas@2: /** nicholas@2: * ape.js nicholas@2: * Create the APE interface nicholas@2: */ nicholas@2: n@38: // preTest - In preTest state n@38: // testRun-ID - In test running, test Id number at the end 'testRun-2' n@38: // testRunPost-ID - Post test of test ID n@38: // testRunPre-ID - Pre-test of test ID n@38: // postTest - End of test, final submission! n@38: n@32: nicholas@2: // Once this is loaded and parsed, begin execution nicholas@2: loadInterface(projectXML); nicholas@2: nicholas@2: function loadInterface(xmlDoc) { nicholas@2: n@16: // 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 nicholas@2: var insertPoint = document.getElementById("topLevelBody"); n@22: var testContent = document.createElement('div'); nicholas@135: n@22: testContent.id = 'testContent'; nicholas@2: nicholas@7: nicholas@2: // Decode parts of the xmlDoc that are needed nicholas@2: // xmlDoc MUST already be parsed by jQuery! nicholas@2: var xmlSetup = xmlDoc.find('setup'); nicholas@2: // Should put in an error function here incase of malprocessed or malformed XML nicholas@2: nicholas@129: // Create pre and post test questions nicholas@129: nicholas@129: var preTest = xmlSetup.find('PreTest'); nicholas@129: var postTest = xmlSetup.find('PostTest'); nicholas@129: preTest = preTest[0]; nicholas@129: postTest = postTest[0]; nicholas@129: nicholas@129: if (preTest == undefined) {preTest = document.createElement("preTest");} nicholas@129: if (postTest == undefined){postTest= document.createElement("postTest");} nicholas@129: nicholas@129: testState.stateMap.push(preTest); nicholas@129: n@34: // Extract the different test XML DOM trees n@50: var audioHolders = xmlDoc.find('audioHolder'); nicholas@129: var testXMLSetups = []; n@50: audioHolders.each(function(index,element) { n@50: var repeatN = element.attributes['repeatCount'].value; n@50: for (var r=0; r<=repeatN; r++) { nicholas@129: testXMLSetups.push(element); n@50: } n@50: }); n@44: n@50: // New check if we need to randomise the test order n@50: var randomise = xmlSetup[0].attributes['randomiseOrder']; n@50: if (randomise != undefined) { nicholas@109: if (randomise.value === 'true'){ nicholas@109: randomise = true; nicholas@109: } else { nicholas@109: randomise = false; nicholas@109: } n@50: } else { n@50: randomise = false; n@50: } nicholas@109: n@50: if (randomise) n@50: { n@54: testXMLSetups = randomiseOrder(testXMLSetups); n@50: } nicholas@129: nicholas@129: $(testXMLSetups).each(function(index,elem){ nicholas@129: testState.stateMap.push(elem); nicholas@129: }) nicholas@129: nicholas@129: testState.stateMap.push(postTest); n@44: n@50: // Obtain the metrics enabled n@50: var metricNode = xmlSetup.find('Metric'); n@50: var metricNode = metricNode.find('metricEnable'); n@50: metricNode.each(function(index,node){ n@50: var enabled = node.textContent; n@50: switch(enabled) n@50: { n@50: case 'testTimer': n@50: sessionMetrics.prototype.enableTestTimer = true; n@50: break; n@50: case 'elementTimer': n@50: sessionMetrics.prototype.enableElementTimer = true; n@50: break; n@50: case 'elementTracker': n@50: sessionMetrics.prototype.enableElementTracker = true; n@50: break; n@164: case 'elementListenTracker': n@164: sessionMetrics.prototype.enableElementListenTracker = true; n@164: break; n@170: case 'elementInitialPosition': n@50: sessionMetrics.prototype.enableElementInitialPosition = true; n@50: break; n@50: case 'elementFlagListenedTo': n@50: sessionMetrics.prototype.enableFlagListenedTo = true; n@50: break; n@50: case 'elementFlagMoved': n@50: sessionMetrics.prototype.enableFlagMoved = true; n@50: break; n@50: case 'elementFlagComments': n@50: sessionMetrics.prototype.enableFlagComments = true; n@50: break; n@50: } n@50: }); n@34: n@52: // Create APE specific metric functions n@52: audioEngineContext.metric.initialiseTest = function() n@52: { n@52: }; n@52: n@52: audioEngineContext.metric.sliderMoveStart = function(id) n@52: { n@52: if (this.data == -1) n@52: { n@52: this.data = id; n@52: } else { n@52: console.log('ERROR: Metric tracker detecting two moves!'); n@52: this.data = -1; n@52: } n@52: }; n@52: audioEngineContext.metric.sliderMoved = function() n@52: { n@52: var time = audioEngineContext.timer.getTestTime(); n@52: var id = this.data; n@52: this.data = -1; n@52: var position = convSliderPosToRate(id); b@115: console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id n@52: if (audioEngineContext.timer.testStarted) n@52: { n@52: audioEngineContext.audioObjects[id].metric.moved(time,position); n@52: } n@52: }; n@52: n@52: audioEngineContext.metric.sliderPlayed = function(id) n@52: { n@52: var time = audioEngineContext.timer.getTestTime(); n@52: if (audioEngineContext.timer.testStarted) n@52: { n@52: if (this.lastClicked >= 0) n@52: { n@52: audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); n@52: } n@52: this.lastClicked = id; n@52: audioEngineContext.audioObjects[id].metric.listening(time); n@52: } b@115: console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id n@52: }; n@52: nicholas@2: // Create the top div for the Title element nicholas@2: var titleAttr = xmlSetup[0].attributes['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@25: titleSpan.innerHTML = titleAttr.value; nicholas@2: } else { b@75: titleSpan.innerHTML = 'Listening test'; nicholas@2: } nicholas@2: // Insert the titleSpan element into the title div element. nicholas@2: title.appendChild(titleSpan); nicholas@2: n@47: var pagetitle = document.createElement('div'); n@47: pagetitle.className = "pageTitle"; n@47: pagetitle.align = "center"; n@47: var titleSpan = document.createElement('span'); n@47: titleSpan.id = "pageTitle"; n@47: pagetitle.appendChild(titleSpan); n@47: nicholas@7: // Store the return URL path in global projectReturn n@140: projectReturn = xmlSetup[0].attributes['projectReturn']; n@140: if (projectReturn == undefined) { n@140: console.log("WARNING - projectReturn not specified! Will assume null."); n@140: projectReturn = "null"; n@140: } else { n@140: projectReturn = projectReturn.value; n@140: } nicholas@7: nicholas@7: // Create Interface buttons! nicholas@7: var interfaceButtons = document.createElement('div'); nicholas@7: interfaceButtons.id = 'interface-buttons'; nicholas@7: nicholas@7: // MANUAL DOWNLOAD POINT nicholas@7: // If project return is null, this MUST be specified as the location to create the download link nicholas@7: var downloadPoint = document.createElement('div'); nicholas@7: downloadPoint.id = 'download-point'; nicholas@7: nicholas@7: // Create playback start/stop points nicholas@7: var playback = document.createElement("button"); b@101: playback.innerHTML = 'Stop'; n@49: playback.id = 'playback-button'; n@16: // onclick function. Check if it is playing or not, call the correct function in the n@16: // audioEngine, change the button text to reflect the next state. nicholas@7: playback.onclick = function() { b@101: if (audioEngineContext.status == 1) { b@101: audioEngineContext.stop(); n@25: this.innerHTML = 'Stop'; b@115: var time = audioEngineContext.timer.getTestTime(); b@115: console.log('Stopped at ' + time); // DEBUG/SAFETY nicholas@7: } n@16: }; nicholas@7: // Create Submit (save) button nicholas@7: var submit = document.createElement("button"); n@25: submit.innerHTML = 'Submit'; n@43: submit.onclick = buttonSubmitClick; n@49: submit.id = 'submit-button'; n@16: // Append the interface buttons into the interfaceButtons object. nicholas@7: interfaceButtons.appendChild(playback); nicholas@7: interfaceButtons.appendChild(submit); nicholas@7: interfaceButtons.appendChild(downloadPoint); nicholas@7: nicholas@2: // Now create the slider and HTML5 canvas boxes nicholas@2: n@16: // Create the div box to center align nicholas@2: var sliderBox = document.createElement('div'); nicholas@2: sliderBox.className = 'sliderCanvasDiv'; n@16: sliderBox.id = 'sliderCanvasHolder'; nicholas@2: n@16: // Create the slider box to hold the slider elements nicholas@5: var canvas = document.createElement('div'); nicholas@2: canvas.id = 'slider'; n@127: canvas.align = "left"; n@127: canvas.addEventListener('dragover',function(event){ n@127: event.preventDefault(); n@127: return false; n@127: },false); n@127: var sliderMargin = document.createAttribute('marginsize'); n@127: sliderMargin.nodeValue = 42; // Set default margins to 42px either side n@16: // Must have a known EXACT width, as this is used later to determine the ratings n@127: var w = (Number(sliderMargin.nodeValue)+8)*2; n@127: canvas.style.width = width - w +"px"; n@127: canvas.style.marginLeft = sliderMargin.nodeValue +'px'; n@127: canvas.setAttributeNode(sliderMargin); nicholas@2: sliderBox.appendChild(canvas); n@16: n@47: // Create the div to hold any scale objects n@47: var scale = document.createElement('div'); n@47: scale.className = 'sliderScale'; n@47: scale.id = 'sliderScaleHolder'; n@47: scale.align = 'left'; n@47: sliderBox.appendChild(scale); n@47: n@16: // Global parent for the comment boxes on the page nicholas@3: var feedbackHolder = document.createElement('div'); n@34: feedbackHolder.id = 'feedbackHolder'; nicholas@2: n@38: testContent.style.zIndex = 1; n@38: insertPoint.innerHTML = null; // Clear the current schema n@38: n@38: currentState = 'preTest'; n@22: n@38: // Inject into HTML n@38: testContent.appendChild(title); // Insert the title n@47: testContent.appendChild(pagetitle); n@38: testContent.appendChild(interfaceButtons); n@38: testContent.appendChild(sliderBox); n@38: testContent.appendChild(feedbackHolder); n@38: insertPoint.appendChild(testContent); n@22: n@36: // Load the full interface nicholas@129: testState.initialise(); nicholas@129: testState.advanceState(); nicholas@135: nicholas@2: } nicholas@5: nicholas@129: function loadTest(textXML) n@34: { nicholas@110: nicholas@110: // Reset audioEngineContext.Metric globals for new test n@113: audioEngineContext.newTestPage(); nicholas@110: nicholas@129: var id = textXML.id; n@34: n@34: var feedbackHolder = document.getElementById('feedbackHolder'); n@34: var canvas = document.getElementById('slider'); n@34: feedbackHolder.innerHTML = null; n@34: canvas.innerHTML = null; n@47: n@47: // Setup question title n@47: var interfaceObj = $(textXML).find('interface'); n@47: var titleNode = interfaceObj.find('title'); n@47: if (titleNode[0] != undefined) n@47: { n@47: document.getElementById('pageTitle').textContent = titleNode[0].textContent; n@47: } n@47: var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2); n@127: var offset = Number(document.getElementById('slider').attributes['marginsize'].value); n@47: var scale = document.getElementById('sliderScaleHolder'); n@47: scale.innerHTML = null; n@47: interfaceObj.find('scale').each(function(index,scaleObj){ n@127: var value = document.createAttribute('value'); n@47: var position = Number(scaleObj.attributes['position'].value)*0.01; n@127: value.nodeValue = position; n@47: var pixelPosition = (position*positionScale)+offset; n@47: var scaleDOM = document.createElement('span'); n@47: scaleDOM.textContent = scaleObj.textContent; n@47: scale.appendChild(scaleDOM); n@47: scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px'; n@127: scaleDOM.setAttributeNode(value); n@47: }); n@174: n@174: var commentBoxPrefix = interfaceObj.find('commentBoxPrefix'); n@174: if (commentBoxPrefix.length != 0) { n@174: commentBoxPrefix = commentBoxPrefix[0].textContent; n@174: } else { n@174: commentBoxPrefix = "Comment on track"; n@174: } n@34: n@34: // Extract the hostURL attribute. If not set, create an empty string. n@34: var hostURL = textXML.attributes['hostURL']; n@34: if (hostURL == undefined) { n@34: hostURL = ""; n@34: } else { n@34: hostURL = hostURL.value; n@34: } n@34: // Extract the sampleRate. If set, convert the string to a Number. n@34: var hostFs = textXML.attributes['sampleRate']; n@34: if (hostFs != undefined) { n@34: hostFs = Number(hostFs.value); n@34: } n@34: n@34: /// CHECK FOR SAMPLE RATE COMPATIBILITY n@34: if (hostFs != undefined) { n@34: if (Number(hostFs) != audioContext.sampleRate) { n@34: var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; n@34: alert(errStr); n@34: return; n@34: } n@34: } n@45: nicholas@66: var commentShow = textXML.attributes['elementComments']; nicholas@66: if (commentShow != undefined) { nicholas@66: if (commentShow.value == 'false') {commentShow = false;} nicholas@66: else {commentShow = true;} nicholas@66: } else {commentShow = true;} nicholas@66: n@57: var loopPlayback = textXML.attributes['loop']; n@57: if (loopPlayback != undefined) n@57: { n@57: loopPlayback = loopPlayback.value; n@57: if (loopPlayback == 'true') { n@57: loopPlayback = true; n@57: } else { n@57: loopPlayback = false; n@57: } n@57: } else { n@57: loopPlayback = false; n@57: } n@57: audioEngineContext.loopPlayback = loopPlayback; n@57: // Create AudioEngine bindings for playback n@57: if (loopPlayback) { n@57: audioEngineContext.selectedTrack = function(id) { n@57: for (var i=0; i'; n@34: trackSliderObj.draggable = true; n@34: trackSliderObj.ondragend = dragEnd; n@49: trackSliderObj.ondragstart = function() n@49: { n@154: var id = Number(event.srcElement.attributes['trackIndex'].value); n@49: audioEngineContext.metric.sliderMoveStart(id); n@49: }; n@34: n@34: // Onclick, switch playback to that track n@34: trackSliderObj.onclick = function() { nicholas@108: // Start the test on first click, that way timings are more accurate. nicholas@108: audioEngineContext.play(); nicholas@135: if (audioEngineContext.audioObjectsReady) { nicholas@135: // Cannot continue to issue play command until audioObjects reported as ready! nicholas@135: // Get the track ID from the object ID n@154: var id = Number(event.srcElement.attributes['trackIndex'].value); nicholas@135: //audioEngineContext.metric.sliderPlayed(id); nicholas@135: audioEngineContext.selectedTrack(id); nicholas@135: // Currently playing track red, rest green n@154: n@154: //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000"; n@154: $('.track-slider').removeClass('track-slider-playing'); n@154: $(event.srcElement).addClass('track-slider-playing'); n@154: $('.comment-div').removeClass('comment-box-playing'); n@154: $('#comment-div-'+id).addClass('comment-box-playing'); nicholas@135: } n@34: }; n@34: n@34: canvas.appendChild(trackSliderObj); n@138: audioEngineContext.audioObjects[index].metric.initialised(convSliderPosToRate(index)); b@102: n@34: }); n@39: nicholas@65: // Append any commentQuestion boxes nicholas@65: var commentQuestions = $(textXML).find('CommentQuestion'); nicholas@65: $(commentQuestions).each(function(index,element) { nicholas@65: // Create document objects to hold the comment boxes nicholas@65: var trackComment = document.createElement('div'); nicholas@65: trackComment.className = 'comment-div commentQuestion'; nicholas@65: trackComment.id = element.attributes['id'].value; nicholas@65: // Create a string next to each comment asking for a comment nicholas@65: var trackString = document.createElement('span'); nicholas@65: trackString.innerHTML = element.textContent; nicholas@65: // Create the HTML5 comment box 'textarea' nicholas@65: var trackCommentBox = document.createElement('textarea'); nicholas@65: trackCommentBox.rows = '4'; nicholas@65: trackCommentBox.cols = '100'; nicholas@65: trackCommentBox.name = 'commentQuestion'+index; nicholas@65: trackCommentBox.className = 'trackComment'; nicholas@65: var br = document.createElement('br'); nicholas@65: // Add to the holder. nicholas@65: trackComment.appendChild(trackString); nicholas@65: trackComment.appendChild(br); nicholas@65: trackComment.appendChild(trackCommentBox); nicholas@65: feedbackHolder.appendChild(trackComment); nicholas@65: }); n@153: n@153: n@153: testWaitIndicator(); n@39: } n@39: nicholas@119: nicholas@5: function dragEnd(ev) { nicholas@5: // Function call when a div has been dropped n@33: var slider = document.getElementById('slider'); n@127: var marginSize = Number(slider.attributes['marginsize'].value); n@51: var w = slider.style.width; n@51: w = Number(w.substr(0,w.length-2)); nicholas@133: var x = ev.x; n@127: if (x >= marginSize && x < w+marginSize) { n@51: this.style.left = (x)+'px'; nicholas@5: } else { n@127: if (x 1) { nicholas@107: for (var i=0; i