nickjillings@1575: /** nickjillings@1575: * ape.js nickjillings@1575: * Create the APE interface nickjillings@1575: */ nickjillings@1575: nickjillings@1575: // preTest - In preTest state nickjillings@1575: // testRun-ID - In test running, test Id number at the end 'testRun-2' nickjillings@1575: // testRunPost-ID - Post test of test ID nickjillings@1575: // testRunPre-ID - Pre-test of test ID nickjillings@1575: // postTest - End of test, final submission! nickjillings@1575: nickjillings@1575: nickjillings@1575: // Once this is loaded and parsed, begin execution nickjillings@1581: loadInterface(); nickjillings@1575: nickjillings@1581: function loadInterface() { nickjillings@1575: nickjillings@1575: // Get the dimensions of the screen available to the page nickjillings@1575: var width = window.innerWidth; nickjillings@1575: var height = window.innerHeight; nickjillings@1575: nickjillings@1575: // The injection point into the HTML page nickjillings@1582: interfaceContext.insertPoint = document.getElementById("topLevelBody"); nickjillings@1575: var testContent = document.createElement('div'); nickjillings@1575: nickjillings@1575: testContent.id = 'testContent'; nickjillings@1576: nickjillings@1575: nickjillings@1575: // Create APE specific metric functions nickjillings@1575: audioEngineContext.metric.initialiseTest = function() nickjillings@1575: { nickjillings@1575: }; nickjillings@1575: nickjillings@1575: audioEngineContext.metric.sliderMoveStart = function(id) nickjillings@1575: { nickjillings@1575: if (this.data == -1) nickjillings@1575: { nickjillings@1575: this.data = id; nickjillings@1575: } else { nickjillings@1575: console.log('ERROR: Metric tracker detecting two moves!'); nickjillings@1575: this.data = -1; nickjillings@1575: } nickjillings@1575: }; nickjillings@1575: audioEngineContext.metric.sliderMoved = function() nickjillings@1575: { nickjillings@1575: var time = audioEngineContext.timer.getTestTime(); nickjillings@1575: var id = this.data; nickjillings@1575: this.data = -1; nickjillings@1575: var position = convSliderPosToRate(id); nickjillings@1575: console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id nickjillings@1575: if (audioEngineContext.timer.testStarted) nickjillings@1575: { nickjillings@1575: audioEngineContext.audioObjects[id].metric.moved(time,position); nickjillings@1575: } nickjillings@1575: }; nickjillings@1575: nickjillings@1575: audioEngineContext.metric.sliderPlayed = function(id) nickjillings@1575: { nickjillings@1575: var time = audioEngineContext.timer.getTestTime(); nickjillings@1575: if (audioEngineContext.timer.testStarted) nickjillings@1575: { nickjillings@1575: if (this.lastClicked >= 0) nickjillings@1575: { nickjillings@1575: audioEngineContext.audioObjects[this.lastClicked].metric.listening(time); nickjillings@1575: } nickjillings@1575: this.lastClicked = id; nickjillings@1575: audioEngineContext.audioObjects[id].metric.listening(time); nickjillings@1575: } nickjillings@1575: console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id nickjillings@1575: }; nickjillings@1575: nickjillings@1577: // Bindings for audioObjects nickjillings@1577: nickjillings@1575: // Create the top div for the Title element nickjillings@1581: var titleAttr = specification.title; nickjillings@1575: var title = document.createElement('div'); nickjillings@1575: title.className = "title"; nickjillings@1575: title.align = "center"; nickjillings@1575: var titleSpan = document.createElement('span'); nickjillings@1575: nickjillings@1575: // Set title to that defined in XML, else set to default nickjillings@1575: if (titleAttr != undefined) { nickjillings@1581: titleSpan.textContent = titleAttr; nickjillings@1575: } else { nickjillings@1581: titleSpan.textContent = 'Listening test'; nickjillings@1575: } nickjillings@1575: // Insert the titleSpan element into the title div element. nickjillings@1575: title.appendChild(titleSpan); nickjillings@1575: nickjillings@1575: var pagetitle = document.createElement('div'); nickjillings@1575: pagetitle.className = "pageTitle"; nickjillings@1575: pagetitle.align = "center"; nickjillings@1575: var titleSpan = document.createElement('span'); nickjillings@1575: titleSpan.id = "pageTitle"; nickjillings@1575: pagetitle.appendChild(titleSpan); nickjillings@1575: nickjillings@1575: // Create Interface buttons! nickjillings@1575: var interfaceButtons = document.createElement('div'); nickjillings@1575: interfaceButtons.id = 'interface-buttons'; nickjillings@1575: nickjillings@1575: // Create playback start/stop points nickjillings@1575: var playback = document.createElement("button"); nickjillings@1575: playback.innerHTML = 'Stop'; nickjillings@1575: playback.id = 'playback-button'; nickjillings@1575: // onclick function. Check if it is playing or not, call the correct function in the nickjillings@1575: // audioEngine, change the button text to reflect the next state. nickjillings@1575: playback.onclick = function() { nickjillings@1575: if (audioEngineContext.status == 1) { nickjillings@1575: audioEngineContext.stop(); nickjillings@1575: this.innerHTML = 'Stop'; nickjillings@1575: var time = audioEngineContext.timer.getTestTime(); nickjillings@1575: console.log('Stopped at ' + time); // DEBUG/SAFETY nickjillings@1575: } nickjillings@1575: }; nickjillings@1575: // Create Submit (save) button nickjillings@1575: var submit = document.createElement("button"); nickjillings@1575: submit.innerHTML = 'Submit'; nickjillings@1575: submit.onclick = buttonSubmitClick; nickjillings@1575: submit.id = 'submit-button'; nickjillings@1575: // Append the interface buttons into the interfaceButtons object. nickjillings@1575: interfaceButtons.appendChild(playback); nickjillings@1575: interfaceButtons.appendChild(submit); nickjillings@1575: nickjillings@1575: // Now create the slider and HTML5 canvas boxes nickjillings@1575: nickjillings@1575: // Create the div box to center align nickjillings@1575: var sliderBox = document.createElement('div'); nickjillings@1575: sliderBox.className = 'sliderCanvasDiv'; nickjillings@1575: sliderBox.id = 'sliderCanvasHolder'; nickjillings@1575: nickjillings@1575: // Create the slider box to hold the slider elements nickjillings@1575: var canvas = document.createElement('div'); nickjillings@1575: canvas.id = 'slider'; nickjillings@1575: canvas.align = "left"; nickjillings@1575: canvas.addEventListener('dragover',function(event){ nickjillings@1575: event.preventDefault(); nickjillings@1575: return false; nickjillings@1575: },false); nickjillings@1575: var sliderMargin = document.createAttribute('marginsize'); nickjillings@1575: sliderMargin.nodeValue = 42; // Set default margins to 42px either side nickjillings@1575: // Must have a known EXACT width, as this is used later to determine the ratings nickjillings@1575: var w = (Number(sliderMargin.nodeValue)+8)*2; nickjillings@1575: canvas.style.width = width - w +"px"; nickjillings@1575: canvas.style.marginLeft = sliderMargin.nodeValue +'px'; nickjillings@1575: canvas.setAttributeNode(sliderMargin); nickjillings@1575: sliderBox.appendChild(canvas); nickjillings@1575: nickjillings@1575: // Create the div to hold any scale objects nickjillings@1575: var scale = document.createElement('div'); nickjillings@1575: scale.className = 'sliderScale'; nickjillings@1575: scale.id = 'sliderScaleHolder'; nickjillings@1575: scale.align = 'left'; nickjillings@1575: sliderBox.appendChild(scale); nickjillings@1575: nickjillings@1575: // Global parent for the comment boxes on the page nickjillings@1575: var feedbackHolder = document.createElement('div'); nickjillings@1575: feedbackHolder.id = 'feedbackHolder'; nickjillings@1575: nickjillings@1575: testContent.style.zIndex = 1; nickjillings@1582: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema nickjillings@1575: nickjillings@1575: // Inject into HTML nickjillings@1575: testContent.appendChild(title); // Insert the title nickjillings@1575: testContent.appendChild(pagetitle); nickjillings@1575: testContent.appendChild(interfaceButtons); nickjillings@1575: testContent.appendChild(sliderBox); nickjillings@1575: testContent.appendChild(feedbackHolder); nickjillings@1582: interfaceContext.insertPoint.appendChild(testContent); nickjillings@1575: nickjillings@1575: // Load the full interface nickjillings@1575: testState.initialise(); nickjillings@1575: testState.advanceState(); nickjillings@1575: nickjillings@1575: } nickjillings@1575: nickjillings@1581: function loadTest(audioHolderObject) nickjillings@1575: { nickjillings@1575: nickjillings@1575: // Reset audioEngineContext.Metric globals for new test nickjillings@1575: audioEngineContext.newTestPage(); nickjillings@1575: nickjillings@1581: var id = audioHolderObject.id; nickjillings@1575: nickjillings@1575: var feedbackHolder = document.getElementById('feedbackHolder'); nickjillings@1575: var canvas = document.getElementById('slider'); nickjillings@1575: feedbackHolder.innerHTML = null; nickjillings@1575: canvas.innerHTML = null; nickjillings@1575: nickjillings@1575: // Setup question title nickjillings@1581: var interfaceObj = $(audioHolderObject).find('interface'); nickjillings@1575: var titleNode = interfaceObj.find('title'); nickjillings@1575: if (titleNode[0] != undefined) nickjillings@1575: { nickjillings@1575: document.getElementById('pageTitle').textContent = titleNode[0].textContent; nickjillings@1575: } nickjillings@1575: var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2); nickjillings@1575: var offset = Number(document.getElementById('slider').attributes['marginsize'].value); nickjillings@1575: var scale = document.getElementById('sliderScaleHolder'); nickjillings@1575: scale.innerHTML = null; nickjillings@1575: interfaceObj.find('scale').each(function(index,scaleObj){ nickjillings@1575: var value = document.createAttribute('value'); nickjillings@1575: var position = Number(scaleObj.attributes['position'].value)*0.01; nickjillings@1575: value.nodeValue = position; nickjillings@1575: var pixelPosition = (position*positionScale)+offset; nickjillings@1575: var scaleDOM = document.createElement('span'); nickjillings@1575: scaleDOM.textContent = scaleObj.textContent; nickjillings@1575: scale.appendChild(scaleDOM); nickjillings@1575: scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px'; nickjillings@1575: scaleDOM.setAttributeNode(value); nickjillings@1575: }); nickjillings@1575: nickjillings@1575: var commentBoxPrefix = interfaceObj.find('commentBoxPrefix'); nickjillings@1575: if (commentBoxPrefix.length != 0) { nickjillings@1575: commentBoxPrefix = commentBoxPrefix[0].textContent; nickjillings@1575: } else { nickjillings@1575: commentBoxPrefix = "Comment on track"; nickjillings@1575: } nickjillings@1575: nickjillings@1575: /// CHECK FOR SAMPLE RATE COMPATIBILITY nickjillings@1581: if (audioHolderObject.sampleRate != undefined) { nickjillings@1581: if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) { nickjillings@1575: 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@1575: alert(errStr); nickjillings@1575: return; nickjillings@1575: } nickjillings@1575: } nickjillings@1575: nickjillings@1581: var commentShow = audioHolderObject.elementComments; nickjillings@1575: nickjillings@1581: var loopPlayback = audioHolderObject.loop; nickjillings@1581: nickjillings@1575: audioEngineContext.loopPlayback = loopPlayback; nickjillings@1575: // Create AudioEngine bindings for playback nickjillings@1575: if (loopPlayback) { nickjillings@1575: audioEngineContext.selectedTrack = function(id) { nickjillings@1575: for (var i=0; i'; nickjillings@1575: trackSliderObj.draggable = true; nickjillings@1575: trackSliderObj.ondragend = dragEnd; nickjillings@1575: trackSliderObj.ondragstart = function() nickjillings@1575: { nickjillings@1575: var id = Number(event.srcElement.attributes['trackIndex'].value); nickjillings@1575: audioEngineContext.metric.sliderMoveStart(id); nickjillings@1575: }; nickjillings@1575: nickjillings@1575: // Onclick, switch playback to that track nickjillings@1575: trackSliderObj.onclick = function() { nickjillings@1575: // Start the test on first click, that way timings are more accurate. nickjillings@1575: audioEngineContext.play(); nickjillings@1575: if (audioEngineContext.audioObjectsReady) { nickjillings@1575: // Cannot continue to issue play command until audioObjects reported as ready! nickjillings@1575: // Get the track ID from the object ID nickjillings@1575: var id = Number(event.srcElement.attributes['trackIndex'].value); nickjillings@1575: //audioEngineContext.metric.sliderPlayed(id); nickjillings@1575: audioEngineContext.selectedTrack(id); nickjillings@1575: // Currently playing track red, rest green nickjillings@1575: nickjillings@1575: //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000"; nickjillings@1575: $('.track-slider').removeClass('track-slider-playing'); nickjillings@1575: $(event.srcElement).addClass('track-slider-playing'); nickjillings@1575: $('.comment-div').removeClass('comment-box-playing'); nickjillings@1575: $('#comment-div-'+id).addClass('comment-box-playing'); nickjillings@1575: } nickjillings@1575: }; nickjillings@1575: nickjillings@1577: // Attach binding nickjillings@1579: audioObject.sliderDOM = trackSliderObj; nickjillings@1577: nickjillings@1575: canvas.appendChild(trackSliderObj); nickjillings@1579: audioObject.metric.initialised(convSliderPosToRate(index)); nickjillings@1575: nickjillings@1575: }); nickjillings@1582: if (commentShow) { nickjillings@1582: interfaceContext.showCommentBoxes(feedbackHolder,true); nickjillings@1582: } nickjillings@1575: nickjillings@1575: // Append any commentQuestion boxes nickjillings@1581: $(audioHolderObject.commentQuestions).each(function(index,element) { nickjillings@1575: // Create document objects to hold the comment boxes nickjillings@1575: var trackComment = document.createElement('div'); nickjillings@1575: trackComment.className = 'comment-div commentQuestion'; nickjillings@1581: trackComment.id = element.id; nickjillings@1575: // Create a string next to each comment asking for a comment nickjillings@1575: var trackString = document.createElement('span'); nickjillings@1581: trackString.innerHTML = element.question; nickjillings@1575: // Create the HTML5 comment box 'textarea' nickjillings@1575: var trackCommentBox = document.createElement('textarea'); nickjillings@1575: trackCommentBox.rows = '4'; nickjillings@1575: trackCommentBox.cols = '100'; nickjillings@1575: trackCommentBox.name = 'commentQuestion'+index; nickjillings@1575: trackCommentBox.className = 'trackComment'; nickjillings@1575: var br = document.createElement('br'); nickjillings@1575: // Add to the holder. nickjillings@1575: trackComment.appendChild(trackString); nickjillings@1575: trackComment.appendChild(br); nickjillings@1575: trackComment.appendChild(trackCommentBox); nickjillings@1575: feedbackHolder.appendChild(trackComment); nickjillings@1575: }); nickjillings@1575: nickjillings@1575: nickjillings@1575: testWaitIndicator(); nickjillings@1575: } nickjillings@1575: nickjillings@1575: nickjillings@1575: function dragEnd(ev) { nickjillings@1575: // Function call when a div has been dropped nickjillings@1575: var slider = document.getElementById('slider'); nickjillings@1575: var marginSize = Number(slider.attributes['marginsize'].value); nickjillings@1575: var w = slider.style.width; nickjillings@1575: w = Number(w.substr(0,w.length-2)); nickjillings@1575: var x = ev.x; nickjillings@1575: if (x >= marginSize && x < w+marginSize) { nickjillings@1575: this.style.left = (x)+'px'; nickjillings@1575: } else { nickjillings@1575: if (x 1) { nickjillings@1575: for (var i=0; i