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