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