nicholas@2: /** nicholas@2: * ape.js nicholas@2: * Create the APE interface nicholas@2: */ nicholas@2: n@662: // preTest - In preTest state n@662: // testRun-ID - In test running, test Id number at the end 'testRun-2' n@662: // testRunPost-ID - Post test of test ID n@662: // testRunPre-ID - Pre-test of test ID n@662: // postTest - End of test, final submission! n@662: n@656: nicholas@2: // Once this is loaded and parsed, begin execution nicholas@2: loadInterface(projectXML); nicholas@2: nicholas@2: function loadInterface(xmlDoc) { nicholas@2: n@701: // 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@706: var testContent = document.createElement('div'); nicholas@934: n@706: 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@964: // Create pre and post test questions nicholas@964: nicholas@964: var preTest = xmlSetup.find('PreTest'); nicholas@964: var postTest = xmlSetup.find('PostTest'); nicholas@964: preTest = preTest[0]; nicholas@964: postTest = postTest[0]; nicholas@964: nicholas@964: if (preTest == undefined) {preTest = document.createElement("preTest");} nicholas@964: if (postTest == undefined){postTest= document.createElement("postTest");} nicholas@964: nicholas@964: testState.stateMap.push(preTest); nicholas@964: n@658: // Extract the different test XML DOM trees n@674: var audioHolders = xmlDoc.find('audioHolder'); nicholas@964: var testXMLSetups = []; n@674: audioHolders.each(function(index,element) { n@674: var repeatN = element.attributes['repeatCount'].value; n@674: for (var r=0; r<=repeatN; r++) { nicholas@964: testXMLSetups.push(element); n@674: } n@674: }); n@668: n@674: // New check if we need to randomise the test order n@674: var randomise = xmlSetup[0].attributes['randomiseOrder']; n@674: if (randomise != undefined) { nicholas@946: if (randomise.value === 'true'){ nicholas@946: randomise = true; nicholas@946: } else { nicholas@946: randomise = false; nicholas@946: } n@674: } else { n@674: randomise = false; n@674: } nicholas@946: n@674: if (randomise) n@674: { n@678: testXMLSetups = randomiseOrder(testXMLSetups); n@674: } nicholas@964: nicholas@964: $(testXMLSetups).each(function(index,elem){ nicholas@964: testState.stateMap.push(elem); nicholas@964: }) nicholas@964: nicholas@964: testState.stateMap.push(postTest); n@668: n@674: // Obtain the metrics enabled n@674: var metricNode = xmlSetup.find('Metric'); n@674: var metricNode = metricNode.find('metricEnable'); n@674: metricNode.each(function(index,node){ n@674: var enabled = node.textContent; n@674: switch(enabled) n@674: { n@674: case 'testTimer': n@674: sessionMetrics.prototype.enableTestTimer = true; n@674: break; n@674: case 'elementTimer': n@674: sessionMetrics.prototype.enableElementTimer = true; n@674: break; n@674: case 'elementTracker': n@674: sessionMetrics.prototype.enableElementTracker = true; n@674: break; n@674: case 'elementInitalPosition': n@674: sessionMetrics.prototype.enableElementInitialPosition = true; n@674: break; n@674: case 'elementFlagListenedTo': n@674: sessionMetrics.prototype.enableFlagListenedTo = true; n@674: break; n@674: case 'elementFlagMoved': n@674: sessionMetrics.prototype.enableFlagMoved = true; n@674: break; n@674: case 'elementFlagComments': n@674: sessionMetrics.prototype.enableFlagComments = true; n@674: break; n@674: } n@674: }); n@658: n@676: // Create APE specific metric functions n@676: audioEngineContext.metric.initialiseTest = function() n@676: { n@676: }; n@676: n@676: audioEngineContext.metric.sliderMoveStart = function(id) n@676: { n@676: if (this.data == -1) n@676: { n@676: this.data = id; n@676: } else { n@676: console.log('ERROR: Metric tracker detecting two moves!'); n@676: this.data = -1; n@676: } n@676: }; n@676: audioEngineContext.metric.sliderMoved = function() n@676: { n@676: var time = audioEngineContext.timer.getTestTime(); n@676: var id = this.data; n@676: this.data = -1; n@676: var position = convSliderPosToRate(id); BrechtDeMan@936: console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id n@676: if (audioEngineContext.timer.testStarted) n@676: { n@676: audioEngineContext.audioObjects[id].metric.moved(time,position); n@676: } n@676: }; n@676: n@676: audioEngineContext.metric.sliderPlayed = function(id) n@676: { n@676: var time = audioEngineContext.timer.getTestTime(); n@676: if (audioEngineContext.timer.testStarted) n@676: { n@676: if (this.lastClicked >= 0) n@676: { n@676: audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); n@676: } n@676: this.lastClicked = id; n@676: audioEngineContext.audioObjects[id].metric.listening(time); n@676: } BrechtDeMan@936: console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id n@676: }; n@676: 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@709: titleSpan.innerHTML = titleAttr.value; nicholas@2: } else { BrechtDeMan@975: 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@671: var pagetitle = document.createElement('div'); n@671: pagetitle.className = "pageTitle"; n@671: pagetitle.align = "center"; n@671: var titleSpan = document.createElement('span'); n@671: titleSpan.id = "pageTitle"; n@671: pagetitle.appendChild(titleSpan); n@671: nicholas@7: // Store the return URL path in global projectReturn n@933: projectReturn = xmlSetup[0].attributes['projectReturn']; n@933: if (projectReturn == undefined) { n@933: console.log("WARNING - projectReturn not specified! Will assume null."); n@933: projectReturn = "null"; n@933: } else { n@933: projectReturn = projectReturn.value; n@933: } 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"); BrechtDeMan@939: playback.innerHTML = 'Stop'; n@673: playback.id = 'playback-button'; n@701: // onclick function. Check if it is playing or not, call the correct function in the n@701: // audioEngine, change the button text to reflect the next state. nicholas@7: playback.onclick = function() { BrechtDeMan@939: if (audioEngineContext.status == 1) { BrechtDeMan@939: audioEngineContext.stop(); n@709: this.innerHTML = 'Stop'; BrechtDeMan@936: var time = audioEngineContext.timer.getTestTime(); BrechtDeMan@936: console.log('Stopped at ' + time); // DEBUG/SAFETY nicholas@7: } n@701: }; nicholas@7: // Create Submit (save) button nicholas@7: var submit = document.createElement("button"); n@709: submit.innerHTML = 'Submit'; n@667: submit.onclick = buttonSubmitClick; n@673: submit.id = 'submit-button'; n@701: // 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@701: // Create the div box to center align nicholas@2: var sliderBox = document.createElement('div'); nicholas@2: sliderBox.className = 'sliderCanvasDiv'; n@701: sliderBox.id = 'sliderCanvasHolder'; nicholas@2: n@701: // Create the slider box to hold the slider elements nicholas@5: var canvas = document.createElement('div'); nicholas@2: canvas.id = 'slider'; n@963: canvas.align = "left"; n@963: canvas.addEventListener('dragover',function(event){ n@963: event.preventDefault(); n@963: return false; n@963: },false); n@963: var sliderMargin = document.createAttribute('marginsize'); n@963: sliderMargin.nodeValue = 42; // Set default margins to 42px either side n@701: // Must have a known EXACT width, as this is used later to determine the ratings n@963: var w = (Number(sliderMargin.nodeValue)+8)*2; n@963: canvas.style.width = width - w +"px"; n@963: canvas.style.marginLeft = sliderMargin.nodeValue +'px'; n@963: canvas.setAttributeNode(sliderMargin); nicholas@2: sliderBox.appendChild(canvas); n@701: n@671: // Create the div to hold any scale objects n@671: var scale = document.createElement('div'); n@671: scale.className = 'sliderScale'; n@671: scale.id = 'sliderScaleHolder'; n@671: scale.align = 'left'; n@671: sliderBox.appendChild(scale); n@671: n@701: // Global parent for the comment boxes on the page nicholas@3: var feedbackHolder = document.createElement('div'); n@658: feedbackHolder.id = 'feedbackHolder'; n@656: n@662: testContent.style.zIndex = 1; n@662: insertPoint.innerHTML = null; // Clear the current schema n@662: n@662: currentState = 'preTest'; n@656: n@662: // Inject into HTML n@662: testContent.appendChild(title); // Insert the title n@671: testContent.appendChild(pagetitle); n@662: testContent.appendChild(interfaceButtons); n@662: testContent.appendChild(sliderBox); n@662: testContent.appendChild(feedbackHolder); n@662: insertPoint.appendChild(testContent); n@656: n@660: // Load the full interface nicholas@964: testState.initialise(); nicholas@964: testState.advanceState(); n@663: n@656: } n@656: nicholas@964: function loadTest(textXML) n@658: { nicholas@947: nicholas@947: // Reset audioEngineContext.Metric globals for new test n@950: audioEngineContext.newTestPage(); nicholas@947: nicholas@964: var id = textXML.id; n@658: n@658: var feedbackHolder = document.getElementById('feedbackHolder'); n@658: var canvas = document.getElementById('slider'); n@658: feedbackHolder.innerHTML = null; n@658: canvas.innerHTML = null; n@671: n@671: // Setup question title n@671: var interfaceObj = $(textXML).find('interface'); n@671: var titleNode = interfaceObj.find('title'); n@671: if (titleNode[0] != undefined) n@671: { n@671: document.getElementById('pageTitle').textContent = titleNode[0].textContent; n@671: } n@671: var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2); n@963: var offset = Number(document.getElementById('slider').attributes['marginsize'].value); n@671: var scale = document.getElementById('sliderScaleHolder'); n@671: scale.innerHTML = null; n@671: interfaceObj.find('scale').each(function(index,scaleObj){ n@963: var value = document.createAttribute('value'); n@671: var position = Number(scaleObj.attributes['position'].value)*0.01; n@963: value.nodeValue = position; n@671: var pixelPosition = (position*positionScale)+offset; n@671: var scaleDOM = document.createElement('span'); n@671: scaleDOM.textContent = scaleObj.textContent; n@671: scale.appendChild(scaleDOM); n@671: scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px'; n@963: scaleDOM.setAttributeNode(value); n@671: }); n@658: n@701: // Extract the hostURL attribute. If not set, create an empty string. n@658: var hostURL = textXML.attributes['hostURL']; nicholas@7: if (hostURL == undefined) { nicholas@7: hostURL = ""; nicholas@7: } else { nicholas@7: hostURL = hostURL.value; nicholas@7: } n@701: // Extract the sampleRate. If set, convert the string to a Number. n@658: var hostFs = textXML.attributes['sampleRate']; n@700: if (hostFs != undefined) { n@700: hostFs = Number(hostFs.value); nicholas@7: } nicholas@7: nicholas@7: /// CHECK FOR SAMPLE RATE COMPATIBILITY n@700: if (hostFs != undefined) { nicholas@7: if (Number(hostFs) != audioContext.sampleRate) { nicholas@7: var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; nicholas@7: alert(errStr); nicholas@7: return; nicholas@7: } nicholas@7: } n@669: nicholas@689: var commentShow = textXML.attributes['elementComments']; nicholas@689: if (commentShow != undefined) { nicholas@689: if (commentShow.value == 'false') {commentShow = false;} nicholas@689: else {commentShow = true;} nicholas@689: } else {commentShow = true;} nicholas@689: n@681: var loopPlayback = textXML.attributes['loop']; n@681: if (loopPlayback != undefined) n@681: { n@681: loopPlayback = loopPlayback.value; n@681: if (loopPlayback == 'true') { n@681: loopPlayback = true; n@681: } else { n@681: loopPlayback = false; n@681: } n@681: } else { n@681: loopPlayback = false; n@681: } n@681: audioEngineContext.loopPlayback = loopPlayback; n@681: // Create AudioEngine bindings for playback n@681: if (loopPlayback) { n@681: audioEngineContext.selectedTrack = function(id) { n@681: for (var i=0; i'; nicholas@5: trackSliderObj.draggable = true; nicholas@5: trackSliderObj.ondragend = dragEnd; n@673: trackSliderObj.ondragstart = function() n@673: { n@673: var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! n@673: audioEngineContext.metric.sliderMoveStart(id); n@673: }; nicholas@8: nicholas@8: // Onclick, switch playback to that track nicholas@8: trackSliderObj.onclick = function() { nicholas@945: // Start the test on first click, that way timings are more accurate. nicholas@945: audioEngineContext.play(); nicholas@934: if (audioEngineContext.audioObjectsReady) { nicholas@934: // Cannot continue to issue play command until audioObjects reported as ready! nicholas@934: // Get the track ID from the object ID nicholas@934: var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! nicholas@934: //audioEngineContext.metric.sliderPlayed(id); nicholas@934: audioEngineContext.selectedTrack(id); nicholas@934: // Currently playing track red, rest green nicholas@934: document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000"; nicholas@934: for (var i = 0; i<$(currentTrackOrder).length; i++) nicholas@934: { nicholas@934: if (i!=index) // Make all other sliders green nicholas@934: { nicholas@934: document.getElementById('track-slider-'+i).style.backgroundColor = "rgb(100,200,100)"; nicholas@934: } nicholas@934: nicholas@934: } nicholas@934: } n@700: }; nicholas@8: nicholas@5: canvas.appendChild(trackSliderObj); n@1000: audioEngineContext.audioObjects[index].metric.initialised(convSliderPosToRate(index)); BrechtDeMan@940: n@700: }); nicholas@3: nicholas@688: // Append any commentQuestion boxes nicholas@688: var commentQuestions = $(textXML).find('CommentQuestion'); nicholas@688: $(commentQuestions).each(function(index,element) { nicholas@688: // Create document objects to hold the comment boxes nicholas@688: var trackComment = document.createElement('div'); nicholas@688: trackComment.className = 'comment-div commentQuestion'; nicholas@688: trackComment.id = element.attributes['id'].value; nicholas@688: // Create a string next to each comment asking for a comment nicholas@688: var trackString = document.createElement('span'); nicholas@688: trackString.innerHTML = element.textContent; nicholas@688: // Create the HTML5 comment box 'textarea' nicholas@688: var trackCommentBox = document.createElement('textarea'); nicholas@688: trackCommentBox.rows = '4'; nicholas@688: trackCommentBox.cols = '100'; nicholas@688: trackCommentBox.name = 'commentQuestion'+index; nicholas@688: trackCommentBox.className = 'trackComment'; nicholas@688: var br = document.createElement('br'); nicholas@688: // Add to the holder. nicholas@688: trackComment.appendChild(trackString); nicholas@688: trackComment.appendChild(br); nicholas@688: trackComment.appendChild(trackCommentBox); nicholas@688: feedbackHolder.appendChild(trackComment); nicholas@688: }); n@1007: n@1007: n@1007: testWaitIndicator(); n@663: } n@663: nicholas@5: nicholas@5: function dragEnd(ev) { nicholas@5: // Function call when a div has been dropped n@657: var slider = document.getElementById('slider'); n@963: var marginSize = Number(slider.attributes['marginsize'].value); n@675: var w = slider.style.width; n@675: w = Number(w.substr(0,w.length-2)); n@675: var x = ev.x; n@963: if (x >= marginSize && x < w+marginSize) { n@675: this.style.left = (x)+'px'; nicholas@5: } else { n@963: if (x 1) { nicholas@944: for (var i=0; i