nickjillings@1392: /** nickjillings@1392: * core.js nickjillings@1392: * nickjillings@1392: * Main script to run, calls all other core functions and manages loading/store to backend. nickjillings@1392: * Also contains all global variables. nickjillings@1392: */ nickjillings@1392: nickjillings@1392: /* create the web audio API context and store in audioContext*/ nickjillings@1392: var audioContext; // Hold the browser web audio API nickjillings@1392: var projectXML; // Hold the parsed setup XML nickjillings@1392: var specification; nickjillings@1392: var interfaceContext; nickjillings@1392: var popup; // Hold the interfacePopup object nickjillings@1392: var testState; nickjillings@1392: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order nickjillings@1392: var audioEngineContext; // The custome AudioEngine object nickjillings@1392: var projectReturn; // Hold the URL for the return nickjillings@1392: nickjillings@1392: nickjillings@1392: // Add a prototype to the bufferSourceNode to reference to the audioObject holding it nickjillings@1392: AudioBufferSourceNode.prototype.owner = undefined; nickjillings@1392: // Add a prototype to the bufferNode to hold the desired LINEAR gain nickjillings@1392: AudioBuffer.prototype.gain = undefined; nickjillings@1392: // Add a prototype to the bufferNode to hold the computed LUFS loudness nickjillings@1392: AudioBuffer.prototype.lufs = undefined; nickjillings@1392: nickjillings@1392: window.onload = function() { nickjillings@1392: // Function called once the browser has loaded all files. nickjillings@1392: // This should perform any initial commands such as structure / loading documents nickjillings@1392: nickjillings@1392: // Create a web audio API context nickjillings@1392: // Fixed for cross-browser support nickjillings@1392: var AudioContext = window.AudioContext || window.webkitAudioContext; nickjillings@1392: audioContext = new AudioContext; nickjillings@1392: nickjillings@1392: // Create test state nickjillings@1392: testState = new stateMachine(); nickjillings@1392: nickjillings@1392: // Create the popup interface object nickjillings@1392: popup = new interfacePopup(); nickjillings@1392: nickjillings@1392: // Create the specification object nickjillings@1392: specification = new Specification(); nickjillings@1392: nickjillings@1392: // Create the interface object nickjillings@1392: interfaceContext = new Interface(specification); nickjillings@1392: // Define window callbacks for interface nickjillings@1392: window.onresize = function(event){interfaceContext.resizeWindow(event);}; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: function loadProjectSpec(url) { nickjillings@1392: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data nickjillings@1392: // If url is null, request client to upload project XML document nickjillings@1392: var r = new XMLHttpRequest(); nickjillings@1392: r.open('GET',url,true); nickjillings@1392: r.onload = function() { nickjillings@1392: loadProjectSpecCallback(r.response); nickjillings@1392: }; nickjillings@1392: r.send(); nickjillings@1392: }; nickjillings@1392: nickjillings@1392: function loadProjectSpecCallback(response) { nickjillings@1392: // Function called after asynchronous download of XML project specification nickjillings@1392: //var decode = $.parseXML(response); nickjillings@1392: //projectXML = $(decode); nickjillings@1392: nickjillings@1392: var parse = new DOMParser(); nickjillings@1392: projectXML = parse.parseFromString(response,'text/xml'); nickjillings@1392: nickjillings@1392: // Build the specification nickjillings@1392: specification.decode(projectXML); nickjillings@1392: nickjillings@1392: // Detect the interface to use and load the relevant javascripts. nickjillings@1392: var interfaceJS = document.createElement('script'); nickjillings@1392: interfaceJS.setAttribute("type","text/javascript"); nickjillings@1392: if (specification.interfaceType == 'APE') { nickjillings@1392: interfaceJS.setAttribute("src","ape.js"); nickjillings@1392: nickjillings@1392: // APE comes with a css file nickjillings@1392: var css = document.createElement('link'); nickjillings@1392: css.rel = 'stylesheet'; nickjillings@1392: css.type = 'text/css'; nickjillings@1392: css.href = 'ape.css'; nickjillings@1392: nickjillings@1392: document.getElementsByTagName("head")[0].appendChild(css); nickjillings@1392: } else if (specification.interfaceType == "MUSHRA") nickjillings@1392: { nickjillings@1392: interfaceJS.setAttribute("src","mushra.js"); nickjillings@1392: nickjillings@1392: // MUSHRA comes with a css file nickjillings@1392: var css = document.createElement('link'); nickjillings@1392: css.rel = 'stylesheet'; nickjillings@1392: css.type = 'text/css'; nickjillings@1392: css.href = 'mushra.css'; nickjillings@1392: nickjillings@1392: document.getElementsByTagName("head")[0].appendChild(css); nickjillings@1392: } nickjillings@1392: document.getElementsByTagName("head")[0].appendChild(interfaceJS); nickjillings@1392: nickjillings@1392: // Create the audio engine object nickjillings@1392: audioEngineContext = new AudioEngine(specification); nickjillings@1392: nickjillings@1392: testState.stateMap.push(specification.preTest); nickjillings@1392: nickjillings@1392: $(specification.audioHolders).each(function(index,elem){ nickjillings@1392: testState.stateMap.push(elem); nickjillings@1392: $(elem.audioElements).each(function(i,audioElem){ nickjillings@1392: var URL = audioElem.parent.hostURL + audioElem.url; nickjillings@1392: var buffer = null; nickjillings@1392: for (var i=0; i 0) nickjillings@1392: this.buttonPrevious.style.visibility = 'visible'; nickjillings@1392: else nickjillings@1392: this.buttonPrevious.style.visibility = 'hidden'; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.initState = function(node) { nickjillings@1392: //Call this with your preTest and postTest nodes when needed to nickjillings@1392: // initialise the popup procedure. nickjillings@1392: this.popupOptions = node.options; nickjillings@1392: if (this.popupOptions.length > 0) { nickjillings@1392: if (node.type == 'pretest') { nickjillings@1392: this.responses = document.createElement('PreTest'); nickjillings@1392: } else if (node.type == 'posttest') { nickjillings@1392: this.responses = document.createElement('PostTest'); nickjillings@1392: } else { nickjillings@1392: console.log ('WARNING - popup node neither pre or post!'); nickjillings@1392: this.responses = document.createElement('responses'); nickjillings@1392: } nickjillings@1392: this.currentIndex = 0; nickjillings@1392: this.showPopup(); nickjillings@1392: this.postNode(); nickjillings@1392: } else { nickjillings@1392: advanceState(); nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.proceedClicked = function() { nickjillings@1392: // Each time the popup button is clicked! nickjillings@1392: var node = this.popupOptions[this.currentIndex]; nickjillings@1392: if (node.type == 'question') { nickjillings@1392: // Must extract the question data nickjillings@1392: var textArea = $(popup.popupContent).find('textarea')[0]; nickjillings@1392: if (node.mandatory == true && textArea.value.length == 0) { nickjillings@1392: alert('This question is mandatory'); nickjillings@1392: return; nickjillings@1392: } else { nickjillings@1392: // Save the text content nickjillings@1392: var hold = document.createElement('comment'); nickjillings@1392: hold.id = node.id; nickjillings@1392: hold.innerHTML = textArea.value; nickjillings@1392: console.log("Question: "+ node.question); nickjillings@1392: console.log("Question Response: "+ textArea.value); nickjillings@1392: this.responses.appendChild(hold); nickjillings@1392: } nickjillings@1392: } else if (node.type == 'checkbox') { nickjillings@1392: // Must extract checkbox data nickjillings@1392: var optHold = this.popupResponse; nickjillings@1392: var hold = document.createElement('checkbox'); nickjillings@1392: console.log("Checkbox: "+ node.statement); nickjillings@1392: hold.id = node.id; nickjillings@1392: for (var i=0; i node.max && node.max != null) { nickjillings@1392: alert('Number is above the maximum value of '+node.max); nickjillings@1392: return; nickjillings@1392: } nickjillings@1392: var hold = document.createElement('number'); nickjillings@1392: hold.id = node.id; nickjillings@1392: hold.textContent = input.value; nickjillings@1392: this.responses.appendChild(hold); nickjillings@1392: } nickjillings@1392: this.currentIndex++; nickjillings@1392: if (this.currentIndex < this.popupOptions.length) { nickjillings@1392: this.postNode(); nickjillings@1392: } else { nickjillings@1392: // Reached the end of the popupOptions nickjillings@1392: this.hidePopup(); nickjillings@1392: if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) { nickjillings@1392: testState.stateResults[testState.stateIndex] = this.responses; nickjillings@1392: } else { nickjillings@1392: testState.stateResults[testState.stateIndex].appendChild(this.responses); nickjillings@1392: } nickjillings@1392: advanceState(); nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.previousClick = function() { nickjillings@1392: // Triggered when the 'Back' button is clicked in the survey nickjillings@1392: if (this.currentIndex > 0) { nickjillings@1392: this.currentIndex--; nickjillings@1392: var node = this.popupOptions[this.currentIndex]; nickjillings@1392: if (node.type != 'statement') { nickjillings@1392: var prevResp = this.responses.childNodes[this.responses.childElementCount-1]; nickjillings@1392: this.responses.removeChild(prevResp); nickjillings@1392: } nickjillings@1392: this.postNode(); nickjillings@1392: if (node.type == 'question') { nickjillings@1392: this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent; nickjillings@1392: } else if (node.type == 'checkbox') { nickjillings@1392: var options = this.popupContent.getElementsByTagName('input'); nickjillings@1392: var savedOptions = prevResp.getElementsByTagName('option'); nickjillings@1392: for (var i=0; i 0) { nickjillings@1392: if(this.stateIndex != null) { nickjillings@1392: console.log('NOTE - State already initialise'); nickjillings@1392: } nickjillings@1392: this.stateIndex = -1; nickjillings@1392: var that = this; nickjillings@1392: var aH_pId = 0; nickjillings@1392: for (var id=0; id= this.stateMap.length) { nickjillings@1392: console.log('Test Completed'); nickjillings@1392: createProjectSave(specification.projectReturn); nickjillings@1392: } else { nickjillings@1392: this.currentStateMap = this.stateMap[this.stateIndex]; nickjillings@1392: if (this.currentStateMap.type == "audioHolder") { nickjillings@1392: console.log('Loading test page'); nickjillings@1392: interfaceContext.newPage(this.currentStateMap); nickjillings@1392: this.initialiseInnerState(this.currentStateMap); nickjillings@1392: } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") { nickjillings@1392: if (this.currentStateMap.options.length >= 1) { nickjillings@1392: popup.initState(this.currentStateMap); nickjillings@1392: } else { nickjillings@1392: this.advanceState(); nickjillings@1392: } nickjillings@1392: } else { nickjillings@1392: this.advanceState(); nickjillings@1392: } nickjillings@1392: } nickjillings@1392: } else { nickjillings@1392: this.advanceInnerState(); nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.testPageCompleted = function(store, testXML, testId) { nickjillings@1392: // Function called each time a test page has been completed nickjillings@1392: var metric = document.createElement('metric'); nickjillings@1392: if (audioEngineContext.metric.enableTestTimer) nickjillings@1392: { nickjillings@1392: var testTime = document.createElement('metricResult'); nickjillings@1392: testTime.id = 'testTime'; nickjillings@1392: testTime.textContent = audioEngineContext.timer.testDuration; nickjillings@1392: metric.appendChild(testTime); nickjillings@1392: } nickjillings@1392: store.appendChild(metric); nickjillings@1392: var audioObjects = audioEngineContext.audioObjects; nickjillings@1392: for (var i=0; i= this.currentStateMap.length) { nickjillings@1392: this.currentIndex = null; nickjillings@1392: this.currentStateMap = this.stateMap[this.stateIndex]; nickjillings@1392: this.advanceState(); nickjillings@1392: } else { nickjillings@1392: if (this.currentStateMap[this.currentIndex].type == "audioHolder") { nickjillings@1392: console.log("Loading test page"+this.currentTestId); nickjillings@1392: } else if (this.currentStateMap[this.currentIndex].type == "pretest") { nickjillings@1392: popup.initState(this.currentStateMap[this.currentIndex]); nickjillings@1392: } else if (this.currentStateMap[this.currentIndex].type == "posttest") { nickjillings@1392: popup.initState(this.currentStateMap[this.currentIndex]); nickjillings@1392: } else { nickjillings@1392: this.advanceInnerState(); nickjillings@1392: } nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.previousState = function(){}; nickjillings@1392: } nickjillings@1392: nickjillings@1392: function AudioEngine(specification) { nickjillings@1392: nickjillings@1392: // Create two output paths, the main outputGain and fooGain. nickjillings@1392: // Output gain is default to 1 and any items for playback route here nickjillings@1392: // Foo gain is used for analysis to ensure paths get processed, but are not heard nickjillings@1392: // because web audio will optimise and any route which does not go to the destination gets ignored. nickjillings@1392: this.outputGain = audioContext.createGain(); nickjillings@1392: this.fooGain = audioContext.createGain(); nickjillings@1392: this.fooGain.gain = 0; nickjillings@1392: nickjillings@1392: // Use this to detect playback state: 0 - stopped, 1 - playing nickjillings@1392: this.status = 0; nickjillings@1392: nickjillings@1392: // Connect both gains to output nickjillings@1392: this.outputGain.connect(audioContext.destination); nickjillings@1392: this.fooGain.connect(audioContext.destination); nickjillings@1392: nickjillings@1392: // Create the timer Object nickjillings@1392: this.timer = new timer(); nickjillings@1392: // Create session metrics nickjillings@1392: this.metric = new sessionMetrics(this,specification); nickjillings@1392: nickjillings@1392: this.loopPlayback = false; nickjillings@1392: nickjillings@1392: // Create store for new audioObjects nickjillings@1392: this.audioObjects = []; nickjillings@1392: nickjillings@1392: this.buffers = []; nickjillings@1392: this.bufferObj = function() nickjillings@1392: { nickjillings@1392: this.url = null; nickjillings@1392: this.buffer = null; nickjillings@1392: this.xmlRequest = new XMLHttpRequest(); nickjillings@1396: this.xmlRequest.parent = this; nickjillings@1392: this.users = []; nickjillings@1392: this.getMedia = function(url) { nickjillings@1392: this.url = url; nickjillings@1392: this.xmlRequest.open('GET',this.url,true); nickjillings@1392: this.xmlRequest.responseType = 'arraybuffer'; nickjillings@1392: nickjillings@1392: var bufferObj = this; nickjillings@1392: nickjillings@1392: // Create callback to decode the data asynchronously nickjillings@1392: this.xmlRequest.onloadend = function() { nickjillings@1392: audioContext.decodeAudioData(bufferObj.xmlRequest.response, function(decodedData) { nickjillings@1392: bufferObj.buffer = decodedData; nickjillings@1392: for (var i=0; i nickjillings@1392: // DD/MM/YY nickjillings@1392: // nickjillings@1392: // nickjillings@1392: var dateTime = new Date(); nickjillings@1392: var year = document.createAttribute('year'); nickjillings@1392: var month = document.createAttribute('month'); nickjillings@1392: var day = document.createAttribute('day'); nickjillings@1392: var hour = document.createAttribute('hour'); nickjillings@1392: var minute = document.createAttribute('minute'); nickjillings@1392: var secs = document.createAttribute('secs'); nickjillings@1392: nickjillings@1392: year.nodeValue = dateTime.getFullYear(); nickjillings@1392: month.nodeValue = dateTime.getMonth()+1; nickjillings@1392: day.nodeValue = dateTime.getDate(); nickjillings@1392: hour.nodeValue = dateTime.getHours(); nickjillings@1392: minute.nodeValue = dateTime.getMinutes(); nickjillings@1392: secs.nodeValue = dateTime.getSeconds(); nickjillings@1392: nickjillings@1392: var hold = document.createElement("datetime"); nickjillings@1392: var date = document.createElement("date"); nickjillings@1392: date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue; nickjillings@1392: var time = document.createElement("time"); nickjillings@1392: time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue; nickjillings@1392: nickjillings@1392: date.setAttributeNode(year); nickjillings@1392: date.setAttributeNode(month); nickjillings@1392: date.setAttributeNode(day); nickjillings@1392: time.setAttributeNode(hour); nickjillings@1392: time.setAttributeNode(minute); nickjillings@1392: time.setAttributeNode(secs); nickjillings@1392: nickjillings@1392: hold.appendChild(date); nickjillings@1392: hold.appendChild(time); nickjillings@1392: return hold; nickjillings@1392: nickjillings@1392: } nickjillings@1392: nickjillings@1392: function Specification() { nickjillings@1392: // Handles the decoding of the project specification XML into a simple JavaScript Object. nickjillings@1392: nickjillings@1392: this.interfaceType = null; nickjillings@1392: this.commonInterface = new function() nickjillings@1392: { nickjillings@1392: this.options = []; nickjillings@1392: this.optionNode = function(input) nickjillings@1392: { nickjillings@1392: var name = input.getAttribute('name'); nickjillings@1392: this.type = name; nickjillings@1392: if(this.type == "option") nickjillings@1392: { nickjillings@1392: this.name = input.id; nickjillings@1392: } else if (this.type == "check") nickjillings@1392: { nickjillings@1392: this.check = input.id; nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.randomiseOrder = function(input) nickjillings@1392: { nickjillings@1392: // This takes an array of information and randomises the order nickjillings@1392: var N = input.length; nickjillings@1392: nickjillings@1392: var inputSequence = []; // For safety purposes: keep track of randomisation nickjillings@1392: for (var counter = 0; counter < N; ++counter) nickjillings@1392: inputSequence.push(counter) // Fill array nickjillings@1392: var inputSequenceClone = inputSequence.slice(0); nickjillings@1392: nickjillings@1392: var holdArr = []; nickjillings@1392: var outputSequence = []; nickjillings@1392: for (var n=0; n 0) { nickjillings@1392: metricCollection = metricCollection[0].getElementsByTagName('metricEnable'); nickjillings@1392: for (var i=0; i 0) { nickjillings@1392: commonInterfaceNode = commonInterfaceNode[0]; nickjillings@1392: } else { nickjillings@1392: commonInterfaceNode = undefined; nickjillings@1392: } nickjillings@1392: nickjillings@1392: this.commonInterface = new function() { nickjillings@1392: this.OptionNode = function(child) { nickjillings@1392: this.type = child.nodeName; nickjillings@1392: if (this.type == 'option') nickjillings@1392: { nickjillings@1392: this.name = child.getAttribute('name'); nickjillings@1392: } nickjillings@1392: else if (this.type == 'check') { nickjillings@1392: this.check = child.getAttribute('name'); nickjillings@1392: if (this.check == 'scalerange') { nickjillings@1392: this.min = child.getAttribute('min'); nickjillings@1392: this.max = child.getAttribute('max'); nickjillings@1392: if (this.min == null) {this.min = 1;} nickjillings@1392: else if (Number(this.min) > 1 && this.min != null) { nickjillings@1392: this.min = Number(this.min)/100; nickjillings@1392: } else { nickjillings@1392: this.min = Number(this.min); nickjillings@1392: } nickjillings@1392: if (this.max == null) {this.max = 0;} nickjillings@1392: else if (Number(this.max) > 1 && this.max != null) { nickjillings@1392: this.max = Number(this.max)/100; nickjillings@1392: } else { nickjillings@1392: this.max = Number(this.max); nickjillings@1392: } nickjillings@1392: } nickjillings@1392: } else if (this.type == 'anchor' || this.type == 'reference') { nickjillings@1392: this.value = Number(child.textContent); nickjillings@1392: this.enforce = child.getAttribute('enforce'); nickjillings@1392: if (this.enforce == 'true') {this.enforce = true;} nickjillings@1392: else {this.enforce = false;} nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: this.options = []; nickjillings@1392: if (commonInterfaceNode != undefined) { nickjillings@1392: var child = commonInterfaceNode.firstElementChild; nickjillings@1392: while (child != undefined) { nickjillings@1392: this.options.push(new this.OptionNode(child)); nickjillings@1392: child = child.nextElementSibling; nickjillings@1392: } nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: nickjillings@1392: var audioHolders = projectXML.getElementsByTagName('audioHolder'); nickjillings@1392: for (var i=0; i audioHolders.length) nickjillings@1392: { nickjillings@1392: console.log('Warning: You have specified '+audioHolders.length+' tests but requested '+this.testPages+' be completed!'); nickjillings@1392: this.testPages = audioHolders.length; nickjillings@1392: } nickjillings@1392: var aH = this.audioHolders; nickjillings@1392: this.audioHolders = []; nickjillings@1392: for (var i=0; i tag compiled nickjillings@1392: var setupNode = root.createElement("setup"); nickjillings@1392: setupNode.setAttribute('interface',this.interfaceType); nickjillings@1392: setupNode.setAttribute('projectReturn',this.projectReturn); nickjillings@1392: setupNode.setAttribute('randomiseOrder',this.randomiseOrder); nickjillings@1392: setupNode.setAttribute('collectMetrics',this.collectMetrics); nickjillings@1392: setupNode.setAttribute('testPages',this.testPages); nickjillings@1392: if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);} nickjillings@1392: nickjillings@1392: var setupPreTest = root.createElement("PreTest"); nickjillings@1392: for (var i=0; i tag nickjillings@1392: var Metric = root.createElement("Metric"); nickjillings@1392: for (var i=0; i tag nickjillings@1392: var CommonInterface = root.createElement("interface"); nickjillings@1392: for (var i=0; i tags nickjillings@1392: for (var ahIndex = 0; ahIndex < this.audioHolders.length; ahIndex++) nickjillings@1392: { nickjillings@1392: var node = this.audioHolders[ahIndex].encode(root); nickjillings@1392: root.getElementsByTagName("BrowserEvalProjectDocument")[0].appendChild(node); nickjillings@1392: } nickjillings@1392: return root; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.prepostNode = function(type) { nickjillings@1392: this.type = type; nickjillings@1392: this.options = []; nickjillings@1392: nickjillings@1392: this.OptionNode = function() { nickjillings@1392: nickjillings@1392: this.childOption = function() { nickjillings@1392: this.type = 'option'; nickjillings@1392: this.id = null; nickjillings@1392: this.name = undefined; nickjillings@1392: this.text = null; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.type = undefined; nickjillings@1392: this.id = undefined; nickjillings@1392: this.mandatory = undefined; nickjillings@1392: this.question = undefined; nickjillings@1392: this.statement = undefined; nickjillings@1392: this.boxsize = undefined; nickjillings@1392: this.options = []; nickjillings@1392: this.min = undefined; nickjillings@1392: this.max = undefined; nickjillings@1392: this.step = undefined; nickjillings@1392: nickjillings@1392: this.decode = function(child) nickjillings@1392: { nickjillings@1392: this.type = child.nodeName; nickjillings@1392: if (child.nodeName == "question") { nickjillings@1392: this.id = child.id; nickjillings@1392: this.mandatory; nickjillings@1392: if (child.getAttribute('mandatory') == "true") {this.mandatory = true;} nickjillings@1392: else {this.mandatory = false;} nickjillings@1392: this.question = child.textContent; nickjillings@1392: if (child.getAttribute('boxsize') == null) { nickjillings@1392: this.boxsize = 'normal'; nickjillings@1392: } else { nickjillings@1392: this.boxsize = child.getAttribute('boxsize'); nickjillings@1392: } nickjillings@1392: } else if (child.nodeName == "statement") { nickjillings@1392: this.statement = child.textContent; nickjillings@1392: } else if (child.nodeName == "checkbox" || child.nodeName == "radio") { nickjillings@1392: var element = child.firstElementChild; nickjillings@1392: this.id = child.id; nickjillings@1392: if (element == null) { nickjillings@1392: console.log('Malformed' +child.nodeName+ 'entry'); nickjillings@1392: this.statement = 'Malformed' +child.nodeName+ 'entry'; nickjillings@1392: this.type = 'statement'; nickjillings@1392: } else { nickjillings@1392: this.options = []; nickjillings@1392: while (element != null) { nickjillings@1392: if (element.nodeName == 'statement' && this.statement == undefined){ nickjillings@1392: this.statement = element.textContent; nickjillings@1392: } else if (element.nodeName == 'option') { nickjillings@1392: var node = new this.childOption(); nickjillings@1392: node.id = element.id; nickjillings@1392: node.name = element.getAttribute('name'); nickjillings@1392: node.text = element.textContent; nickjillings@1392: this.options.push(node); nickjillings@1392: } nickjillings@1392: element = element.nextElementSibling; nickjillings@1392: } nickjillings@1392: } nickjillings@1392: } else if (child.nodeName == "number") { nickjillings@1392: this.statement = child.textContent; nickjillings@1392: this.id = child.id; nickjillings@1392: this.min = child.getAttribute('min'); nickjillings@1392: this.max = child.getAttribute('max'); nickjillings@1392: this.step = child.getAttribute('step'); nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.exportXML = function(root) nickjillings@1392: { nickjillings@1392: var node = root.createElement(this.type); nickjillings@1392: switch(this.type) nickjillings@1392: { nickjillings@1392: case "statement": nickjillings@1392: node.textContent = this.statement; nickjillings@1392: break; nickjillings@1392: case "question": nickjillings@1392: node.id = this.id; nickjillings@1392: node.setAttribute("mandatory",this.mandatory); nickjillings@1392: node.setAttribute("boxsize",this.boxsize); nickjillings@1392: node.textContent = this.question; nickjillings@1392: break; nickjillings@1392: case "number": nickjillings@1392: node.id = this.id; nickjillings@1392: node.setAttribute("mandatory",this.mandatory); nickjillings@1392: node.setAttribute("min", this.min); nickjillings@1392: node.setAttribute("max", this.max); nickjillings@1392: node.setAttribute("step", this.step); nickjillings@1392: node.textContent = this.statement; nickjillings@1392: break; nickjillings@1392: case "checkbox": nickjillings@1392: node.id = this.id; nickjillings@1392: var statement = root.createElement("statement"); nickjillings@1392: statement.textContent = this.statement; nickjillings@1392: node.appendChild(statement); nickjillings@1392: for (var i=0; i 1) nickjillings@1395: { nickjillings@1395: xmlInitialPosition /= 100; nickjillings@1395: } nickjillings@1395: this.initialPosition = xmlInitialPosition; nickjillings@1395: } nickjillings@1395: } nickjillings@1392: if (xml.getAttribute('loudness') != null) nickjillings@1392: { nickjillings@1392: var XMLloudness = xml.getAttribute('loudness'); nickjillings@1392: if (isNaN(Number(XMLloudness)) == false) nickjillings@1392: { nickjillings@1392: this.loudness = Number(XMLloudness); nickjillings@1392: } nickjillings@1392: } nickjillings@1392: var setupPreTestNode = xml.getElementsByTagName('PreTest'); nickjillings@1392: if (setupPreTestNode.length != 0) nickjillings@1392: { nickjillings@1392: setupPreTestNode = setupPreTestNode[0]; nickjillings@1392: this.preTest.construct(setupPreTestNode); nickjillings@1392: } nickjillings@1392: nickjillings@1392: var setupPostTestNode = xml.getElementsByTagName('PostTest'); nickjillings@1392: if (setupPostTestNode.length != 0) nickjillings@1392: { nickjillings@1392: setupPostTestNode = setupPostTestNode[0]; nickjillings@1392: this.postTest.construct(setupPostTestNode); nickjillings@1392: } nickjillings@1392: nickjillings@1392: var interfaceDOM = xml.getElementsByTagName('interface'); nickjillings@1392: for (var i=0; i nickjillings@1392: for (var i=0; i nickjillings@1392: var AHPreTest = root.createElement("PreTest"); nickjillings@1392: for (var i=0; i 1) nickjillings@1392: { this.marker /= 100.0;} nickjillings@1392: if (this.marker >= 0 && this.marker <= 1) nickjillings@1392: { nickjillings@1392: this.enforce = true; nickjillings@1392: return; nickjillings@1392: } else { nickjillings@1392: console.log("ERROR - Marker of audioElement "+this.id+" is not between 0 and 1 (float) or 0 and 100 (integer)!"); nickjillings@1392: console.log("ERROR - Marker not enforced!"); nickjillings@1392: } nickjillings@1392: } else { nickjillings@1392: console.log("ERROR - Marker of audioElement "+this.id+" is not a number!"); nickjillings@1392: console.log("ERROR - Marker not enforced!"); nickjillings@1392: } nickjillings@1392: } nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: this.encode = function(root) nickjillings@1392: { nickjillings@1392: var AENode = root.createElement("audioElements"); nickjillings@1392: AENode.id = this.id; nickjillings@1392: AENode.setAttribute("url",this.url); nickjillings@1392: AENode.setAttribute("type",this.type); nickjillings@1392: AENode.setAttribute("gain",linearToDecibel(this.gain)); nickjillings@1392: if (this.marker != false) nickjillings@1392: { nickjillings@1392: AENode.setAttribute("marker",this.marker*100); nickjillings@1392: } nickjillings@1392: return AENode; nickjillings@1392: }; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.commentQuestionNode = function(xml) { nickjillings@1392: this.id = null; nickjillings@1392: this.type = undefined; nickjillings@1392: this.question = undefined; nickjillings@1392: this.options = []; nickjillings@1392: this.statement = undefined; nickjillings@1392: nickjillings@1392: this.childOption = function() { nickjillings@1392: this.type = 'option'; nickjillings@1392: this.name = null; nickjillings@1392: this.text = null; nickjillings@1392: }; nickjillings@1392: this.exportXML = function(root) nickjillings@1392: { nickjillings@1392: var CQNode = root.createElement("CommentQuestion"); nickjillings@1392: CQNode.id = this.id; nickjillings@1392: CQNode.setAttribute("type",this.type); nickjillings@1392: switch(this.type) nickjillings@1392: { nickjillings@1392: case "text": nickjillings@1392: CQNode.textContent = this.question; nickjillings@1392: break; nickjillings@1392: case "radio": nickjillings@1392: var statement = root.createElement("statement"); nickjillings@1392: statement.textContent = this.statement; nickjillings@1392: CQNode.appendChild(statement); nickjillings@1392: for (var i=0; i tag. nickjillings@1392: this.interfaceObjects = []; nickjillings@1392: this.interfaceObject = function(){}; nickjillings@1392: nickjillings@1392: this.resizeWindow = function(event) nickjillings@1392: { nickjillings@1392: popup.resize(event); nickjillings@1392: for(var i=0; i= 600) nickjillings@1392: { nickjillings@1392: boxwidth = 600; nickjillings@1392: } nickjillings@1392: else if (boxwidth < 400) nickjillings@1392: { nickjillings@1392: boxwidth = 400; nickjillings@1392: } nickjillings@1392: this.trackComment.style.width = boxwidth+"px"; nickjillings@1392: this.trackCommentBox.style.width = boxwidth-6+"px"; nickjillings@1392: }; nickjillings@1392: this.resize(); nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.commentQuestions = []; nickjillings@1392: nickjillings@1392: this.commentBox = function(commentQuestion) { nickjillings@1392: this.specification = commentQuestion; nickjillings@1392: // Create document objects to hold the comment boxes nickjillings@1392: this.holder = document.createElement('div'); nickjillings@1392: this.holder.className = 'comment-div'; nickjillings@1392: // Create a string next to each comment asking for a comment nickjillings@1392: this.string = document.createElement('span'); nickjillings@1392: this.string.innerHTML = commentQuestion.question; nickjillings@1392: // Create the HTML5 comment box 'textarea' nickjillings@1392: this.textArea = document.createElement('textarea'); nickjillings@1392: this.textArea.rows = '4'; nickjillings@1392: this.textArea.cols = '100'; nickjillings@1392: this.textArea.className = 'trackComment'; nickjillings@1392: var br = document.createElement('br'); nickjillings@1392: // Add to the holder. nickjillings@1392: this.holder.appendChild(this.string); nickjillings@1392: this.holder.appendChild(br); nickjillings@1392: this.holder.appendChild(this.textArea); nickjillings@1392: nickjillings@1392: this.exportXMLDOM = function() { nickjillings@1392: var root = document.createElement('comment'); nickjillings@1392: root.id = this.specification.id; nickjillings@1392: root.setAttribute('type',this.specification.type); nickjillings@1392: root.textContent = this.textArea.value; nickjillings@1392: console.log("Question: "+this.string.textContent); nickjillings@1392: console.log("Response: "+root.textContent); nickjillings@1392: return root; nickjillings@1392: }; nickjillings@1392: this.resize = function() nickjillings@1392: { nickjillings@1392: var boxwidth = (window.innerWidth-100)/2; nickjillings@1392: if (boxwidth >= 600) nickjillings@1392: { nickjillings@1392: boxwidth = 600; nickjillings@1392: } nickjillings@1392: else if (boxwidth < 400) nickjillings@1392: { nickjillings@1392: boxwidth = 400; nickjillings@1392: } nickjillings@1392: this.holder.style.width = boxwidth+"px"; nickjillings@1392: this.textArea.style.width = boxwidth-6+"px"; nickjillings@1392: }; nickjillings@1392: this.resize(); nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.radioBox = function(commentQuestion) { nickjillings@1392: this.specification = commentQuestion; nickjillings@1392: // Create document objects to hold the comment boxes nickjillings@1392: this.holder = document.createElement('div'); nickjillings@1392: this.holder.className = 'comment-div'; nickjillings@1392: // Create a string next to each comment asking for a comment nickjillings@1392: this.string = document.createElement('span'); nickjillings@1392: this.string.innerHTML = commentQuestion.statement; nickjillings@1392: var br = document.createElement('br'); nickjillings@1392: // Add to the holder. nickjillings@1392: this.holder.appendChild(this.string); nickjillings@1392: this.holder.appendChild(br); nickjillings@1392: this.options = []; nickjillings@1392: this.inputs = document.createElement('div'); nickjillings@1392: this.span = document.createElement('div'); nickjillings@1392: this.inputs.align = 'center'; nickjillings@1392: this.inputs.style.marginLeft = '12px'; nickjillings@1392: this.span.style.marginLeft = '12px'; nickjillings@1392: this.span.align = 'center'; nickjillings@1392: this.span.style.marginTop = '15px'; nickjillings@1392: nickjillings@1392: var optCount = commentQuestion.options.length; nickjillings@1392: for (var i=0; i= this.options.length) { nickjillings@1392: break; nickjillings@1392: } nickjillings@1392: } nickjillings@1392: if (i >= this.options.length) { nickjillings@1392: response.textContent = 'null'; nickjillings@1392: } else { nickjillings@1392: response.textContent = this.options[i].getAttribute('setvalue'); nickjillings@1392: response.setAttribute('number',i); nickjillings@1392: } nickjillings@1392: console.log('Comment: '+question.textContent); nickjillings@1392: console.log('Response: '+response.textContent); nickjillings@1392: root.appendChild(question); nickjillings@1392: root.appendChild(response); nickjillings@1392: return root; nickjillings@1392: }; nickjillings@1392: this.resize = function() nickjillings@1392: { nickjillings@1392: var boxwidth = (window.innerWidth-100)/2; nickjillings@1392: if (boxwidth >= 600) nickjillings@1392: { nickjillings@1392: boxwidth = 600; nickjillings@1392: } nickjillings@1392: else if (boxwidth < 400) nickjillings@1392: { nickjillings@1392: boxwidth = 400; nickjillings@1392: } nickjillings@1392: this.holder.style.width = boxwidth+"px"; nickjillings@1392: var text = this.holder.children[2]; nickjillings@1392: var options = this.holder.children[3]; nickjillings@1392: var optCount = options.children.length; nickjillings@1392: var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px'; nickjillings@1392: var options = options.firstChild; nickjillings@1392: var text = text.firstChild; nickjillings@1392: options.style.marginRight = spanMargin; nickjillings@1392: options.style.marginLeft = spanMargin; nickjillings@1392: text.style.marginRight = spanMargin; nickjillings@1392: text.style.marginLeft = spanMargin; nickjillings@1392: while(options.nextSibling != undefined) nickjillings@1392: { nickjillings@1392: options = options.nextSibling; nickjillings@1392: text = text.nextSibling; nickjillings@1392: options.style.marginRight = spanMargin; nickjillings@1392: options.style.marginLeft = spanMargin; nickjillings@1392: text.style.marginRight = spanMargin; nickjillings@1392: text.style.marginLeft = spanMargin; nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: this.resize(); nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.checkboxBox = function(commentQuestion) { nickjillings@1392: this.specification = commentQuestion; nickjillings@1392: // Create document objects to hold the comment boxes nickjillings@1392: this.holder = document.createElement('div'); nickjillings@1392: this.holder.className = 'comment-div'; nickjillings@1392: // Create a string next to each comment asking for a comment nickjillings@1392: this.string = document.createElement('span'); nickjillings@1392: this.string.innerHTML = commentQuestion.statement; nickjillings@1392: var br = document.createElement('br'); nickjillings@1392: // Add to the holder. nickjillings@1392: this.holder.appendChild(this.string); nickjillings@1392: this.holder.appendChild(br); nickjillings@1392: this.options = []; nickjillings@1392: this.inputs = document.createElement('div'); nickjillings@1392: this.span = document.createElement('div'); nickjillings@1392: this.inputs.align = 'center'; nickjillings@1392: this.inputs.style.marginLeft = '12px'; nickjillings@1392: this.span.style.marginLeft = '12px'; nickjillings@1392: this.span.align = 'center'; nickjillings@1392: this.span.style.marginTop = '15px'; nickjillings@1392: nickjillings@1392: var optCount = commentQuestion.options.length; nickjillings@1392: for (var i=0; i= 600) nickjillings@1392: { nickjillings@1392: boxwidth = 600; nickjillings@1392: } nickjillings@1392: else if (boxwidth < 400) nickjillings@1392: { nickjillings@1392: boxwidth = 400; nickjillings@1392: } nickjillings@1392: this.holder.style.width = boxwidth+"px"; nickjillings@1392: var text = this.holder.children[2]; nickjillings@1392: var options = this.holder.children[3]; nickjillings@1392: var optCount = options.children.length; nickjillings@1392: var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px'; nickjillings@1392: var options = options.firstChild; nickjillings@1392: var text = text.firstChild; nickjillings@1392: options.style.marginRight = spanMargin; nickjillings@1392: options.style.marginLeft = spanMargin; nickjillings@1392: text.style.marginRight = spanMargin; nickjillings@1392: text.style.marginLeft = spanMargin; nickjillings@1392: while(options.nextSibling != undefined) nickjillings@1392: { nickjillings@1392: options = options.nextSibling; nickjillings@1392: text = text.nextSibling; nickjillings@1392: options.style.marginRight = spanMargin; nickjillings@1392: options.style.marginLeft = spanMargin; nickjillings@1392: text.style.marginRight = spanMargin; nickjillings@1392: text.style.marginLeft = spanMargin; nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: this.resize(); nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.createCommentBox = function(audioObject) { nickjillings@1392: var node = new this.elementCommentBox(audioObject); nickjillings@1392: this.commentBoxes.push(node); nickjillings@1392: audioObject.commentDOM = node; nickjillings@1392: return node; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.sortCommentBoxes = function() { nickjillings@1392: var holder = []; nickjillings@1392: while (this.commentBoxes.length > 0) { nickjillings@1392: var node = this.commentBoxes.pop(0); nickjillings@1392: holder[node.id] = node; nickjillings@1392: } nickjillings@1392: this.commentBoxes = holder; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.showCommentBoxes = function(inject, sort) { nickjillings@1392: if (sort) {interfaceContext.sortCommentBoxes();} nickjillings@1392: for (var i=0; i 0) { nickjillings@1392: var time = this.playbackObject.getCurrentPosition(); nickjillings@1392: if (time > 0) { nickjillings@1392: var width = 490; nickjillings@1392: var pix = Math.floor(time/this.timePerPixel); nickjillings@1392: this.scrubberHead.style.left = pix+'px'; nickjillings@1392: if (this.maxTime > 60.0) { nickjillings@1392: var secs = time%60; nickjillings@1392: var mins = Math.floor((time-secs)/60); nickjillings@1392: secs = secs.toString(); nickjillings@1392: secs = secs.substr(0,2); nickjillings@1392: mins = mins.toString(); nickjillings@1392: this.curTimeSpan.textContent = mins+':'+secs; nickjillings@1392: } else { nickjillings@1392: time = time.toString(); nickjillings@1392: this.curTimeSpan.textContent = time.substr(0,4); nickjillings@1392: } nickjillings@1392: } else { nickjillings@1392: this.scrubberHead.style.left = '0px'; nickjillings@1392: if (this.maxTime < 60) { nickjillings@1392: this.curTimeSpan.textContent = '0.00'; nickjillings@1392: } else { nickjillings@1392: this.curTimeSpan.textContent = '00:00'; nickjillings@1392: } nickjillings@1392: } nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.interval = undefined; nickjillings@1392: nickjillings@1392: this.start = function() { nickjillings@1392: if (this.playbackObject != undefined && this.interval == undefined) { nickjillings@1392: if (this.maxTime < 60) { nickjillings@1392: this.interval = setInterval(function(){interfaceContext.playhead.update();},10); nickjillings@1392: } else { nickjillings@1392: this.interval = setInterval(function(){interfaceContext.playhead.update();},100); nickjillings@1392: } nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: this.stop = function() { nickjillings@1392: clearInterval(this.interval); nickjillings@1392: this.interval = undefined; nickjillings@1392: if (this.maxTime < 60) { nickjillings@1392: this.curTimeSpan.textContent = '0.00'; nickjillings@1392: } else { nickjillings@1392: this.curTimeSpan.textContent = '00:00'; nickjillings@1392: } nickjillings@1392: }; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: // Global Checkers nickjillings@1392: // These functions will help enforce the checkers nickjillings@1392: this.checkHiddenAnchor = function() nickjillings@1392: { nickjillings@1392: var audioHolder = testState.currentStateMap[testState.currentIndex]; nickjillings@1392: if (audioHolder.anchorId != null) nickjillings@1392: { nickjillings@1392: var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId]; nickjillings@1392: if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true) nickjillings@1392: { nickjillings@1392: // Anchor is not set below nickjillings@1392: console.log('Anchor node not below marker value'); nickjillings@1392: alert('Please keep listening'); nickjillings@1392: return false; nickjillings@1392: } nickjillings@1392: } nickjillings@1392: return true; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.checkHiddenReference = function() nickjillings@1392: { nickjillings@1392: var audioHolder = testState.currentStateMap[testState.currentIndex]; nickjillings@1392: if (audioHolder.referenceId != null) nickjillings@1392: { nickjillings@1392: var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId]; nickjillings@1392: if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true) nickjillings@1392: { nickjillings@1392: // Anchor is not set below nickjillings@1392: console.log('Reference node not above marker value'); nickjillings@1392: alert('Please keep listening'); nickjillings@1392: return false; nickjillings@1392: } nickjillings@1392: } nickjillings@1392: return true; nickjillings@1392: }; nickjillings@1392: nickjillings@1392: this.checkFragmentsFullyPlayed = function () nickjillings@1392: { nickjillings@1392: // Checks the entire file has been played back nickjillings@1392: // NOTE ! This will return true IF playback is Looped!!! nickjillings@1392: if (audioEngineContext.loopPlayback) nickjillings@1392: { nickjillings@1392: console.log("WARNING - Looped source: Cannot check fragments are fully played"); nickjillings@1392: return true; nickjillings@1392: } nickjillings@1392: var check_pass = true; nickjillings@1392: var error_obj = []; nickjillings@1392: for (var i = 0; i= time) nickjillings@1392: { nickjillings@1392: passed = true; nickjillings@1392: break; nickjillings@1392: } nickjillings@1392: } nickjillings@1392: if (passed == false) nickjillings@1392: { nickjillings@1392: check_pass = false; nickjillings@1392: console.log("Continue listening to track-"+i); nickjillings@1392: error_obj.push(i); nickjillings@1392: } nickjillings@1392: } nickjillings@1392: if (check_pass == false) nickjillings@1392: { nickjillings@1393: var str_start = "You have not completely listened to fragments "; nickjillings@1392: for (var i=0; i