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