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