nickjillings@1318: /** nickjillings@1318: * mushra.js nickjillings@1318: * Create the MUSHRA interface nickjillings@1318: */ nickjillings@1318: nickjillings@1318: // Once this is loaded and parsed, begin execution nickjillings@1318: loadInterface(); nickjillings@1318: nickjillings@1318: function loadInterface() { nickjillings@1318: // Get the dimensions of the screen available to the page nickjillings@1318: var width = window.innerWidth; nickjillings@1318: var height = window.innerHeight; nickjillings@1318: nickjillings@1318: // The injection point into the HTML page nickjillings@1318: interfaceContext.insertPoint = document.getElementById("topLevelBody"); nickjillings@1318: var testContent = document.createElement('div'); nickjillings@1318: testContent.id = 'testContent'; nickjillings@1318: nickjillings@1318: // Create the top div for the Title element nickjillings@1318: var titleAttr = specification.title; nickjillings@1318: var title = document.createElement('div'); nickjillings@1318: title.className = "title"; nickjillings@1318: title.align = "center"; nickjillings@1318: var titleSpan = document.createElement('span'); nickjillings@1318: nickjillings@1318: // Set title to that defined in XML, else set to default nickjillings@1318: if (titleAttr != undefined) { nickjillings@1318: titleSpan.textContent = titleAttr; nickjillings@1318: } else { nickjillings@1318: titleSpan.textContent = 'Listening test'; nickjillings@1318: } nickjillings@1318: // Insert the titleSpan element into the title div element. nickjillings@1318: title.appendChild(titleSpan); nickjillings@1318: nickjillings@1318: var pagetitle = document.createElement('div'); nickjillings@1318: pagetitle.className = "pageTitle"; nickjillings@1318: pagetitle.align = "center"; nickjillings@1318: var titleSpan = document.createElement('span'); nickjillings@1318: titleSpan.id = "pageTitle"; nickjillings@1318: pagetitle.appendChild(titleSpan); nickjillings@1318: nickjillings@1318: // Create Interface buttons! nickjillings@1318: var interfaceButtons = document.createElement('div'); nickjillings@1318: interfaceButtons.id = 'interface-buttons'; nickjillings@1318: nickjillings@1318: // Create playback start/stop points nickjillings@1318: var playback = document.createElement("button"); nickjillings@1318: playback.innerHTML = 'Stop'; nickjillings@1318: playback.id = 'playback-button'; nickjillings@1318: playback.style.float = 'left'; nickjillings@1318: // onclick function. Check if it is playing or not, call the correct function in the nickjillings@1318: // audioEngine, change the button text to reflect the next state. nickjillings@1318: playback.onclick = function() { nickjillings@1318: if (audioEngineContext.status == 1) { nickjillings@1318: audioEngineContext.stop(); nickjillings@1318: this.innerHTML = 'Stop'; nickjillings@1318: var time = audioEngineContext.timer.getTestTime(); nickjillings@1318: console.log('Stopped at ' + time); // DEBUG/SAFETY nickjillings@1318: } nickjillings@1318: }; nickjillings@1318: // Create Submit (save) button nickjillings@1318: var submit = document.createElement("button"); nickjillings@1318: submit.innerHTML = 'Submit'; nickjillings@1318: submit.onclick = buttonSubmitClick; nickjillings@1318: submit.id = 'submit-button'; nickjillings@1318: submit.style.float = 'left'; nickjillings@1318: // Append the interface buttons into the interfaceButtons object. nickjillings@1318: interfaceButtons.appendChild(playback); nickjillings@1318: interfaceButtons.appendChild(submit); nickjillings@1318: nickjillings@1318: // Create a slider box nickjillings@1318: var sliderBox = document.createElement('div'); nickjillings@1318: sliderBox.style.width = "100%"; nickjillings@1318: sliderBox.style.height = window.innerHeight - 200+12 + 'px'; nickjillings@1318: sliderBox.style.marginBottom = '10px'; nickjillings@1318: sliderBox.id = 'slider'; nickjillings@1318: var scaleHolder = document.createElement('div'); nickjillings@1318: scaleHolder.id = "scale-holder"; nickjillings@1318: sliderBox.appendChild(scaleHolder); nickjillings@1318: var sliderObjectHolder = document.createElement('div'); nickjillings@1318: sliderObjectHolder.id = 'slider-holder'; nickjillings@1318: sliderObjectHolder.align = "center"; nickjillings@1318: sliderBox.appendChild(sliderObjectHolder); nickjillings@1318: nickjillings@1318: // Global parent for the comment boxes on the page nickjillings@1318: var feedbackHolder = document.createElement('div'); nickjillings@1318: feedbackHolder.id = 'feedbackHolder'; nickjillings@1318: nickjillings@1318: testContent.style.zIndex = 1; nickjillings@1318: interfaceContext.insertPoint.innerHTML = null; // Clear the current schema nickjillings@1318: nickjillings@1318: // Inject into HTML nickjillings@1318: testContent.appendChild(title); // Insert the title nickjillings@1318: testContent.appendChild(pagetitle); nickjillings@1318: testContent.appendChild(interfaceButtons); nickjillings@1318: testContent.appendChild(sliderBox); nickjillings@1318: testContent.appendChild(feedbackHolder); nickjillings@1318: interfaceContext.insertPoint.appendChild(testContent); nickjillings@1318: nickjillings@1318: // Load the full interface nickjillings@1318: testState.initialise(); nickjillings@1318: testState.advanceState(); nickjillings@1318: } nickjillings@1318: nickjillings@1318: function loadTest(audioHolderObject) nickjillings@1318: { nickjillings@1318: var id = audioHolderObject.id; nickjillings@1318: nickjillings@1318: var feedbackHolder = document.getElementById('feedbackHolder'); nickjillings@1318: var interfaceObj = audioHolderObject.interfaces; nickjillings@1318: if (interfaceObj.length > 1) nickjillings@1318: { nickjillings@1318: console.log("WARNING - This interface only supports one node per page. Using first interface node"); nickjillings@1318: } nickjillings@1318: interfaceObj = interfaceObj[0]; nickjillings@1318: if(interfaceObj.title != null) nickjillings@1318: { nickjillings@1318: document.getElementById("pageTitle").textContent = interfaceObj.title; nickjillings@1318: } nickjillings@1318: nickjillings@1318: // Delete outside reference nickjillings@1318: var outsideReferenceHolder = document.getElementById('outside-reference'); nickjillings@1318: if (outsideReferenceHolder != null) { nickjillings@1318: document.getElementById('interface-buttons').removeChild(outsideReferenceHolder); nickjillings@1318: } nickjillings@1318: nickjillings@1318: var sliderBox = document.getElementById('slider-holder'); nickjillings@1318: feedbackHolder.innerHTML = null; nickjillings@1318: sliderBox.innerHTML = null; nickjillings@1318: nickjillings@1318: var commentBoxPrefix = "Comment on track"; nickjillings@1318: if (interfaceObj.commentBoxPrefix != undefined) { nickjillings@1318: commentBoxPrefix = interfaceObj.commentBoxPrefix; nickjillings@1318: } nickjillings@1318: var loopPlayback = audioHolderObject.loop; nickjillings@1318: nickjillings@1318: currentTestHolder = document.createElement('audioHolder'); nickjillings@1318: currentTestHolder.id = audioHolderObject.id; nickjillings@1318: currentTestHolder.repeatCount = audioHolderObject.repeatCount; nickjillings@1318: nickjillings@1318: $(audioHolderObject.commentQuestions).each(function(index,element) { nickjillings@1318: var node = interfaceContext.createCommentQuestion(element); nickjillings@1318: feedbackHolder.appendChild(node.holder); nickjillings@1318: }); nickjillings@1318: nickjillings@1318: // Find all the audioElements from the audioHolder nickjillings@1318: $(audioHolderObject.audioElements).each(function(index,element){ nickjillings@1318: // Find URL of track nickjillings@1318: // In this jQuery loop, variable 'this' holds the current audioElement. nickjillings@1318: nickjillings@1318: // Check if an outside reference nickjillings@1318: if (index == audioHolderObject.outsideReference) nickjillings@1318: { nickjillings@1318: return; nickjillings@1318: } nickjillings@1318: nickjillings@1318: var audioObject = audioEngineContext.newTrack(element); nickjillings@1318: nickjillings@1318: var node = interfaceContext.createCommentBox(audioObject); nickjillings@1318: nickjillings@1318: // Create a slider per track nickjillings@1318: audioObject.interfaceDOM = new sliderObject(audioObject); nickjillings@1318: nickjillings@1318: if (typeof audioHolderObject.initialPosition === "number") nickjillings@1318: { nickjillings@1318: // Set the values nickjillings@1318: audioObject.interfaceDOM.slider.value = audioHolderObject.initalPosition; nickjillings@1318: } else { nickjillings@1318: // Distribute it randomnly nickjillings@1318: audioObject.interfaceDOM.slider.value = Math.random(); nickjillings@1318: } nickjillings@1318: nickjillings@1318: sliderBox.appendChild(audioObject.interfaceDOM.holder); nickjillings@1318: audioObject.metric.initialised(audioObject.interfaceDOM.slider.value); nickjillings@1318: nickjillings@1318: }); nickjillings@1318: nickjillings@1318: // Auto-align nickjillings@1318: var numObj = audioHolderObject.audioElements.length; nickjillings@1318: var totalWidth = (numObj-1)*150+100; nickjillings@1318: var diff = (window.innerWidth - totalWidth)/2; nickjillings@1318: sliderBox.style.marginLeft = diff + 'px'; nickjillings@1318: nickjillings@1318: // Construct outside reference; nickjillings@1318: if (audioHolderObject.outsideReference != null) { nickjillings@1318: var outsideReferenceHolder = document.createElement('div'); nickjillings@1318: outsideReferenceHolder.id = 'outside-reference'; nickjillings@1318: outsideReferenceHolder.className = 'outside-reference'; nickjillings@1318: outsideReferenceHolderspan = document.createElement('span'); nickjillings@1318: outsideReferenceHolderspan.textContent = 'Reference'; nickjillings@1318: outsideReferenceHolder.appendChild(outsideReferenceHolderspan); nickjillings@1318: nickjillings@1318: var audioObject = audioEngineContext.newTrack(audioHolderObject.audioElements[audioHolderObject.outsideReference]); nickjillings@1318: nickjillings@1318: outsideReferenceHolder.onclick = function(event) nickjillings@1318: { nickjillings@1318: audioEngineContext.play(audioEngineContext.audioObjects.length-1); nickjillings@1318: $('.track-slider').removeClass('track-slider-playing'); nickjillings@1318: $('.comment-div').removeClass('comment-box-playing'); nickjillings@1318: if (event.currentTarget.nodeName == 'DIV') { nickjillings@1318: $(event.currentTarget).addClass('track-slider-playing'); nickjillings@1318: } else { nickjillings@1318: $(event.currentTarget.parentElement).addClass('track-slider-playing'); nickjillings@1318: } nickjillings@1318: }; nickjillings@1318: nickjillings@1318: document.getElementById('interface-buttons').appendChild(outsideReferenceHolder); nickjillings@1318: } nickjillings@1318: } nickjillings@1318: nickjillings@1318: function sliderObject(audioObject) nickjillings@1318: { nickjillings@1318: // Constructs the slider object. We use the HTML5 slider object nickjillings@1318: this.parent = audioObject; nickjillings@1318: this.holder = document.createElement('div'); nickjillings@1318: this.title = document.createElement('span'); nickjillings@1318: this.slider = document.createElement('input'); nickjillings@1318: this.play = document.createElement('button'); nickjillings@1318: nickjillings@1318: this.holder.className = 'track-slider'; nickjillings@1318: this.holder.style.height = window.innerHeight-200 + 'px'; nickjillings@1318: this.holder.appendChild(this.title); nickjillings@1318: this.holder.appendChild(this.slider); nickjillings@1318: this.holder.appendChild(this.play); nickjillings@1318: this.holder.align = "center"; nickjillings@1318: if (audioObject.id == 0) nickjillings@1318: { nickjillings@1318: this.holder.style.marginLeft = '0px'; nickjillings@1318: } nickjillings@1318: this.holder.setAttribute('trackIndex',audioObject.id); nickjillings@1318: nickjillings@1318: this.title.textContent = audioObject.id; nickjillings@1318: this.title.style.width = "100%"; nickjillings@1318: this.title.style.float = "left"; nickjillings@1318: nickjillings@1318: this.slider.type = "range"; nickjillings@1318: this.slider.className = "track-slider-range track-slider-not-moved"; nickjillings@1318: this.slider.min = "0"; nickjillings@1318: this.slider.max = "1"; nickjillings@1318: this.slider.step = "0.01"; nickjillings@1318: this.slider.setAttribute('orient','vertical'); nickjillings@1318: this.slider.style.height = window.innerHeight-250 + 'px'; nickjillings@1318: this.slider.onchange = function() nickjillings@1318: { nickjillings@1318: var time = audioEngineContext.timer.getTestTime(); nickjillings@1318: var id = Number(this.parentNode.getAttribute('trackIndex')); nickjillings@1318: audioEngineContext.audioObjects[id].metric.moved(time,this.value); nickjillings@1318: console.log('slider '+id+' moved to '+this.value+' ('+time+')'); nickjillings@1318: $(this).removeClass('track-slider-not-moved'); nickjillings@1318: }; nickjillings@1318: nickjillings@1318: this.play.textContent = "Loading..."; nickjillings@1318: this.play.value = audioObject.id; nickjillings@1318: this.play.style.float = "left"; nickjillings@1318: this.play.style.width = "100%"; nickjillings@1318: this.play.disabled = true; nickjillings@1318: this.play.onclick = function(event) nickjillings@1318: { nickjillings@1318: var id = Number(event.currentTarget.value); nickjillings@1318: //audioEngineContext.metric.sliderPlayed(id); nickjillings@1318: audioEngineContext.play(id); nickjillings@1318: $(".track-slider").removeClass('track-slider-playing'); nickjillings@1318: $(event.currentTarget.parentElement).addClass('track-slider-playing'); nickjillings@1318: var outsideReference = document.getElementById('outside-reference'); nickjillings@1318: if (outsideReference != null) { nickjillings@1318: $(outsideReference).removeClass('track-slider-playing'); nickjillings@1318: } nickjillings@1318: }; nickjillings@1318: nickjillings@1318: this.enable = function() { nickjillings@1318: this.play.disabled = false; nickjillings@1318: this.play.textContent = "Play"; nickjillings@1318: $(this.slider).removeClass('track-slider-disabled'); nickjillings@1318: }; nickjillings@1318: nickjillings@1318: this.exportXMLDOM = function(audioObject) { nickjillings@1318: // Called by the audioObject holding this element. Must be present nickjillings@1318: var node = document.createElement('value'); nickjillings@1318: node.textContent = this.slider.value; nickjillings@1318: return node; nickjillings@1318: }; nickjillings@1318: this.getValue = function() { nickjillings@1318: return this.slider.value; nickjillings@1318: }; nickjillings@1318: nickjillings@1318: this.resize = function(event) nickjillings@1318: { nickjillings@1318: this.holder.style.height = window.innerHeight-200 + 'px'; nickjillings@1318: this.slider.style.height = window.innerHeight-250 + 'px'; nickjillings@1318: }; nickjillings@1318: this.updateLoading = function(progress) nickjillings@1318: { nickjillings@1318: progress = String(progress); nickjillings@1318: progress = progress.substr(0,5); nickjillings@1318: this.play.textContent = "Loading: "+progress+"%"; nickjillings@1318: }; nickjillings@1318: nickjillings@1318: if (this.parent.state == 1) nickjillings@1318: { nickjillings@1318: this.enable(); nickjillings@1318: } nickjillings@1318: } nickjillings@1318: nickjillings@1318: function resizeWindow(event) nickjillings@1318: { nickjillings@1318: // Function called when the window has been resized. nickjillings@1318: // MANDATORY FUNCTION nickjillings@1318: nickjillings@1318: // Auto-align nickjillings@1318: var numObj = audioEngineContext.audioObjects.length; nickjillings@1318: var totalWidth = (numObj-1)*150+100; nickjillings@1318: var diff = (window.innerWidth - totalWidth)/2; nickjillings@1318: document.getElementById('slider').style.height = window.innerHeight - 180 + 'px'; nickjillings@1318: if (diff <= 0){diff = 0;} nickjillings@1318: document.getElementById('slider-holder').style.marginLeft = diff + 'px'; nickjillings@1318: for (var i in audioEngineContext.audioObjects) nickjillings@1318: { nickjillings@1318: audioEngineContext.audioObjects[i].interfaceDOM.resize(event); nickjillings@1318: } nickjillings@1318: } nickjillings@1318: nickjillings@1318: nickjillings@1318: function buttonSubmitClick() // TODO: Only when all songs have been played! nickjillings@1318: { nickjillings@1318: var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options; nickjillings@1318: var canContinue = true; nickjillings@1318: nickjillings@1318: // Check that the anchor and reference objects are correctly placed nickjillings@1318: if (interfaceContext.checkHiddenAnchor() == false) {return;} nickjillings@1318: if (interfaceContext.checkHiddenReference() == false) {return;} nickjillings@1318: nickjillings@1318: for (var i=0; i