nicholas@2224: /** nicholas@2224: * core.js nicholas@2224: * nicholas@2224: * Main script to run, calls all other core functions and manages loading/store to backend. nicholas@2224: * Also contains all global variables. nicholas@2224: */ nicholas@2224: nicholas@2224: /* create the web audio API context and store in audioContext*/ nicholas@2224: var audioContext; // Hold the browser web audio API nicholas@2224: var projectXML; // Hold the parsed setup XML nicholas@2224: var schemaXSD; // Hold the parsed schema XSD nicholas@2224: var specification; nicholas@2224: var interfaceContext; nicholas@2224: var storage; nicholas@2224: var popup; // Hold the interfacePopup object nicholas@2224: var testState; nicholas@2224: var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order nicholas@2224: var audioEngineContext; // The custome AudioEngine object nicholas@2224: var projectReturn; // Hold the URL for the return nicholas@2224: nicholas@2224: nicholas@2224: // Add a prototype to the bufferSourceNode to reference to the audioObject holding it nicholas@2224: AudioBufferSourceNode.prototype.owner = undefined; nicholas@2224: // Add a prototype to the bufferSourceNode to hold when the object was given a play command nicholas@2224: AudioBufferSourceNode.prototype.playbackStartTime = undefined; nicholas@2224: // Add a prototype to the bufferNode to hold the desired LINEAR gain nicholas@2224: AudioBuffer.prototype.playbackGain = undefined; nicholas@2224: // Add a prototype to the bufferNode to hold the computed LUFS loudness nicholas@2224: AudioBuffer.prototype.lufs = undefined; nicholas@2224: nicholas@2224: // Convert relative URLs into absolutes nicholas@2224: function escapeHTML(s) { nicholas@2224: return s.split('&').join('&').split('<').join('<').split('"').join('"'); nicholas@2224: } nicholas@2224: function qualifyURL(url) { nicholas@2224: var el= document.createElement('div'); nicholas@2224: el.innerHTML= 'x'; nicholas@2224: return el.firstChild.href; nicholas@2224: } nicholas@2224: nicholas@2224: // Firefox does not have an XMLDocument.prototype.getElementsByName nicholas@2224: // and there is no searchAll style command, this custom function will nicholas@2224: // search all children recusrively for the name. Used for XSD where all nicholas@2224: // element nodes must have a name and therefore can pull the schema node nicholas@2224: XMLDocument.prototype.getAllElementsByName = function(name) nicholas@2224: { nicholas@2224: name = String(name); nicholas@2224: var selected = this.documentElement.getAllElementsByName(name); nicholas@2224: return selected; nicholas@2224: } nicholas@2224: nicholas@2224: Element.prototype.getAllElementsByName = function(name) nicholas@2224: { nicholas@2224: name = String(name); nicholas@2224: var selected = []; nicholas@2224: var node = this.firstElementChild; nicholas@2224: while(node != null) nicholas@2224: { nicholas@2224: if (node.getAttribute('name') == name) nicholas@2224: { nicholas@2224: selected.push(node); nicholas@2224: } nicholas@2224: if (node.childElementCount > 0) nicholas@2224: { nicholas@2224: selected = selected.concat(node.getAllElementsByName(name)); nicholas@2224: } nicholas@2224: node = node.nextElementSibling; nicholas@2224: } nicholas@2224: return selected; nicholas@2224: } nicholas@2224: nicholas@2224: XMLDocument.prototype.getAllElementsByTagName = function(name) nicholas@2224: { nicholas@2224: name = String(name); nicholas@2224: var selected = this.documentElement.getAllElementsByTagName(name); nicholas@2224: return selected; nicholas@2224: } nicholas@2224: nicholas@2224: Element.prototype.getAllElementsByTagName = function(name) nicholas@2224: { nicholas@2224: name = String(name); nicholas@2224: var selected = []; nicholas@2224: var node = this.firstElementChild; nicholas@2224: while(node != null) nicholas@2224: { nicholas@2224: if (node.nodeName == name) nicholas@2224: { nicholas@2224: selected.push(node); nicholas@2224: } nicholas@2224: if (node.childElementCount > 0) nicholas@2224: { nicholas@2224: selected = selected.concat(node.getAllElementsByTagName(name)); nicholas@2224: } nicholas@2224: node = node.nextElementSibling; nicholas@2224: } nicholas@2224: return selected; nicholas@2224: } nicholas@2224: nicholas@2224: // Firefox does not have an XMLDocument.prototype.getElementsByName nicholas@2224: if (typeof XMLDocument.prototype.getElementsByName != "function") { nicholas@2224: XMLDocument.prototype.getElementsByName = function(name) nicholas@2224: { nicholas@2224: name = String(name); nicholas@2224: var node = this.documentElement.firstElementChild; nicholas@2224: var selected = []; nicholas@2224: while(node != null) nicholas@2224: { nicholas@2224: if (node.getAttribute('name') == name) nicholas@2224: { nicholas@2224: selected.push(node); nicholas@2224: } nicholas@2224: node = node.nextElementSibling; nicholas@2224: } nicholas@2224: return selected; nicholas@2224: } nicholas@2224: } nicholas@2224: nicholas@2224: window.onload = function() { nicholas@2224: // Function called once the browser has loaded all files. nicholas@2224: // This should perform any initial commands such as structure / loading documents nicholas@2224: nicholas@2224: // Create a web audio API context nicholas@2224: // Fixed for cross-browser support nicholas@2224: var AudioContext = window.AudioContext || window.webkitAudioContext; nicholas@2224: audioContext = new AudioContext; nicholas@2224: nicholas@2224: // Create test state nicholas@2224: testState = new stateMachine(); nicholas@2224: nicholas@2224: // Create the popup interface object nicholas@2224: popup = new interfacePopup(); nicholas@2224: nicholas@2224: // Create the specification object nicholas@2224: specification = new Specification(); nicholas@2224: nicholas@2224: // Create the interface object nicholas@2224: interfaceContext = new Interface(specification); nicholas@2224: nicholas@2224: // Create the storage object nicholas@2224: storage = new Storage(); nicholas@2224: // Define window callbacks for interface nicholas@2224: window.onresize = function(event){interfaceContext.resizeWindow(event);}; nicholas@2224: }; nicholas@2224: nicholas@2224: function loadProjectSpec(url) { nicholas@2224: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data nicholas@2224: // If url is null, request client to upload project XML document nicholas@2224: var xmlhttp = new XMLHttpRequest(); nicholas@2224: xmlhttp.open("GET",'xml/test-schema.xsd',true); nicholas@2224: xmlhttp.onload = function() nicholas@2224: { nicholas@2224: schemaXSD = xmlhttp.response; nicholas@2224: var parse = new DOMParser(); nicholas@2224: specification.schema = parse.parseFromString(xmlhttp.response,'text/xml'); nicholas@2224: var r = new XMLHttpRequest(); nicholas@2224: r.open('GET',url,true); nicholas@2224: r.onload = function() { nicholas@2224: loadProjectSpecCallback(r.response); nicholas@2224: }; nicholas@2224: r.onerror = function() { nicholas@2224: document.getElementsByTagName('body')[0].innerHTML = null; nicholas@2224: var msg = document.createElement("h3"); nicholas@2224: msg.textContent = "FATAL ERROR"; nicholas@2224: var span = document.createElement("p"); nicholas@2224: span.textContent = "There was an error when loading your XML file. Please check your path in the URL. After the path to this page, there should be '?url=path/to/your/file.xml'. Check the spelling of your filename as well. If you are still having issues, check the log of the python server or your webserver distribution for 404 codes for your file."; nicholas@2224: document.getElementsByTagName('body')[0].appendChild(msg); nicholas@2224: document.getElementsByTagName('body')[0].appendChild(span); nicholas@2224: } nicholas@2224: r.send(); nicholas@2224: }; nicholas@2224: xmlhttp.send(); nicholas@2224: }; nicholas@2224: nicholas@2224: function loadProjectSpecCallback(response) { nicholas@2224: // Function called after asynchronous download of XML project specification nicholas@2224: //var decode = $.parseXML(response); nicholas@2224: //projectXML = $(decode); nicholas@2224: nicholas@2224: // Check if XML is new or a resumption nicholas@2224: var parse = new DOMParser(); nicholas@2224: var responseDocument = parse.parseFromString(response,'text/xml'); nicholas@2224: var errorNode = responseDocument.getElementsByTagName('parsererror'); nicholas@2224: if (errorNode.length >= 1) nicholas@2224: { nicholas@2224: var msg = document.createElement("h3"); nicholas@2224: msg.textContent = "FATAL ERROR"; nicholas@2224: var span = document.createElement("span"); nicholas@2224: span.textContent = "The XML parser returned the following errors when decoding your XML file"; nicholas@2224: document.getElementsByTagName('body')[0].innerHTML = null; nicholas@2224: document.getElementsByTagName('body')[0].appendChild(msg); nicholas@2224: document.getElementsByTagName('body')[0].appendChild(span); nicholas@2224: document.getElementsByTagName('body')[0].appendChild(errorNode[0]); nicholas@2224: return; nicholas@2224: } nicholas@2247: if (responseDocument == undefined || responseDocument.firstChild == undefined) { nicholas@2224: var msg = document.createElement("h3"); nicholas@2224: msg.textContent = "FATAL ERROR"; nicholas@2224: var span = document.createElement("span"); nicholas@2224: span.textContent = "The project XML was not decoded properly, try refreshing your browser and clearing caches. If the problem persists, contact the test creator."; nicholas@2224: document.getElementsByTagName('body')[0].innerHTML = null; nicholas@2224: document.getElementsByTagName('body')[0].appendChild(msg); nicholas@2224: document.getElementsByTagName('body')[0].appendChild(span); nicholas@2224: return; nicholas@2224: } nicholas@2247: if (responseDocument.firstChild.nodeName == "waet") { nicholas@2224: // document is a specification nicholas@2224: nicholas@2224: // Perform XML schema validation nicholas@2224: var Module = { nicholas@2224: xml: response, nicholas@2224: schema: schemaXSD, nicholas@2224: arguments:["--noout", "--schema", 'test-schema.xsd','document.xml'] nicholas@2224: }; nicholas@2224: projectXML = responseDocument; nicholas@2224: var xmllint = validateXML(Module); nicholas@2224: console.log(xmllint); nicholas@2224: if(xmllint != 'document.xml validates\n') nicholas@2224: { nicholas@2224: document.getElementsByTagName('body')[0].innerHTML = null; nicholas@2224: var msg = document.createElement("h3"); nicholas@2224: msg.textContent = "FATAL ERROR"; nicholas@2224: var span = document.createElement("h4"); nicholas@2224: span.textContent = "The XML validator returned the following errors when decoding your XML file"; nicholas@2224: document.getElementsByTagName('body')[0].appendChild(msg); nicholas@2224: document.getElementsByTagName('body')[0].appendChild(span); nicholas@2224: xmllint = xmllint.split('\n'); nicholas@2224: for (var i in xmllint) nicholas@2224: { nicholas@2224: document.getElementsByTagName('body')[0].appendChild(document.createElement('br')); nicholas@2224: var span = document.createElement("span"); nicholas@2224: span.textContent = xmllint[i]; nicholas@2224: document.getElementsByTagName('body')[0].appendChild(span); nicholas@2224: } nicholas@2224: return; nicholas@2224: } nicholas@2224: // Build the specification nicholas@2224: specification.decode(projectXML); nicholas@2224: // Generate the session-key nicholas@2224: storage.initialise(); nicholas@2224: nicholas@2247: } else if (responseDocument.firstChild.nodeName == "waetresult") { nicholas@2224: // document is a result nicholas@2224: projectXML = document.implementation.createDocument(null,"waet"); nicholas@2224: projectXML.children[0].appendChild(responseDocument.getElementsByTagName('waet')[0].getElementsByTagName("setup")[0].cloneNode(true)); nicholas@2224: var child = responseDocument.children[0].children[0]; nicholas@2224: while (child != null) { nicholas@2224: if (child.nodeName == "survey") { nicholas@2224: // One of the global survey elements nicholas@2224: if (child.getAttribute("state") == "complete") { nicholas@2224: // We need to remove this survey from nicholas@2224: var location = child.getAttribute("location"); nicholas@2224: var globalSurveys = projectXML.getElementsByTagName("setup")[0].getElementsByTagName("survey")[0]; nicholas@2224: while(globalSurveys != null) { nicholas@2224: if (location == "pre" || location == "before") { nicholas@2224: if (globalSurveys.getAttribute("location") == "pre" || globalSurveys.getAttribute("location") == "before") { nicholas@2224: projectXML.getElementsByTagName("setup")[0].removeChild(globalSurveys); nicholas@2224: break; nicholas@2224: } nicholas@2224: } else { nicholas@2224: if (globalSurveys.getAttribute("location") == "post" || globalSurveys.getAttribute("location") == "after") { nicholas@2224: projectXML.getElementsByTagName("setup")[0].removeChild(globalSurveys); nicholas@2224: break; nicholas@2224: } nicholas@2224: } nicholas@2224: globalSurveys = globalSurveys.nextElementSibling; nicholas@2224: } nicholas@2224: } else { nicholas@2224: // We need to complete this, so it must be regenerated by store nicholas@2224: var copy = child; nicholas@2224: child = child.previousElementSibling; nicholas@2224: responseDocument.children[0].removeChild(copy); nicholas@2224: } nicholas@2224: } else if (child.nodeName == "page") { nicholas@2224: if (child.getAttribute("state") == "empty") { nicholas@2224: // We need to complete this page nicholas@2224: projectXML.children[0].appendChild(responseDocument.getElementById(child.getAttribute("ref")).cloneNode(true)); nicholas@2224: var copy = child; nicholas@2224: child = child.previousElementSibling; nicholas@2224: responseDocument.children[0].removeChild(copy); nicholas@2224: } nicholas@2224: } nicholas@2224: child = child.nextElementSibling; nicholas@2224: } nicholas@2224: // Build the specification nicholas@2224: specification.decode(projectXML); nicholas@2224: // Use the original nicholas@2224: storage.initialise(responseDocument); nicholas@2224: } nicholas@2224: /// CHECK FOR SAMPLE RATE COMPATIBILITY nicholas@2224: if (specification.sampleRate != undefined) { nicholas@2224: if (Number(specification.sampleRate) != audioContext.sampleRate) { nicholas@2224: var errStr = 'Sample rates do not match! Requested '+Number(specification.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.'; nicholas@2224: alert(errStr); nicholas@2224: return; nicholas@2224: } nicholas@2224: } nicholas@2224: nicholas@2224: // Detect the interface to use and load the relevant javascripts. nicholas@2224: var interfaceJS = document.createElement('script'); nicholas@2224: interfaceJS.setAttribute("type","text/javascript"); nicholas@2224: switch(specification.interface) nicholas@2224: { nicholas@2224: case "APE": nicholas@2224: interfaceJS.setAttribute("src","interfaces/ape.js"); nicholas@2224: nicholas@2224: // APE comes with a css file nicholas@2224: var css = document.createElement('link'); nicholas@2224: css.rel = 'stylesheet'; nicholas@2224: css.type = 'text/css'; nicholas@2224: css.href = 'interfaces/ape.css'; nicholas@2224: nicholas@2224: document.getElementsByTagName("head")[0].appendChild(css); nicholas@2224: break; nicholas@2224: nicholas@2224: case "MUSHRA": nicholas@2224: interfaceJS.setAttribute("src","interfaces/mushra.js"); nicholas@2224: nicholas@2224: // MUSHRA comes with a css file nicholas@2224: var css = document.createElement('link'); nicholas@2224: css.rel = 'stylesheet'; nicholas@2224: css.type = 'text/css'; nicholas@2224: css.href = 'interfaces/mushra.css'; nicholas@2224: nicholas@2224: document.getElementsByTagName("head")[0].appendChild(css); nicholas@2224: break; nicholas@2224: nicholas@2224: case "AB": nicholas@2224: interfaceJS.setAttribute("src","interfaces/AB.js"); nicholas@2224: nicholas@2224: // AB comes with a css file nicholas@2224: var css = document.createElement('link'); nicholas@2224: css.rel = 'stylesheet'; nicholas@2224: css.type = 'text/css'; nicholas@2224: css.href = 'interfaces/AB.css'; nicholas@2224: nicholas@2224: document.getElementsByTagName("head")[0].appendChild(css); nicholas@2224: break; nicholas@2224: nicholas@2224: case "ABX": nicholas@2224: interfaceJS.setAttribute("src","interfaces/ABX.js"); nicholas@2224: nicholas@2224: // AB comes with a css file nicholas@2224: var css = document.createElement('link'); nicholas@2224: css.rel = 'stylesheet'; nicholas@2224: css.type = 'text/css'; nicholas@2224: css.href = 'interfaces/ABX.css'; nicholas@2224: nicholas@2224: document.getElementsByTagName("head")[0].appendChild(css); nicholas@2224: break; nicholas@2224: nicholas@2224: case "Bipolar": nicholas@2224: case "ACR": nicholas@2224: case "DCR": nicholas@2224: case "CCR": nicholas@2224: case "ABC": nicholas@2224: // Above enumerate to horizontal sliders nicholas@2224: interfaceJS.setAttribute("src","interfaces/horizontal-sliders.js"); nicholas@2224: nicholas@2224: // horizontal-sliders comes with a css file nicholas@2224: var css = document.createElement('link'); nicholas@2224: css.rel = 'stylesheet'; nicholas@2224: css.type = 'text/css'; nicholas@2224: css.href = 'interfaces/horizontal-sliders.css'; nicholas@2224: nicholas@2224: document.getElementsByTagName("head")[0].appendChild(css); nicholas@2224: break; nicholas@2224: case "discrete": nicholas@2224: case "likert": nicholas@2224: // Above enumerate to horizontal discrete radios nicholas@2224: interfaceJS.setAttribute("src","interfaces/discrete.js"); nicholas@2224: nicholas@2224: // horizontal-sliders comes with a css file nicholas@2224: var css = document.createElement('link'); nicholas@2224: css.rel = 'stylesheet'; nicholas@2224: css.type = 'text/css'; nicholas@2224: css.href = 'interfaces/discrete.css'; nicholas@2224: nicholas@2224: document.getElementsByTagName("head")[0].appendChild(css); nicholas@2224: break; nicholas@2224: } nicholas@2224: document.getElementsByTagName("head")[0].appendChild(interfaceJS); nicholas@2224: nicholas@2224: // Create the audio engine object nicholas@2224: audioEngineContext = new AudioEngine(specification); nicholas@2224: } nicholas@2224: nicholas@2224: function createProjectSave(destURL) { nicholas@2224: // Clear the window.onbeforeunload nicholas@2224: window.onbeforeunload = null; nicholas@2224: // Save the data from interface into XML and send to destURL nicholas@2224: // If destURL is null then download XML in client nicholas@2224: // Now time to render file locally nicholas@2224: var xmlDoc = interfaceXMLSave(); nicholas@2224: var parent = document.createElement("div"); nicholas@2224: parent.appendChild(xmlDoc); nicholas@2224: var file = [parent.innerHTML]; nicholas@2224: if (destURL == "local") { nicholas@2224: var bb = new Blob(file,{type : 'application/xml'}); nicholas@2224: var dnlk = window.URL.createObjectURL(bb); nicholas@2224: var a = document.createElement("a"); nicholas@2224: a.hidden = ''; nicholas@2224: a.href = dnlk; nicholas@2224: a.download = "save.xml"; nicholas@2224: a.textContent = "Save File"; nicholas@2224: nicholas@2224: popup.showPopup(); nicholas@2224: popup.popupContent.innerHTML = "Please save the file below to give to your test supervisor
"; nicholas@2224: popup.popupContent.appendChild(a); nicholas@2224: } else { nicholas@2224: var xmlhttp = new XMLHttpRequest; nicholas@2224: xmlhttp.open("POST","php/save.php?key="+storage.SessionKey.key,true); nicholas@2224: xmlhttp.setRequestHeader('Content-Type', 'text/xml'); nicholas@2224: xmlhttp.onerror = function(){ nicholas@2224: console.log('Error saving file to server! Presenting download locally'); nicholas@2224: createProjectSave("local"); nicholas@2224: }; nicholas@2224: xmlhttp.onload = function() { nicholas@2224: console.log(xmlhttp); nicholas@2224: if (this.status >= 300) { nicholas@2224: console.log("WARNING - Could not update at this time"); nicholas@2224: createProjectSave("local"); nicholas@2224: } else { nicholas@2224: var parser = new DOMParser(); nicholas@2224: var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml"); nicholas@2224: var response = xmlDoc.getElementsByTagName('response')[0]; nicholas@2224: if (response.getAttribute("state") == "OK") { nicholas@2224: var file = response.getElementsByTagName("file")[0]; nicholas@2224: console.log("Save: OK, written "+file.getAttribute("bytes")+"B"); nicholas@2224: popup.popupContent.textContent = specification.exitText; nicholas@2224: } else { nicholas@2224: var message = response.getElementsByTagName("message"); nicholas@2224: console.log("Save: Error! "+message.textContent); nicholas@2224: createProjectSave("local"); nicholas@2224: } nicholas@2224: } nicholas@2224: }; nicholas@2224: xmlhttp.send(file); nicholas@2224: popup.showPopup(); nicholas@2224: popup.popupContent.innerHTML = null; nicholas@2224: popup.popupContent.textContent = "Submitting. Please Wait"; nicholas@2224: popup.hideNextButton(); nicholas@2224: popup.hidePreviousButton(); nicholas@2224: } nicholas@2224: } nicholas@2224: nicholas@2224: function errorSessionDump(msg){ nicholas@2224: // Create the partial interface XML save nicholas@2224: // Include error node with message on why the dump occured nicholas@2224: popup.showPopup(); nicholas@2224: popup.popupContent.innerHTML = null; nicholas@2224: var err = document.createElement('error'); nicholas@2224: var parent = document.createElement("div"); nicholas@2224: if (typeof msg === "object") nicholas@2224: { nicholas@2224: err.appendChild(msg); nicholas@2224: popup.popupContent.appendChild(msg); nicholas@2224: nicholas@2224: } else { nicholas@2224: err.textContent = msg; nicholas@2224: popup.popupContent.innerHTML = "ERROR : "+msg; nicholas@2224: } nicholas@2224: var xmlDoc = interfaceXMLSave(); nicholas@2224: xmlDoc.appendChild(err); nicholas@2224: parent.appendChild(xmlDoc); nicholas@2224: var file = [parent.innerHTML]; nicholas@2224: var bb = new Blob(file,{type : 'application/xml'}); nicholas@2224: var dnlk = window.URL.createObjectURL(bb); nicholas@2224: var a = document.createElement("a"); nicholas@2224: a.hidden = ''; nicholas@2224: a.href = dnlk; nicholas@2224: a.download = "save.xml"; nicholas@2224: a.textContent = "Save File"; nicholas@2224: nicholas@2224: nicholas@2224: nicholas@2224: popup.popupContent.appendChild(a); nicholas@2224: } nicholas@2224: nicholas@2224: // Only other global function which must be defined in the interface class. Determines how to create the XML document. nicholas@2224: function interfaceXMLSave(){ nicholas@2224: // Create the XML string to be exported with results nicholas@2224: return storage.finish(); nicholas@2224: } nicholas@2224: nicholas@2224: function linearToDecibel(gain) nicholas@2224: { nicholas@2224: return 20.0*Math.log10(gain); nicholas@2224: } nicholas@2224: nicholas@2224: function decibelToLinear(gain) nicholas@2224: { nicholas@2224: return Math.pow(10,gain/20.0); nicholas@2224: } nicholas@2224: nicholas@2224: function secondsToSamples(time,fs) { nicholas@2224: return Math.round(time*fs); nicholas@2224: } nicholas@2224: nicholas@2224: function samplesToSeconds(samples,fs) { nicholas@2224: return samples / fs; nicholas@2224: } nicholas@2224: nicholas@2224: function randomString(length) { nicholas@2224: return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1); nicholas@2224: } nicholas@2224: nicholas@2224: function randomiseOrder(input) nicholas@2224: { nicholas@2224: // This takes an array of information and randomises the order nicholas@2224: var N = input.length; nicholas@2224: nicholas@2224: var inputSequence = []; // For safety purposes: keep track of randomisation nicholas@2224: for (var counter = 0; counter < N; ++counter) nicholas@2224: inputSequence.push(counter) // Fill array nicholas@2224: var inputSequenceClone = inputSequence.slice(0); nicholas@2224: nicholas@2224: var holdArr = []; nicholas@2224: var outputSequence = []; nicholas@2224: for (var n=0; n array.length) { nicholas@2224: num = array.length; nicholas@2224: } nicholas@2224: var ret = []; nicholas@2224: while (num > 0) { nicholas@2224: var index = Math.floor(Math.random() * array.length); nicholas@2224: ret.push( array.splice(index,1)[0] ); nicholas@2224: num--; nicholas@2224: } nicholas@2224: return ret; nicholas@2224: } nicholas@2224: nicholas@2224: function interfacePopup() { nicholas@2224: // Creates an object to manage the popup nicholas@2224: this.popup = null; nicholas@2224: this.popupContent = null; nicholas@2224: this.popupTitle = null; nicholas@2224: this.popupResponse = null; nicholas@2224: this.buttonProceed = null; nicholas@2224: this.buttonPrevious = null; nicholas@2224: this.popupOptions = null; nicholas@2224: this.currentIndex = null; nicholas@2224: this.node = null; nicholas@2224: this.store = null; nicholas@2224: $(window).keypress(function(e){ nicholas@2224: if (e.keyCode == 13 && popup.popup.style.visibility == 'visible') nicholas@2224: { nicholas@2224: console.log(e); nicholas@2224: popup.buttonProceed.onclick(); nicholas@2224: e.preventDefault(); nicholas@2224: } nicholas@2224: }); nicholas@2224: nicholas@2224: this.createPopup = function(){ nicholas@2224: // Create popup window interface nicholas@2224: var insertPoint = document.getElementById("topLevelBody"); nicholas@2224: nicholas@2224: this.popup = document.getElementById('popupHolder'); nicholas@2224: this.popup.style.left = (window.innerWidth/2)-250 + 'px'; nicholas@2224: this.popup.style.top = (window.innerHeight/2)-125 + 'px'; nicholas@2224: nicholas@2224: this.popupContent = document.getElementById('popupContent'); nicholas@2224: nicholas@2224: this.popupTitle = document.getElementById('popupTitle'); nicholas@2224: nicholas@2224: this.popupResponse = document.getElementById('popupResponse'); nicholas@2224: nicholas@2224: this.buttonProceed = document.getElementById('popup-proceed'); nicholas@2224: this.buttonProceed.onclick = function(){popup.proceedClicked();}; nicholas@2224: nicholas@2224: this.buttonPrevious = document.getElementById('popup-previous'); nicholas@2224: this.buttonPrevious.onclick = function(){popup.previousClick();}; nicholas@2224: nicholas@2224: this.hidePopup(); nicholas@2224: nicholas@2224: this.popup.style.zIndex = -1; nicholas@2224: this.popup.style.visibility = 'hidden'; nicholas@2224: }; nicholas@2224: nicholas@2224: this.showPopup = function(){ nicholas@2224: if (this.popup == null) { nicholas@2224: this.createPopup(); nicholas@2224: } nicholas@2224: this.popup.style.zIndex = 3; nicholas@2224: this.popup.style.visibility = 'visible'; nicholas@2224: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@2224: blank.style.zIndex = 2; nicholas@2224: blank.style.visibility = 'visible'; nicholas@2224: this.popupResponse.style.left="0%"; nicholas@2224: }; nicholas@2224: nicholas@2224: this.hidePopup = function(){ nicholas@2224: if (this.popup) { nicholas@2224: this.popup.style.zIndex = -1; nicholas@2224: this.popup.style.visibility = 'hidden'; nicholas@2224: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@2224: blank.style.zIndex = -2; nicholas@2224: blank.style.visibility = 'hidden'; nicholas@2224: this.buttonPrevious.style.visibility = 'inherit'; nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.postNode = function() { nicholas@2224: // This will take the node from the popupOptions and display it nicholas@2224: var node = this.popupOptions[this.currentIndex]; nicholas@2224: this.popupResponse.innerHTML = null; nicholas@2224: this.popupTitle.textContent = node.specification.statement; nicholas@2224: if (node.specification.type == 'question') { nicholas@2224: var textArea = document.createElement('textarea'); nicholas@2224: switch (node.specification.boxsize) { nicholas@2224: case 'small': nicholas@2224: textArea.cols = "20"; nicholas@2224: textArea.rows = "1"; nicholas@2224: break; nicholas@2224: case 'normal': nicholas@2224: textArea.cols = "30"; nicholas@2224: textArea.rows = "2"; nicholas@2224: break; nicholas@2224: case 'large': nicholas@2224: textArea.cols = "40"; nicholas@2224: textArea.rows = "5"; nicholas@2224: break; nicholas@2224: case 'huge': nicholas@2224: textArea.cols = "50"; nicholas@2224: textArea.rows = "10"; nicholas@2224: break; nicholas@2224: } nicholas@2224: if (node.response == undefined) { nicholas@2224: node.response = ""; nicholas@2224: } else { nicholas@2224: textArea.value = node.response; nicholas@2224: } nicholas@2224: this.popupResponse.appendChild(textArea); nicholas@2224: textArea.focus(); nicholas@2224: this.popupResponse.style.textAlign="center"; nicholas@2224: this.popupResponse.style.left="0%"; nicholas@2224: } else if (node.specification.type == 'checkbox') { nicholas@2224: if (node.response == undefined) { nicholas@2224: node.response = Array(node.specification.options.length); nicholas@2224: } nicholas@2224: var index = 0; nicholas@2224: var max_w = 0; nicholas@2224: for (var option of node.specification.options) { nicholas@2224: var input = document.createElement('input'); nicholas@2224: input.id = option.name; nicholas@2224: input.type = 'checkbox'; nicholas@2224: var span = document.createElement('span'); nicholas@2224: span.textContent = option.text; nicholas@2224: var hold = document.createElement('div'); nicholas@2224: hold.setAttribute('name','option'); nicholas@2224: hold.className = "popup-option-checbox"; nicholas@2224: hold.appendChild(input); nicholas@2224: hold.appendChild(span); nicholas@2224: this.popupResponse.appendChild(hold); nicholas@2224: if (node.response[index] != undefined){ nicholas@2224: if (node.response[index].checked == true) { nicholas@2224: input.checked = "true"; nicholas@2224: } nicholas@2224: } nicholas@2224: var w = $(hold).width(); nicholas@2224: if (w > max_w) nicholas@2224: max_w = w; nicholas@2224: index++; nicholas@2224: } nicholas@2224: this.popupResponse.style.textAlign=""; nicholas@2224: var leftP = 50-(((max_w/$('#popupContent').width())/2)*100); nicholas@2224: this.popupResponse.style.left=leftP+"%"; nicholas@2224: } else if (node.specification.type == 'radio') { nicholas@2224: if (node.response == undefined) { nicholas@2224: node.response = {name: "", text: ""}; nicholas@2224: } nicholas@2224: var index = 0; nicholas@2224: var max_w = 0; nicholas@2224: for (var option of node.specification.options) { nicholas@2224: var input = document.createElement('input'); nicholas@2224: input.id = option.name; nicholas@2224: input.type = 'radio'; nicholas@2224: input.name = node.specification.id; nicholas@2224: var span = document.createElement('span'); nicholas@2224: span.textContent = option.text; nicholas@2224: var hold = document.createElement('div'); nicholas@2224: hold.setAttribute('name','option'); nicholas@2224: hold.className = "popup-option-checbox"; nicholas@2224: hold.appendChild(input); nicholas@2224: hold.appendChild(span); nicholas@2224: this.popupResponse.appendChild(hold); nicholas@2224: if (input.id == node.response.name) { nicholas@2224: input.checked = "true"; nicholas@2224: } nicholas@2224: var w = $(hold).width(); nicholas@2224: if (w > max_w) nicholas@2224: max_w = w; nicholas@2224: } nicholas@2224: this.popupResponse.style.textAlign=""; nicholas@2224: var leftP = 50-(((max_w/$('#popupContent').width())/2)*100); nicholas@2224: this.popupResponse.style.left=leftP+"%"; nicholas@2224: } else if (node.specification.type == 'number') { nicholas@2224: var input = document.createElement('input'); nicholas@2224: input.type = 'textarea'; nicholas@2224: if (node.min != null) {input.min = node.specification.min;} nicholas@2224: if (node.max != null) {input.max = node.specification.max;} nicholas@2224: if (node.step != null) {input.step = node.specification.step;} nicholas@2224: if (node.response != undefined) { nicholas@2224: input.value = node.response; nicholas@2224: } nicholas@2224: this.popupResponse.appendChild(input); nicholas@2224: this.popupResponse.style.textAlign="center"; nicholas@2224: this.popupResponse.style.left="0%"; nicholas@2224: } nicholas@2224: if(this.currentIndex+1 == this.popupOptions.length) { nicholas@2224: if (this.node.location == "pre") { nicholas@2224: this.buttonProceed.textContent = 'Start'; nicholas@2224: } else { nicholas@2224: this.buttonProceed.textContent = 'Submit'; nicholas@2224: } nicholas@2224: } else { nicholas@2224: this.buttonProceed.textContent = 'Next'; nicholas@2224: } nicholas@2224: if(this.currentIndex > 0) nicholas@2224: this.buttonPrevious.style.visibility = 'visible'; nicholas@2224: else nicholas@2224: this.buttonPrevious.style.visibility = 'hidden'; nicholas@2224: }; nicholas@2224: nicholas@2224: this.initState = function(node,store) { nicholas@2224: //Call this with your preTest and postTest nodes when needed to nicholas@2224: // initialise the popup procedure. nicholas@2224: if (node.options.length > 0) { nicholas@2224: this.popupOptions = []; nicholas@2224: this.node = node; nicholas@2224: this.store = store; nicholas@2224: for (var opt of node.options) nicholas@2224: { nicholas@2224: this.popupOptions.push({ nicholas@2224: specification: opt, nicholas@2224: response: null nicholas@2224: }); nicholas@2224: } nicholas@2224: this.currentIndex = 0; nicholas@2224: this.showPopup(); nicholas@2224: this.postNode(); nicholas@2224: } else { nicholas@2224: advanceState(); nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.proceedClicked = function() { nicholas@2224: // Each time the popup button is clicked! nicholas@2224: if (testState.stateIndex == 0 && specification.calibration) { nicholas@2224: interfaceContext.calibrationModuleObject.collect(); nicholas@2224: advanceState(); nicholas@2224: return; nicholas@2224: } nicholas@2224: var node = this.popupOptions[this.currentIndex]; nicholas@2224: if (node.specification.type == 'question') { nicholas@2224: // Must extract the question data nicholas@2224: var textArea = $(popup.popupContent).find('textarea')[0]; nicholas@2224: if (node.specification.mandatory == true && textArea.value.length == 0) { nicholas@2224: alert('This question is mandatory'); nicholas@2224: return; nicholas@2224: } else { nicholas@2224: // Save the text content nicholas@2224: console.log("Question: "+ node.specification.statement); nicholas@2224: console.log("Question Response: "+ textArea.value); nicholas@2224: node.response = textArea.value; nicholas@2224: } nicholas@2224: } else if (node.specification.type == 'checkbox') { nicholas@2224: // Must extract checkbox data nicholas@2224: console.log("Checkbox: "+ node.specification.statement); nicholas@2224: var inputs = this.popupResponse.getElementsByTagName('input'); nicholas@2224: node.response = []; nicholas@2224: for (var i=0; i node.max && node.max != null) { nicholas@2224: alert('Number is above the maximum value of '+node.max); nicholas@2224: return; nicholas@2224: } nicholas@2224: node.response = input.value; nicholas@2224: } nicholas@2224: this.currentIndex++; nicholas@2224: if (this.currentIndex < this.popupOptions.length) { nicholas@2224: this.postNode(); nicholas@2224: } else { nicholas@2224: // Reached the end of the popupOptions nicholas@2224: this.hidePopup(); nicholas@2224: for (var node of this.popupOptions) nicholas@2224: { nicholas@2224: this.store.postResult(node); nicholas@2224: } nicholas@2224: this.store.complete(); nicholas@2224: advanceState(); nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.previousClick = function() { nicholas@2224: // Triggered when the 'Back' button is clicked in the survey nicholas@2224: if (this.currentIndex > 0) { nicholas@2224: this.currentIndex--; nicholas@2224: this.postNode(); nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.resize = function(event) nicholas@2224: { nicholas@2224: // Called on window resize; nicholas@2224: if (this.popup != null) { nicholas@2224: this.popup.style.left = (window.innerWidth/2)-250 + 'px'; nicholas@2224: this.popup.style.top = (window.innerHeight/2)-125 + 'px'; nicholas@2224: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@2224: blank.style.width = window.innerWidth; nicholas@2224: blank.style.height = window.innerHeight; nicholas@2224: } nicholas@2224: }; nicholas@2224: this.hideNextButton = function() { nicholas@2224: this.buttonProceed.style.visibility = "hidden"; nicholas@2224: } nicholas@2224: this.hidePreviousButton = function() { nicholas@2224: this.buttonPrevious.style.visibility = "hidden"; nicholas@2224: } nicholas@2224: this.showNextButton = function() { nicholas@2224: this.buttonProceed.style.visibility = "visible"; nicholas@2224: } nicholas@2224: this.showPreviousButton = function() { nicholas@2224: this.buttonPrevious.style.visibility = "visible"; nicholas@2224: } nicholas@2224: } nicholas@2224: nicholas@2224: function advanceState() nicholas@2224: { nicholas@2224: // Just for complete clarity nicholas@2224: testState.advanceState(); nicholas@2224: } nicholas@2224: nicholas@2224: function stateMachine() nicholas@2224: { nicholas@2224: // Object prototype for tracking and managing the test state nicholas@2224: this.stateMap = []; nicholas@2224: this.preTestSurvey = null; nicholas@2224: this.postTestSurvey = null; nicholas@2224: this.stateIndex = null; nicholas@2224: this.currentStateMap = null; nicholas@2224: this.currentStatePosition = null; nicholas@2224: this.currentStore = null; nicholas@2224: this.initialise = function(){ nicholas@2224: nicholas@2224: // Get the data from Specification nicholas@2224: var pagePool = []; nicholas@2224: var pageInclude = []; nicholas@2224: for (var page of specification.pages) nicholas@2224: { nicholas@2224: if (page.alwaysInclude) { nicholas@2224: pageInclude.push(page); nicholas@2224: } else { nicholas@2224: pagePool.push(page); nicholas@2224: } nicholas@2224: } nicholas@2224: nicholas@2224: // Find how many are left to get nicholas@2224: var numPages = specification.poolSize; nicholas@2224: if (numPages > pagePool.length) { nicholas@2224: console.log("WARNING - You have specified more pages in than you have created!!"); nicholas@2224: numPages = specification.pages.length; nicholas@2224: } nicholas@2224: if (specification.poolSize == 0) { nicholas@2224: numPages = specification.pages.length; nicholas@2224: } nicholas@2224: numPages -= pageInclude.length; nicholas@2224: nicholas@2224: if (numPages > 0) { nicholas@2224: // Go find the rest of the pages from the pool nicholas@2224: var subarr = null; nicholas@2224: if (specification.randomiseOrder) { nicholas@2224: // Append a random sub-array nicholas@2224: subarr = randomSubArray(pagePool,numPages); nicholas@2224: } else { nicholas@2224: // Append the matching number nicholas@2224: subarr = pagePool.slice(0,numPages); nicholas@2224: } nicholas@2224: pageInclude = pageInclude.concat(subarr); nicholas@2224: } nicholas@2224: nicholas@2224: // We now have our selected pages in pageInclude array nicholas@2224: if (specification.randomiseOrder) nicholas@2224: { nicholas@2224: pageInclude = randomiseOrder(pageInclude); nicholas@2224: } nicholas@2224: for (var i=0; i 0) { nicholas@2224: if(this.stateIndex != null) { nicholas@2224: console.log('NOTE - State already initialise'); nicholas@2224: } nicholas@2224: this.stateIndex = -2; nicholas@2224: console.log('Starting test...'); nicholas@2224: } else { nicholas@2224: console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP'); nicholas@2224: } nicholas@2224: }; nicholas@2224: this.advanceState = function(){ nicholas@2224: if (this.stateIndex == null) { nicholas@2224: this.initialise(); nicholas@2224: } nicholas@2224: storage.update(); nicholas@2224: if (this.stateIndex == -2) { nicholas@2224: this.stateIndex++; nicholas@2224: if (this.preTestSurvey != null) nicholas@2224: { nicholas@2224: popup.initState(this.preTestSurvey,storage.globalPreTest); nicholas@2224: } else { nicholas@2224: this.advanceState(); nicholas@2224: } nicholas@2224: } else if (this.stateIndex == -1) { nicholas@2224: this.stateIndex++; nicholas@2224: if (specification.calibration) { nicholas@2224: popup.showPopup(); nicholas@2224: popup.popupTitle.textContent = "Calibration. Set the levels so all tones are of equal amplitude. Move your mouse over the sliders to hear the tones. The red slider is the reference tone"; nicholas@2224: interfaceContext.calibrationModuleObject = new interfaceContext.calibrationModule(); nicholas@2224: interfaceContext.calibrationModuleObject.build(popup.popupResponse); nicholas@2224: popup.hidePreviousButton(); nicholas@2224: } else { nicholas@2224: this.advanceState(); nicholas@2224: } nicholas@2224: } nicholas@2224: else if (this.stateIndex == this.stateMap.length) nicholas@2224: { nicholas@2224: // All test pages complete, post test nicholas@2224: console.log('Ending test ...'); nicholas@2224: this.stateIndex++; nicholas@2224: if (this.postTestSurvey == null) { nicholas@2224: this.advanceState(); nicholas@2224: } else { nicholas@2224: popup.initState(this.postTestSurvey,storage.globalPostTest); nicholas@2224: } nicholas@2224: } else if (this.stateIndex > this.stateMap.length) nicholas@2224: { nicholas@2224: createProjectSave(specification.projectReturn); nicholas@2224: } nicholas@2224: else nicholas@2224: { nicholas@2224: popup.hidePopup(); nicholas@2224: if (this.currentStateMap == null) nicholas@2224: { nicholas@2224: this.currentStateMap = this.stateMap[this.stateIndex]; nicholas@2224: if (this.currentStateMap.randomiseOrder) nicholas@2224: { nicholas@2224: this.currentStateMap.audioElements = randomiseOrder(this.currentStateMap.audioElements); nicholas@2224: } nicholas@2224: this.currentStore = storage.testPages[this.stateIndex]; nicholas@2224: if (this.currentStateMap.preTest != null) nicholas@2224: { nicholas@2224: this.currentStatePosition = 'pre'; nicholas@2224: popup.initState(this.currentStateMap.preTest,storage.testPages[this.stateIndex].preTest); nicholas@2224: } else { nicholas@2224: this.currentStatePosition = 'test'; nicholas@2224: } nicholas@2224: interfaceContext.newPage(this.currentStateMap,storage.testPages[this.stateIndex]); nicholas@2224: return; nicholas@2224: } nicholas@2224: switch(this.currentStatePosition) nicholas@2224: { nicholas@2224: case 'pre': nicholas@2224: this.currentStatePosition = 'test'; nicholas@2224: break; nicholas@2224: case 'test': nicholas@2224: this.currentStatePosition = 'post'; nicholas@2224: // Save the data nicholas@2224: this.testPageCompleted(); nicholas@2224: if (this.currentStateMap.postTest == null) nicholas@2224: { nicholas@2224: this.advanceState(); nicholas@2224: return; nicholas@2224: } else { nicholas@2224: popup.initState(this.currentStateMap.postTest,storage.testPages[this.stateIndex].postTest); nicholas@2224: } nicholas@2224: break; nicholas@2224: case 'post': nicholas@2224: this.stateIndex++; nicholas@2224: this.currentStateMap = null; nicholas@2224: this.advanceState(); nicholas@2224: break; nicholas@2224: }; nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.testPageCompleted = function() { nicholas@2224: // Function called each time a test page has been completed nicholas@2224: var storePoint = storage.testPages[this.stateIndex]; nicholas@2224: // First get the test metric nicholas@2224: nicholas@2224: var metric = storePoint.XMLDOM.getElementsByTagName('metric')[0]; nicholas@2224: if (audioEngineContext.metric.enableTestTimer) nicholas@2224: { nicholas@2224: var testTime = storePoint.parent.document.createElement('metricresult'); nicholas@2224: testTime.id = 'testTime'; nicholas@2224: testTime.textContent = audioEngineContext.timer.testDuration; nicholas@2224: metric.appendChild(testTime); nicholas@2224: } nicholas@2224: nicholas@2224: var audioObjects = audioEngineContext.audioObjects; nicholas@2224: for (var ao of audioEngineContext.audioObjects) nicholas@2224: { nicholas@2224: ao.exportXMLDOM(); nicholas@2224: } nicholas@2224: for (var element of interfaceContext.commentQuestions) nicholas@2224: { nicholas@2224: element.exportXMLDOM(storePoint); nicholas@2224: } nicholas@2224: pageXMLSave(storePoint.XMLDOM, this.currentStateMap); nicholas@2224: storePoint.complete(); nicholas@2224: }; nicholas@2224: } nicholas@2224: nicholas@2224: function AudioEngine(specification) { nicholas@2224: nicholas@2224: // Create two output paths, the main outputGain and fooGain. nicholas@2224: // Output gain is default to 1 and any items for playback route here nicholas@2224: // Foo gain is used for analysis to ensure paths get processed, but are not heard nicholas@2224: // because web audio will optimise and any route which does not go to the destination gets ignored. nicholas@2224: this.outputGain = audioContext.createGain(); nicholas@2224: this.fooGain = audioContext.createGain(); nicholas@2224: this.fooGain.gain = 0; nicholas@2224: nicholas@2224: // Use this to detect playback state: 0 - stopped, 1 - playing nicholas@2224: this.status = 0; nicholas@2224: nicholas@2224: // Connect both gains to output nicholas@2224: this.outputGain.connect(audioContext.destination); nicholas@2224: this.fooGain.connect(audioContext.destination); nicholas@2224: nicholas@2224: // Create the timer Object nicholas@2224: this.timer = new timer(); nicholas@2224: // Create session metrics nicholas@2224: this.metric = new sessionMetrics(this,specification); nicholas@2224: nicholas@2224: this.loopPlayback = false; nicholas@2224: nicholas@2224: this.pageStore = null; nicholas@2224: nicholas@2224: // Create store for new audioObjects nicholas@2224: this.audioObjects = []; nicholas@2224: nicholas@2224: this.buffers = []; nicholas@2224: this.bufferObj = function() nicholas@2224: { nicholas@2224: this.url = null; nicholas@2224: this.buffer = null; nicholas@2224: this.xmlRequest = new XMLHttpRequest(); nicholas@2224: this.xmlRequest.parent = this; nicholas@2224: this.users = []; nicholas@2224: this.progress = 0; nicholas@2224: this.status = 0; nicholas@2224: this.ready = function() nicholas@2224: { nicholas@2224: if (this.status >= 2) nicholas@2224: { nicholas@2224: this.status = 3; nicholas@2224: } nicholas@2224: for (var i=0; i 0) {this.wasMoved = true;} nicholas@2224: this.movementTracker[this.movementTracker.length] = [time, position]; nicholas@2224: }; nicholas@2224: nicholas@2224: this.startListening = function(time) nicholas@2224: { nicholas@2224: if (this.listenHold == false) nicholas@2224: { nicholas@2224: this.wasListenedTo = true; nicholas@2224: this.listenStart = time; nicholas@2224: this.listenHold = true; nicholas@2224: nicholas@2224: var evnt = document.createElement('event'); nicholas@2224: var testTime = document.createElement('testTime'); nicholas@2224: testTime.setAttribute('start',time); nicholas@2224: var bufferTime = document.createElement('bufferTime'); nicholas@2224: bufferTime.setAttribute('start',this.parent.getCurrentPosition()); nicholas@2224: evnt.appendChild(testTime); nicholas@2224: evnt.appendChild(bufferTime); nicholas@2224: this.listenTracker.push(evnt); nicholas@2224: nicholas@2224: console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.stopListening = function(time,bufferStopTime) nicholas@2224: { nicholas@2224: if (this.listenHold == true) nicholas@2224: { nicholas@2224: var diff = time - this.listenStart; nicholas@2224: this.listenedTimer += (diff); nicholas@2224: this.listenStart = 0; nicholas@2224: this.listenHold = false; nicholas@2224: nicholas@2224: var evnt = this.listenTracker[this.listenTracker.length-1]; nicholas@2224: var testTime = evnt.getElementsByTagName('testTime')[0]; nicholas@2224: var bufferTime = evnt.getElementsByTagName('bufferTime')[0]; nicholas@2224: testTime.setAttribute('stop',time); nicholas@2224: if (bufferStopTime == undefined) { nicholas@2224: bufferTime.setAttribute('stop',this.parent.getCurrentPosition()); nicholas@2224: } else { nicholas@2224: bufferTime.setAttribute('stop',bufferStopTime); nicholas@2224: } nicholas@2224: console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.exportXMLDOM = function() { nicholas@2224: var storeDOM = []; nicholas@2224: if (audioEngineContext.metric.enableElementTimer) { nicholas@2224: var mElementTimer = storage.document.createElement('metricresult'); nicholas@2224: mElementTimer.setAttribute('name','enableElementTimer'); nicholas@2224: mElementTimer.textContent = this.listenedTimer; nicholas@2224: storeDOM.push(mElementTimer); nicholas@2224: } nicholas@2224: if (audioEngineContext.metric.enableElementTracker) { b@2281: var elementTrackerFull = storage.document.createElement('metricresult'); nicholas@2224: elementTrackerFull.setAttribute('name','elementTrackerFull'); nicholas@2224: for (var k=0; k tag. nicholas@2224: this.interfaceObjects = []; nicholas@2224: this.interfaceObject = function(){}; nicholas@2224: nicholas@2224: this.resizeWindow = function(event) nicholas@2224: { nicholas@2224: popup.resize(event); nicholas@2224: for(var i=0; i nicholas@2224: // DD/MM/YY nicholas@2224: // nicholas@2224: // nicholas@2224: var dateTime = new Date(); nicholas@2224: var hold = storage.document.createElement("datetime"); nicholas@2224: var date = storage.document.createElement("date"); nicholas@2224: var time = storage.document.createElement("time"); nicholas@2224: date.setAttribute('year',dateTime.getFullYear()); nicholas@2224: date.setAttribute('month',dateTime.getMonth()+1); nicholas@2224: date.setAttribute('day',dateTime.getDate()); nicholas@2224: time.setAttribute('hour',dateTime.getHours()); nicholas@2224: time.setAttribute('minute',dateTime.getMinutes()); nicholas@2224: time.setAttribute('secs',dateTime.getSeconds()); nicholas@2224: nicholas@2224: hold.appendChild(date); nicholas@2224: hold.appendChild(time); nicholas@2224: return hold; nicholas@2224: nicholas@2224: } nicholas@2224: nicholas@2224: this.commentBoxes = new function() { nicholas@2224: this.boxes = []; nicholas@2224: this.injectPoint = null; nicholas@2224: this.elementCommentBox = function(audioObject) { nicholas@2224: var element = audioObject.specification; nicholas@2224: this.audioObject = audioObject; nicholas@2224: this.id = audioObject.id; nicholas@2224: var audioHolderObject = audioObject.specification.parent; nicholas@2224: // Create document objects to hold the comment boxes nicholas@2224: this.trackComment = document.createElement('div'); nicholas@2224: this.trackComment.className = 'comment-div'; nicholas@2224: this.trackComment.id = 'comment-div-'+audioObject.id; nicholas@2224: // Create a string next to each comment asking for a comment nicholas@2224: this.trackString = document.createElement('span'); nicholas@2224: this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.interfaceDOM.getPresentedId(); nicholas@2224: // Create the HTML5 comment box 'textarea' nicholas@2224: this.trackCommentBox = document.createElement('textarea'); nicholas@2224: this.trackCommentBox.rows = '4'; nicholas@2224: this.trackCommentBox.cols = '100'; nicholas@2224: this.trackCommentBox.name = 'trackComment'+audioObject.id; nicholas@2224: this.trackCommentBox.className = 'trackComment'; nicholas@2224: var br = document.createElement('br'); nicholas@2224: // Add to the holder. nicholas@2224: this.trackComment.appendChild(this.trackString); nicholas@2224: this.trackComment.appendChild(br); nicholas@2224: this.trackComment.appendChild(this.trackCommentBox); nicholas@2224: nicholas@2224: this.exportXMLDOM = function() { nicholas@2224: var root = document.createElement('comment'); nicholas@2224: var question = document.createElement('question'); nicholas@2224: question.textContent = this.trackString.textContent; nicholas@2224: var response = document.createElement('response'); nicholas@2224: response.textContent = this.trackCommentBox.value; nicholas@2224: console.log("Comment frag-"+this.id+": "+response.textContent); nicholas@2224: root.appendChild(question); nicholas@2224: root.appendChild(response); nicholas@2224: return root; nicholas@2224: }; nicholas@2224: this.resize = function() nicholas@2224: { nicholas@2224: var boxwidth = (window.innerWidth-100)/2; nicholas@2224: if (boxwidth >= 600) nicholas@2224: { nicholas@2224: boxwidth = 600; nicholas@2224: } nicholas@2224: else if (boxwidth < 400) nicholas@2224: { nicholas@2224: boxwidth = 400; nicholas@2224: } nicholas@2224: this.trackComment.style.width = boxwidth+"px"; nicholas@2224: this.trackCommentBox.style.width = boxwidth-6+"px"; nicholas@2224: }; nicholas@2224: this.resize(); nicholas@2224: }; nicholas@2224: this.createCommentBox = function(audioObject) { nicholas@2224: var node = new this.elementCommentBox(audioObject); nicholas@2224: this.boxes.push(node); nicholas@2224: audioObject.commentDOM = node; nicholas@2224: return node; nicholas@2224: }; nicholas@2224: this.sortCommentBoxes = function() { nicholas@2224: this.boxes.sort(function(a,b){return a.id - b.id;}); nicholas@2224: }; nicholas@2224: nicholas@2224: this.showCommentBoxes = function(inject, sort) { nicholas@2224: this.injectPoint = inject; nicholas@2224: if (sort) {this.sortCommentBoxes();} nicholas@2224: for (var box of this.boxes) { nicholas@2224: inject.appendChild(box.trackComment); nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.deleteCommentBoxes = function() { nicholas@2224: if (this.injectPoint != null) { nicholas@2224: for (var box of this.boxes) { nicholas@2224: this.injectPoint.removeChild(box.trackComment); nicholas@2224: } nicholas@2224: this.injectPoint = null; nicholas@2224: } nicholas@2224: this.boxes = []; nicholas@2224: }; nicholas@2224: } nicholas@2224: nicholas@2224: this.commentQuestions = []; nicholas@2224: nicholas@2224: this.commentBox = function(commentQuestion) { nicholas@2224: this.specification = commentQuestion; nicholas@2224: // Create document objects to hold the comment boxes nicholas@2224: this.holder = document.createElement('div'); nicholas@2224: this.holder.className = 'comment-div'; nicholas@2224: // Create a string next to each comment asking for a comment nicholas@2224: this.string = document.createElement('span'); nicholas@2224: this.string.innerHTML = commentQuestion.statement; nicholas@2224: // Create the HTML5 comment box 'textarea' nicholas@2224: this.textArea = document.createElement('textarea'); nicholas@2224: this.textArea.rows = '4'; nicholas@2224: this.textArea.cols = '100'; nicholas@2224: this.textArea.className = 'trackComment'; nicholas@2224: var br = document.createElement('br'); nicholas@2224: // Add to the holder. nicholas@2224: this.holder.appendChild(this.string); nicholas@2224: this.holder.appendChild(br); nicholas@2224: this.holder.appendChild(this.textArea); nicholas@2224: nicholas@2224: this.exportXMLDOM = function(storePoint) { nicholas@2224: var root = storePoint.parent.document.createElement('comment'); nicholas@2224: root.id = this.specification.id; nicholas@2224: root.setAttribute('type',this.specification.type); nicholas@2224: console.log("Question: "+this.string.textContent); nicholas@2224: console.log("Response: "+root.textContent); nicholas@2224: var question = storePoint.parent.document.createElement('question'); nicholas@2224: question.textContent = this.string.textContent; nicholas@2224: var response = storePoint.parent.document.createElement('response'); nicholas@2224: response.textContent = this.textArea.value; nicholas@2224: root.appendChild(question); nicholas@2224: root.appendChild(response); nicholas@2224: storePoint.XMLDOM.appendChild(root); nicholas@2224: return root; nicholas@2224: }; nicholas@2224: this.resize = function() nicholas@2224: { nicholas@2224: var boxwidth = (window.innerWidth-100)/2; nicholas@2224: if (boxwidth >= 600) nicholas@2224: { nicholas@2224: boxwidth = 600; nicholas@2224: } nicholas@2224: else if (boxwidth < 400) nicholas@2224: { nicholas@2224: boxwidth = 400; nicholas@2224: } nicholas@2224: this.holder.style.width = boxwidth+"px"; nicholas@2224: this.textArea.style.width = boxwidth-6+"px"; nicholas@2224: }; nicholas@2224: this.resize(); nicholas@2224: }; nicholas@2224: nicholas@2224: this.radioBox = function(commentQuestion) { nicholas@2224: this.specification = commentQuestion; nicholas@2224: // Create document objects to hold the comment boxes nicholas@2224: this.holder = document.createElement('div'); nicholas@2224: this.holder.className = 'comment-div'; nicholas@2224: // Create a string next to each comment asking for a comment nicholas@2224: this.string = document.createElement('span'); nicholas@2224: this.string.innerHTML = commentQuestion.statement; nicholas@2224: var br = document.createElement('br'); nicholas@2224: // Add to the holder. nicholas@2224: this.holder.appendChild(this.string); nicholas@2224: this.holder.appendChild(br); nicholas@2224: this.options = []; nicholas@2224: this.inputs = document.createElement('div'); nicholas@2224: this.span = document.createElement('div'); nicholas@2224: this.inputs.align = 'center'; nicholas@2224: this.inputs.style.marginLeft = '12px'; nicholas@2224: this.span.style.marginLeft = '12px'; nicholas@2224: this.span.align = 'center'; nicholas@2224: this.span.style.marginTop = '15px'; nicholas@2224: nicholas@2224: var optCount = commentQuestion.options.length; nicholas@2224: for (var optNode of commentQuestion.options) nicholas@2224: { nicholas@2224: var div = document.createElement('div'); nicholas@2224: div.style.width = '80px'; nicholas@2224: div.style.float = 'left'; nicholas@2224: var input = document.createElement('input'); nicholas@2224: input.type = 'radio'; nicholas@2224: input.name = commentQuestion.id; nicholas@2224: input.setAttribute('setvalue',optNode.name); nicholas@2224: input.className = 'comment-radio'; nicholas@2224: div.appendChild(input); nicholas@2224: this.inputs.appendChild(div); nicholas@2224: nicholas@2224: nicholas@2224: div = document.createElement('div'); nicholas@2224: div.style.width = '80px'; nicholas@2224: div.style.float = 'left'; nicholas@2224: div.align = 'center'; nicholas@2224: var span = document.createElement('span'); nicholas@2224: span.textContent = optNode.text; nicholas@2224: span.className = 'comment-radio-span'; nicholas@2224: div.appendChild(span); nicholas@2224: this.span.appendChild(div); nicholas@2224: this.options.push(input); nicholas@2224: } nicholas@2224: this.holder.appendChild(this.span); nicholas@2224: this.holder.appendChild(this.inputs); nicholas@2224: nicholas@2224: this.exportXMLDOM = function(storePoint) { nicholas@2224: var root = storePoint.parent.document.createElement('comment'); nicholas@2224: root.id = this.specification.id; nicholas@2224: root.setAttribute('type',this.specification.type); nicholas@2224: var question = document.createElement('question'); nicholas@2224: question.textContent = this.string.textContent; nicholas@2224: var response = document.createElement('response'); nicholas@2224: var i=0; nicholas@2224: while(this.options[i].checked == false) { nicholas@2224: i++; nicholas@2224: if (i >= this.options.length) { nicholas@2224: break; nicholas@2224: } nicholas@2224: } nicholas@2224: if (i >= this.options.length) { nicholas@2224: response.textContent = 'null'; nicholas@2224: } else { nicholas@2224: response.textContent = this.options[i].getAttribute('setvalue'); nicholas@2224: response.setAttribute('number',i); nicholas@2224: } nicholas@2224: console.log('Comment: '+question.textContent); nicholas@2224: console.log('Response: '+response.textContent); nicholas@2224: root.appendChild(question); nicholas@2224: root.appendChild(response); nicholas@2224: storePoint.XMLDOM.appendChild(root); nicholas@2224: return root; nicholas@2224: }; nicholas@2224: this.resize = function() nicholas@2224: { nicholas@2224: var boxwidth = (window.innerWidth-100)/2; nicholas@2224: if (boxwidth >= 600) nicholas@2224: { nicholas@2224: boxwidth = 600; nicholas@2224: } nicholas@2224: else if (boxwidth < 400) nicholas@2224: { nicholas@2224: boxwidth = 400; nicholas@2224: } nicholas@2224: this.holder.style.width = boxwidth+"px"; nicholas@2224: var text = this.holder.children[2]; nicholas@2224: var options = this.holder.children[3]; nicholas@2224: var optCount = options.children.length; nicholas@2224: var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px'; nicholas@2224: var options = options.firstChild; nicholas@2224: var text = text.firstChild; nicholas@2224: options.style.marginRight = spanMargin; nicholas@2224: options.style.marginLeft = spanMargin; nicholas@2224: text.style.marginRight = spanMargin; nicholas@2224: text.style.marginLeft = spanMargin; nicholas@2224: while(options.nextSibling != undefined) nicholas@2224: { nicholas@2224: options = options.nextSibling; nicholas@2224: text = text.nextSibling; nicholas@2224: options.style.marginRight = spanMargin; nicholas@2224: options.style.marginLeft = spanMargin; nicholas@2224: text.style.marginRight = spanMargin; nicholas@2224: text.style.marginLeft = spanMargin; nicholas@2224: } nicholas@2224: }; nicholas@2224: this.resize(); nicholas@2224: }; nicholas@2224: nicholas@2224: this.checkboxBox = function(commentQuestion) { nicholas@2224: this.specification = commentQuestion; nicholas@2224: // Create document objects to hold the comment boxes nicholas@2224: this.holder = document.createElement('div'); nicholas@2224: this.holder.className = 'comment-div'; nicholas@2224: // Create a string next to each comment asking for a comment nicholas@2224: this.string = document.createElement('span'); nicholas@2224: this.string.innerHTML = commentQuestion.statement; nicholas@2224: var br = document.createElement('br'); nicholas@2224: // Add to the holder. nicholas@2224: this.holder.appendChild(this.string); nicholas@2224: this.holder.appendChild(br); nicholas@2224: this.options = []; nicholas@2224: this.inputs = document.createElement('div'); nicholas@2224: this.span = document.createElement('div'); nicholas@2224: this.inputs.align = 'center'; nicholas@2224: this.inputs.style.marginLeft = '12px'; nicholas@2224: this.span.style.marginLeft = '12px'; nicholas@2224: this.span.align = 'center'; nicholas@2224: this.span.style.marginTop = '15px'; nicholas@2224: nicholas@2224: var optCount = commentQuestion.options.length; nicholas@2224: for (var i=0; i= 600) nicholas@2224: { nicholas@2224: boxwidth = 600; nicholas@2224: } nicholas@2224: else if (boxwidth < 400) nicholas@2224: { nicholas@2224: boxwidth = 400; nicholas@2224: } nicholas@2224: this.holder.style.width = boxwidth+"px"; nicholas@2224: var text = this.holder.children[2]; nicholas@2224: var options = this.holder.children[3]; nicholas@2224: var optCount = options.children.length; nicholas@2224: var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px'; nicholas@2224: var options = options.firstChild; nicholas@2224: var text = text.firstChild; nicholas@2224: options.style.marginRight = spanMargin; nicholas@2224: options.style.marginLeft = spanMargin; nicholas@2224: text.style.marginRight = spanMargin; nicholas@2224: text.style.marginLeft = spanMargin; nicholas@2224: while(options.nextSibling != undefined) nicholas@2224: { nicholas@2224: options = options.nextSibling; nicholas@2224: text = text.nextSibling; nicholas@2224: options.style.marginRight = spanMargin; nicholas@2224: options.style.marginLeft = spanMargin; nicholas@2224: text.style.marginRight = spanMargin; nicholas@2224: text.style.marginLeft = spanMargin; nicholas@2224: } nicholas@2224: }; nicholas@2224: this.resize(); nicholas@2224: }; nicholas@2224: nicholas@2224: this.createCommentQuestion = function(element) { nicholas@2224: var node; nicholas@2224: if (element.type == 'question') { nicholas@2224: node = new this.commentBox(element); nicholas@2224: } else if (element.type == 'radio') { nicholas@2224: node = new this.radioBox(element); nicholas@2224: } else if (element.type == 'checkbox') { nicholas@2224: node = new this.checkboxBox(element); nicholas@2224: } nicholas@2224: this.commentQuestions.push(node); nicholas@2224: return node; nicholas@2224: }; nicholas@2224: nicholas@2224: this.deleteCommentQuestions = function() nicholas@2224: { nicholas@2224: this.commentQuestions = []; nicholas@2224: }; nicholas@2224: nicholas@2224: this.outsideReferenceDOM = function(audioObject,index,inject) nicholas@2224: { nicholas@2224: this.parent = audioObject; nicholas@2224: this.outsideReferenceHolder = document.createElement('button'); nicholas@2224: this.outsideReferenceHolder.id = 'outside-reference'; nicholas@2224: this.outsideReferenceHolder.className = 'outside-reference'; nicholas@2224: this.outsideReferenceHolder.setAttribute('track-id',index); nicholas@2224: this.outsideReferenceHolder.textContent = "Play Reference"; nicholas@2224: this.outsideReferenceHolder.disabled = true; nicholas@2224: nicholas@2224: this.outsideReferenceHolder.onclick = function(event) nicholas@2224: { nicholas@2224: audioEngineContext.play(event.currentTarget.getAttribute('track-id')); nicholas@2224: }; nicholas@2224: inject.appendChild(this.outsideReferenceHolder); nicholas@2224: this.enable = function() nicholas@2224: { nicholas@2224: if (this.parent.state == 1) nicholas@2224: { nicholas@2224: this.outsideReferenceHolder.disabled = false; nicholas@2224: } nicholas@2224: }; nicholas@2224: this.updateLoading = function(progress) nicholas@2224: { nicholas@2224: if (progress != 100) nicholas@2224: { nicholas@2224: progress = String(progress); nicholas@2224: progress = progress.split('.')[0]; nicholas@2224: this.outsideReferenceHolder.textContent = progress+'%'; nicholas@2224: } else { nicholas@2224: this.outsideReferenceHolder.textContent = "Play Reference"; nicholas@2224: } nicholas@2224: }; nicholas@2224: this.startPlayback = function() nicholas@2224: { nicholas@2224: // Called when playback has begun nicholas@2224: $('.track-slider').removeClass('track-slider-playing'); nicholas@2224: $('.comment-div').removeClass('comment-box-playing'); nicholas@2224: this.outsideReferenceHolder.style.backgroundColor = "#FDD"; nicholas@2224: }; nicholas@2224: this.stopPlayback = function() nicholas@2224: { nicholas@2224: // Called when playback has stopped. This gets called even if playback never started! nicholas@2224: this.outsideReferenceHolder.style.backgroundColor = ""; nicholas@2224: }; nicholas@2224: this.exportXMLDOM = function(audioObject) nicholas@2224: { nicholas@2224: return null; nicholas@2224: }; nicholas@2224: this.getValue = function() nicholas@2224: { nicholas@2224: return 0; nicholas@2224: }; nicholas@2224: this.getPresentedId = function() nicholas@2224: { nicholas@2224: return 'Reference'; nicholas@2224: }; nicholas@2224: this.canMove = function() nicholas@2224: { nicholas@2224: return false; nicholas@2224: }; nicholas@2224: this.error = function() { nicholas@2224: // audioObject has an error!! nicholas@2224: this.outsideReferenceHolder.textContent = "Error"; nicholas@2224: this.outsideReferenceHolder.style.backgroundColor = "#F00"; nicholas@2224: } nicholas@2224: } nicholas@2224: nicholas@2224: this.playhead = new function() nicholas@2224: { nicholas@2224: this.object = document.createElement('div'); nicholas@2224: this.object.className = 'playhead'; nicholas@2224: this.object.align = 'left'; nicholas@2224: var curTime = document.createElement('div'); nicholas@2224: curTime.style.width = '50px'; nicholas@2224: this.curTimeSpan = document.createElement('span'); nicholas@2224: this.curTimeSpan.textContent = '00:00'; nicholas@2224: curTime.appendChild(this.curTimeSpan); nicholas@2224: this.object.appendChild(curTime); nicholas@2224: this.scrubberTrack = document.createElement('div'); nicholas@2224: this.scrubberTrack.className = 'playhead-scrub-track'; nicholas@2224: nicholas@2224: this.scrubberHead = document.createElement('div'); nicholas@2224: this.scrubberHead.id = 'playhead-scrubber'; nicholas@2224: this.scrubberTrack.appendChild(this.scrubberHead); nicholas@2224: this.object.appendChild(this.scrubberTrack); nicholas@2224: nicholas@2224: this.timePerPixel = 0; nicholas@2224: this.maxTime = 0; nicholas@2224: nicholas@2224: this.playbackObject; nicholas@2224: nicholas@2224: this.setTimePerPixel = function(audioObject) { nicholas@2224: //maxTime must be in seconds nicholas@2224: this.playbackObject = audioObject; nicholas@2224: this.maxTime = audioObject.buffer.buffer.duration; nicholas@2224: var width = 490; //500 - 10, 5 each side of the tracker head nicholas@2224: this.timePerPixel = this.maxTime/490; nicholas@2224: if (this.maxTime < 60) { nicholas@2224: this.curTimeSpan.textContent = '0.00'; nicholas@2224: } else { nicholas@2224: this.curTimeSpan.textContent = '00:00'; nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.update = function() { nicholas@2224: // Update the playhead position, startPlay must be called nicholas@2224: if (this.timePerPixel > 0) { nicholas@2224: var time = this.playbackObject.getCurrentPosition(); nicholas@2224: if (time > 0 && time < this.maxTime) { nicholas@2224: var width = 490; nicholas@2224: var pix = Math.floor(time/this.timePerPixel); nicholas@2224: this.scrubberHead.style.left = pix+'px'; nicholas@2224: if (this.maxTime > 60.0) { nicholas@2224: var secs = time%60; nicholas@2224: var mins = Math.floor((time-secs)/60); nicholas@2224: secs = secs.toString(); nicholas@2224: secs = secs.substr(0,2); nicholas@2224: mins = mins.toString(); nicholas@2224: this.curTimeSpan.textContent = mins+':'+secs; nicholas@2224: } else { nicholas@2224: time = time.toString(); nicholas@2224: this.curTimeSpan.textContent = time.substr(0,4); nicholas@2224: } nicholas@2224: } else { nicholas@2224: this.scrubberHead.style.left = '0px'; nicholas@2224: if (this.maxTime < 60) { nicholas@2224: this.curTimeSpan.textContent = '0.00'; nicholas@2224: } else { nicholas@2224: this.curTimeSpan.textContent = '00:00'; nicholas@2224: } nicholas@2224: } nicholas@2224: } nicholas@2224: }; nicholas@2224: nicholas@2224: this.interval = undefined; nicholas@2224: nicholas@2224: this.start = function() { nicholas@2224: if (this.playbackObject != undefined && this.interval == undefined) { nicholas@2224: if (this.maxTime < 60) { nicholas@2224: this.interval = setInterval(function(){interfaceContext.playhead.update();},10); nicholas@2224: } else { nicholas@2224: this.interval = setInterval(function(){interfaceContext.playhead.update();},100); nicholas@2224: } nicholas@2224: } nicholas@2224: }; nicholas@2224: this.stop = function() { nicholas@2224: clearInterval(this.interval); nicholas@2224: this.interval = undefined; nicholas@2224: this.scrubberHead.style.left = '0px'; nicholas@2224: if (this.maxTime < 60) { nicholas@2224: this.curTimeSpan.textContent = '0.00'; nicholas@2224: } else { nicholas@2224: this.curTimeSpan.textContent = '00:00'; nicholas@2224: } nicholas@2224: }; nicholas@2224: }; nicholas@2224: nicholas@2224: this.volume = new function() nicholas@2224: { nicholas@2224: // An in-built volume module which can be viewed on page nicholas@2224: // Includes trackers on page-by-page data nicholas@2224: // Volume does NOT reset to 0dB on each page load nicholas@2224: this.valueLin = 1.0; nicholas@2224: this.valueDB = 0.0; nicholas@2224: this.object = document.createElement('div'); nicholas@2224: this.object.id = 'master-volume-holder'; nicholas@2224: this.slider = document.createElement('input'); nicholas@2224: this.slider.id = 'master-volume-control'; nicholas@2224: this.slider.type = 'range'; nicholas@2224: this.valueText = document.createElement('span'); nicholas@2224: this.valueText.id = 'master-volume-feedback'; nicholas@2224: this.valueText.textContent = '0dB'; nicholas@2224: nicholas@2224: this.slider.min = -60; nicholas@2224: this.slider.max = 12; nicholas@2224: this.slider.value = 0; nicholas@2224: this.slider.step = 1; nicholas@2224: this.slider.onmousemove = function(event) nicholas@2224: { nicholas@2224: interfaceContext.volume.valueDB = event.currentTarget.value; nicholas@2224: interfaceContext.volume.valueLin = decibelToLinear(interfaceContext.volume.valueDB); nicholas@2224: interfaceContext.volume.valueText.textContent = interfaceContext.volume.valueDB+'dB'; nicholas@2224: audioEngineContext.outputGain.gain.value = interfaceContext.volume.valueLin; nicholas@2224: } nicholas@2224: this.slider.onmouseup = function(event) nicholas@2224: { nicholas@2224: var storePoint = testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].getAllElementsByName('volumeTracker'); nicholas@2224: if (storePoint.length == 0) nicholas@2224: { nicholas@2224: storePoint = storage.document.createElement('metricresult'); nicholas@2224: storePoint.setAttribute('name','volumeTracker'); nicholas@2224: testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].appendChild(storePoint); nicholas@2224: } nicholas@2224: else { nicholas@2224: storePoint = storePoint[0]; nicholas@2224: } nicholas@2224: var node = storage.document.createElement('movement'); nicholas@2224: node.setAttribute('test-time',audioEngineContext.timer.getTestTime()); nicholas@2224: node.setAttribute('volume',interfaceContext.volume.valueDB); nicholas@2224: node.setAttribute('format','dBFS'); nicholas@2224: storePoint.appendChild(node); nicholas@2224: } nicholas@2224: nicholas@2224: var title = document.createElement('div'); nicholas@2224: title.innerHTML = 'Master Volume Control'; nicholas@2224: title.style.fontSize = '0.75em'; nicholas@2224: title.style.width = "100%"; nicholas@2224: title.align = 'center'; nicholas@2224: this.object.appendChild(title); nicholas@2224: nicholas@2224: this.object.appendChild(this.slider); nicholas@2224: this.object.appendChild(this.valueText); nicholas@2224: } nicholas@2224: nicholas@2224: this.calibrationModuleObject = null; nicholas@2224: this.calibrationModule = function() { nicholas@2224: // This creates an on-page calibration module nicholas@2224: this.storeDOM = storage.document.createElement("calibration"); nicholas@2224: storage.root.appendChild(this.storeDOM); nicholas@2224: // The calibration is a fixed state module nicholas@2224: this.calibrationNodes = []; nicholas@2224: this.holder = null; nicholas@2224: this.build = function(inject) { nicholas@2224: var f0 = 62.5; nicholas@2224: this.holder = document.createElement("div"); nicholas@2224: this.holder.className = "calibration-holder"; nicholas@2224: this.calibrationNodes = []; nicholas@2224: while(f0 < 20000) { nicholas@2224: var obj = { nicholas@2224: root: document.createElement("div"), nicholas@2224: input: document.createElement("input"), nicholas@2224: oscillator: audioContext.createOscillator(), nicholas@2224: gain: audioContext.createGain(), nicholas@2224: f: f0, nicholas@2224: parent: this, nicholas@2224: handleEvent: function(event) { nicholas@2224: switch(event.type) { nicholas@2224: case "mouseenter": nicholas@2224: this.oscillator.start(0); nicholas@2224: break; nicholas@2224: case "mouseleave": nicholas@2224: this.oscillator.stop(0); nicholas@2224: this.oscillator = audioContext.createOscillator(); nicholas@2224: this.oscillator.connect(this.gain); nicholas@2224: this.oscillator.frequency.value = this.f; nicholas@2224: break; nicholas@2224: case "mousemove": nicholas@2224: var value = Math.pow(10,this.input.value/20); nicholas@2224: if (this.f == 1000) { nicholas@2224: audioEngineContext.outputGain.gain.value = value; nicholas@2224: interfaceContext.volume.slider.value = this.input.value; nicholas@2224: } else { nicholas@2224: this.gain.gain.value = value nicholas@2224: } nicholas@2224: break; nicholas@2224: } nicholas@2224: }, nicholas@2224: disconnect: function() { nicholas@2224: this.gain.disconnect(); nicholas@2224: } nicholas@2224: } nicholas@2224: obj.root.className = "calibration-slider"; nicholas@2224: obj.root.appendChild(obj.input); nicholas@2224: obj.oscillator.connect(obj.gain); nicholas@2224: obj.gain.connect(audioEngineContext.outputGain); nicholas@2224: obj.gain.gain.value = Math.random()*2; nicholas@2224: obj.input.value = obj.gain.gain.value; nicholas@2224: obj.input.setAttribute('orient','vertical'); nicholas@2224: obj.input.type = "range"; nicholas@2224: obj.input.min = -6; nicholas@2224: obj.input.max = 6; nicholas@2224: obj.input.step = 0.25; nicholas@2224: if (f0 != 1000) { nicholas@2224: obj.input.value = (Math.random()*12)-6; nicholas@2224: } else { nicholas@2224: obj.input.value = 0; nicholas@2224: obj.root.style.backgroundColor="rgb(255,125,125)"; nicholas@2224: } nicholas@2224: obj.input.addEventListener("mousemove",obj); nicholas@2224: obj.input.addEventListener("mouseenter",obj); nicholas@2224: obj.input.addEventListener("mouseleave",obj); nicholas@2224: obj.gain.gain.value = Math.pow(10,obj.input.value/20); nicholas@2224: obj.oscillator.frequency.value = f0; nicholas@2224: this.calibrationNodes.push(obj); nicholas@2224: this.holder.appendChild(obj.root); nicholas@2224: f0 *= 2; nicholas@2224: } nicholas@2224: inject.appendChild(this.holder); nicholas@2224: } nicholas@2224: this.collect = function() { nicholas@2224: for (var obj of this.calibrationNodes) { nicholas@2224: var node = storage.document.createElement("calibrationresult"); nicholas@2224: node.setAttribute("frequency",obj.f); nicholas@2224: node.setAttribute("range-min",obj.input.min); nicholas@2224: node.setAttribute("range-max",obj.input.max); nicholas@2224: node.setAttribute("gain-lin",obj.gain.gain.value); nicholas@2224: this.storeDOM.appendChild(node); nicholas@2224: } nicholas@2224: } nicholas@2224: } nicholas@2224: nicholas@2224: nicholas@2224: // Global Checkers nicholas@2224: // These functions will help enforce the checkers nicholas@2224: this.checkHiddenAnchor = function() nicholas@2224: { nicholas@2224: for (var ao of audioEngineContext.audioObjects) nicholas@2224: { nicholas@2224: if (ao.specification.type == "anchor") nicholas@2224: { nicholas@2224: if (ao.interfaceDOM.getValue() > (ao.specification.marker/100) && ao.specification.marker > 0) { nicholas@2224: // Anchor is not set below nicholas@2224: console.log('Anchor node not below marker value'); nicholas@2224: alert('Please keep listening'); nicholas@2224: this.storeErrorNode('Anchor node not below marker value'); nicholas@2224: return false; nicholas@2224: } nicholas@2224: } nicholas@2224: } nicholas@2224: return true; nicholas@2224: }; nicholas@2224: nicholas@2224: this.checkHiddenReference = function() nicholas@2224: { nicholas@2224: for (var ao of audioEngineContext.audioObjects) nicholas@2224: { nicholas@2224: if (ao.specification.type == "reference") nicholas@2224: { nicholas@2224: if (ao.interfaceDOM.getValue() < (ao.specification.marker/100) && ao.specification.marker > 0) { nicholas@2224: // Anchor is not set below nicholas@2224: console.log('Reference node not above marker value'); nicholas@2224: this.storeErrorNode('Reference node not above marker value'); nicholas@2224: alert('Please keep listening'); nicholas@2224: return false; nicholas@2224: } nicholas@2224: } nicholas@2224: } nicholas@2224: return true; nicholas@2224: }; nicholas@2224: nicholas@2224: this.checkFragmentsFullyPlayed = function () nicholas@2224: { nicholas@2224: // Checks the entire file has been played back nicholas@2224: // NOTE ! This will return true IF playback is Looped!!! nicholas@2224: if (audioEngineContext.loopPlayback) nicholas@2224: { nicholas@2224: console.log("WARNING - Looped source: Cannot check fragments are fully played"); nicholas@2224: return true; nicholas@2224: } nicholas@2224: var check_pass = true; nicholas@2224: var error_obj = []; nicholas@2224: for (var i = 0; i= time) nicholas@2224: { nicholas@2224: passed = true; nicholas@2224: break; nicholas@2224: } nicholas@2224: } nicholas@2224: if (passed == false) nicholas@2224: { nicholas@2224: check_pass = false; b@2258: console.log("Continue listening to track-"+object.interfaceDOM.getPresentedId()); nicholas@2224: error_obj.push(object.interfaceDOM.getPresentedId()); nicholas@2224: } nicholas@2224: } nicholas@2224: if (check_pass == false) nicholas@2224: { nicholas@2224: var str_start = "You have not completely listened to fragments "; nicholas@2224: for (var i=0; i= 300) { nicholas@2224: console.log("WARNING - Could not update at this time"); nicholas@2224: } else { nicholas@2224: var parser = new DOMParser(); nicholas@2224: var xmlDoc = parser.parseFromString(xmlhttp.responseText, "application/xml"); nicholas@2224: var response = xmlDoc.getElementsByTagName('response')[0]; nicholas@2224: if (response.getAttribute("state") == "OK") { nicholas@2224: var file = response.getElementsByTagName("file")[0]; nicholas@2224: console.log("Intermediate save: OK, written "+file.getAttribute("bytes")+"B"); nicholas@2224: } else { nicholas@2224: var message = response.getElementsByTagName("message"); nicholas@2224: console.log("Intermediate save: Error! "+message.textContent); nicholas@2224: } nicholas@2224: } nicholas@2224: } nicholas@2224: xmlhttp.send([hold.innerHTML]); nicholas@2224: } nicholas@2224: } nicholas@2224: nicholas@2224: this.createTestPageStore = function(specification) nicholas@2224: { nicholas@2224: var store = new this.pageNode(this,specification); nicholas@2224: this.testPages.push(store); nicholas@2224: return this.testPages[this.testPages.length-1]; nicholas@2224: }; nicholas@2224: nicholas@2224: this.surveyNode = function(parent,root,specification) nicholas@2224: { nicholas@2224: this.specification = specification; nicholas@2224: this.parent = parent; nicholas@2224: this.state = "empty"; nicholas@2224: this.XMLDOM = this.parent.document.createElement('survey'); nicholas@2224: this.XMLDOM.setAttribute('location',this.specification.location); nicholas@2224: this.XMLDOM.setAttribute("state",this.state); nicholas@2224: for (var optNode of this.specification.options) nicholas@2224: { nicholas@2224: if (optNode.type != 'statement') nicholas@2224: { nicholas@2224: var node = this.parent.document.createElement('surveyresult'); nicholas@2224: node.setAttribute("ref",optNode.id); nicholas@2224: node.setAttribute('type',optNode.type); nicholas@2224: this.XMLDOM.appendChild(node); nicholas@2224: } nicholas@2224: } nicholas@2224: root.appendChild(this.XMLDOM); nicholas@2224: nicholas@2224: this.postResult = function(node) nicholas@2224: { nicholas@2224: // From popup: node is the popupOption node containing both spec. and results nicholas@2224: // ID is the position nicholas@2224: if (node.specification.type == 'statement'){return;} nicholas@2224: var surveyresult = this.XMLDOM.children[0]; nicholas@2224: while(surveyresult != null) { nicholas@2224: if (surveyresult.getAttribute("ref") == node.specification.id) nicholas@2224: { nicholas@2224: break; nicholas@2224: } nicholas@2224: surveyresult = surveyresult.nextElementSibling; nicholas@2224: } nicholas@2224: switch(node.specification.type) nicholas@2224: { nicholas@2224: case "number": nicholas@2224: case "question": nicholas@2224: var child = this.parent.document.createElement('response'); nicholas@2224: child.textContent = node.response; nicholas@2224: surveyresult.appendChild(child); nicholas@2224: break; nicholas@2224: case "radio": nicholas@2224: var child = this.parent.document.createElement('response'); nicholas@2224: child.setAttribute('name',node.response.name); nicholas@2224: child.textContent = node.response.text; nicholas@2224: surveyresult.appendChild(child); nicholas@2224: break; nicholas@2224: case "checkbox": nicholas@2224: for (var i=0; i 0) nicholas@2224: { nicholas@2224: aeNode.setAttribute('marker',element.marker); nicholas@2224: } nicholas@2224: } nicholas@2224: var ae_metric = this.parent.document.createElement('metric'); nicholas@2224: aeNode.appendChild(ae_metric); nicholas@2224: this.XMLDOM.appendChild(aeNode); nicholas@2224: } nicholas@2224: nicholas@2224: this.parent.root.appendChild(this.XMLDOM); nicholas@2224: nicholas@2224: this.complete = function() { nicholas@2224: this.state = "complete"; nicholas@2224: this.XMLDOM.setAttribute("state","complete"); nicholas@2224: } nicholas@2224: }; nicholas@2224: this.update = function() { nicholas@2224: this.SessionKey.update(); nicholas@2224: } nicholas@2224: this.finish = function() nicholas@2224: { nicholas@2224: if (this.state == 0) nicholas@2224: { nicholas@2224: this.update(); nicholas@2224: } nicholas@2224: this.state = 1; nicholas@2224: return this.root; nicholas@2224: }; nicholas@2224: }