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