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