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