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