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