nickjillings@1341: /** nickjillings@1341: * mushra.js nickjillings@1341: * Create the MUSHRA interface nickjillings@1341: */ nickjillings@1341: nickjillings@1341: // Once this is loaded and parsed, begin execution nickjillings@1341: loadInterface(); nickjillings@1341: nickjillings@1341: function loadInterface() { nickjillings@1341: // Get the dimensions of the screen available to the page nickjillings@1341: var width = window.innerWidth; nickjillings@1341: var height = window.innerHeight; nickjillings@1341: nickjillings@1341: // The injection point into the HTML page nickjillings@1341: interfaceContext.insertPoint = document.getElementById("topLevelBody"); nickjillings@1341: var testContent = document.createElement('div'); nickjillings@1341: testContent.id = 'testContent'; nickjillings@1341: nickjillings@1341: // Create the top div for the Title element nickjillings@1341: var titleAttr = specification.title; nickjillings@1341: var title = document.createElement('div'); nickjillings@1341: title.className = "title"; nickjillings@1341: title.align = "center"; nickjillings@1341: var titleSpan = document.createElement('span'); nickjillings@1341: nickjillings@1341: // Set title to that defined in XML, else set to default nickjillings@1341: if (titleAttr != undefined) { nickjillings@1341: titleSpan.textContent = titleAttr; nickjillings@1341: } else { nickjillings@1341: titleSpan.textContent = 'Listening test'; nickjillings@1341: } nickjillings@1341: // Insert the titleSpan element into the title div element. nickjillings@1341: title.appendChild(titleSpan); nickjillings@1341: nickjillings@1341: var pagetitle = document.createElement('div'); nickjillings@1341: pagetitle.className = "pageTitle"; nickjillings@1341: pagetitle.align = "center"; nickjillings@1341: var titleSpan = document.createElement('span'); nickjillings@1341: titleSpan.id = "pageTitle"; nickjillings@1341: pagetitle.appendChild(titleSpan); nickjillings@1341: nickjillings@1341: // Create Interface buttons! nickjillings@1341: var interfaceButtons = document.createElement('div'); nickjillings@1341: interfaceButtons.id = 'interface-buttons'; nickjillings@1341: interfaceButtons.style.height = '25px'; nickjillings@1341: nickjillings@1341: // Create playback start/stop points nickjillings@1341: var playback = document.createElement("button"); nickjillings@1341: playback.innerHTML = 'Stop'; nickjillings@1341: playback.id = 'playback-button'; nickjillings@1341: playback.style.float = 'left'; nickjillings@1341: // onclick function. Check if it is playing or not, call the correct function in the nickjillings@1341: // audioEngine, change the button text to reflect the next state. nickjillings@1341: playback.onclick = function() { nickjillings@1341: if (audioEngineContext.status == 1) { nickjillings@1341: audioEngineContext.stop(); nickjillings@1341: this.innerHTML = 'Stop'; nickjillings@1341: var time = audioEngineContext.timer.getTestTime(); nickjillings@1341: console.log('Stopped at ' + time); // DEBUG/SAFETY nickjillings@1341: } nickjillings@1341: }; nickjillings@1341: // Create Submit (save) button nickjillings@1341: var submit = document.createElement("button"); nickjillings@1295: submit.innerHTML = 'Next'; nickjillings@1341: submit.onclick = buttonSubmitClick; nickjillings@1341: submit.id = 'submit-button'; nickjillings@1341: submit.style.float = 'left'; nickjillings@1341: // Append the interface buttons into the interfaceButtons object. nickjillings@1341: interfaceButtons.appendChild(playback); nickjillings@1341: interfaceButtons.appendChild(submit); nickjillings@1341: nickjillings@1341: // Create a slider box nickjillings@1341: var sliderBox = document.createElement('div'); nickjillings@1341: sliderBox.style.width = "100%"; nickjillings@1341: sliderBox.style.height = window.innerHeight - 200+12 + 'px'; nickjillings@1341: sliderBox.style.marginBottom = '10px'; nickjillings@1341: sliderBox.id = 'slider'; nickjillings@1341: var scaleHolder = document.createElement('div'); nickjillings@1341: scaleHolder.id = "scale-holder"; nickjillings@1341: sliderBox.appendChild(scaleHolder); nickjillings@1341: var scaleText = document.createElement('div'); nickjillings@1341: scaleText.id = "scale-text-holder"; nickjillings@1341: scaleHolder.appendChild(scaleText); nickjillings@1341: var scaleCanvas = document.createElement('canvas'); nickjillings@1341: scaleCanvas.id = "scale-canvas"; nickjillings@1341: scaleHolder.appendChild(scaleCanvas); nickjillings@1341: var sliderObjectHolder = document.createElement('div'); nickjillings@1341: sliderObjectHolder.id = 'slider-holder'; nickjillings@1341: sliderObjectHolder.align = "center"; nickjillings@1341: sliderBox.appendChild(sliderObjectHolder); nickjillings@1341: nickjillings@1341: // Global parent for the comment boxes on the page nickjillings@1341: var feedbackHolder = document.createElement('div'); nickjillings@1341: feedbackHolder.id = 'feedbackHolder'; nickjillings@1341: nickjillings@1341: testContent.style.zIndex = 1; nickjillings@1341: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema nickjillings@1341: nickjillings@1341: // Inject into HTML nickjillings@1341: testContent.appendChild(title); // Insert the title nickjillings@1341: testContent.appendChild(pagetitle); nickjillings@1341: testContent.appendChild(interfaceButtons); nickjillings@1341: testContent.appendChild(sliderBox); nickjillings@1341: testContent.appendChild(feedbackHolder); nickjillings@1341: interfaceContext.insertPoint.appendChild(testContent); nickjillings@1341: nickjillings@1341: // Load the full interface nickjillings@1341: testState.initialise(); nickjillings@1341: testState.advanceState(); nickjillings@1341: } nickjillings@1341: nickjillings@1341: function loadTest(audioHolderObject) nickjillings@1341: { nickjillings@1341: var id = audioHolderObject.id; nickjillings@1341: nickjillings@1341: var feedbackHolder = document.getElementById('feedbackHolder'); nickjillings@1356: feedbackHolder.innerHTML = null; nickjillings@1341: var interfaceObj = audioHolderObject.interfaces; nickjillings@1341: if (interfaceObj.length > 1) nickjillings@1341: { nickjillings@1341: console.log("WARNING - This interface only supports one node per page. Using first interface node"); nickjillings@1341: } nickjillings@1341: interfaceObj = interfaceObj[0]; nickjillings@1341: if(interfaceObj.title != null) nickjillings@1341: { nickjillings@1341: document.getElementById("pageTitle").textContent = interfaceObj.title; nickjillings@1341: } nickjillings@1356: var interfaceOptions = specification.interfaces.options.concat(interfaceObj.options); nickjillings@1356: for (var option of interfaceOptions) nickjillings@1356: { nickjillings@1356: if (option.type == "show") nickjillings@1356: { nickjillings@1356: switch(option.name) { nickjillings@1356: case "playhead": nickjillings@1356: var playbackHolder = document.getElementById('playback-holder'); nickjillings@1356: if (playbackHolder == null) nickjillings@1356: { nickjillings@1356: playbackHolder = document.createElement('div'); nickjillings@1356: playbackHolder.style.width = "100%"; nickjillings@1356: playbackHolder.align = 'center'; nickjillings@1356: playbackHolder.appendChild(interfaceContext.playhead.object); nickjillings@1356: feedbackHolder.appendChild(playbackHolder); nickjillings@1356: } nickjillings@1356: break; nickjillings@1356: case "page-count": nickjillings@1356: var pagecountHolder = document.getElementById('page-count'); nickjillings@1356: if (pagecountHolder == null) nickjillings@1356: { nickjillings@1356: pagecountHolder = document.createElement('div'); nickjillings@1356: pagecountHolder.id = 'page-count'; nickjillings@1356: } nickjillings@2125: pagecountHolder.innerHTML = 'Page '+(testState.stateIndex+1)+' of '+testState.stateMap.length+''; nickjillings@1356: var inject = document.getElementById('interface-buttons'); nickjillings@1356: inject.appendChild(pagecountHolder); nickjillings@1356: break; nickjillings@1356: case "volume": nickjillings@1356: if (document.getElementById('master-volume-holder') == null) nickjillings@1356: { nickjillings@1356: feedbackHolder.appendChild(interfaceContext.volume.object); nickjillings@1356: } nickjillings@1356: break; nickjillings@1356: } nickjillings@1356: } nickjillings@1356: } nickjillings@1341: nickjillings@1341: // Delete outside reference nickjillings@1341: var outsideReferenceHolder = document.getElementById('outside-reference'); nickjillings@1341: if (outsideReferenceHolder != null) { nickjillings@1341: document.getElementById('interface-buttons').removeChild(outsideReferenceHolder); nickjillings@1341: } nickjillings@1341: nickjillings@1341: var sliderBox = document.getElementById('slider-holder'); nickjillings@1341: sliderBox.innerHTML = null; nickjillings@1341: nickjillings@1341: var commentBoxPrefix = "Comment on track"; nickjillings@1341: if (interfaceObj.commentBoxPrefix != undefined) { nickjillings@1341: commentBoxPrefix = interfaceObj.commentBoxPrefix; nickjillings@1341: } nickjillings@1341: var loopPlayback = audioHolderObject.loop; nickjillings@1341: nickjillings@1341: currentTestHolder = document.createElement('audioHolder'); nickjillings@1341: currentTestHolder.id = audioHolderObject.id; nickjillings@1341: currentTestHolder.repeatCount = audioHolderObject.repeatCount; nickjillings@1341: nickjillings@1341: // Find all the audioElements from the audioHolder nickjillings@1295: var index = 0; nickjillings@1341: $(audioHolderObject.audioElements).each(function(index,element){ nickjillings@1341: // Find URL of track nickjillings@1341: // In this jQuery loop, variable 'this' holds the current audioElement. nickjillings@1341: nickjillings@1341: var audioObject = audioEngineContext.newTrack(element); nickjillings@1341: if (element.type == 'outside-reference') nickjillings@1341: { nickjillings@1341: // Construct outside reference; nickjillings@2176: var orNode = new interfaceContext.outsideReferenceDOM(audioObject,index,document.getElementById('interface-buttons')); nickjillings@1341: audioObject.bindInterface(orNode); nickjillings@1341: } else { nickjillings@1341: // Create a slider per track nickjillings@1295: switch(audioObject.specification.parent.label) { nickjillings@1295: case "none": nickjillings@1295: label = ""; nickjillings@1295: break; nickjillings@1295: case "letter": nickjillings@1295: label = String.fromCharCode(97 + index); nickjillings@1295: break; nickjillings@1295: case "capital": nickjillings@1295: label = String.fromCharCode(65 + index); nickjillings@1295: break; nickjillings@1295: default: nickjillings@1295: label = ""+index; nickjillings@1295: break; nickjillings@1295: } nickjillings@1342: var sliderObj = new sliderObject(audioObject,label); nickjillings@1341: nickjillings@1341: if (typeof audioHolderObject.initialPosition === "number") nickjillings@1341: { nickjillings@1341: // Set the values nickjillings@1342: sliderObj.slider.value = audioHolderObject.initalPosition; nickjillings@1341: } else { nickjillings@1341: // Distribute it randomnly nickjillings@1342: sliderObj.slider.value = Math.random(); nickjillings@1341: } nickjillings@1342: sliderBox.appendChild(sliderObj.holder); nickjillings@1342: audioObject.bindInterface(sliderObj); nickjillings@2117: interfaceContext.commentBoxes.createCommentBox(audioObject); nickjillings@1295: index += 1; nickjillings@1341: } nickjillings@1341: nickjillings@1341: }); nickjillings@2107: nickjillings@2107: if (audioHolderObject.showElementComments) { nickjillings@2117: interfaceContext.commentBoxes.showCommentBoxes(feedbackHolder,true); nickjillings@2107: } nickjillings@2107: nickjillings@2107: $(audioHolderObject.commentQuestions).each(function(index,element) { nickjillings@2107: var node = interfaceContext.createCommentQuestion(element); nickjillings@2107: feedbackHolder.appendChild(node.holder); nickjillings@2107: }); nickjillings@1341: nickjillings@1341: // Auto-align nickjillings@1341: resizeWindow(null); nickjillings@1341: } nickjillings@1341: nickjillings@1341: function sliderObject(audioObject,label) nickjillings@1341: { nickjillings@1341: // Constructs the slider object. We use the HTML5 slider object nickjillings@1341: this.parent = audioObject; nickjillings@1341: this.holder = document.createElement('div'); nickjillings@1341: this.title = document.createElement('span'); nickjillings@1341: this.slider = document.createElement('input'); nickjillings@1341: this.play = document.createElement('button'); nickjillings@1341: nickjillings@1341: this.holder.className = 'track-slider'; nickjillings@1341: this.holder.style.height = window.innerHeight-200 + 'px'; nickjillings@1341: this.holder.appendChild(this.title); nickjillings@1341: this.holder.appendChild(this.slider); nickjillings@1341: this.holder.appendChild(this.play); nickjillings@1341: this.holder.align = "center"; nickjillings@1343: if (label == 0) nickjillings@1341: { nickjillings@1341: this.holder.style.marginLeft = '0px'; nickjillings@1341: } nickjillings@1341: this.holder.setAttribute('trackIndex',audioObject.id); nickjillings@1341: nickjillings@1341: this.title.textContent = label; nickjillings@1341: this.title.style.width = "100%"; nickjillings@1341: this.title.style.float = "left"; nickjillings@1341: nickjillings@1341: this.slider.type = "range"; nickjillings@1341: this.slider.className = "track-slider-range track-slider-not-moved"; nickjillings@1341: this.slider.min = "0"; nickjillings@1341: this.slider.max = "1"; nickjillings@1341: this.slider.step = "0.01"; nickjillings@1341: this.slider.setAttribute('orient','vertical'); nickjillings@1341: this.slider.style.height = window.innerHeight-250 + 'px'; nickjillings@1341: this.slider.onchange = function() nickjillings@1341: { nickjillings@1341: var time = audioEngineContext.timer.getTestTime(); nickjillings@1341: var id = Number(this.parentNode.getAttribute('trackIndex')); nickjillings@1341: audioEngineContext.audioObjects[id].metric.moved(time,this.value); nickjillings@1341: console.log('slider '+id+' moved to '+this.value+' ('+time+')'); nickjillings@1341: $(this).removeClass('track-slider-not-moved'); nickjillings@1341: }; nickjillings@1341: nickjillings@1341: this.play.textContent = "Loading..."; nickjillings@1341: this.play.value = audioObject.id; nickjillings@1341: this.play.style.float = "left"; nickjillings@1341: this.play.style.width = "100%"; nickjillings@1341: this.play.disabled = true; nickjillings@1377: this.play.setAttribute("playstate","ready"); nickjillings@1341: this.play.onclick = function(event) nickjillings@1341: { nickjillings@1341: var id = Number(event.currentTarget.value); nickjillings@1341: //audioEngineContext.metric.sliderPlayed(id); nickjillings@1377: if (event.currentTarget.getAttribute("playstate") == "ready") nickjillings@1377: {audioEngineContext.play(id);} nickjillings@1377: else if (event.currentTarget.getAttribute("playstate") == "playing") nickjillings@1377: {audioEngineContext.stop();} nickjillings@1341: }; nickjillings@1341: nickjillings@1341: this.enable = function() { nickjillings@1341: this.play.disabled = false; nickjillings@1341: this.play.textContent = "Play"; nickjillings@1341: $(this.slider).removeClass('track-slider-disabled'); nickjillings@1341: }; nickjillings@1341: nickjillings@1341: this.exportXMLDOM = function(audioObject) { nickjillings@1341: // Called by the audioObject holding this element. Must be present nickjillings@1341: var node = storage.document.createElement('value'); nickjillings@1341: node.textContent = this.slider.value; nickjillings@1341: return node; nickjillings@1341: }; nickjillings@1360: this.startPlayback = function() nickjillings@1360: { nickjillings@1360: // Called when playback has begun nickjillings@1377: this.play.setAttribute("playstate","playing"); nickjillings@1360: $(".track-slider").removeClass('track-slider-playing'); nickjillings@1360: $(this.holder).addClass('track-slider-playing'); nickjillings@1360: var outsideReference = document.getElementById('outside-reference'); nickjillings@1360: if (outsideReference != null) { nickjillings@1360: $(outsideReference).removeClass('track-slider-playing'); nickjillings@1360: } nickjillings@1383: this.play.textContent = "Stop"; nickjillings@1360: }; nickjillings@1360: this.stopPlayback = function() nickjillings@1360: { nickjillings@1360: // Called when playback has stopped. This gets called even if playback never started! nickjillings@1377: this.play.setAttribute("playstate","ready"); nickjillings@1360: $(this.holder).removeClass('track-slider-playing'); nickjillings@1383: this.play.textContent = "Play"; nickjillings@1360: }; nickjillings@1341: this.getValue = function() { nickjillings@1341: return this.slider.value; nickjillings@1341: }; nickjillings@1341: nickjillings@1341: this.resize = function(event) nickjillings@1341: { nickjillings@1341: this.holder.style.height = window.innerHeight-200 + 'px'; nickjillings@1341: this.slider.style.height = window.innerHeight-250 + 'px'; nickjillings@1341: }; nickjillings@1341: this.updateLoading = function(progress) nickjillings@1341: { nickjillings@1341: progress = String(progress); nickjillings@1341: progress = progress.substr(0,5); nickjillings@1341: this.play.textContent = "Loading: "+progress+"%"; nickjillings@1341: }; nickjillings@1341: nickjillings@1341: if (this.parent.state == 1) nickjillings@1341: { nickjillings@1341: this.enable(); nickjillings@1341: } nickjillings@1341: this.getPresentedId = function() nickjillings@1341: { nickjillings@1341: return this.title.textContent; nickjillings@1341: }; nickjillings@1341: this.canMove = function() nickjillings@1341: { nickjillings@1341: return true; nickjillings@1341: }; nickjillings@2113: this.error = function() { nickjillings@2113: // audioObject has an error!! nickjillings@2113: this.playback.textContent = "Error"; nickjillings@2113: $(this.playback).addClass("error-colour"); nickjillings@2113: } nickjillings@1341: } nickjillings@1341: nickjillings@1341: function resizeWindow(event) nickjillings@1341: { nickjillings@1341: // Function called when the window has been resized. nickjillings@1341: // MANDATORY FUNCTION nickjillings@1341: nickjillings@1341: var outsideRef = document.getElementById('outside-reference'); nickjillings@1341: if(outsideRef != null) nickjillings@1341: { nickjillings@1341: outsideRef.style.left = (window.innerWidth-120)/2 + 'px'; nickjillings@1341: } nickjillings@1341: nickjillings@1341: // Auto-align nickjillings@1341: var numObj = document.getElementsByClassName('track-slider').length; nickjillings@1341: var totalWidth = (numObj-1)*150+100; nickjillings@1341: var diff = (window.innerWidth - totalWidth)/2; nickjillings@1341: document.getElementById('slider').style.height = window.innerHeight - 180 + 'px'; nickjillings@1341: if (diff <= 0){diff = 0;} nickjillings@1341: document.getElementById('slider-holder').style.marginLeft = diff + 'px'; nickjillings@1341: for (var i in audioEngineContext.audioObjects) nickjillings@1341: { nickjillings@1341: if (audioEngineContext.audioObjects[i].specification.type != 'outside-reference'){ nickjillings@1341: audioEngineContext.audioObjects[i].interfaceDOM.resize(event); nickjillings@1341: } nickjillings@1341: } nickjillings@1341: document.getElementById('scale-holder').style.marginLeft = (diff-100) + 'px'; nickjillings@1341: document.getElementById('scale-text-holder').style.height = window.innerHeight-194 + 'px'; nickjillings@1341: var canvas = document.getElementById('scale-canvas'); nickjillings@1341: canvas.width = totalWidth; nickjillings@1341: canvas.height = window.innerHeight-194; nickjillings@1341: drawScale(); nickjillings@1341: } nickjillings@1341: nickjillings@1341: function drawScale() nickjillings@1341: { nickjillings@1341: var interfaceObj = testState.currentStateMap.interfaces[0]; nickjillings@1341: var scales = testState.currentStateMap.interfaces[0].scales; nickjillings@1341: scales = scales.sort(function(a,b) { nickjillings@1341: return a.position - b.position; nickjillings@1341: }); nickjillings@1341: var canvas = document.getElementById('scale-canvas'); nickjillings@1341: var ctx = canvas.getContext("2d"); nickjillings@1341: var height = canvas.height; nickjillings@1341: var width = canvas.width; nickjillings@1341: var draw_heights = [24, height-34]; nickjillings@1341: var textHolder = document.getElementById('scale-text-holder'); nickjillings@1341: textHolder.innerHTML = null; nickjillings@1341: var lastHeight = 0; nickjillings@1341: for (var scale of scales) nickjillings@1341: { nickjillings@1341: var posPercent = scale.position / 100.0; nickjillings@1341: var posPix = (1-posPercent)*(draw_heights[1]-draw_heights[0])+draw_heights[0]; nickjillings@1341: ctx.fillStyle = "#000000"; nickjillings@1341: ctx.setLineDash([1,2]); nickjillings@1341: ctx.moveTo(0,posPix); nickjillings@1341: ctx.lineTo(width,posPix); nickjillings@1341: ctx.stroke(); nickjillings@1341: var text = document.createElement('div'); nickjillings@1341: text.align = "right"; nickjillings@1341: var textC = document.createElement('span'); nickjillings@1341: textC.textContent = scale.text; nickjillings@1341: text.appendChild(textC); nickjillings@1341: text.className = "scale-text"; nickjillings@1341: textHolder.appendChild(text); nickjillings@1341: text.style.top = (posPix-9) + 'px'; nickjillings@1341: text.style.left = 100 - ($(text).width()+3) + 'px'; nickjillings@1341: lastHeight = posPix; nickjillings@1341: } nickjillings@1341: } nickjillings@1341: nickjillings@1341: function buttonSubmitClick() // TODO: Only when all songs have been played! nickjillings@1341: { nickjillings@1341: var checks = []; nickjillings@1341: checks = checks.concat(testState.currentStateMap.interfaces[0].options); nickjillings@1341: checks = checks.concat(specification.interfaces.options); nickjillings@1341: var canContinue = true; nickjillings@1341: nickjillings@1341: // Check that the anchor and reference objects are correctly placed nickjillings@1341: if (interfaceContext.checkHiddenAnchor() == false) {return;} nickjillings@1341: if (interfaceContext.checkHiddenReference() == false) {return;} nickjillings@1341: nickjillings@1341: for (var i=0; i saves nickjillings@1341: // Get the current information in store (remember to appendChild your data to it) nickjillings@1341: // pageSpecification is the current page node configuration nickjillings@1341: // To create new XML nodes, use storage.document.createElement(); nickjillings@1341: }