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