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