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@2682: /*globals window, document, XMLDocument, Element, XMLHttpRequest, DOMParser, console, Blob, $, Promise, navigator */ nicholas@2682: /*globals AudioBuffer, AudioBufferSourceNode */ nicholas@2682: /*globals Specification, calculateLoudness, WAVE, validateXML, showdown, pageXMLSave, loadTest, resizeWindow */ nicholas@2682: 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@2329: var gReturnURL; giuliomoro@2337: var gSaveFilenamePrefix; 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@2498: nicholas@2224: function qualifyURL(url) { nicholas@2498: var el = document.createElement('div'); nicholas@2498: 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@2498: XMLDocument.prototype.getAllElementsByName = function (name) { nicholas@2224: name = String(name); nicholas@2224: var selected = this.documentElement.getAllElementsByName(name); nicholas@2224: return selected; nicholas@2677: }; nicholas@2224: nicholas@2498: Element.prototype.getAllElementsByName = function (name) { nicholas@2224: name = String(name); nicholas@2224: var selected = []; nicholas@2224: var node = this.firstElementChild; nicholas@2677: while (node !== null) { nicholas@2498: if (node.getAttribute('name') == name) { nicholas@2224: selected.push(node); nicholas@2224: } nicholas@2498: if (node.childElementCount > 0) { nicholas@2224: selected = selected.concat(node.getAllElementsByName(name)); nicholas@2224: } nicholas@2224: node = node.nextElementSibling; nicholas@2224: } nicholas@2224: return selected; nicholas@2677: }; nicholas@2224: nicholas@2498: XMLDocument.prototype.getAllElementsByTagName = function (name) { nicholas@2224: name = String(name); nicholas@2224: var selected = this.documentElement.getAllElementsByTagName(name); nicholas@2224: return selected; nicholas@2677: }; nicholas@2224: nicholas@2498: Element.prototype.getAllElementsByTagName = function (name) { nicholas@2224: name = String(name); nicholas@2224: var selected = []; nicholas@2224: var node = this.firstElementChild; nicholas@2677: while (node !== null) { nicholas@2498: if (node.nodeName == name) { nicholas@2224: selected.push(node); nicholas@2224: } nicholas@2498: if (node.childElementCount > 0) { nicholas@2224: selected = selected.concat(node.getAllElementsByTagName(name)); nicholas@2224: } nicholas@2224: node = node.nextElementSibling; nicholas@2224: } nicholas@2224: return selected; nicholas@2677: }; nicholas@2224: nicholas@2224: // Firefox does not have an XMLDocument.prototype.getElementsByName nicholas@2224: if (typeof XMLDocument.prototype.getElementsByName != "function") { nicholas@2498: XMLDocument.prototype.getElementsByName = function (name) { nicholas@2224: name = String(name); nicholas@2224: var node = this.documentElement.firstElementChild; nicholas@2224: var selected = []; nicholas@2677: while (node !== null) { nicholas@2498: if (node.getAttribute('name') == name) { nicholas@2224: selected.push(node); nicholas@2224: } nicholas@2224: node = node.nextElementSibling; nicholas@2224: } nicholas@2224: return selected; nicholas@2677: }; nicholas@2224: } nicholas@2224: nicholas@2498: var check_dependancies = function () { nicholas@2401: // This will check for the data dependancies nicholas@2498: if (typeof (jQuery) != "function") { nicholas@2498: return false; nicholas@2498: } nicholas@2498: if (typeof (Specification) != "function") { nicholas@2498: return false; nicholas@2498: } nicholas@2498: if (typeof (calculateLoudness) != "function") { nicholas@2498: return false; nicholas@2498: } nicholas@2498: if (typeof (WAVE) != "function") { nicholas@2498: return false; nicholas@2498: } nicholas@2498: if (typeof (validateXML) != "function") { nicholas@2498: return false; nicholas@2498: } nicholas@2401: return true; nicholas@2677: }; nicholas@2401: nicholas@2498: var onload = function () { nicholas@2498: // Function called once the browser has loaded all files. nicholas@2498: // This should perform any initial commands such as structure / loading documents nicholas@2498: nicholas@2498: // Create a web audio API context nicholas@2498: // Fixed for cross-browser support nicholas@2498: var AudioContext = window.AudioContext || window.webkitAudioContext; nicholas@2677: audioContext = new AudioContext(); nicholas@2498: nicholas@2498: // Create test state nicholas@2498: testState = new stateMachine(); nicholas@2498: nicholas@2498: // Create the popup interface object nicholas@2498: popup = new interfacePopup(); nicholas@2498: nicholas@2224: // Create the specification object nicholas@2498: specification = new Specification(); nicholas@2498: nicholas@2498: // Create the interface object nicholas@2498: interfaceContext = new Interface(specification); nicholas@2498: nicholas@2498: // Create the storage object nicholas@2498: storage = new Storage(); nicholas@2498: // Define window callbacks for interface nicholas@2498: window.onresize = function (event) { nicholas@2498: interfaceContext.resizeWindow(event); nicholas@2498: }; nicholas@2498: nicholas@2677: if (window.location.search.length !== 0) { nicholas@2319: var search = window.location.search.split('?')[1]; nicholas@2319: // Now split the requests into pairs nicholas@2319: var searchQueries = search.split('&'); nicholas@2682: var url; nicholas@2498: for (var i in searchQueries) { giuliomoro@2331: // Split each key-value pair nicholas@2319: searchQueries[i] = searchQueries[i].split('='); giuliomoro@2331: var key = searchQueries[i][0]; giuliomoro@2331: var value = decodeURIComponent(searchQueries[i][1]); nicholas@2498: switch (key) { nicholas@2498: case "url": nicholas@2498: url = value; nicholas@2682: specification.url = url; nicholas@2498: break; nicholas@2498: case "returnURL": nicholas@2498: gReturnURL = value; nicholas@2498: break; nicholas@2498: case "saveFilenamePrefix": nicholas@2498: gSaveFilenamePrefix = value; nicholas@2498: break; nicholas@2319: } nicholas@2319: } nicholas@2319: loadProjectSpec(url); nicholas@2498: window.onbeforeunload = function () { nicholas@2319: return "Please only leave this page once you have completed the tests. Are you sure you have completed all testing?"; nicholas@2319: }; nicholas@2319: } nicholas@2360: interfaceContext.lightbox.resize(); nicholas@2224: }; nicholas@2224: nicholas@2224: function loadProjectSpec(url) { nicholas@2498: // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data nicholas@2498: // If url is null, request client to upload project XML document nicholas@2498: var xmlhttp = new XMLHttpRequest(); nicholas@2498: xmlhttp.open("GET", 'xml/test-schema.xsd', true); nicholas@2498: xmlhttp.onload = function () { nicholas@2687: specification.processSchema(xmlhttp.response); nicholas@2498: var r = new XMLHttpRequest(); nicholas@2498: r.open('GET', url, true); nicholas@2498: r.onload = function () { nicholas@2498: loadProjectSpecCallback(r.response); nicholas@2498: }; nicholas@2498: 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@2677: }; nicholas@2498: r.send(); nicholas@2498: }; nicholas@2498: xmlhttp.send(); nicholas@2677: } nicholas@2224: nicholas@2224: function loadProjectSpecCallback(response) { nicholas@2498: // Function called after asynchronous download of XML project specification nicholas@2498: //var decode = $.parseXML(response); nicholas@2498: //projectXML = $(decode); nicholas@2498: nicholas@2224: // Check if XML is new or a resumption nicholas@2224: var parse = new DOMParser(); nicholas@2498: var responseDocument = parse.parseFromString(response, 'text/xml'); nicholas@2224: var errorNode = responseDocument.getElementsByTagName('parsererror'); nicholas@2677: var msg, span; nicholas@2498: if (errorNode.length >= 1) { nicholas@2677: msg = document.createElement("h3"); nicholas@2498: msg.textContent = "FATAL ERROR"; nicholas@2677: span = document.createElement("span"); nicholas@2498: span.textContent = "The XML parser returned the following errors when decoding your XML file"; nicholas@2498: document.getElementsByTagName('body')[0].innerHTML = null; nicholas@2498: document.getElementsByTagName('body')[0].appendChild(msg); nicholas@2498: document.getElementsByTagName('body')[0].appendChild(span); nicholas@2498: document.getElementsByTagName('body')[0].appendChild(errorNode[0]); nicholas@2498: return; nicholas@2498: } nicholas@2677: if (responseDocument === undefined || responseDocument.firstChild === undefined) { nicholas@2677: msg = document.createElement("h3"); nicholas@2498: msg.textContent = "FATAL ERROR"; nicholas@2677: span = document.createElement("span"); nicholas@2498: 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@2498: document.getElementsByTagName('body')[0].innerHTML = null; nicholas@2498: document.getElementsByTagName('body')[0].appendChild(msg); nicholas@2498: document.getElementsByTagName('body')[0].appendChild(span); nicholas@2498: return; nicholas@2224: } nicholas@2247: if (responseDocument.firstChild.nodeName == "waet") { nicholas@2224: // document is a specification nicholas@2498: nicholas@2224: // Perform XML schema validation nicholas@2224: var Module = { nicholas@2224: xml: response, nicholas@2687: schema: specification.getSchemaString(), nicholas@2498: arguments: ["--noout", "--schema", 'test-schema.xsd', 'document.xml'] nicholas@2224: }; nicholas@2498: projectXML = responseDocument; nicholas@2224: var xmllint = validateXML(Module); nicholas@2224: console.log(xmllint); nicholas@2498: if (xmllint != 'document.xml validates\n') { nicholas@2224: document.getElementsByTagName('body')[0].innerHTML = null; nicholas@2677: msg = document.createElement("h3"); nicholas@2224: msg.textContent = "FATAL ERROR"; nicholas@2677: 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@2498: for (var i in xmllint) { nicholas@2224: document.getElementsByTagName('body')[0].appendChild(document.createElement('br')); nicholas@2677: 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@2498: specification.decode(projectXML); nicholas@2224: // Generate the session-key nicholas@2224: storage.initialise(); nicholas@2498: nicholas@2247: } else if (responseDocument.firstChild.nodeName == "waetresult") { nicholas@2224: // document is a result nicholas@2498: projectXML = document.implementation.createDocument(null, "waet"); nicholas@2294: projectXML.firstChild.appendChild(responseDocument.getElementsByTagName('waet')[0].getElementsByTagName("setup")[0].cloneNode(true)); nicholas@2677: var child = responseDocument.firstChild.firstChild, nicholas@2677: copy; nicholas@2677: 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@2677: 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@2677: copy = child; nicholas@2224: child = child.previousElementSibling; nicholas@2294: responseDocument.firstChild.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@2294: projectXML.firstChild.appendChild(responseDocument.getElementById(child.getAttribute("ref")).cloneNode(true)); nicholas@2677: copy = child; nicholas@2224: child = child.previousElementSibling; nicholas@2294: responseDocument.firstChild.removeChild(copy); nicholas@2224: } nicholas@2224: } nicholas@2224: child = child.nextElementSibling; nicholas@2224: } nicholas@2224: // Build the specification nicholas@2498: specification.decode(projectXML); nicholas@2224: // Use the original nicholas@2224: storage.initialise(responseDocument); nicholas@2224: } nicholas@2498: /// CHECK FOR SAMPLE RATE COMPATIBILITY nicholas@2689: if (isFinite(specification.sampleRate)) { nicholas@2498: if (Number(specification.sampleRate) != audioContext.sampleRate) { nicholas@2498: 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@2498: interfaceContext.lightbox.post("Error", errStr); nicholas@2498: return; nicholas@2498: } nicholas@2498: } nicholas@2498: nicholas@2624: var getInterfaces = new XMLHttpRequest(); nicholas@2624: getInterfaces.open("GET", "interfaces/interfaces.json"); nicholas@2624: getInterfaces.onerror = function (e) { nicholas@2624: throw (e); nicholas@2677: }; nicholas@2624: getInterfaces.onload = function () { nicholas@2624: if (getInterfaces.status !== 200) { nicholas@2624: throw (new Error(getInterfaces.status)); nicholas@2624: } nicholas@2624: // Get the current interface nicholas@2624: var name = specification.interface, nicholas@2624: head = document.getElementsByTagName("head")[0], nicholas@2624: data = JSON.parse(getInterfaces.responseText), nicholas@2624: interfaceObject = data.interfaces.find(function (e) { nicholas@2624: return e.name == name; nicholas@2624: }); nicholas@2624: if (!interfaceObject) { nicholas@2624: throw ("Cannot load desired interface"); nicholas@2624: } nicholas@2624: interfaceObject.scripts.forEach(function (v) { nicholas@2624: var script = document.createElement("script"); nicholas@2624: script.setAttribute("type", "text/javascript"); nicholas@2624: script.setAttribute("src", v); nicholas@2624: head.appendChild(script); nicholas@2624: }); nicholas@2624: interfaceObject.css.forEach(function (v) { nicholas@2624: var css = document.createElement("link"); nicholas@2624: css.setAttribute("rel", "stylesheet"); nicholas@2624: css.setAttribute("type", "text/css"); nicholas@2624: css.setAttribute("href", v); nicholas@2624: head.appendChild(css); nicholas@2624: }); nicholas@2677: }; nicholas@2624: getInterfaces.send(); nicholas@2498: nicholas@2677: if (gReturnURL !== undefined) { nicholas@2498: console.log("returnURL Overide from " + specification.returnURL + " to " + gReturnURL); nicholas@2329: specification.returnURL = gReturnURL; nicholas@2329: } nicholas@2677: if (gSaveFilenamePrefix !== undefined) { giuliomoro@2337: specification.saveFilenamePrefix = gSaveFilenamePrefix; giuliomoro@2337: } nicholas@2498: nicholas@2498: // Create the audio engine object nicholas@2498: 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@2498: // Save the data from interface into XML and send to destURL nicholas@2498: // If destURL is null then download XML in client nicholas@2498: // Now time to render file locally nicholas@2498: var xmlDoc = interfaceXMLSave(); nicholas@2498: var parent = document.createElement("div"); nicholas@2498: parent.appendChild(xmlDoc); nicholas@2498: var file = [parent.innerHTML]; nicholas@2498: if (destURL == "local") { nicholas@2498: var bb = new Blob(file, { nicholas@2498: type: 'application/xml' nicholas@2498: }); nicholas@2498: var dnlk = window.URL.createObjectURL(bb); nicholas@2498: var a = document.createElement("a"); nicholas@2498: a.hidden = ''; nicholas@2498: a.href = dnlk; nicholas@2498: a.download = "save.xml"; nicholas@2498: a.textContent = "Save File"; nicholas@2498: nicholas@2498: popup.showPopup(); nicholas@2498: popup.popupContent.innerHTML = "Please save the file below to give to your test supervisor
"; nicholas@2498: popup.popupContent.appendChild(a); nicholas@2498: } else { nicholas@2498: var saveUrlSuffix = ""; nicholas@2498: var saveFilenamePrefix = specification.saveFilenamePrefix; nicholas@2498: if (typeof (saveFilenamePrefix) === "string" && saveFilenamePrefix.length > 0) { nicholas@2498: saveUrlSuffix = "&saveFilenamePrefix=" + saveFilenamePrefix; nicholas@2498: } nicholas@2498: var projectReturn = ""; nicholas@2498: if (typeof specification.projectReturn == "string") { nicholas@2498: if (specification.projectReturn.substr(0, 4) == "http") { nicholas@2498: projectReturn = specification.projectReturn; nicholas@2498: } nicholas@2498: } nicholas@2498: var saveURL = projectReturn + "php/save.php?key=" + storage.SessionKey.key + saveUrlSuffix; nicholas@2677: var xmlhttp = new XMLHttpRequest(); nicholas@2498: xmlhttp.open("POST", saveURL, true); nicholas@2498: xmlhttp.setRequestHeader('Content-Type', 'text/xml'); nicholas@2498: xmlhttp.onerror = function () { nicholas@2498: console.log('Error saving file to server! Presenting download locally'); nicholas@2498: createProjectSave("local"); nicholas@2498: }; nicholas@2498: 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@2546: var response = xmlDoc.firstElementChild; nicholas@2546: if (response.nodeName == "response" && response.getAttribute("state") == "OK") { nicholas@2303: window.onbeforeunload = undefined; nicholas@2224: var file = response.getElementsByTagName("file")[0]; nicholas@2498: console.log("Save: OK, written " + file.getAttribute("bytes") + "B"); giuliomoro@2335: if (typeof specification.returnURL == "string" && specification.returnURL.length > 0) { nicholas@2498: window.location = specification.returnURL; nicholas@2303: } else { nicholas@2303: popup.popupContent.textContent = specification.exitText; nicholas@2303: } nicholas@2224: } else { nicholas@2224: var message = response.getElementsByTagName("message"); nicholas@2498: console.log("Save: Error! " + message.textContent); nicholas@2224: createProjectSave("local"); nicholas@2224: } nicholas@2224: } nicholas@2224: }; nicholas@2498: xmlhttp.send(file); nicholas@2498: popup.showPopup(); nicholas@2498: popup.popupContent.innerHTML = null; nicholas@2498: popup.popupContent.textContent = "Submitting. Please Wait"; nicholas@2498: if (typeof (popup.hideNextButton) === "function") { nicholas@2498: popup.hideNextButton(); nicholas@2498: } nicholas@2498: if (typeof (popup.hidePreviousButton) === "function") { nicholas@2498: popup.hidePreviousButton(); nicholas@2498: } nicholas@2498: } nicholas@2224: } nicholas@2224: nicholas@2498: function errorSessionDump(msg) { nicholas@2498: // Create the partial interface XML save nicholas@2498: // Include error node with message on why the dump occured nicholas@2498: popup.showPopup(); nicholas@2498: popup.popupContent.innerHTML = null; nicholas@2498: var err = document.createElement('error'); nicholas@2498: var parent = document.createElement("div"); nicholas@2498: if (typeof msg === "object") { nicholas@2498: err.appendChild(msg); nicholas@2498: popup.popupContent.appendChild(msg); nicholas@2498: nicholas@2498: } else { nicholas@2498: err.textContent = msg; nicholas@2498: popup.popupContent.innerHTML = "ERROR : " + msg; nicholas@2498: } nicholas@2498: var xmlDoc = interfaceXMLSave(); nicholas@2498: xmlDoc.appendChild(err); nicholas@2498: parent.appendChild(xmlDoc); nicholas@2498: var file = [parent.innerHTML]; nicholas@2498: var bb = new Blob(file, { nicholas@2498: type: 'application/xml' nicholas@2498: }); nicholas@2498: var dnlk = window.URL.createObjectURL(bb); nicholas@2498: var a = document.createElement("a"); nicholas@2498: a.hidden = ''; nicholas@2498: a.href = dnlk; nicholas@2498: a.download = "save.xml"; nicholas@2498: a.textContent = "Save File"; nicholas@2498: nicholas@2498: nicholas@2498: nicholas@2498: 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@2498: function interfaceXMLSave() { nicholas@2498: // Create the XML string to be exported with results nicholas@2498: return storage.finish(); nicholas@2224: } nicholas@2224: nicholas@2498: function linearToDecibel(gain) { nicholas@2498: return 20.0 * Math.log10(gain); nicholas@2224: } nicholas@2224: nicholas@2498: function decibelToLinear(gain) { nicholas@2498: return Math.pow(10, gain / 20.0); nicholas@2224: } nicholas@2224: nicholas@2498: function secondsToSamples(time, fs) { nicholas@2498: return Math.round(time * fs); nicholas@2224: } nicholas@2224: nicholas@2498: function samplesToSeconds(samples, fs) { nicholas@2224: return samples / fs; nicholas@2224: } nicholas@2224: nicholas@2224: function randomString(length) { nicholas@2677: var str = ""; nicholas@2498: for (var i = 0; i < length; i += 2) { nicholas@2498: var num = Math.floor(Math.random() * 1295); nicholas@2376: str += num.toString(36); nicholas@2376: } nicholas@2376: return str; nicholas@2376: //return Math.round((Math.pow(36, length + 1) - Math.random() * Math.pow(36, length))).toString(36).slice(1); nicholas@2224: } nicholas@2224: nicholas@2498: function randomiseOrder(input) { nicholas@2498: // This takes an array of information and randomises the order nicholas@2498: var N = input.length; nicholas@2498: nicholas@2498: var inputSequence = []; // For safety purposes: keep track of randomisation nicholas@2498: for (var counter = 0; counter < N; ++counter) nicholas@2677: inputSequence.push(counter); // Fill array nicholas@2498: var inputSequenceClone = inputSequence.slice(0); nicholas@2498: nicholas@2498: var holdArr = []; nicholas@2498: var outputSequence = []; nicholas@2498: for (var n = 0; n < N; n++) { nicholas@2498: // First pick a random number nicholas@2498: var r = Math.random(); nicholas@2498: // Multiply and floor by the number of elements left nicholas@2498: r = Math.floor(r * input.length); nicholas@2498: // Pick out that element and delete from the array nicholas@2498: holdArr.push(input.splice(r, 1)[0]); nicholas@2498: // Do the same with sequence nicholas@2498: outputSequence.push(inputSequence.splice(r, 1)[0]); nicholas@2498: } nicholas@2498: console.log(inputSequenceClone.toString()); // print original array to console nicholas@2498: console.log(outputSequence.toString()); // print randomised array to console nicholas@2498: return holdArr; nicholas@2224: } nicholas@2224: nicholas@2498: function randomSubArray(array, num) { nicholas@2224: if (num > 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@2498: 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@2498: // Creates an object to manage the popup nicholas@2498: this.popup = null; nicholas@2498: this.popupContent = null; nicholas@2498: this.popupTitle = null; nicholas@2498: this.popupResponse = null; nicholas@2498: this.buttonProceed = null; nicholas@2498: this.buttonPrevious = null; nicholas@2498: this.popupOptions = null; nicholas@2498: this.currentIndex = null; nicholas@2498: this.node = null; nicholas@2498: this.store = null; nicholas@2498: $(window).keypress(function (e) { nicholas@2498: if (e.keyCode == 13 && popup.popup.style.visibility == 'visible') { nicholas@2498: console.log(e); nicholas@2498: popup.buttonProceed.onclick(); nicholas@2498: e.preventDefault(); nicholas@2498: } nicholas@2498: }); nicholas@2677: // Generators & Processors // nicholas@2677: nicholas@2677: function processConditional(node, value) { nicholas@2677: function jumpToId(jumpID) { nicholas@2677: var index = this.popupOptions.findIndex(function (item, index, element) { nicholas@2677: if (item.specification.id == jumpID) { nicholas@2677: return true; nicholas@2677: } else { nicholas@2677: return false; nicholas@2677: } nicholas@2677: }, this); nicholas@2677: this.currentIndex = index - 1; nicholas@2677: } nicholas@2677: var conditionFunction; nicholas@2677: if (node.specification.type === "question") { nicholas@2677: conditionFunction = processQuestionConditional; nicholas@2677: } else if (node.specification.type === "checkbox") { nicholas@2677: conditionFunction = processCheckboxConditional; nicholas@2677: } else if (node.specification.type === "radio") { nicholas@2677: conditionFunction = processRadioConditional; nicholas@2689: } else if (node.specification.type === "number") { nicholas@2677: conditionFunction = processNumberConditional; nicholas@2677: } else if (node.specification.type === "slider") { nicholas@2677: conditionFunction = processSliderConditional; nicholas@2677: } else { nicholas@2677: return; nicholas@2677: } nicholas@2679: for (var i = 0; i < node.specification.conditions.length; i++) { nicholas@2677: var condition = node.specification.conditions[i]; nicholas@2677: var pass = conditionFunction(condition, value); nicholas@2677: var jumpID; nicholas@2677: if (pass) { nicholas@2677: jumpID = condition.jumpToOnPass; nicholas@2677: } else { nicholas@2677: jumpID = condition.jumpToOnFail; nicholas@2677: } nicholas@2677: if (jumpID !== undefined) { nicholas@2679: jumpToId.call(this, jumpID); nicholas@2677: break; nicholas@2677: } nicholas@2677: } nicholas@2677: } nicholas@2677: nicholas@2677: function postQuestion(node) { nicholas@2677: var textArea = document.createElement('textarea'); nicholas@2677: switch (node.specification.boxsize) { nicholas@2677: case 'small': nicholas@2677: textArea.cols = "20"; nicholas@2677: textArea.rows = "1"; nicholas@2677: break; nicholas@2677: case 'normal': nicholas@2677: textArea.cols = "30"; nicholas@2677: textArea.rows = "2"; nicholas@2677: break; nicholas@2677: case 'large': nicholas@2677: textArea.cols = "40"; nicholas@2677: textArea.rows = "5"; nicholas@2677: break; nicholas@2677: case 'huge': nicholas@2677: textArea.cols = "50"; nicholas@2677: textArea.rows = "10"; nicholas@2677: break; nicholas@2677: } nicholas@2677: if (node.response === undefined) { nicholas@2677: node.response = ""; nicholas@2677: } else { nicholas@2677: textArea.value = node.response; nicholas@2677: } nicholas@2677: this.popupResponse.appendChild(textArea); nicholas@2677: textArea.focus(); nicholas@2677: this.popupResponse.style.textAlign = "center"; nicholas@2677: this.popupResponse.style.left = "0%"; nicholas@2677: } nicholas@2677: nicholas@2677: function processQuestionConditional(condition, value) { nicholas@2677: switch (condition.check) { nicholas@2677: case "equals": nicholas@2677: // Deliberately loose check nicholas@2677: if (value == condition.value) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: case "greaterThan": nicholas@2677: case "lessThan": nicholas@2677: console.log("Survey Element of type 'question' cannot interpret greaterThan/lessThan conditions. IGNORING"); nicholas@2677: break; nicholas@2677: case "contains": nicholas@2682: if (value.includes(condition.value)) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: } nicholas@2677: return false; nicholas@2677: } nicholas@2677: nicholas@2677: function processQuestion(node) { nicholas@2679: var textArea = this.popupResponse.getElementsByTagName("textarea")[0]; nicholas@2677: if (node.specification.mandatory === true && textArea.value.length === 0) { nicholas@2677: interfaceContext.lightbox.post("Error", "This question is mandatory"); nicholas@2679: return false; nicholas@2677: } nicholas@2679: // Save the text content nicholas@2679: console.log("Question: " + node.specification.statement); nicholas@2679: console.log("Question Response: " + textArea.value); nicholas@2679: node.response = textArea.value; nicholas@2679: processConditional.call(this, node, textArea.value); nicholas@2679: return true; nicholas@2677: } nicholas@2677: nicholas@2677: function postCheckbox(node) { nicholas@2677: if (node.response === undefined) { nicholas@2677: node.response = Array(node.specification.options.length); nicholas@2677: } nicholas@2677: var table = document.createElement("table"); nicholas@2677: table.className = "popup-option-list"; nicholas@2677: table.border = "0"; nicholas@2679: node.response = []; nicholas@2679: node.specification.options.forEach(function (option, index) { nicholas@2677: var tr = document.createElement("tr"); nicholas@2677: table.appendChild(tr); nicholas@2677: var td = document.createElement("td"); nicholas@2677: tr.appendChild(td); nicholas@2677: var input = document.createElement('input'); nicholas@2677: input.id = option.name; nicholas@2677: input.type = 'checkbox'; nicholas@2677: td.appendChild(input); nicholas@2677: nicholas@2677: td = document.createElement("td"); nicholas@2677: tr.appendChild(td); nicholas@2677: var span = document.createElement('span'); nicholas@2677: span.textContent = option.text; nicholas@2677: td.appendChild(span); nicholas@2677: tr = document.createElement('div'); nicholas@2677: tr.setAttribute('name', 'option'); nicholas@2677: tr.className = "popup-option-checbox"; nicholas@2677: if (node.response[index] !== undefined) { nicholas@2677: if (node.response[index].checked === true) { nicholas@2677: input.checked = "true"; nicholas@2677: } nicholas@2677: } nicholas@2677: index++; nicholas@2677: }); nicholas@2677: this.popupResponse.appendChild(table); nicholas@2677: } nicholas@2677: nicholas@2677: function processCheckbox(node) { nicholas@2677: console.log("Checkbox: " + node.specification.statement); nicholas@2677: var inputs = this.popupResponse.getElementsByTagName('input'); nicholas@2677: node.response = []; nicholas@2677: var numChecked = 0, nicholas@2677: i; nicholas@2677: for (i = 0; i < node.specification.options.length; i++) { nicholas@2677: if (inputs[i].checked) { nicholas@2677: numChecked++; nicholas@2677: } nicholas@2677: } nicholas@2677: if (node.specification.min !== undefined) { nicholas@2677: if (node.specification.max === undefined) { nicholas@2677: if (numChecked < node.specification.min) { nicholas@2677: var msg = "You must select at least " + node.specification.min + " option"; nicholas@2677: if (node.specification.min > 1) { nicholas@2677: msg += "s"; nicholas@2677: } nicholas@2677: interfaceContext.lightbox.post("Error", msg); nicholas@2677: return; nicholas@2677: } nicholas@2677: } else { nicholas@2677: if (numChecked < node.specification.min || numChecked > node.specification.max) { nicholas@2677: if (node.specification.min == node.specification.max) { nicholas@2677: interfaceContext.lightbox.post("Error", "You must only select " + node.specification.min); nicholas@2677: } else { nicholas@2677: interfaceContext.lightbox.post("Error", "You must select between " + node.specification.min + " and " + node.specification.max); nicholas@2677: } nicholas@2679: return false; nicholas@2677: } nicholas@2677: } nicholas@2677: } nicholas@2677: for (i = 0; i < node.specification.options.length; i++) { nicholas@2677: node.response.push({ nicholas@2677: name: node.specification.options[i].name, nicholas@2677: text: node.specification.options[i].text, nicholas@2677: checked: inputs[i].checked nicholas@2677: }); nicholas@2677: console.log(node.specification.options[i].name + ": " + inputs[i].checked); nicholas@2677: } nicholas@2679: processConditional.call(this, node, node.response); nicholas@2679: return true; nicholas@2677: } nicholas@2677: nicholas@2677: function processCheckboxConditional(condition, response) { nicholas@2677: switch (condition.check) { nicholas@2677: case "contains": nicholas@2677: for (var i = 0; i < response.length; i++) { nicholas@2677: var value = response[i]; nicholas@2677: if (value.name === condition.value && value.checked) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: } nicholas@2677: break; nicholas@2677: case "equals": nicholas@2677: case "greaterThan": nicholas@2677: case "lessThan": nicholas@2677: console.log("Survey Element of type 'checkbox' cannot interpret equals/greaterThan/lessThan conditions. IGNORING"); nicholas@2677: break; nicholas@2677: default: nicholas@2677: console.log("Unknown condition. IGNORING"); nicholas@2677: break; nicholas@2677: } nicholas@2677: return false; nicholas@2677: } nicholas@2677: nicholas@2677: function postRadio(node) { nicholas@2689: if (node.response === null) { nicholas@2677: node.response = { nicholas@2677: name: "", nicholas@2677: text: "" nicholas@2677: }; nicholas@2677: } nicholas@2677: var table = document.createElement("table"); nicholas@2677: table.className = "popup-option-list"; nicholas@2677: table.border = "0"; nicholas@2689: if (node.response === null || node.response.length === 0) { nicholas@2689: node.response = []; nicholas@2689: } nicholas@2689: node.specification.options.forEach(function (option, index) { nicholas@2677: var tr = document.createElement("tr"); nicholas@2677: table.appendChild(tr); nicholas@2677: var td = document.createElement("td"); nicholas@2677: tr.appendChild(td); nicholas@2677: var input = document.createElement('input'); nicholas@2677: input.id = option.name; nicholas@2677: input.type = 'radio'; nicholas@2677: input.name = node.specification.id; nicholas@2677: td.appendChild(input); nicholas@2677: nicholas@2677: td = document.createElement("td"); nicholas@2677: tr.appendChild(td); nicholas@2677: var span = document.createElement('span'); nicholas@2677: span.textContent = option.text; nicholas@2677: td.appendChild(span); nicholas@2677: tr = document.createElement('div'); nicholas@2677: tr.setAttribute('name', 'option'); nicholas@2677: tr.className = "popup-option-checbox"; nicholas@2689: table.appendChild(tr); nicholas@2677: }); nicholas@2677: this.popupResponse.appendChild(table); nicholas@2677: } nicholas@2677: nicholas@2677: function processRadio(node) { nicholas@2677: var optHold = this.popupResponse; nicholas@2677: console.log("Radio: " + node.specification.statement); nicholas@2677: node.response = null; nicholas@2677: var i = 0; nicholas@2677: var inputs = optHold.getElementsByTagName('input'); nicholas@2677: while (node.response === null) { nicholas@2677: if (i == inputs.length) { nicholas@2677: if (node.specification.mandatory === true) { nicholas@2677: interfaceContext.lightbox.post("Error", "Please select one option"); nicholas@2679: return false; nicholas@2677: } nicholas@2677: break; nicholas@2677: } nicholas@2677: if (inputs[i].checked === true) { nicholas@2677: node.response = node.specification.options[i]; nicholas@2677: console.log("Selected: " + node.specification.options[i].name); nicholas@2677: } nicholas@2677: i++; nicholas@2677: } nicholas@2679: processConditional.call(this, node, node.response); nicholas@2679: return true; nicholas@2677: } nicholas@2677: nicholas@2677: function processRadioConditional(condition, response) { nicholas@2677: switch (condition.check) { nicholas@2677: case "equals": nicholas@2682: if (response === condition.value) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: case "contains": nicholas@2677: case "greaterThan": nicholas@2677: case "lessThan": nicholas@2677: console.log("Survey Element of type 'radio' cannot interpret contains/greaterThan/lessThan conditions. IGNORING"); nicholas@2677: break; nicholas@2677: default: nicholas@2677: console.log("Unknown condition. IGNORING"); nicholas@2677: break; nicholas@2677: } nicholas@2677: return false; nicholas@2677: } nicholas@2677: nicholas@2677: function postNumber(node) { nicholas@2677: var input = document.createElement('input'); nicholas@2677: input.type = 'textarea'; nicholas@2677: if (node.specification.min !== null) { nicholas@2677: input.min = node.specification.min; nicholas@2677: } nicholas@2677: if (node.specification.max !== null) { nicholas@2677: input.max = node.specification.max; nicholas@2677: } nicholas@2677: if (node.specification.step !== null) { nicholas@2677: input.step = node.specification.step; nicholas@2677: } nicholas@2677: if (node.response !== undefined) { nicholas@2677: input.value = node.response; nicholas@2677: } nicholas@2677: this.popupResponse.appendChild(input); nicholas@2677: this.popupResponse.style.textAlign = "center"; nicholas@2677: this.popupResponse.style.left = "0%"; nicholas@2677: } nicholas@2677: nicholas@2677: function processNumber(node) { nicholas@2677: var input = this.popupContent.getElementsByTagName('input')[0]; nicholas@2677: if (node.mandatory === true && input.value.length === 0) { nicholas@2677: interfaceContext.lightbox.post("Error", 'This question is mandatory. Please enter a number'); nicholas@2679: return false; nicholas@2677: } nicholas@2677: var enteredNumber = Number(input.value); nicholas@2677: if (isNaN(enteredNumber)) { nicholas@2677: interfaceContext.lightbox.post("Error", 'Please enter a valid number'); nicholas@2679: return false; nicholas@2677: } nicholas@2677: if (enteredNumber < node.min && node.min !== null) { nicholas@2677: interfaceContext.lightbox.post("Error", 'Number is below the minimum value of ' + node.min); nicholas@2679: return false; nicholas@2677: } nicholas@2677: if (enteredNumber > node.max && node.max !== null) { nicholas@2677: interfaceContext.lightbox.post("Error", 'Number is above the maximum value of ' + node.max); nicholas@2679: return false; nicholas@2677: } nicholas@2677: node.response = input.value; nicholas@2679: processConditional.call(this, node, node.response); nicholas@2679: return true; nicholas@2677: } nicholas@2677: nicholas@2677: function processNumberConditional(condtion, value) { nicholas@2682: var condition = condition; nicholas@2677: switch (condition.check) { nicholas@2677: case "greaterThan": nicholas@2682: if (value > Number(condition.value)) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: case "lessThan": nicholas@2682: if (value < Number(condition.value)) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: case "equals": nicholas@2682: if (value == condition.value) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: case "contains": nicholas@2677: console.log("Survey Element of type 'number' cannot interpret \"contains\" conditions. IGNORING"); nicholas@2677: break; nicholas@2677: default: nicholas@2677: console.log("Unknown condition. IGNORING"); nicholas@2677: break; nicholas@2677: } nicholas@2677: return false; nicholas@2677: } nicholas@2677: nicholas@2677: function postVideo(node) { nicholas@2677: var video = document.createElement("video"); nicholas@2677: video.src = node.specification.url; nicholas@2677: this.popupResponse.appendChild(video); nicholas@2677: } nicholas@2677: nicholas@2677: function postYoutube(node) { nicholas@2677: var iframe = document.createElement("iframe"); nicholas@2677: iframe.className = "youtube"; nicholas@2677: iframe.src = node.specification.url; nicholas@2677: this.popupResponse.appendChild(iframe); nicholas@2677: } nicholas@2677: nicholas@2677: function postSlider(node) { nicholas@2677: var hold = document.createElement('div'); nicholas@2677: var input = document.createElement('input'); nicholas@2677: input.type = 'range'; nicholas@2677: input.style.width = "90%"; nicholas@2677: if (node.specification.min !== null) { nicholas@2677: input.min = node.specification.min; nicholas@2677: } nicholas@2677: if (node.specification.max !== null) { nicholas@2677: input.max = node.specification.max; nicholas@2677: } nicholas@2677: if (node.response !== undefined) { nicholas@2677: input.value = node.response; nicholas@2677: } nicholas@2677: hold.className = "survey-slider-text-holder"; nicholas@2677: var minText = document.createElement('span'); nicholas@2677: var maxText = document.createElement('span'); nicholas@2677: minText.textContent = node.specification.leftText; nicholas@2677: maxText.textContent = node.specification.rightText; nicholas@2677: hold.appendChild(minText); nicholas@2677: hold.appendChild(maxText); nicholas@2677: this.popupResponse.appendChild(input); nicholas@2677: this.popupResponse.appendChild(hold); nicholas@2677: this.popupResponse.style.textAlign = "center"; nicholas@2677: } nicholas@2677: nicholas@2677: function processSlider(node) { nicholas@2677: var input = this.popupContent.getElementsByTagName('input')[0]; nicholas@2677: node.response = input.value; nicholas@2679: processConditional.call(this, node, node.response); nicholas@2679: return true; nicholas@2677: } nicholas@2677: nicholas@2677: function processSliderConditional(condition, value) { nicholas@2677: switch (condition.check) { nicholas@2677: case "contains": nicholas@2677: console.log("Survey Element of type 'number' cannot interpret contains conditions. IGNORING"); nicholas@2677: break; nicholas@2677: case "greaterThan": nicholas@2682: if (value > Number(condition.value)) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: case "lessThan": nicholas@2682: if (value < Number(condition.value)) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: case "equals": nicholas@2682: if (value == condition.value) { nicholas@2677: return true; nicholas@2677: } nicholas@2677: break; nicholas@2677: default: nicholas@2677: console.log("Unknown condition. IGNORING"); nicholas@2677: break; nicholas@2677: } nicholas@2677: return false; nicholas@2677: } nicholas@2498: nicholas@2498: this.createPopup = function () { nicholas@2498: // Create popup window interface nicholas@2498: var insertPoint = document.getElementById("topLevelBody"); nicholas@2498: nicholas@2498: this.popup = document.getElementById('popupHolder'); nicholas@2498: this.popup.style.left = (window.innerWidth / 2) - 250 + 'px'; nicholas@2498: this.popup.style.top = (window.innerHeight / 2) - 125 + 'px'; nicholas@2498: nicholas@2498: this.popupContent = document.getElementById('popupContent'); nicholas@2498: nicholas@2645: this.popupTitle = document.getElementById('popupTitleHolder'); nicholas@2498: nicholas@2498: this.popupResponse = document.getElementById('popupResponse'); nicholas@2498: nicholas@2498: this.buttonProceed = document.getElementById('popup-proceed'); nicholas@2498: this.buttonProceed.onclick = function () { nicholas@2498: popup.proceedClicked(); nicholas@2498: }; nicholas@2498: nicholas@2498: this.buttonPrevious = document.getElementById('popup-previous'); nicholas@2498: this.buttonPrevious.onclick = function () { nicholas@2498: popup.previousClick(); nicholas@2498: }; nicholas@2498: nicholas@2224: this.hidePopup(); nicholas@2498: this.popup.style.visibility = 'hidden'; nicholas@2498: }; nicholas@2498: nicholas@2498: this.showPopup = function () { nicholas@2677: if (this.popup === null) { nicholas@2498: this.createPopup(); nicholas@2498: } nicholas@2498: this.popup.style.visibility = 'visible'; nicholas@2498: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@2498: blank.style.visibility = 'visible'; nicholas@2498: this.popupResponse.style.left = "0%"; nicholas@2498: }; nicholas@2498: nicholas@2498: this.hidePopup = function () { nicholas@2224: if (this.popup) { nicholas@2224: this.popup.style.visibility = 'hidden'; nicholas@2224: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@2224: blank.style.visibility = 'hidden'; nicholas@2224: this.buttonPrevious.style.visibility = 'inherit'; nicholas@2224: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.postNode = function () { nicholas@2498: // This will take the node from the popupOptions and display it nicholas@2645: var node = this.popupOptions[this.currentIndex], nicholas@2646: converter = new showdown.Converter(), nicholas@2646: p = new DOMParser(); nicholas@2498: this.popupResponse.innerHTML = ""; nicholas@2648: this.popupTitle.innerHTML = ""; nicholas@2647: this.popupTitle.appendChild(p.parseFromString(converter.makeHtml(node.specification.statement), "text/html").getElementsByTagName("body")[0].firstElementChild); nicholas@2498: if (node.specification.type == 'question') { nicholas@2679: postQuestion.call(this, node); nicholas@2498: } else if (node.specification.type == 'checkbox') { nicholas@2679: postCheckbox.call(this, node); nicholas@2498: } else if (node.specification.type == 'radio') { nicholas@2679: postRadio.call(this, node); nicholas@2498: } else if (node.specification.type == 'number') { nicholas@2679: postNumber.call(this, node); nicholas@2498: } else if (node.specification.type == "video") { nicholas@2679: postVideo.call(this, node); nicholas@2491: } else if (node.specification.type == "youtube") { nicholas@2679: postYoutube.call(this, node); n@2583: } else if (node.specification.type == "slider") { nicholas@2679: postSlider.call(this, node); nicholas@2491: } nicholas@2498: if (this.currentIndex + 1 == this.popupOptions.length) { nicholas@2498: if (this.node.location == "pre") { nicholas@2498: this.buttonProceed.textContent = 'Start'; nicholas@2498: } else { nicholas@2498: this.buttonProceed.textContent = 'Submit'; nicholas@2498: } nicholas@2498: } else { nicholas@2498: this.buttonProceed.textContent = 'Next'; nicholas@2498: } nicholas@2498: if (this.currentIndex > 0) nicholas@2498: this.buttonPrevious.style.visibility = 'visible'; nicholas@2498: else nicholas@2498: this.buttonPrevious.style.visibility = 'hidden'; nicholas@2498: }; nicholas@2498: nicholas@2498: this.initState = function (node, store) { nicholas@2498: //Call this with your preTest and postTest nodes when needed to nicholas@2498: // initialise the popup procedure. nicholas@2498: if (node.options.length > 0) { nicholas@2498: this.popupOptions = []; nicholas@2498: this.node = node; nicholas@2498: this.store = store; nicholas@2677: node.options.forEach(function (opt) { nicholas@2498: this.popupOptions.push({ nicholas@2498: specification: opt, nicholas@2498: response: null nicholas@2498: }); nicholas@2677: }, this); nicholas@2498: this.currentIndex = 0; nicholas@2498: this.showPopup(); nicholas@2498: this.postNode(); nicholas@2498: } else { nicholas@2498: advanceState(); nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.proceedClicked = function () { nicholas@2498: // Each time the popup button is clicked! nicholas@2677: if (testState.stateIndex === 0 && specification.calibration) { nicholas@2224: interfaceContext.calibrationModuleObject.collect(); nicholas@2224: advanceState(); nicholas@2224: return; nicholas@2224: } nicholas@2679: var node = this.popupOptions[this.currentIndex], nicholas@2679: pass = true; nicholas@2498: if (node.specification.type == 'question') { nicholas@2498: // Must extract the question data nicholas@2679: pass = processQuestion.call(this, node); nicholas@2498: } else if (node.specification.type == 'checkbox') { nicholas@2498: // Must extract checkbox data nicholas@2679: pass = processCheckbox.call(this, node); nicholas@2677: } else if (node.specification.type == "radio") { nicholas@2464: // Perform the conditional nicholas@2679: pass = processRadio.call(this, node); nicholas@2677: } else if (node.specification.type == "number") { nicholas@2464: // Perform the conditional nicholas@2679: pass = processNumber.call(this, node); n@2583: } else if (node.specification.type == 'slider') { nicholas@2679: pass = processSlider.call(this, node); nicholas@2679: } nicholas@2679: if (pass === false) { nicholas@2679: return; nicholas@2498: } nicholas@2498: this.currentIndex++; nicholas@2498: if (this.currentIndex < this.popupOptions.length) { nicholas@2498: this.postNode(); nicholas@2498: } else { nicholas@2498: // Reached the end of the popupOptions nicholas@2645: this.popupTitle.innerHTML = ""; nicholas@2498: this.popupResponse.innerHTML = ""; nicholas@2498: this.hidePopup(); nicholas@2677: this.popupOptions.forEach(function (node) { nicholas@2498: this.store.postResult(node); nicholas@2677: }, this); nicholas@2224: this.store.complete(); nicholas@2498: advanceState(); nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.previousClick = function () { nicholas@2498: // Triggered when the 'Back' button is clicked in the survey nicholas@2498: if (this.currentIndex > 0) { nicholas@2498: this.currentIndex--; nicholas@2498: this.postNode(); nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.resize = function (event) { nicholas@2498: // Called on window resize; nicholas@2677: if (this.popup !== null) { nicholas@2498: this.popup.style.left = (window.innerWidth / 2) - 250 + 'px'; nicholas@2498: this.popup.style.top = (window.innerHeight / 2) - 125 + 'px'; nicholas@2498: var blank = document.getElementsByClassName('testHalt')[0]; nicholas@2498: blank.style.width = window.innerWidth; nicholas@2498: blank.style.height = window.innerHeight; nicholas@2498: } nicholas@2498: }; nicholas@2498: this.hideNextButton = function () { nicholas@2224: this.buttonProceed.style.visibility = "hidden"; nicholas@2677: }; nicholas@2498: this.hidePreviousButton = function () { nicholas@2224: this.buttonPrevious.style.visibility = "hidden"; nicholas@2677: }; nicholas@2498: this.showNextButton = function () { nicholas@2224: this.buttonProceed.style.visibility = "visible"; nicholas@2677: }; nicholas@2498: this.showPreviousButton = function () { nicholas@2224: this.buttonPrevious.style.visibility = "visible"; nicholas@2677: }; nicholas@2224: } nicholas@2224: nicholas@2498: function advanceState() { nicholas@2498: // Just for complete clarity nicholas@2498: testState.advanceState(); nicholas@2224: } nicholas@2224: nicholas@2498: function stateMachine() { nicholas@2498: // Object prototype for tracking and managing the test state nicholas@2498: this.stateMap = []; nicholas@2498: this.preTestSurvey = null; nicholas@2498: this.postTestSurvey = null; nicholas@2498: this.stateIndex = null; nicholas@2498: this.currentStateMap = null; nicholas@2498: this.currentStatePosition = null; nicholas@2224: this.currentStore = null; nicholas@2498: this.initialise = function () { nicholas@2498: nicholas@2498: // Get the data from Specification nicholas@2498: var pagePool = []; nicholas@2224: var pageInclude = []; nicholas@2681: var i; nicholas@2681: for (i = 0; i < specification.pages.length; i++) { nicholas@2678: var page = specification.pages[i]; nicholas@2224: if (page.alwaysInclude) { nicholas@2224: pageInclude.push(page); nicholas@2224: } else { nicholas@2224: pagePool.push(page); nicholas@2224: } nicholas@2498: } nicholas@2498: 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@2678: if (specification.poolSize === 0) { nicholas@2224: numPages = specification.pages.length; nicholas@2224: } nicholas@2224: numPages -= pageInclude.length; nicholas@2498: 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@2498: subarr = randomSubArray(pagePool, numPages); nicholas@2224: } else { nicholas@2224: // Append the matching number nicholas@2498: subarr = pagePool.slice(0, numPages); nicholas@2224: } nicholas@2224: pageInclude = pageInclude.concat(subarr); nicholas@2224: } nicholas@2498: nicholas@2224: // We now have our selected pages in pageInclude array nicholas@2498: if (specification.randomiseOrder) { nicholas@2498: pageInclude = randomiseOrder(pageInclude); nicholas@2498: } nicholas@2681: for (i = 0; i < pageInclude.length; i++) { nicholas@2224: pageInclude[i].presentedId = i; nicholas@2498: this.stateMap.push(pageInclude[i]); nicholas@2224: // For each selected page, we must get the sub pool nicholas@2678: if (pageInclude[i].poolSize !== 0 && pageInclude[i].poolSize !== pageInclude[i].audioElements.length) { nicholas@2224: var elemInclude = []; nicholas@2224: var elemPool = []; nicholas@2681: for (var j = 0; j < pageInclude[i].audioElements.length; j++) { nicholas@2681: var elem = pageInclude[i].audioElements[j]; nicholas@2559: if (elem.alwaysInclude || elem.type != "normal") { nicholas@2224: elemInclude.push(elem); nicholas@2224: } else { nicholas@2224: elemPool.push(elem); nicholas@2224: } nicholas@2224: } nicholas@2224: var numElems = pageInclude[i].poolSize - elemInclude.length; nicholas@2498: pageInclude[i].audioElements = elemInclude.concat(randomSubArray(elemPool, numElems)); nicholas@2224: } nicholas@2224: storage.createTestPageStore(pageInclude[i]); nicholas@2224: audioEngineContext.loadPageData(pageInclude[i]); nicholas@2498: } nicholas@2498: nicholas@2678: if (specification.preTest !== null) { nicholas@2498: this.preTestSurvey = specification.preTest; nicholas@2498: } nicholas@2678: if (specification.postTest !== null) { nicholas@2498: this.postTestSurvey = specification.postTest; nicholas@2498: } nicholas@2498: nicholas@2498: if (this.stateMap.length > 0) { nicholas@2678: if (this.stateIndex !== null) { nicholas@2498: console.log('NOTE - State already initialise'); nicholas@2498: } nicholas@2498: this.stateIndex = -2; nicholas@2224: console.log('Starting test...'); nicholas@2498: } else { nicholas@2498: console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP'); nicholas@2498: } nicholas@2498: }; nicholas@2498: this.advanceState = function () { nicholas@2678: if (this.stateIndex === null) { nicholas@2498: this.initialise(); nicholas@2498: } nicholas@2357: if (this.stateIndex > -2) { nicholas@2357: storage.update(); nicholas@2357: } nicholas@2498: if (this.stateIndex == -2) { nicholas@2224: this.stateIndex++; nicholas@2678: if (this.preTestSurvey !== null) { nicholas@2498: popup.initState(this.preTestSurvey, storage.globalPreTest); nicholas@2498: } else { nicholas@2498: this.advanceState(); nicholas@2498: } nicholas@2498: } 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@2498: } else if (this.stateIndex == this.stateMap.length) { nicholas@2498: // All test pages complete, post test nicholas@2498: console.log('Ending test ...'); nicholas@2498: this.stateIndex++; nicholas@2678: if (this.postTestSurvey === null) { nicholas@2498: this.advanceState(); nicholas@2498: } else { nicholas@2498: popup.initState(this.postTestSurvey, storage.globalPostTest); nicholas@2498: } nicholas@2498: } else if (this.stateIndex > this.stateMap.length) { nicholas@2498: createProjectSave(specification.projectReturn); nicholas@2498: } else { nicholas@2224: popup.hidePopup(); nicholas@2678: if (this.currentStateMap === null) { nicholas@2498: this.currentStateMap = this.stateMap[this.stateIndex]; nicholas@2349: // Find and extract the outside reference nicholas@2498: var elements = [], nicholas@2498: ref = []; nicholas@2681: var elem = this.currentStateMap.audioElements.pop(); nicholas@2681: while (elem) { nicholas@2399: if (elem.type == "outside-reference") { nicholas@2399: ref.push(elem); nicholas@2498: } else { nicholas@2399: elements.push(elem); nicholas@2399: } nicholas@2681: elem = this.currentStateMap.audioElements.pop(); nicholas@2349: } nicholas@2443: elements = elements.reverse(); nicholas@2498: if (this.currentStateMap.randomiseOrder) { nicholas@2498: elements = randomiseOrder(elements); nicholas@2498: } nicholas@2399: this.currentStateMap.audioElements = elements.concat(ref); nicholas@2498: nicholas@2224: this.currentStore = storage.testPages[this.stateIndex]; nicholas@2678: if (this.currentStateMap.preTest !== null) { nicholas@2498: this.currentStatePosition = 'pre'; nicholas@2498: popup.initState(this.currentStateMap.preTest, storage.testPages[this.stateIndex].preTest); nicholas@2498: } else { nicholas@2498: this.currentStatePosition = 'test'; nicholas@2498: } nicholas@2498: interfaceContext.newPage(this.currentStateMap, storage.testPages[this.stateIndex]); nicholas@2498: return; nicholas@2498: } nicholas@2498: switch (this.currentStatePosition) { nicholas@2498: case 'pre': nicholas@2498: this.currentStatePosition = 'test'; nicholas@2498: break; nicholas@2498: case 'test': nicholas@2498: this.currentStatePosition = 'post'; nicholas@2498: // Save the data nicholas@2498: this.testPageCompleted(); nicholas@2678: if (this.currentStateMap.postTest === null) { nicholas@2498: this.advanceState(); nicholas@2498: return; nicholas@2498: } else { nicholas@2498: popup.initState(this.currentStateMap.postTest, storage.testPages[this.stateIndex].postTest); nicholas@2498: } nicholas@2498: break; nicholas@2498: case 'post': nicholas@2498: this.stateIndex++; nicholas@2498: this.currentStateMap = null; nicholas@2498: this.advanceState(); nicholas@2498: break; nicholas@2678: } nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.testPageCompleted = function () { nicholas@2498: // Function called each time a test page has been completed nicholas@2498: var storePoint = storage.testPages[this.stateIndex]; nicholas@2498: // First get the test metric nicholas@2498: nicholas@2498: var metric = storePoint.XMLDOM.getElementsByTagName('metric')[0]; nicholas@2498: if (audioEngineContext.metric.enableTestTimer) { nicholas@2498: var testTime = storePoint.parent.document.createElement('metricresult'); nicholas@2498: testTime.id = 'testTime'; nicholas@2498: testTime.textContent = audioEngineContext.timer.testDuration; nicholas@2498: metric.appendChild(testTime); nicholas@2498: } nicholas@2498: nicholas@2498: var audioObjects = audioEngineContext.audioObjects; nicholas@2681: audioEngineContext.audioObjects.forEach(function (ao) { nicholas@2498: ao.exportXMLDOM(); nicholas@2681: }); nicholas@2681: interfaceContext.commentQuestions.forEach(function (element) { nicholas@2498: element.exportXMLDOM(storePoint); nicholas@2681: }); nicholas@2498: pageXMLSave(storePoint.XMLDOM, this.currentStateMap); nicholas@2224: storePoint.complete(); nicholas@2498: }; nicholas@2498: nicholas@2498: this.getCurrentTestPage = function () { nicholas@2498: if (this.stateIndex >= 0 && this.stateIndex < this.stateMap.length) { nicholas@2310: return this.currentStateMap; nicholas@2310: } else { nicholas@2310: return null; nicholas@2310: } nicholas@2678: }; nicholas@2498: this.getCurrentTestPageStore = function () { nicholas@2498: if (this.stateIndex >= 0 && this.stateIndex < this.stateMap.length) { nicholas@2312: return this.currentStore; nicholas@2312: } else { nicholas@2312: return null; nicholas@2312: } nicholas@2678: }; nicholas@2224: } nicholas@2224: nicholas@2224: function AudioEngine(specification) { nicholas@2498: nicholas@2498: // Create two output paths, the main outputGain and fooGain. nicholas@2498: // Output gain is default to 1 and any items for playback route here nicholas@2498: // Foo gain is used for analysis to ensure paths get processed, but are not heard nicholas@2498: // because web audio will optimise and any route which does not go to the destination gets ignored. nicholas@2498: this.outputGain = audioContext.createGain(); nicholas@2498: this.fooGain = audioContext.createGain(); nicholas@2508: this.fooGain.gain.value = 0; nicholas@2498: nicholas@2498: // Use this to detect playback state: 0 - stopped, 1 - playing nicholas@2498: this.status = 0; nicholas@2498: nicholas@2498: // Connect both gains to output nicholas@2498: this.outputGain.connect(audioContext.destination); nicholas@2498: this.fooGain.connect(audioContext.destination); nicholas@2498: nicholas@2498: // Create the timer Object nicholas@2498: this.timer = new timer(); nicholas@2498: // Create session metrics nicholas@2498: this.metric = new sessionMetrics(this, specification); nicholas@2498: nicholas@2498: this.loopPlayback = false; nicholas@2351: this.synchPlayback = false; nicholas@2351: this.pageSpecification = null; nicholas@2498: nicholas@2498: this.pageStore = null; nicholas@2498: nicholas@2508: // Chrome 53+ Error solution nicholas@2508: // Empty buffer for keep-alive nicholas@2508: var nullBuffer = audioContext.createBuffer(1, audioContext.sampleRate, audioContext.sampleRate); nicholas@2508: this.nullBufferSource = audioContext.createBufferSource(); nicholas@2508: this.nullBufferSource.buffer = nullBuffer; nicholas@2508: this.nullBufferSource.loop = true; nicholas@2508: this.nullBufferSource.start(0); nicholas@2508: nicholas@2498: // Create store for new audioObjects nicholas@2498: this.audioObjects = []; nicholas@2498: nicholas@2498: this.buffers = []; nicholas@2498: this.bufferObj = function () { nicholas@2617: var urls = []; nicholas@2498: this.buffer = null; nicholas@2498: this.users = []; nicholas@2224: this.progress = 0; nicholas@2224: this.status = 0; nicholas@2498: this.ready = function () { nicholas@2498: if (this.status >= 2) { nicholas@2224: this.status = 3; nicholas@2224: } nicholas@2498: for (var i = 0; i < this.users.length; i++) { nicholas@2498: this.users[i].state = 1; nicholas@2678: if (this.users[i].interfaceDOM !== null) { nicholas@2498: this.users[i].bufferLoaded(this); nicholas@2498: } nicholas@2498: } nicholas@2498: }; nicholas@2617: this.setUrls = function (obj) { nicholas@2617: // Obj must be an array of pairs: nicholas@2617: // [{sampleRate, url}] nicholas@2617: var localFs = audioContext.sampleRate, nicholas@2617: list = [], nicholas@2617: i; nicholas@2617: for (i = 0; i < obj.length; i++) { nicholas@2617: if (obj[i].sampleRate == localFs) { nicholas@2617: list.push(obj.splice(i, 1)[0]); nicholas@2617: } nicholas@2617: } nicholas@2617: list = list.concat(obj); nicholas@2617: urls = list; nicholas@2617: }; nicholas@2617: this.hasUrl = function (checkUrl) { nicholas@2617: var l = urls.length, nicholas@2617: i; nicholas@2617: for (i = 0; i < l; i++) { nicholas@2617: if (urls[i].url == checkUrl) { nicholas@2617: return true; nicholas@2617: } nicholas@2617: } nicholas@2617: return false; nicholas@2678: }; nicholas@2617: this.getMedia = function () { nicholas@2615: var self = this; nicholas@2616: var currentUrlIndex = 0; nicholas@2498: nicholas@2615: function get(fqurl) { nicholas@2615: return new Promise(function (resolve, reject) { nicholas@2615: var req = new XMLHttpRequest(); nicholas@2615: req.open('GET', fqurl, true); nicholas@2615: req.responseType = 'arraybuffer'; nicholas@2615: req.onload = function () { nicholas@2615: if (req.status == 200) { nicholas@2615: resolve(req.response); nicholas@2615: } nicholas@2615: }; nicholas@2615: req.onerror = function () { nicholas@2615: reject(new Error(req.statusText)); nicholas@2615: }; nicholas@2615: nicholas@2615: req.addEventListener("progress", progressCallback.bind(self)); nicholas@2615: req.send(); nicholas@2615: }); nicholas@2615: } nicholas@2615: nicholas@2615: function getNextURL() { nicholas@2615: currentUrlIndex++; nicholas@2615: var self = this; nicholas@2617: if (currentUrlIndex >= urls.length) { nicholas@2615: processError(); nicholas@2615: } else { nicholas@2617: return get(urls[currentUrlIndex].url).then(processAudio.bind(self)).catch(getNextURL.bind(self)); nicholas@2615: } nicholas@2615: } nicholas@2498: nicholas@2498: // Create callback to decode the data asynchronously nicholas@2615: function processAudio(response) { nicholas@2615: var self = this; nicholas@2615: return audioContext.decodeAudioData(response, function (decodedData) { nicholas@2615: self.buffer = decodedData; nicholas@2615: self.status = 2; nicholas@2615: calculateLoudness(self, "I"); nicholas@2615: return true; nicholas@2498: }, function (e) { nicholas@2403: var waveObj = new WAVE(); nicholas@2678: if (waveObj.open(response) === 0) { nicholas@2615: self.buffer = audioContext.createBuffer(waveObj.num_channels, waveObj.num_samples, waveObj.sample_rate); nicholas@2498: for (var c = 0; c < waveObj.num_channels; c++) { nicholas@2615: var buffer_ptr = self.buffer.getChannelData(c); nicholas@2498: for (var n = 0; n < waveObj.num_samples; n++) { nicholas@2403: buffer_ptr[n] = waveObj.decoded_data[c][n]; nicholas@2224: } nicholas@2224: } nicholas@2403: } nicholas@2678: if (self.buffer !== undefined) { nicholas@2615: self.status = 2; nicholas@2615: calculateLoudness(self, "I"); nicholas@2615: return true; nicholas@2403: } nicholas@2678: waveObj = undefined; nicholas@2615: return false; nicholas@2403: }); nicholas@2615: } nicholas@2498: nicholas@2224: // Create callback for any error in loading nicholas@2615: function processError() { nicholas@2615: this.status = -1; nicholas@2615: for (var i = 0; i < this.users.length; i++) { nicholas@2615: this.users[i].state = -1; nicholas@2678: if (this.users[i].interfaceDOM !== null) { nicholas@2615: this.users[i].bufferLoaded(this); nicholas@2224: } nicholas@2224: } nicholas@2617: interfaceContext.lightbox.post("Error", "Could not load resource " + urls[currentUrlIndex].url); nicholas@2224: } nicholas@2498: nicholas@2615: function progressCallback(event) { nicholas@2498: if (event.lengthComputable) { nicholas@2615: this.progress = event.loaded / event.total; nicholas@2615: for (var i = 0; i < this.users.length; i++) { nicholas@2678: if (this.users[i].interfaceDOM !== null) { nicholas@2615: if (typeof this.users[i].interfaceDOM.updateLoading === "function") { nicholas@2615: this.users[i].interfaceDOM.updateLoading(this.progress * 100); nicholas@2498: } nicholas@2498: } nicholas@2498: } nicholas@2498: } nicholas@2681: } nicholas@2615: nicholas@2615: this.progress = 0; nicholas@2224: this.status = 1; nicholas@2617: currentUrlIndex = 0; nicholas@2617: get(urls[0].url).then(processAudio.bind(self)).catch(getNextURL.bind(self)); nicholas@2498: }; nicholas@2498: nicholas@2498: this.registerAudioObject = function (audioObject) { nicholas@2224: // Called by an audioObject to register to the buffer for use nicholas@2224: // First check if already in the register pool nicholas@2681: this.users.forEach(function (object) { nicholas@2682: if (audioObject.id == object.id) { nicholas@2498: return 0; nicholas@2498: } nicholas@2681: }); nicholas@2224: this.users.push(audioObject); nicholas@2498: if (this.status == 3 || this.status == -1) { nicholas@2224: // The buffer is already ready, trigger bufferLoaded nicholas@2224: audioObject.bufferLoaded(this); nicholas@2224: } nicholas@2224: }; nicholas@2498: nicholas@2498: this.copyBuffer = function (preSilenceTime, postSilenceTime) { nicholas@2224: // Copies the entire bufferObj. nicholas@2678: if (preSilenceTime === undefined) { nicholas@2498: preSilenceTime = 0; nicholas@2498: } nicholas@2678: if (postSilenceTime === undefined) { nicholas@2498: postSilenceTime = 0; nicholas@2498: } nicholas@2498: var preSilenceSamples = secondsToSamples(preSilenceTime, this.buffer.sampleRate); nicholas@2498: var postSilenceSamples = secondsToSamples(postSilenceTime, this.buffer.sampleRate); nicholas@2498: var newLength = this.buffer.length + preSilenceSamples + postSilenceSamples; nicholas@2460: var copybuffer = audioContext.createBuffer(this.buffer.numberOfChannels, newLength, this.buffer.sampleRate); nicholas@2681: var c; nicholas@2224: // Now we can use some efficient background copy schemes if we are just padding the end nicholas@2678: if (preSilenceSamples === 0 && typeof copybuffer.copyToChannel === "function") { nicholas@2681: for (c = 0; c < this.buffer.numberOfChannels; c++) { nicholas@2498: copybuffer.copyToChannel(this.buffer.getChannelData(c), c); nicholas@2224: } nicholas@2224: } else { nicholas@2681: for (c = 0; c < this.buffer.numberOfChannels; c++) { nicholas@2224: var src = this.buffer.getChannelData(c); nicholas@2460: var dst = copybuffer.getChannelData(c); nicholas@2498: for (var n = 0; n < src.length; n++) nicholas@2498: dst[n + preSilenceSamples] = src[n]; nicholas@2224: } nicholas@2224: } nicholas@2224: // Copy in the rest of the buffer information nicholas@2460: copybuffer.lufs = this.buffer.lufs; nicholas@2460: copybuffer.playbackGain = this.buffer.playbackGain; nicholas@2460: return copybuffer; nicholas@2678: }; nicholas@2498: nicholas@2498: this.cropBuffer = function (startTime, stopTime) { nicholas@2460: // Copy and return the cropped buffer nicholas@2498: var start_sample = Math.floor(startTime * this.buffer.sampleRate); nicholas@2498: var stop_sample = Math.floor(stopTime * this.buffer.sampleRate); nicholas@2460: var newLength = stop_sample - start_sample; nicholas@2460: var copybuffer = audioContext.createBuffer(this.buffer.numberOfChannels, newLength, this.buffer.sampleRate); nicholas@2460: // Now we can use some efficient background copy schemes if we are just padding the end nicholas@2498: for (var c = 0; c < this.buffer.numberOfChannels; c++) { nicholas@2460: var buffer = this.buffer.getChannelData(c); nicholas@2498: var sub_frame = buffer.subarray(start_sample, stop_sample); nicholas@2460: if (typeof copybuffer.copyToChannel == "function") { nicholas@2498: copybuffer.copyToChannel(sub_frame, c); nicholas@2460: } else { nicholas@2460: var dst = copybuffer.getChannelData(c); nicholas@2498: for (var n = 0; n < newLength; n++) nicholas@2505: dst[n] = buffer[n + start_sample]; nicholas@2460: } nicholas@2460: } nicholas@2460: return copybuffer; nicholas@2678: }; nicholas@2498: }; nicholas@2498: nicholas@2498: this.loadPageData = function (page) { nicholas@2224: // Load the URL from pages nicholas@2681: function loadAudioElementData(element) { nicholas@2224: var URL = page.hostURL + element.url; nicholas@2681: var buffer = this.buffers.find(function (buffObj) { nicholas@2681: return buffObj.hasUrl(URL); nicholas@2681: }); nicholas@2681: if (buffer === undefined) { nicholas@2224: buffer = new this.bufferObj(); nicholas@2617: var urls = [{ nicholas@2617: url: URL, nicholas@2617: sampleRate: element.sampleRate nicholas@2617: }]; nicholas@2615: element.alternatives.forEach(function (e) { nicholas@2617: urls.push({ nicholas@2617: url: e.url, nicholas@2617: sampleRate: e.sampleRate nicholas@2617: }); nicholas@2615: }); nicholas@2617: buffer.setUrls(urls); nicholas@2617: buffer.getMedia(); nicholas@2224: this.buffers.push(buffer); nicholas@2224: } nicholas@2224: } nicholas@2681: page.audioElements.forEach(loadAudioElementData, this); nicholas@2224: }; nicholas@2498: nicholas@2681: function playNormal(id) { nicholas@2681: var playTime = audioContext.currentTime + 0.1; nicholas@2681: var stopTime = playTime + specification.crossFade; nicholas@2681: this.audioObjects.forEach(function (ao) { nicholas@2681: if (ao.id === id) { nicholas@2681: ao.play(playTime); nicholas@2681: } else { nicholas@2681: ao.stop(stopTime); nicholas@2681: } nicholas@2681: }); nicholas@2681: } nicholas@2681: nicholas@2681: function playLoopSync(id) { nicholas@2681: var playTime = audioContext.currentTime + 0.1; nicholas@2681: var stopTime = playTime + specification.crossFade; nicholas@2681: this.audioObjects.forEach(function (ao) { nicholas@2681: ao.play(playTime); nicholas@2681: if (ao.id === id) { nicholas@2681: ao.loopStart(playTime); nicholas@2681: } else { nicholas@2681: ao.loopStop(stopTime); nicholas@2681: } nicholas@2681: }); nicholas@2681: } nicholas@2681: nicholas@2498: this.play = function (id) { nicholas@2498: // Start the timer and set the audioEngine state to playing (1) nicholas@2681: if (typeof id !== "number" || id < 0 || id > this.audioObjects.length) { nicholas@2681: throw ('FATAL - Passed id was undefined - AudioEngineContext.play(id)'); nicholas@2498: } nicholas@2678: if (this.status === 1) { nicholas@2498: this.timer.startTest(); nicholas@2681: interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]); nicholas@2498: if (this.synchPlayback && this.loopPlayback) { nicholas@2351: // Traditional looped playback nicholas@2681: playLoopSync.call(this, id); nicholas@2681: } else { nicholas@2681: if (this.bufferReady(id) === false) { nicholas@2681: console.log("Cannot play. Buffer not ready"); nicholas@2681: return; nicholas@2498: } nicholas@2681: playNormal.call(this, id); nicholas@2498: } nicholas@2498: interfaceContext.playhead.start(); nicholas@2498: } nicholas@2498: }; nicholas@2224: nicholas@2498: this.stop = function () { nicholas@2498: // Send stop and reset command to all playback buffers nicholas@2498: if (this.status == 1) { nicholas@2498: var setTime = audioContext.currentTime + 0.1; nicholas@2681: this.audioObjects.forEach(function (a) { nicholas@2681: a.stop(setTime); nicholas@2681: }); nicholas@2498: interfaceContext.playhead.stop(); nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.newTrack = function (element) { nicholas@2498: // Pull data from given URL into new audio buffer nicholas@2498: // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin' nicholas@2498: nicholas@2498: // Create the audioObject with ID of the new track length; nicholas@2682: var audioObjectId = this.audioObjects.length; nicholas@2498: this.audioObjects[audioObjectId] = new audioObject(audioObjectId); nicholas@2498: nicholas@2498: // Check if audioObject buffer is currently stored by full URL nicholas@2498: var URL = testState.currentStateMap.hostURL + element.url; nicholas@2681: var buffer = this.buffers.find(function (buffObj) { nicholas@2681: return buffObj.hasUrl(URL); nicholas@2681: }); nicholas@2681: if (buffer === undefined) { nicholas@2498: console.log("[WARN]: Buffer was not loaded in pre-test! " + URL); nicholas@2498: buffer = new this.bufferObj(); nicholas@2224: this.buffers.push(buffer); nicholas@2498: buffer.getMedia(URL); nicholas@2498: } nicholas@2498: this.audioObjects[audioObjectId].specification = element; nicholas@2498: this.audioObjects[audioObjectId].url = URL; nicholas@2498: // Obtain store node nicholas@2498: var aeNodes = this.pageStore.XMLDOM.getElementsByTagName('audioelement'); nicholas@2498: for (var i = 0; i < aeNodes.length; i++) { nicholas@2498: if (aeNodes[i].getAttribute("ref") == element.id) { nicholas@2498: this.audioObjects[audioObjectId].storeDOM = aeNodes[i]; nicholas@2498: break; nicholas@2498: } nicholas@2498: } nicholas@2224: buffer.registerAudioObject(this.audioObjects[audioObjectId]); nicholas@2498: return this.audioObjects[audioObjectId]; nicholas@2498: }; nicholas@2498: nicholas@2498: this.newTestPage = function (audioHolderObject, store) { nicholas@2498: this.pageStore = store; nicholas@2351: this.pageSpecification = audioHolderObject; nicholas@2498: this.status = 0; nicholas@2498: this.audioObjectsReady = false; nicholas@2498: this.metric.reset(); nicholas@2681: this.buffers.forEach(function (buffer) { nicholas@2681: buffer.users = []; nicholas@2681: }); nicholas@2498: this.audioObjects = []; nicholas@2224: this.timer = new timer(); nicholas@2224: this.loopPlayback = audioHolderObject.loop; nicholas@2351: this.synchPlayback = audioHolderObject.synchronous; nicholas@2498: }; nicholas@2498: nicholas@2498: this.checkAllPlayed = function () { nicholas@2682: var arr = []; nicholas@2498: for (var id = 0; id < this.audioObjects.length; id++) { nicholas@2678: if (this.audioObjects[id].metric.wasListenedTo === false) { nicholas@2498: arr.push(this.audioObjects[id].id); nicholas@2498: } nicholas@2498: } nicholas@2498: return arr; nicholas@2498: }; nicholas@2498: nicholas@2498: this.checkAllReady = function () { nicholas@2498: var ready = true; nicholas@2498: for (var i = 0; i < this.audioObjects.length; i++) { nicholas@2678: if (this.audioObjects[i].state === 0) { nicholas@2498: // Track not ready nicholas@2498: console.log('WAIT -- audioObject ' + i + ' not ready yet!'); nicholas@2498: ready = false; nicholas@2678: } nicholas@2498: } nicholas@2498: return ready; nicholas@2498: }; nicholas@2498: nicholas@2498: this.setSynchronousLoop = function () { nicholas@2570: // Pads the signals so they are all exactly the same duration nicholas@2570: // Get the duration of the longest signal. nicholas@2570: var duration = 0; nicholas@2498: var maxId; nicholas@2498: for (var i = 0; i < this.audioObjects.length; i++) { nicholas@2570: if (duration < this.audioObjects[i].buffer.buffer.duration) { nicholas@2570: duration = this.audioObjects[i].buffer.buffer.duration; nicholas@2498: maxId = i; nicholas@2498: } nicholas@2498: } nicholas@2498: // Extract the audio and zero-pad nicholas@2681: this.audioObjects.forEach(function (ao) { nicholas@2570: if (ao.buffer.buffer.duration !== duration) { nicholas@2570: ao.buffer.buffer = ao.buffer.copyBuffer(0, duration - ao.buffer.buffer.duration); nicholas@2500: } nicholas@2681: }); nicholas@2498: }; nicholas@2498: nicholas@2498: this.bufferReady = function (id) { nicholas@2498: if (this.checkAllReady()) { nicholas@2498: if (this.synchPlayback) { nicholas@2498: this.setSynchronousLoop(); nicholas@2498: } nicholas@2460: this.status = 1; nicholas@2460: return true; nicholas@2460: } nicholas@2460: return false; nicholas@2678: }; nicholas@2498: nicholas@2224: } nicholas@2224: nicholas@2224: function audioObject(id) { nicholas@2498: // The main buffer object with common control nodes to the AudioEngine nicholas@2498: nicholas@2681: this.specification = undefined; nicholas@2498: this.id = id; nicholas@2498: this.state = 0; // 0 - no data, 1 - ready nicholas@2498: this.url = null; // Hold the URL given for the output back to the results. nicholas@2498: this.metric = new metricTracker(this); nicholas@2498: this.storeDOM = null; nicholas@2498: nicholas@2498: // Bindings for GUI nicholas@2498: this.interfaceDOM = null; nicholas@2498: this.commentDOM = null; nicholas@2498: nicholas@2498: // Create a buffer and external gain control to allow internal patching of effects and volume leveling. nicholas@2498: this.bufferNode = undefined; nicholas@2498: this.outputGain = audioContext.createGain(); nicholas@2498: nicholas@2498: this.onplayGain = 1.0; nicholas@2498: nicholas@2498: // Connect buffer to the audio graph nicholas@2498: this.outputGain.connect(audioEngineContext.outputGain); nicholas@2508: audioEngineContext.nullBufferSource.connect(this.outputGain); nicholas@2498: nicholas@2498: // the audiobuffer is not designed for multi-start playback nicholas@2498: // When stopeed, the buffer node is deleted and recreated with the stored buffer. nicholas@2681: this.buffer = undefined; nicholas@2498: nicholas@2498: this.bufferLoaded = function (callee) { nicholas@2498: // Called by the associated buffer when it has finished loading, will then 'bind' the buffer to the nicholas@2498: // audioObject and trigger the interfaceDOM.enable() function for user feedback nicholas@2224: if (callee.status == -1) { nicholas@2224: // ERROR nicholas@2224: this.state = -1; nicholas@2678: if (this.interfaceDOM !== null) { nicholas@2498: this.interfaceDOM.error(); nicholas@2498: } nicholas@2224: this.buffer = callee; nicholas@2224: return; nicholas@2224: } nicholas@2224: this.buffer = callee; nicholas@2224: var preSilenceTime = this.specification.preSilence || this.specification.parent.preSilence || specification.preSilence || 0.0; nicholas@2224: var postSilenceTime = this.specification.postSilence || this.specification.parent.postSilence || specification.postSilence || 0.0; nicholas@2460: var startTime = this.specification.startTime; nicholas@2460: var stopTime = this.specification.stopTime; nicholas@2460: var copybuffer = new callee.constructor(); nicholas@2500: nicholas@2500: copybuffer.buffer = callee.cropBuffer(startTime || 0, stopTime || callee.buffer.duration); nicholas@2678: if (preSilenceTime !== 0 || postSilenceTime !== 0) { nicholas@2500: copybuffer.buffer = copybuffer.copyBuffer(preSilenceTime, postSilenceTime); nicholas@2460: } nicholas@2500: nicholas@2660: copybuffer.buffer.lufs = callee.buffer.lufs; nicholas@2500: this.buffer = copybuffer; nicholas@2498: nicholas@2661: var targetLUFS = this.specification.loudness || this.specification.parent.loudness || specification.loudness; nicholas@2498: if (typeof targetLUFS === "number" && isFinite(targetLUFS)) { nicholas@2498: this.buffer.buffer.playbackGain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs); nicholas@2498: } else { nicholas@2498: this.buffer.buffer.playbackGain = 1.0; nicholas@2498: } nicholas@2678: if (this.interfaceDOM !== null) { nicholas@2498: this.interfaceDOM.enable(); nicholas@2498: } nicholas@2498: this.onplayGain = decibelToLinear(this.specification.gain) * (this.buffer.buffer.playbackGain || 1.0); nicholas@2498: this.storeDOM.setAttribute('playGain', linearToDecibel(this.onplayGain)); nicholas@2460: this.state = 1; nicholas@2460: audioEngineContext.bufferReady(id); nicholas@2498: }; nicholas@2498: nicholas@2498: this.bindInterface = function (interfaceObject) { nicholas@2498: this.interfaceDOM = interfaceObject; nicholas@2498: this.metric.initialise(interfaceObject.getValue()); nicholas@2498: if (this.state == 1) { nicholas@2498: this.interfaceDOM.enable(); nicholas@2498: } else if (this.state == -1) { nicholas@2224: // ERROR nicholas@2224: this.interfaceDOM.error(); nicholas@2224: return; nicholas@2224: } nicholas@2498: this.storeDOM.setAttribute('presentedId', interfaceObject.getPresentedId()); nicholas@2498: }; nicholas@2498: nicholas@2498: this.loopStart = function (setTime) { nicholas@2498: this.outputGain.gain.linearRampToValueAtTime(this.onplayGain, setTime); nicholas@2498: this.metric.startListening(audioEngineContext.timer.getTestTime()); nicholas@2224: this.interfaceDOM.startPlayback(); nicholas@2498: }; nicholas@2498: nicholas@2498: this.loopStop = function (setTime) { nicholas@2678: if (this.outputGain.gain.value !== 0.0) { nicholas@2498: this.outputGain.gain.linearRampToValueAtTime(0.0, setTime); nicholas@2498: this.metric.stopListening(audioEngineContext.timer.getTestTime()); nicholas@2498: } nicholas@2224: this.interfaceDOM.stopPlayback(); nicholas@2498: }; nicholas@2498: nicholas@2498: this.play = function (startTime) { nicholas@2678: if (this.bufferNode === undefined && this.buffer.buffer !== undefined) { nicholas@2498: this.bufferNode = audioContext.createBufferSource(); nicholas@2498: this.bufferNode.owner = this; nicholas@2498: this.bufferNode.connect(this.outputGain); nicholas@2498: this.bufferNode.buffer = this.buffer.buffer; nicholas@2498: this.bufferNode.loop = audioEngineContext.loopPlayback; nicholas@2498: this.bufferNode.onended = function (event) { nicholas@2498: // Safari does not like using 'this' to reference the calling object! nicholas@2498: //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition()); nicholas@2678: if (event.currentTarget !== null) { nicholas@2498: event.currentTarget.owner.stop(audioContext.currentTime + 1); nicholas@2224: } nicholas@2498: }; nicholas@2508: this.outputGain.gain.cancelScheduledValues(audioContext.currentTime); nicholas@2498: if (!audioEngineContext.loopPlayback || !audioEngineContext.synchPlayback) { nicholas@2498: this.metric.startListening(audioEngineContext.timer.getTestTime()); nicholas@2529: this.outputGain.gain.linearRampToValueAtTime(this.onplayGain, startTime + specification.crossFade); nicholas@2224: this.interfaceDOM.startPlayback(); nicholas@2498: } else { nicholas@2529: this.outputGain.gain.linearRampToValueAtTime(0.0, startTime); nicholas@2224: } nicholas@2499: if (audioEngineContext.loopPlayback) { nicholas@2499: this.bufferNode.loopStart = this.specification.startTime || 0; nicholas@2499: this.bufferNode.loopEnd = this.specification.stopTime - this.specification.startTime || this.buffer.buffer.duration; nicholas@2499: this.bufferNode.start(startTime); nicholas@2499: } else { nicholas@2499: this.bufferNode.start(startTime, this.specification.startTime || 0, this.specification.stopTime - this.specification.startTime || this.buffer.buffer.duration); nicholas@2499: } nicholas@2224: this.bufferNode.playbackStartTime = audioEngineContext.timer.getTestTime(); nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.stop = function (stopTime) { nicholas@2224: this.outputGain.gain.cancelScheduledValues(audioContext.currentTime); nicholas@2678: if (this.bufferNode !== undefined) { nicholas@2498: this.metric.stopListening(audioEngineContext.timer.getTestTime(), this.getCurrentPosition()); nicholas@2498: this.bufferNode.stop(stopTime); nicholas@2498: this.bufferNode = undefined; nicholas@2498: } nicholas@2529: this.outputGain.gain.linearRampToValueAtTime(0.0, stopTime); nicholas@2224: this.interfaceDOM.stopPlayback(); nicholas@2498: }; nicholas@2498: nicholas@2498: this.getCurrentPosition = function () { nicholas@2498: var time = audioEngineContext.timer.getTestTime(); nicholas@2678: if (this.bufferNode !== undefined) { nicholas@2498: var position = (time - this.bufferNode.playbackStartTime) % this.buffer.buffer.duration; nicholas@2498: if (isNaN(position)) { nicholas@2498: return 0; nicholas@2498: } nicholas@2224: return position; nicholas@2498: } else { nicholas@2498: return 0; nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.exportXMLDOM = function () { nicholas@2498: var file = storage.document.createElement('file'); nicholas@2498: file.setAttribute('sampleRate', this.buffer.buffer.sampleRate); nicholas@2498: file.setAttribute('channels', this.buffer.buffer.numberOfChannels); nicholas@2498: file.setAttribute('sampleCount', this.buffer.buffer.length); nicholas@2498: file.setAttribute('duration', this.buffer.buffer.duration); nicholas@2498: this.storeDOM.appendChild(file); nicholas@2498: if (this.specification.type != 'outside-reference') { nicholas@2498: var interfaceXML = this.interfaceDOM.exportXMLDOM(this); nicholas@2678: if (interfaceXML !== null) { nicholas@2678: if (interfaceXML.length === undefined) { nicholas@2498: this.storeDOM.appendChild(interfaceXML); nicholas@2498: } else { nicholas@2498: for (var i = 0; i < interfaceXML.length; i++) { nicholas@2498: this.storeDOM.appendChild(interfaceXML[i]); nicholas@2498: } nicholas@2498: } nicholas@2498: } nicholas@2678: if (this.commentDOM !== null) { nicholas@2498: this.storeDOM.appendChild(this.commentDOM.exportXMLDOM(this)); nicholas@2498: } nicholas@2498: } nicholas@2682: this.metric.exportXMLDOM(this.storeDOM.getElementsByTagName('metric')[0]); nicholas@2498: }; nicholas@2224: } nicholas@2224: nicholas@2498: function timer() { nicholas@2498: /* Timer object used in audioEngine to keep track of session timings nicholas@2498: * Uses the timer of the web audio API, so sample resolution nicholas@2498: */ nicholas@2498: this.testStarted = false; nicholas@2498: this.testStartTime = 0; nicholas@2498: this.testDuration = 0; nicholas@2498: this.minimumTestTime = 0; // No minimum test time nicholas@2498: this.startTest = function () { nicholas@2678: if (this.testStarted === false) { nicholas@2498: this.testStartTime = audioContext.currentTime; nicholas@2498: this.testStarted = true; nicholas@2498: this.updateTestTime(); nicholas@2498: audioEngineContext.metric.initialiseTest(); nicholas@2498: } nicholas@2498: }; nicholas@2498: this.stopTest = function () { nicholas@2498: if (this.testStarted) { nicholas@2498: this.testDuration = this.getTestTime(); nicholas@2498: this.testStarted = false; nicholas@2498: } else { nicholas@2498: console.log('ERR: Test tried to end before beginning'); nicholas@2498: } nicholas@2498: }; nicholas@2498: this.updateTestTime = function () { nicholas@2498: if (this.testStarted) { nicholas@2498: this.testDuration = audioContext.currentTime - this.testStartTime; nicholas@2498: } nicholas@2498: }; nicholas@2498: this.getTestTime = function () { nicholas@2498: this.updateTestTime(); nicholas@2498: return this.testDuration; nicholas@2498: }; nicholas@2224: } nicholas@2224: nicholas@2498: function sessionMetrics(engine, specification) { nicholas@2498: /* Used by audioEngine to link to audioObjects to minimise the timer call timers; nicholas@2498: */ nicholas@2498: this.engine = engine; nicholas@2498: this.lastClicked = -1; nicholas@2498: this.data = -1; nicholas@2498: this.reset = function () { nicholas@2498: this.lastClicked = -1; nicholas@2498: this.data = -1; nicholas@2498: }; nicholas@2498: nicholas@2498: this.enableElementInitialPosition = false; nicholas@2498: this.enableElementListenTracker = false; nicholas@2498: this.enableElementTimer = false; nicholas@2498: this.enableElementTracker = false; nicholas@2498: this.enableFlagListenedTo = false; nicholas@2498: this.enableFlagMoved = false; nicholas@2498: this.enableTestTimer = false; nicholas@2498: // Obtain the metrics enabled nicholas@2498: for (var i = 0; i < specification.metrics.enabled.length; i++) { nicholas@2498: var node = specification.metrics.enabled[i]; nicholas@2498: switch (node) { nicholas@2498: case 'testTimer': nicholas@2498: this.enableTestTimer = true; nicholas@2498: break; nicholas@2498: case 'elementTimer': nicholas@2498: this.enableElementTimer = true; nicholas@2498: break; nicholas@2498: case 'elementTracker': nicholas@2498: this.enableElementTracker = true; nicholas@2498: break; nicholas@2498: case 'elementListenTracker': nicholas@2498: this.enableElementListenTracker = true; nicholas@2498: break; nicholas@2498: case 'elementInitialPosition': nicholas@2498: this.enableElementInitialPosition = true; nicholas@2498: break; nicholas@2498: case 'elementFlagListenedTo': nicholas@2498: this.enableFlagListenedTo = true; nicholas@2498: break; nicholas@2498: case 'elementFlagMoved': nicholas@2498: this.enableFlagMoved = true; nicholas@2498: break; nicholas@2498: case 'elementFlagComments': nicholas@2498: this.enableFlagComments = true; nicholas@2498: break; nicholas@2498: } nicholas@2498: } nicholas@2498: this.initialiseTest = function () {}; nicholas@2224: } nicholas@2224: nicholas@2498: function metricTracker(caller) { nicholas@2498: /* Custom object to track and collect metric data nicholas@2498: * Used only inside the audioObjects object. nicholas@2498: */ nicholas@2498: nicholas@2498: this.listenedTimer = 0; nicholas@2498: this.listenStart = 0; nicholas@2498: this.listenHold = false; nicholas@2498: this.initialPosition = -1; nicholas@2498: this.movementTracker = []; nicholas@2498: this.listenTracker = []; nicholas@2498: this.wasListenedTo = false; nicholas@2498: this.wasMoved = false; nicholas@2498: this.hasComments = false; nicholas@2498: this.parent = caller; nicholas@2498: nicholas@2498: this.initialise = function (position) { nicholas@2498: if (this.initialPosition == -1) { nicholas@2498: this.initialPosition = position; nicholas@2498: this.moved(0, position); nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.moved = function (time, position) { nicholas@2498: if (time > 0) { nicholas@2498: this.wasMoved = true; nicholas@2498: } nicholas@2498: this.movementTracker[this.movementTracker.length] = [time, position]; nicholas@2498: }; nicholas@2498: nicholas@2498: this.startListening = function (time) { nicholas@2678: if (this.listenHold === false) { nicholas@2498: this.wasListenedTo = true; nicholas@2498: this.listenStart = time; nicholas@2498: this.listenHold = true; nicholas@2498: nicholas@2498: var evnt = document.createElement('event'); nicholas@2498: var testTime = document.createElement('testTime'); nicholas@2498: testTime.setAttribute('start', time); nicholas@2498: var bufferTime = document.createElement('bufferTime'); nicholas@2498: bufferTime.setAttribute('start', this.parent.getCurrentPosition()); nicholas@2498: evnt.appendChild(testTime); nicholas@2498: evnt.appendChild(bufferTime); nicholas@2498: this.listenTracker.push(evnt); nicholas@2498: nicholas@2498: console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.stopListening = function (time, bufferStopTime) { nicholas@2678: if (this.listenHold === true) { nicholas@2498: var diff = time - this.listenStart; nicholas@2498: this.listenedTimer += (diff); nicholas@2498: this.listenStart = 0; nicholas@2498: this.listenHold = false; nicholas@2498: nicholas@2498: var evnt = this.listenTracker[this.listenTracker.length - 1]; nicholas@2498: var testTime = evnt.getElementsByTagName('testTime')[0]; nicholas@2498: var bufferTime = evnt.getElementsByTagName('bufferTime')[0]; nicholas@2498: testTime.setAttribute('stop', time); nicholas@2678: if (bufferStopTime === undefined) { nicholas@2498: bufferTime.setAttribute('stop', this.parent.getCurrentPosition()); nicholas@2498: } else { nicholas@2498: bufferTime.setAttribute('stop', bufferStopTime); nicholas@2498: } nicholas@2498: console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2682: function exportElementTimer(parentElement) { nicholas@2682: var mElementTimer = storage.document.createElement('metricresult'); nicholas@2682: mElementTimer.setAttribute('name', 'enableElementTimer'); nicholas@2682: mElementTimer.textContent = this.listenedTimer; nicholas@2682: parentElement.appendChild(mElementTimer); nicholas@2689: return mElementTimer; nicholas@2682: } nicholas@2682: nicholas@2682: function exportElementTrack(parentElement) { nicholas@2682: var elementTrackerFull = storage.document.createElement('metricresult'); nicholas@2682: elementTrackerFull.setAttribute('name', 'elementTrackerFull'); nicholas@2682: for (var k = 0; k < this.movementTracker.length; k++) { nicholas@2682: var timePos = storage.document.createElement('movement'); nicholas@2682: timePos.setAttribute("time", this.movementTracker[k][0]); nicholas@2682: timePos.setAttribute("value", this.movementTracker[k][1]); nicholas@2682: elementTrackerFull.appendChild(timePos); nicholas@2682: } nicholas@2682: parentElement.appendChild(elementTrackerFull); nicholas@2689: return elementTrackerFull; nicholas@2682: } nicholas@2682: nicholas@2682: function exportElementListenTracker(parentElement) { nicholas@2682: var elementListenTracker = storage.document.createElement('metricresult'); nicholas@2682: elementListenTracker.setAttribute('name', 'elementListenTracker'); nicholas@2682: for (var k = 0; k < this.listenTracker.length; k++) { nicholas@2682: elementListenTracker.appendChild(this.listenTracker[k]); nicholas@2682: } nicholas@2682: parentElement.appendChild(elementListenTracker); nicholas@2689: return elementListenTracker; nicholas@2682: } nicholas@2682: nicholas@2682: function exportElementInitialPosition(parentElement) { nicholas@2682: var elementInitial = storage.document.createElement('metricresult'); nicholas@2682: elementInitial.setAttribute('name', 'elementInitialPosition'); nicholas@2682: elementInitial.textContent = this.initialPosition; nicholas@2682: parentElement.appendChild(elementInitial); nicholas@2689: return elementInitial; nicholas@2682: } nicholas@2682: nicholas@2682: function exportFlagListenedTo(parentElement) { nicholas@2682: var flagListenedTo = storage.document.createElement('metricresult'); nicholas@2682: flagListenedTo.setAttribute('name', 'elementFlagListenedTo'); nicholas@2682: flagListenedTo.textContent = this.wasListenedTo; nicholas@2682: parentElement.appendChild(flagListenedTo); nicholas@2689: return flagListenedTo; nicholas@2682: } nicholas@2682: nicholas@2682: function exportFlagMoved(parentElement) { nicholas@2682: var flagMoved = storage.document.createElement('metricresult'); nicholas@2682: flagMoved.setAttribute('name', 'elementFlagMoved'); nicholas@2682: flagMoved.textContent = this.wasMoved; nicholas@2682: parentElement.appendChild(flagMoved); nicholas@2689: return flagMoved; nicholas@2682: } nicholas@2682: nicholas@2682: function exportFlagComments(parentElement) { nicholas@2682: var flagComments = storage.document.createElement('metricresult'); nicholas@2682: flagComments.setAttribute('name', 'elementFlagComments'); nicholas@2682: if (this.parent.commentDOM === null) { nicholas@2682: flagComments.textContent = 'false'; nicholas@2682: } else if (this.parent.commentDOM.textContent.length === 0) { nicholas@2682: flagComments.textContent = 'false'; nicholas@2682: } else { nicholas@2682: flagComments.textContet = 'true'; nicholas@2682: } nicholas@2682: parentElement.appendChild(flagComments); nicholas@2689: return flagComments; nicholas@2682: } nicholas@2682: nicholas@2682: this.exportXMLDOM = function (parentElement) { nicholas@2690: var elems = []; nicholas@2498: if (audioEngineContext.metric.enableElementTimer) { nicholas@2689: elems.push(exportElementTimer.call(this, parentElement)); nicholas@2498: } nicholas@2498: if (audioEngineContext.metric.enableElementTracker) { nicholas@2689: elems.push(exportElementTrack.call(this, parentElement)); nicholas@2498: } nicholas@2498: if (audioEngineContext.metric.enableElementListenTracker) { nicholas@2689: elems.push(exportElementListenTracker.call(this, parentElement)); nicholas@2498: } nicholas@2498: if (audioEngineContext.metric.enableElementInitialPosition) { nicholas@2689: elems.push(exportElementInitialPosition.call(this, parentElement)); nicholas@2498: } nicholas@2498: if (audioEngineContext.metric.enableFlagListenedTo) { nicholas@2689: elems.push(exportFlagListenedTo.call(this, parentElement)); nicholas@2498: } nicholas@2498: if (audioEngineContext.metric.enableFlagMoved) { nicholas@2689: elems.push(exportFlagMoved.call(this, parentElement)); nicholas@2498: } nicholas@2498: if (audioEngineContext.metric.enableFlagComments) { nicholas@2689: elems.push(exportFlagComments.call(this, parentElement)); nicholas@2498: } nicholas@2689: return elems; nicholas@2498: }; nicholas@2224: } nicholas@2498: nicholas@2224: function Interface(specificationObject) { nicholas@2498: // This handles the bindings between the interface and the audioEngineContext; nicholas@2498: this.specification = specificationObject; nicholas@2498: this.insertPoint = document.getElementById("topLevelBody"); nicholas@2498: nicholas@2498: this.newPage = function (audioHolderObject, store) { nicholas@2498: audioEngineContext.newTestPage(audioHolderObject, store); nicholas@2498: interfaceContext.commentBoxes.deleteCommentBoxes(); nicholas@2498: interfaceContext.deleteCommentQuestions(); nicholas@2498: loadTest(audioHolderObject, store); nicholas@2498: }; nicholas@2498: nicholas@2498: // Bounded by interface!! nicholas@2498: // Interface object MUST have an exportXMLDOM method which returns the various DOM levels nicholas@2498: // For example, APE returns the slider position normalised in a tag. nicholas@2498: this.interfaceObjects = []; nicholas@2498: this.interfaceObject = function () {}; nicholas@2498: nicholas@2498: this.resizeWindow = function (event) { nicholas@2498: popup.resize(event); nicholas@2352: this.volume.resize(); nicholas@2360: this.lightbox.resize(); nicholas@2682: this.commentBoxes.forEach(function (elem) { nicholas@2682: elem.resize(); nicholas@2682: }); nicholas@2682: this.commentQuestions.forEach(function (elem) { nicholas@2682: elem.resize(); nicholas@2682: }); nicholas@2498: try { nicholas@2498: resizeWindow(event); nicholas@2498: } catch (err) { nicholas@2498: console.log("Warning - Interface does not have Resize option"); nicholas@2498: console.log(err); nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.returnNavigator = function () { nicholas@2498: var node = storage.document.createElement("navigator"); nicholas@2498: var platform = storage.document.createElement("platform"); nicholas@2498: platform.textContent = navigator.platform; nicholas@2498: var vendor = storage.document.createElement("vendor"); nicholas@2498: vendor.textContent = navigator.vendor; nicholas@2498: var userAgent = storage.document.createElement("uagent"); nicholas@2498: userAgent.textContent = navigator.userAgent; nicholas@2224: var screen = storage.document.createElement("window"); nicholas@2498: screen.setAttribute('innerWidth', window.innerWidth); nicholas@2498: screen.setAttribute('innerHeight', window.innerHeight); nicholas@2498: node.appendChild(platform); nicholas@2498: node.appendChild(vendor); nicholas@2498: node.appendChild(userAgent); nicholas@2224: node.appendChild(screen); nicholas@2498: return node; nicholas@2498: }; nicholas@2498: nicholas@2498: this.returnDateNode = function () { nicholas@2224: // Create an XML Node for the Date and Time a test was conducted nicholas@2224: // Structure is nicholas@2224: // 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@2498: date.setAttribute('year', dateTime.getFullYear()); nicholas@2498: date.setAttribute('month', dateTime.getMonth() + 1); nicholas@2498: date.setAttribute('day', dateTime.getDate()); nicholas@2498: time.setAttribute('hour', dateTime.getHours()); nicholas@2498: time.setAttribute('minute', dateTime.getMinutes()); nicholas@2498: time.setAttribute('secs', dateTime.getSeconds()); nicholas@2498: nicholas@2224: hold.appendChild(date); nicholas@2224: hold.appendChild(time); nicholas@2224: return hold; nicholas@2224: nicholas@2678: }; nicholas@2498: nicholas@2360: this.lightbox = { nicholas@2360: parent: this, nicholas@2360: root: document.createElement("div"), nicholas@2360: content: document.createElement("div"), nicholas@2360: accept: document.createElement("button"), nicholas@2360: blanker: document.createElement("div"), nicholas@2498: post: function (type, message) { nicholas@2498: switch (type) { nicholas@2360: case "Error": nicholas@2360: this.content.className = "lightbox-error"; nicholas@2360: break; nicholas@2360: case "Warning": nicholas@2360: this.content.className = "lightbox-warning"; nicholas@2360: break; nicholas@2360: default: nicholas@2360: this.content.className = "lightbox-message"; nicholas@2360: break; nicholas@2360: } nicholas@2360: var msg = document.createElement("p"); nicholas@2360: msg.textContent = message; nicholas@2360: this.content.appendChild(msg); nicholas@2360: this.show(); nicholas@2360: }, nicholas@2498: show: function () { nicholas@2360: this.root.style.visibility = "visible"; nicholas@2360: this.blanker.style.visibility = "visible"; nicholas@2360: }, nicholas@2498: clear: function () { nicholas@2360: this.root.style.visibility = ""; nicholas@2360: this.blanker.style.visibility = ""; nicholas@2360: this.content.textContent = ""; nicholas@2360: }, nicholas@2498: handleEvent: function (event) { nicholas@2360: if (event.currentTarget == this.accept) { nicholas@2360: this.clear(); nicholas@2360: } nicholas@2360: }, nicholas@2498: resize: function (event) { nicholas@2498: this.root.style.left = (window.innerWidth / 2) - 250 + 'px'; nicholas@2360: } nicholas@2678: }; nicholas@2498: nicholas@2360: this.lightbox.root.appendChild(this.lightbox.content); nicholas@2360: this.lightbox.root.appendChild(this.lightbox.accept); nicholas@2360: this.lightbox.root.className = "popupHolder"; nicholas@2360: this.lightbox.root.id = "lightbox-root"; nicholas@2360: this.lightbox.accept.className = "popupButton"; nicholas@2360: this.lightbox.accept.style.bottom = "10px"; nicholas@2360: this.lightbox.accept.textContent = "OK"; nicholas@2360: this.lightbox.accept.style.left = "237.5px"; nicholas@2498: this.lightbox.accept.addEventListener("click", this.lightbox); nicholas@2360: this.lightbox.blanker.className = "testHalt"; nicholas@2360: this.lightbox.blanker.id = "lightbox-blanker"; nicholas@2360: document.getElementsByTagName("body")[0].appendChild(this.lightbox.root); nicholas@2360: document.getElementsByTagName("body")[0].appendChild(this.lightbox.blanker); nicholas@2498: nicholas@2498: this.commentBoxes = new function () { nicholas@2224: this.boxes = []; nicholas@2224: this.injectPoint = null; nicholas@2498: 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@2498: 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@2498: 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@2498: 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@2498: 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@2498: 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@2498: this.resize = function () { nicholas@2498: var boxwidth = (window.innerWidth - 100) / 2; nicholas@2498: if (boxwidth >= 600) { nicholas@2224: boxwidth = 600; nicholas@2498: } else if (boxwidth < 400) { nicholas@2224: boxwidth = 400; nicholas@2224: } nicholas@2498: this.trackComment.style.width = boxwidth + "px"; nicholas@2498: this.trackCommentBox.style.width = boxwidth - 6 + "px"; nicholas@2224: }; nicholas@2224: this.resize(); nicholas@2224: }; nicholas@2498: 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@2498: this.sortCommentBoxes = function () { nicholas@2498: this.boxes.sort(function (a, b) { nicholas@2498: return a.id - b.id; nicholas@2498: }); nicholas@2224: }; nicholas@2224: nicholas@2498: this.showCommentBoxes = function (inject, sort) { nicholas@2224: this.injectPoint = inject; nicholas@2498: if (sort) { nicholas@2498: this.sortCommentBoxes(); nicholas@2498: } nicholas@2682: this.boxes.forEach(function (box) { nicholas@2224: inject.appendChild(box.trackComment); nicholas@2682: }); nicholas@2224: }; nicholas@2224: nicholas@2498: this.deleteCommentBoxes = function () { nicholas@2678: if (this.injectPoint !== null) { nicholas@2682: this.boxes.forEach(function (box) { nicholas@2224: this.injectPoint.removeChild(box.trackComment); nicholas@2682: }, this); nicholas@2224: this.injectPoint = null; nicholas@2224: } nicholas@2224: this.boxes = []; nicholas@2224: }; nicholas@2678: }; nicholas@2498: nicholas@2498: this.commentQuestions = []; nicholas@2498: nicholas@2498: this.commentBox = function (commentQuestion) { nicholas@2498: this.specification = commentQuestion; nicholas@2498: // Create document objects to hold the comment boxes nicholas@2498: this.holder = document.createElement('div'); nicholas@2498: this.holder.className = 'comment-div'; nicholas@2498: // Create a string next to each comment asking for a comment nicholas@2498: this.string = document.createElement('span'); nicholas@2498: this.string.innerHTML = commentQuestion.statement; nicholas@2498: // Create the HTML5 comment box 'textarea' nicholas@2498: this.textArea = document.createElement('textarea'); nicholas@2498: this.textArea.rows = '4'; nicholas@2498: this.textArea.cols = '100'; nicholas@2498: this.textArea.className = 'trackComment'; nicholas@2498: var br = document.createElement('br'); nicholas@2498: // Add to the holder. nicholas@2498: this.holder.appendChild(this.string); nicholas@2498: this.holder.appendChild(br); nicholas@2498: this.holder.appendChild(this.textArea); nicholas@2498: nicholas@2498: this.exportXMLDOM = function (storePoint) { nicholas@2498: var root = storePoint.parent.document.createElement('comment'); nicholas@2498: root.id = this.specification.id; nicholas@2498: root.setAttribute('type', this.specification.type); nicholas@2498: console.log("Question: " + this.string.textContent); nicholas@2498: 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@2498: return root; nicholas@2498: }; nicholas@2498: this.resize = function () { nicholas@2498: var boxwidth = (window.innerWidth - 100) / 2; nicholas@2498: if (boxwidth >= 600) { nicholas@2498: boxwidth = 600; nicholas@2498: } else if (boxwidth < 400) { nicholas@2498: boxwidth = 400; nicholas@2498: } nicholas@2498: this.holder.style.width = boxwidth + "px"; nicholas@2498: this.textArea.style.width = boxwidth - 6 + "px"; nicholas@2498: }; nicholas@2498: this.resize(); nicholas@2498: }; nicholas@2498: nicholas@2498: this.radioBox = function (commentQuestion) { nicholas@2498: this.specification = commentQuestion; nicholas@2498: // Create document objects to hold the comment boxes nicholas@2498: this.holder = document.createElement('div'); nicholas@2498: this.holder.className = 'comment-div'; nicholas@2498: // Create a string next to each comment asking for a comment nicholas@2498: this.string = document.createElement('span'); nicholas@2498: this.string.innerHTML = commentQuestion.statement; nicholas@2498: var br = document.createElement('br'); nicholas@2498: // Add to the holder. nicholas@2498: this.holder.appendChild(this.string); nicholas@2498: this.holder.appendChild(br); nicholas@2498: this.options = []; nicholas@2498: this.inputs = document.createElement('div'); nicholas@2498: this.span = document.createElement('div'); nicholas@2498: this.inputs.align = 'center'; nicholas@2498: this.inputs.style.marginLeft = '12px'; nicholas@2294: this.inputs.className = "comment-radio-inputs-holder"; nicholas@2498: this.span.style.marginLeft = '12px'; nicholas@2498: this.span.align = 'center'; nicholas@2498: this.span.style.marginTop = '15px'; nicholas@2294: this.span.className = "comment-radio-span-holder"; nicholas@2498: nicholas@2498: var optCount = commentQuestion.options.length; nicholas@2682: commentQuestion.options.forEach(function (optNode) { nicholas@2498: var div = document.createElement('div'); nicholas@2498: div.style.width = '80px'; nicholas@2498: div.style.float = 'left'; nicholas@2498: var input = document.createElement('input'); nicholas@2498: input.type = 'radio'; nicholas@2498: input.name = commentQuestion.id; nicholas@2498: input.setAttribute('setvalue', optNode.name); nicholas@2498: input.className = 'comment-radio'; nicholas@2498: div.appendChild(input); nicholas@2498: this.inputs.appendChild(div); nicholas@2498: nicholas@2498: nicholas@2498: div = document.createElement('div'); nicholas@2498: div.style.width = '80px'; nicholas@2498: div.style.float = 'left'; nicholas@2498: div.align = 'center'; nicholas@2498: var span = document.createElement('span'); nicholas@2498: span.textContent = optNode.text; nicholas@2498: span.className = 'comment-radio-span'; nicholas@2498: div.appendChild(span); nicholas@2498: this.span.appendChild(div); nicholas@2498: this.options.push(input); nicholas@2682: }, this); nicholas@2498: this.holder.appendChild(this.span); nicholas@2498: this.holder.appendChild(this.inputs); nicholas@2498: nicholas@2498: this.exportXMLDOM = function (storePoint) { nicholas@2498: var root = storePoint.parent.document.createElement('comment'); nicholas@2498: root.id = this.specification.id; nicholas@2498: root.setAttribute('type', this.specification.type); nicholas@2498: var question = document.createElement('question'); nicholas@2498: question.textContent = this.string.textContent; nicholas@2498: var response = document.createElement('response'); nicholas@2498: var i = 0; nicholas@2678: while (this.options[i].checked === false) { nicholas@2498: i++; nicholas@2498: if (i >= this.options.length) { nicholas@2498: break; nicholas@2498: } nicholas@2498: } nicholas@2498: if (i >= this.options.length) { nicholas@2498: response.textContent = 'null'; nicholas@2498: } else { nicholas@2498: response.textContent = this.options[i].getAttribute('setvalue'); nicholas@2498: response.setAttribute('number', i); nicholas@2498: } nicholas@2498: console.log('Comment: ' + question.textContent); nicholas@2498: console.log('Response: ' + response.textContent); nicholas@2498: root.appendChild(question); nicholas@2498: root.appendChild(response); nicholas@2224: storePoint.XMLDOM.appendChild(root); nicholas@2498: return root; nicholas@2498: }; nicholas@2498: this.resize = function () { nicholas@2498: var boxwidth = (window.innerWidth - 100) / 2; nicholas@2498: if (boxwidth >= 600) { nicholas@2498: boxwidth = 600; nicholas@2498: } else if (boxwidth < 400) { nicholas@2498: boxwidth = 400; nicholas@2498: } nicholas@2498: this.holder.style.width = boxwidth + "px"; nicholas@2498: var text = this.holder.getElementsByClassName("comment-radio-span-holder")[0]; nicholas@2498: var options = this.holder.getElementsByClassName("comment-radio-inputs-holder")[0]; nicholas@2498: var optCount = options.childElementCount; nicholas@2498: var spanMargin = Math.floor(((boxwidth - 20 - (optCount * 80)) / (optCount)) / 2) + 'px'; nicholas@2682: options = options.firstChild; nicholas@2682: text = text.firstChild; nicholas@2498: options.style.marginRight = spanMargin; nicholas@2498: options.style.marginLeft = spanMargin; nicholas@2498: text.style.marginRight = spanMargin; nicholas@2498: text.style.marginLeft = spanMargin; nicholas@2689: while (options = options.nextSibling) { nicholas@2498: text = text.nextSibling; nicholas@2498: options.style.marginRight = spanMargin; nicholas@2498: options.style.marginLeft = spanMargin; nicholas@2498: text.style.marginRight = spanMargin; nicholas@2498: text.style.marginLeft = spanMargin; nicholas@2498: } nicholas@2498: }; nicholas@2498: this.resize(); nicholas@2498: }; nicholas@2498: nicholas@2498: this.checkboxBox = function (commentQuestion) { nicholas@2498: this.specification = commentQuestion; nicholas@2498: // Create document objects to hold the comment boxes nicholas@2498: this.holder = document.createElement('div'); nicholas@2498: this.holder.className = 'comment-div'; nicholas@2498: // Create a string next to each comment asking for a comment nicholas@2498: this.string = document.createElement('span'); nicholas@2498: this.string.innerHTML = commentQuestion.statement; nicholas@2498: var br = document.createElement('br'); nicholas@2498: // Add to the holder. nicholas@2498: this.holder.appendChild(this.string); nicholas@2498: this.holder.appendChild(br); nicholas@2498: this.options = []; nicholas@2498: this.inputs = document.createElement('div'); nicholas@2498: this.span = document.createElement('div'); nicholas@2498: this.inputs.align = 'center'; nicholas@2498: this.inputs.style.marginLeft = '12px'; nicholas@2294: this.inputs.className = "comment-checkbox-inputs-holder"; nicholas@2498: this.span.style.marginLeft = '12px'; nicholas@2498: this.span.align = 'center'; nicholas@2498: this.span.style.marginTop = '15px'; nicholas@2294: this.span.className = "comment-checkbox-span-holder"; nicholas@2498: nicholas@2498: var optCount = commentQuestion.options.length; nicholas@2498: for (var i = 0; i < optCount; i++) { nicholas@2498: var div = document.createElement('div'); nicholas@2498: div.style.width = '80px'; nicholas@2498: div.style.float = 'left'; nicholas@2498: var input = document.createElement('input'); nicholas@2498: input.type = 'checkbox'; nicholas@2498: input.name = commentQuestion.id; nicholas@2498: input.setAttribute('setvalue', commentQuestion.options[i].name); nicholas@2498: input.className = 'comment-radio'; nicholas@2498: div.appendChild(input); nicholas@2498: this.inputs.appendChild(div); nicholas@2498: nicholas@2498: nicholas@2498: div = document.createElement('div'); nicholas@2498: div.style.width = '80px'; nicholas@2498: div.style.float = 'left'; nicholas@2498: div.align = 'center'; nicholas@2498: var span = document.createElement('span'); nicholas@2498: span.textContent = commentQuestion.options[i].text; nicholas@2498: span.className = 'comment-radio-span'; nicholas@2498: div.appendChild(span); nicholas@2498: this.span.appendChild(div); nicholas@2498: this.options.push(input); nicholas@2498: } nicholas@2498: this.holder.appendChild(this.span); nicholas@2498: this.holder.appendChild(this.inputs); nicholas@2498: nicholas@2498: this.exportXMLDOM = function (storePoint) { nicholas@2498: var root = storePoint.parent.document.createElement('comment'); nicholas@2498: root.id = this.specification.id; nicholas@2498: root.setAttribute('type', this.specification.type); nicholas@2498: var question = document.createElement('question'); nicholas@2498: question.textContent = this.string.textContent; nicholas@2498: root.appendChild(question); nicholas@2498: console.log('Comment: ' + question.textContent); nicholas@2498: for (var i = 0; i < this.options.length; i++) { nicholas@2498: var response = document.createElement('response'); nicholas@2498: response.textContent = this.options[i].checked; nicholas@2498: response.setAttribute('name', this.options[i].getAttribute('setvalue')); nicholas@2498: root.appendChild(response); nicholas@2498: console.log('Response ' + response.getAttribute('name') + ': ' + response.textContent); nicholas@2498: } nicholas@2224: storePoint.XMLDOM.appendChild(root); nicholas@2498: return root; nicholas@2498: }; nicholas@2498: this.resize = function () { nicholas@2498: var boxwidth = (window.innerWidth - 100) / 2; nicholas@2498: if (boxwidth >= 600) { nicholas@2498: boxwidth = 600; nicholas@2498: } else if (boxwidth < 400) { nicholas@2498: boxwidth = 400; nicholas@2498: } nicholas@2498: this.holder.style.width = boxwidth + "px"; nicholas@2498: var text = this.holder.getElementsByClassName("comment-checkbox-span-holder")[0]; nicholas@2498: var options = this.holder.getElementsByClassName("comment-checkbox-inputs-holder")[0]; nicholas@2498: var optCount = options.childElementCount; nicholas@2498: var spanMargin = Math.floor(((boxwidth - 20 - (optCount * 80)) / (optCount)) / 2) + 'px'; nicholas@2682: options = options.firstChild; nicholas@2682: text = text.firstChild; nicholas@2498: options.style.marginRight = spanMargin; nicholas@2498: options.style.marginLeft = spanMargin; nicholas@2498: text.style.marginRight = spanMargin; nicholas@2498: text.style.marginLeft = spanMargin; nicholas@2689: while (options = options.nextSibling) { nicholas@2498: text = text.nextSibling; nicholas@2498: options.style.marginRight = spanMargin; nicholas@2498: options.style.marginLeft = spanMargin; nicholas@2498: text.style.marginRight = spanMargin; nicholas@2498: text.style.marginLeft = spanMargin; nicholas@2498: } nicholas@2498: }; nicholas@2498: this.resize(); nicholas@2498: }; nicholas@2498: n@2579: this.sliderBox = function (commentQuestion) { n@2579: this.specification = commentQuestion; n@2579: this.holder = document.createElement("div"); n@2579: this.holder.className = 'comment-div'; n@2579: this.string = document.createElement("span"); n@2579: this.string.innerHTML = commentQuestion.statement; n@2579: this.slider = document.createElement("input"); n@2579: this.slider.type = "range"; n@2579: this.slider.min = commentQuestion.min; n@2579: this.slider.max = commentQuestion.max; n@2579: this.slider.step = commentQuestion.step; n@2579: this.slider.value = commentQuestion.value; n@2579: var br = document.createElement('br'); n@2579: n@2580: var textHolder = document.createElement("div"); n@2580: textHolder.className = "comment-slider-text-holder"; n@2580: n@2580: this.leftText = document.createElement("span"); n@2580: this.leftText.textContent = commentQuestion.leftText; n@2580: this.rightText = document.createElement("span"); n@2580: this.rightText.textContent = commentQuestion.rightText; n@2580: textHolder.appendChild(this.leftText); n@2580: textHolder.appendChild(this.rightText); n@2580: n@2579: this.holder.appendChild(this.string); n@2579: this.holder.appendChild(br); n@2579: this.holder.appendChild(this.slider); n@2580: this.holder.appendChild(textHolder); n@2579: n@2579: this.exportXMLDOM = function (storePoint) { n@2579: var root = storePoint.parent.document.createElement('comment'); n@2579: root.id = this.specification.id; n@2579: root.setAttribute('type', this.specification.type); n@2579: console.log("Question: " + this.string.textContent); n@2579: console.log("Response: " + this.slider.value); n@2579: var question = storePoint.parent.document.createElement('question'); n@2579: question.textContent = this.string.textContent; n@2579: var response = storePoint.parent.document.createElement('response'); n@2579: response.textContent = this.slider.value; n@2579: root.appendChild(question); n@2579: root.appendChild(response); n@2579: storePoint.XMLDOM.appendChild(root); n@2579: return root; n@2579: }; n@2579: this.resize = function () { n@2579: var boxwidth = (window.innerWidth - 100) / 2; n@2579: if (boxwidth >= 600) { n@2579: boxwidth = 600; n@2579: } else if (boxwidth < 400) { n@2579: boxwidth = 400; n@2579: } n@2579: this.holder.style.width = boxwidth + "px"; n@2579: this.slider.style.width = boxwidth - 24 + "px"; n@2579: }; n@2579: this.resize(); n@2579: }; n@2579: nicholas@2498: this.createCommentQuestion = function (element) { nicholas@2498: var node; nicholas@2498: if (element.type == 'question') { nicholas@2498: node = new this.commentBox(element); nicholas@2498: } else if (element.type == 'radio') { nicholas@2498: node = new this.radioBox(element); nicholas@2498: } else if (element.type == 'checkbox') { nicholas@2498: node = new this.checkboxBox(element); n@2579: } else if (element.type == 'slider') { n@2579: node = new this.sliderBox(element); nicholas@2498: } nicholas@2498: this.commentQuestions.push(node); nicholas@2498: return node; nicholas@2498: }; nicholas@2498: nicholas@2498: this.deleteCommentQuestions = function () { nicholas@2498: this.commentQuestions = []; nicholas@2498: }; nicholas@2498: nicholas@2498: this.outsideReferenceDOM = function (audioObject, index, inject) { nicholas@2224: this.parent = audioObject; nicholas@2224: this.outsideReferenceHolder = document.createElement('button'); nicholas@2224: this.outsideReferenceHolder.className = 'outside-reference'; nicholas@2498: this.outsideReferenceHolder.setAttribute('track-id', index); nicholas@2409: this.outsideReferenceHolder.textContent = this.parent.specification.label || "Reference"; nicholas@2224: this.outsideReferenceHolder.disabled = true; nicholas@2224: nicholas@2498: this.outsideReferenceHolder.onclick = function (event) { nicholas@2224: audioEngineContext.play(event.currentTarget.getAttribute('track-id')); nicholas@2224: }; nicholas@2224: inject.appendChild(this.outsideReferenceHolder); nicholas@2498: this.enable = function () { nicholas@2498: if (this.parent.state == 1) { nicholas@2224: this.outsideReferenceHolder.disabled = false; nicholas@2224: } nicholas@2224: }; nicholas@2498: this.updateLoading = function (progress) { nicholas@2498: if (progress != 100) { nicholas@2224: progress = String(progress); nicholas@2224: progress = progress.split('.')[0]; nicholas@2498: this.outsideReferenceHolder.textContent = progress + '%'; nicholas@2224: } else { nicholas@2409: this.outsideReferenceHolder.textContent = this.parent.specification.label || "Reference"; nicholas@2224: } nicholas@2224: }; nicholas@2498: this.startPlayback = function () { 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@2498: this.stopPlayback = function () { nicholas@2224: // Called when playback has stopped. This gets called even if playback never started! nicholas@2224: this.outsideReferenceHolder.style.backgroundColor = ""; nicholas@2224: }; nicholas@2498: this.exportXMLDOM = function (audioObject) { nicholas@2224: return null; nicholas@2224: }; nicholas@2498: this.getValue = function () { nicholas@2224: return 0; nicholas@2224: }; nicholas@2498: this.getPresentedId = function () { nicholas@2409: return this.parent.specification.label || "Reference"; nicholas@2224: }; nicholas@2498: this.canMove = function () { nicholas@2224: return false; nicholas@2224: }; nicholas@2498: this.error = function () { nicholas@2498: // audioObject has an error!! nicholas@2224: this.outsideReferenceHolder.textContent = "Error"; nicholas@2224: this.outsideReferenceHolder.style.backgroundColor = "#F00"; nicholas@2678: }; nicholas@2678: }; nicholas@2498: nicholas@2498: this.playhead = new function () { nicholas@2498: this.object = document.createElement('div'); nicholas@2498: this.object.className = 'playhead'; nicholas@2498: this.object.align = 'left'; nicholas@2498: var curTime = document.createElement('div'); nicholas@2498: curTime.style.width = '50px'; nicholas@2498: this.curTimeSpan = document.createElement('span'); nicholas@2498: this.curTimeSpan.textContent = '00:00'; nicholas@2498: curTime.appendChild(this.curTimeSpan); nicholas@2498: this.object.appendChild(curTime); nicholas@2498: this.scrubberTrack = document.createElement('div'); nicholas@2498: this.scrubberTrack.className = 'playhead-scrub-track'; nicholas@2498: nicholas@2498: this.scrubberHead = document.createElement('div'); nicholas@2498: this.scrubberHead.id = 'playhead-scrubber'; nicholas@2498: this.scrubberTrack.appendChild(this.scrubberHead); nicholas@2498: this.object.appendChild(this.scrubberTrack); nicholas@2498: nicholas@2498: this.timePerPixel = 0; nicholas@2498: this.maxTime = 0; nicholas@2498: nicholas@2682: this.playbackObject = undefined; nicholas@2498: nicholas@2498: this.setTimePerPixel = function (audioObject) { nicholas@2498: //maxTime must be in seconds nicholas@2498: this.playbackObject = audioObject; nicholas@2498: this.maxTime = audioObject.buffer.buffer.duration; nicholas@2498: var width = 490; //500 - 10, 5 each side of the tracker head nicholas@2498: this.timePerPixel = this.maxTime / 490; nicholas@2498: if (this.maxTime < 60) { nicholas@2498: this.curTimeSpan.textContent = '0.00'; nicholas@2498: } else { nicholas@2498: this.curTimeSpan.textContent = '00:00'; nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.update = function () { nicholas@2498: // Update the playhead position, startPlay must be called nicholas@2498: if (this.timePerPixel > 0) { nicholas@2498: var time = this.playbackObject.getCurrentPosition(); nicholas@2498: if (time > 0 && time < this.maxTime) { nicholas@2498: var width = 490; nicholas@2498: var pix = Math.floor(time / this.timePerPixel); nicholas@2498: this.scrubberHead.style.left = pix + 'px'; nicholas@2498: if (this.maxTime > 60.0) { nicholas@2498: var secs = time % 60; nicholas@2498: var mins = Math.floor((time - secs) / 60); nicholas@2498: secs = secs.toString(); nicholas@2498: secs = secs.substr(0, 2); nicholas@2498: mins = mins.toString(); nicholas@2498: this.curTimeSpan.textContent = mins + ':' + secs; nicholas@2498: } else { nicholas@2498: time = time.toString(); nicholas@2498: this.curTimeSpan.textContent = time.substr(0, 4); nicholas@2498: } nicholas@2498: } else { nicholas@2498: this.scrubberHead.style.left = '0px'; nicholas@2498: if (this.maxTime < 60) { nicholas@2498: this.curTimeSpan.textContent = '0.00'; nicholas@2498: } else { nicholas@2498: this.curTimeSpan.textContent = '00:00'; nicholas@2498: } nicholas@2498: } nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2498: this.interval = undefined; nicholas@2498: nicholas@2498: this.start = function () { nicholas@2678: if (this.playbackObject !== undefined && this.interval === undefined) { nicholas@2498: if (this.maxTime < 60) { nicholas@2682: this.interval = window.setInterval(function () { nicholas@2498: interfaceContext.playhead.update(); nicholas@2498: }, 10); nicholas@2498: } else { nicholas@2682: this.interval = window.setInterval(function () { nicholas@2498: interfaceContext.playhead.update(); nicholas@2498: }, 100); nicholas@2498: } nicholas@2498: } nicholas@2498: }; nicholas@2498: this.stop = function () { nicholas@2682: window.clearInterval(this.interval); nicholas@2498: this.interval = undefined; nicholas@2224: this.scrubberHead.style.left = '0px'; nicholas@2498: if (this.maxTime < 60) { nicholas@2498: this.curTimeSpan.textContent = '0.00'; nicholas@2498: } else { nicholas@2498: this.curTimeSpan.textContent = '00:00'; nicholas@2498: } nicholas@2498: }; nicholas@2498: }; nicholas@2498: nicholas@2498: this.volume = new function () { 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@2352: this.root = document.createElement('div'); nicholas@2352: this.root.id = 'master-volume-root'; nicholas@2224: this.object = document.createElement('div'); nicholas@2352: this.object.className = 'master-volume-holder-float'; nicholas@2352: this.object.appendChild(this.root); 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@2498: 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@2498: this.slider.onmousemove = function (event) { nicholas@2224: interfaceContext.volume.valueDB = event.currentTarget.value; nicholas@2224: interfaceContext.volume.valueLin = decibelToLinear(interfaceContext.volume.valueDB); nicholas@2498: interfaceContext.volume.valueText.textContent = interfaceContext.volume.valueDB + 'dB'; nicholas@2224: audioEngineContext.outputGain.gain.value = interfaceContext.volume.valueLin; nicholas@2678: }; nicholas@2498: this.slider.onmouseup = function (event) { nicholas@2224: var storePoint = testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].getAllElementsByName('volumeTracker'); nicholas@2678: if (storePoint.length === 0) { nicholas@2224: storePoint = storage.document.createElement('metricresult'); nicholas@2498: storePoint.setAttribute('name', 'volumeTracker'); nicholas@2224: testState.currentStore.XMLDOM.getElementsByTagName('metric')[0].appendChild(storePoint); nicholas@2498: } else { nicholas@2224: storePoint = storePoint[0]; nicholas@2224: } nicholas@2224: var node = storage.document.createElement('movement'); nicholas@2498: node.setAttribute('test-time', audioEngineContext.timer.getTestTime()); nicholas@2498: node.setAttribute('volume', interfaceContext.volume.valueDB); nicholas@2498: node.setAttribute('format', 'dBFS'); nicholas@2224: storePoint.appendChild(node); nicholas@2678: }; nicholas@2498: 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@2352: this.root.appendChild(title); nicholas@2498: nicholas@2352: this.root.appendChild(this.slider); nicholas@2352: this.root.appendChild(this.valueText); nicholas@2498: nicholas@2498: this.resize = function (event) { nicholas@2352: if (window.innerWidth < 1000) { nicholas@2678: this.object.className = "master-volume-holder-inline"; nicholas@2352: } else { nicholas@2352: this.object.className = 'master-volume-holder-float'; nicholas@2352: } nicholas@2678: }; nicholas@2678: }; nicholas@2498: nicholas@2224: this.calibrationModuleObject = null; nicholas@2498: 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@2498: 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@2498: 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@2498: handleEvent: function (event) { nicholas@2498: 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@2498: 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@2678: this.gain.gain.value = value; nicholas@2224: } nicholas@2224: break; nicholas@2224: } nicholas@2224: }, nicholas@2498: disconnect: function () { nicholas@2224: this.gain.disconnect(); nicholas@2224: } nicholas@2678: }; 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@2498: obj.gain.gain.value = Math.random() * 2; nicholas@2224: obj.input.value = obj.gain.gain.value; nicholas@2498: obj.input.setAttribute('orient', 'vertical'); nicholas@2224: obj.input.type = "range"; nicholas@2593: obj.input.min = -12; nicholas@2593: obj.input.max = 0; nicholas@2224: obj.input.step = 0.25; nicholas@2224: if (f0 != 1000) { nicholas@2498: obj.input.value = (Math.random() * 12) - 6; nicholas@2224: } else { nicholas@2224: obj.input.value = 0; nicholas@2498: obj.root.style.backgroundColor = "rgb(255,125,125)"; nicholas@2224: } nicholas@2498: obj.input.addEventListener("mousemove", obj); nicholas@2498: obj.input.addEventListener("mouseenter", obj); nicholas@2498: obj.input.addEventListener("mouseleave", obj); nicholas@2498: 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@2678: }; nicholas@2498: this.collect = function () { nicholas@2682: this.calibrationNodes.forEach(function (obj) { nicholas@2224: var node = storage.document.createElement("calibrationresult"); nicholas@2498: node.setAttribute("frequency", obj.f); nicholas@2498: node.setAttribute("range-min", obj.input.min); nicholas@2498: node.setAttribute("range-max", obj.input.max); nicholas@2498: node.setAttribute("gain-lin", obj.gain.gain.value); nicholas@2224: this.storeDOM.appendChild(node); nicholas@2682: }, this); nicholas@2678: }; nicholas@2678: }; nicholas@2498: nicholas@2498: nicholas@2498: // Global Checkers nicholas@2498: // These functions will help enforce the checkers nicholas@2498: this.checkHiddenAnchor = function () { nicholas@2682: audioEngineContext.audioObjects.forEach(function (ao) { nicholas@2682: if (ao.specification.type === "anchor") { nicholas@2498: if (ao.interfaceDOM.getValue() > (ao.specification.marker / 100) && ao.specification.marker > 0) { nicholas@2498: // Anchor is not set below nicholas@2498: console.log('Anchor node not below marker value'); nicholas@2498: interfaceContext.lightbox.post("Message", 'Please keep listening'); nicholas@2224: this.storeErrorNode('Anchor node not below marker value'); nicholas@2498: return false; nicholas@2498: } nicholas@2498: } nicholas@2682: }, this); nicholas@2498: return true; nicholas@2498: }; nicholas@2498: nicholas@2498: this.checkHiddenReference = function () { nicholas@2682: audioEngineContext.audioObjects.forEach(function (ao) { nicholas@2498: if (ao.specification.type == "reference") { nicholas@2498: if (ao.interfaceDOM.getValue() < (ao.specification.marker / 100) && ao.specification.marker > 0) { nicholas@2498: // Anchor is not set below nicholas@2498: console.log('Reference node not above marker value'); nicholas@2224: this.storeErrorNode('Reference node not above marker value'); nicholas@2498: interfaceContext.lightbox.post("Message", 'Please keep listening'); nicholas@2498: return false; nicholas@2498: } nicholas@2498: } nicholas@2682: }, this); nicholas@2498: return true; nicholas@2498: }; nicholas@2498: nicholas@2498: this.checkFragmentsFullyPlayed = function () { nicholas@2498: // Checks the entire file has been played back nicholas@2498: // NOTE ! This will return true IF playback is Looped!!! nicholas@2498: if (audioEngineContext.loopPlayback) { nicholas@2498: console.log("WARNING - Looped source: Cannot check fragments are fully played"); nicholas@2498: return true; nicholas@2498: } nicholas@2498: var check_pass = true; nicholas@2682: var error_obj = [], nicholas@2682: i; nicholas@2682: for (i = 0; i < audioEngineContext.audioObjects.length; i++) { nicholas@2498: var object = audioEngineContext.audioObjects[i]; nicholas@2498: var time = object.buffer.buffer.duration; nicholas@2498: var metric = object.metric; nicholas@2498: var passed = false; nicholas@2498: for (var j = 0; j < metric.listenTracker.length; j++) { nicholas@2498: var bt = metric.listenTracker[j].getElementsByTagName('testtime'); nicholas@2498: var start_time = Number(bt[0].getAttribute('start')); nicholas@2498: var stop_time = Number(bt[0].getAttribute('stop')); nicholas@2498: var delta = stop_time - start_time; nicholas@2498: if (delta >= time) { nicholas@2498: passed = true; nicholas@2498: break; nicholas@2498: } nicholas@2498: } nicholas@2678: if (passed === false) { nicholas@2498: check_pass = false; nicholas@2498: console.log("Continue listening to track-" + object.interfaceDOM.getPresentedId()); nicholas@2498: error_obj.push(object.interfaceDOM.getPresentedId()); nicholas@2498: } nicholas@2498: } nicholas@2678: if (check_pass === false) { nicholas@2498: var str_start = "You have not completely listened to fragments "; nicholas@2682: for (i = 0; i < error_obj.length; i++) { nicholas@2498: str_start += error_obj[i]; nicholas@2498: if (i != error_obj.length - 1) { nicholas@2498: str_start += ', '; nicholas@2498: } nicholas@2498: } nicholas@2498: str_start += ". Please keep listening"; nicholas@2498: console.log("[ALERT]: " + str_start); nicholas@2498: this.storeErrorNode("[ALERT]: " + str_start); nicholas@2498: interfaceContext.lightbox.post("Error", str_start); nicholas@2444: return false; nicholas@2498: } nicholas@2444: return true; nicholas@2498: }; nicholas@2498: this.checkAllMoved = function () { nicholas@2498: var str = "You have not moved "; nicholas@2498: var failed = []; nicholas@2682: audioEngineContext.audioObjects.forEach(function (ao) { nicholas@2678: if (ao.metric.wasMoved === false && ao.interfaceDOM.canMove() === true) { nicholas@2498: failed.push(ao.interfaceDOM.getPresentedId()); nicholas@2498: } nicholas@2682: }, this); nicholas@2678: if (failed.length === 0) { nicholas@2498: return true; nicholas@2498: } else if (failed.length == 1) { nicholas@2498: str += 'track ' + failed[0]; nicholas@2498: } else { nicholas@2498: str += 'tracks '; nicholas@2498: for (var i = 0; i < failed.length - 1; i++) { nicholas@2498: str += failed[i] + ', '; nicholas@2498: } nicholas@2498: str += 'and ' + failed[i]; nicholas@2498: } nicholas@2498: str += '.'; nicholas@2498: interfaceContext.lightbox.post("Error", str); nicholas@2498: console.log(str); nicholas@2224: this.storeErrorNode(str); nicholas@2498: return false; nicholas@2498: }; nicholas@2498: this.checkAllPlayed = function () { nicholas@2498: var str = "You have not played "; nicholas@2498: var failed = []; nicholas@2682: audioEngineContext.audioObjects.forEach(function (ao) { nicholas@2678: if (ao.metric.wasListenedTo === false) { nicholas@2498: failed.push(ao.interfaceDOM.getPresentedId()); nicholas@2498: } nicholas@2682: }, this); nicholas@2678: if (failed.length === 0) { nicholas@2498: return true; nicholas@2498: } else if (failed.length == 1) { nicholas@2498: str += 'track ' + failed[0]; nicholas@2498: } else { nicholas@2498: str += 'tracks '; nicholas@2498: for (var i = 0; i < failed.length - 1; i++) { nicholas@2498: str += failed[i] + ', '; nicholas@2498: } nicholas@2498: str += 'and ' + failed[i]; nicholas@2498: } nicholas@2498: str += '.'; nicholas@2498: interfaceContext.lightbox.post("Error", str); nicholas@2498: console.log(str); nicholas@2224: this.storeErrorNode(str); nicholas@2498: return false; nicholas@2498: }; nicholas@2540: this.checkAllCommented = function () { nicholas@2540: var str = "You have not commented on all the fragments."; nicholas@2540: var cont = true, nicholas@2540: boxes = this.commentBoxes.boxes, nicholas@2540: numBoxes = boxes.length, nicholas@2540: i; nicholas@2540: for (i = 0; i < numBoxes; i++) { nicholas@2540: if (boxes[i].trackCommentBox.value === "") { nicholas@2540: interfaceContext.lightbox.post("Error", str); nicholas@2540: console.log(str); nicholas@2540: this.storeErrorNode(str); nicholas@2540: return false; nicholas@2540: } nicholas@2540: } nicholas@2540: return true; nicholas@2678: }; nicholas@2498: this.checkScaleRange = function (min, max) { nicholas@2310: var page = testState.getCurrentTestPage(); nicholas@2310: var audioObjects = audioEngineContext.audioObjects; nicholas@2310: var state = true; nicholas@2310: var str = "Please keep listening. "; nicholas@2310: var minRanking = Infinity; nicholas@2310: var maxRanking = -Infinity; nicholas@2682: audioEngineContext.audioObjects.forEach(function (ao) { nicholas@2310: var rank = ao.interfaceDOM.getValue(); nicholas@2498: if (rank < minRanking) { nicholas@2498: minRanking = rank; nicholas@2498: } nicholas@2498: if (rank > maxRanking) { nicholas@2498: maxRanking = rank; nicholas@2498: } nicholas@2682: }, this); nicholas@2498: if (minRanking * 100 > min) { nicholas@2498: str += "At least one fragment must be below the " + min + " mark."; nicholas@2310: state = false; nicholas@2310: } nicholas@2498: if (maxRanking * 100 < max) { nicholas@2678: str += "At least one fragment must be above the " + max + " mark."; nicholas@2310: state = false; nicholas@2310: } nicholas@2310: if (!state) { nicholas@2310: console.log(str); nicholas@2310: this.storeErrorNode(str); nicholas@2498: interfaceContext.lightbox.post("Error", str); nicholas@2310: } nicholas@2310: return state; nicholas@2678: }; nicholas@2498: nicholas@2498: this.storeErrorNode = function (errorMessage) { nicholas@2224: var time = audioEngineContext.timer.getTestTime(); nicholas@2224: var node = storage.document.createElement('error'); nicholas@2498: node.setAttribute('time', time); nicholas@2224: node.textContent = errorMessage; nicholas@2224: testState.currentStore.XMLDOM.appendChild(node); nicholas@2224: }; nicholas@2595: nicholas@2595: this.getLabel = function (labelType, index, labelStart) { nicholas@2595: /* nicholas@2595: Get the correct label based on type, index and offset nicholas@2595: */ nicholas@2595: nicholas@2595: function calculateLabel(labelType, index, offset) { nicholas@2595: if (labelType == "none") { nicholas@2595: return ""; nicholas@2595: } nicholas@2595: switch (labelType) { nicholas@2595: case "letter": nicholas@2596: return String.fromCharCode((index + offset) % 26 + 97); nicholas@2595: case "capital": nicholas@2607: return String.fromCharCode((index + offset) % 26 + 65); nicholas@2625: case "samediff": nicholas@2678: if (index === 0) { nicholas@2625: return "Same"; nicholas@2625: } else if (index == 1) { nicholas@2625: return "Difference"; nicholas@2625: } nicholas@2678: return ""; nicholas@2595: case "number": nicholas@2595: return String(index + offset); nicholas@2595: default: nicholas@2595: return ""; nicholas@2595: } nicholas@2595: } nicholas@2595: nicholas@2678: if (typeof labelStart !== "string" || labelStart.length === 0) { nicholas@2595: labelStart = String.fromCharCode(0); nicholas@2595: } nicholas@2595: nicholas@2595: switch (labelType) { nicholas@2595: case "letter": nicholas@2595: labelStart = labelStart.charCodeAt(0); nicholas@2596: if (labelStart < 97 || labelStart > 122) { nicholas@2595: labelStart = 97; nicholas@2595: } nicholas@2595: labelStart -= 97; nicholas@2595: break; nicholas@2595: case "capital": nicholas@2595: labelStart = labelStart.charCodeAt(0); nicholas@2596: if (labelStart < 65 || labelStart > 90) { nicholas@2595: labelStart = 65; nicholas@2595: } nicholas@2595: labelStart -= 65; nicholas@2595: break; nicholas@2595: case "number": nicholas@2608: labelStart = Number(labelStart); nicholas@2608: if (!isFinite(labelStart)) { nicholas@2595: labelStart = 1; nicholas@2595: } nicholas@2595: break; nicholas@2595: default: nicholas@2596: labelStart = 0; nicholas@2595: } nicholas@2595: if (typeof index == "number") { nicholas@2595: return calculateLabel(labelType, index, labelStart); nicholas@2595: } else if (index.length && index.length > 0) { nicholas@2595: var a = [], nicholas@2595: l = index.length, nicholas@2595: i; nicholas@2595: for (i = 0; i < l; i++) { nicholas@2595: a[i] = calculateLabel(labelType, index[i], labelStart); nicholas@2595: } nicholas@2595: return a; nicholas@2595: } else { nicholas@2595: throw ("Invalid arguments"); nicholas@2595: } nicholas@2678: }; nicholas@2649: nicholas@2649: this.getCombinedInterfaces = function (page) { nicholas@2649: // Combine the interfaces with the global interface nodes nicholas@2649: var global = specification.interfaces, nicholas@2649: local = page.interfaces; nicholas@2649: local.forEach(function (locInt) { nicholas@2649: // Iterate through the options nodes nicholas@2649: var addList = []; nicholas@2649: global.options.forEach(function (gopt) { nicholas@2649: var lopt = locInt.options.find(function (lopt) { nicholas@2649: return (lopt.name == gopt.name) && (lopt.type == gopt.type); nicholas@2649: }); nicholas@2649: if (!lopt) { nicholas@2649: // Global option doesn't exist locally nicholas@2649: addList.push(gopt); nicholas@2649: } nicholas@2649: }); nicholas@2649: locInt.options = locInt.options.concat(addList); nicholas@2649: if (!locInt.scales && global.scales) { nicholas@2649: // Use the global default scales nicholas@2649: locInt.scales = global.scales; nicholas@2649: } nicholas@2649: }); nicholas@2649: return local; nicholas@2678: }; nicholas@2224: } nicholas@2224: nicholas@2498: function Storage() { nicholas@2498: // Holds results in XML format until ready for collection nicholas@2498: this.globalPreTest = null; nicholas@2498: this.globalPostTest = null; nicholas@2498: this.testPages = []; nicholas@2498: this.document = null; nicholas@2498: this.root = null; nicholas@2498: this.state = 0; nicholas@2498: nicholas@2498: this.initialise = function (existingStore) { nicholas@2678: if (existingStore === undefined) { nicholas@2224: // We need to get the sessionKey nicholas@2510: this.SessionKey.requestKey(); nicholas@2498: this.document = document.implementation.createDocument(null, "waetresult", null); nicholas@2224: this.root = this.document.childNodes[0]; nicholas@2224: var projectDocument = specification.projectXML; nicholas@2682: projectDocument.setAttribute('file-name', specification.url); nicholas@2682: projectDocument.setAttribute('url', qualifyURL(specification.url)); nicholas@2224: this.root.appendChild(projectDocument); nicholas@2224: this.root.appendChild(interfaceContext.returnDateNode()); nicholas@2224: this.root.appendChild(interfaceContext.returnNavigator()); nicholas@2224: } else { nicholas@2224: this.document = existingStore; nicholas@2294: this.root = existingStore.firstChild; nicholas@2224: this.SessionKey.key = this.root.getAttribute("key"); nicholas@2224: } nicholas@2678: if (specification.preTest !== undefined) { nicholas@2498: this.globalPreTest = new this.surveyNode(this, this.root, specification.preTest); nicholas@2498: } nicholas@2678: if (specification.postTest !== undefined) { nicholas@2498: this.globalPostTest = new this.surveyNode(this, this.root, specification.postTest); nicholas@2498: } nicholas@2498: }; nicholas@2498: nicholas@2224: this.SessionKey = { nicholas@2224: key: null, nicholas@2224: request: new XMLHttpRequest(), nicholas@2224: parent: this, nicholas@2498: handleEvent: function () { nicholas@2224: var parse = new DOMParser(); nicholas@2498: var xml = parse.parseFromString(this.request.response, "text/xml"); nicholas@2678: if (this.request.response.length === 0) { nicholas@2515: console.error("An unspecified error occured, no server key could be generated"); nicholas@2376: return; nicholas@2376: } nicholas@2498: if (xml.getElementsByTagName("state").length > 0) { nicholas@2498: if (xml.getElementsByTagName("state")[0].textContent == "OK") { nicholas@2498: this.key = xml.getAllElementsByTagName("key")[0].textContent; nicholas@2498: this.parent.root.setAttribute("key", this.key); nicholas@2498: this.parent.root.setAttribute("state", "empty"); nicholas@2516: this.update(); nicholas@2515: return; nicholas@2514: } else if (xml.getElementsByTagName("state")[0].textContent == "ERROR") { nicholas@2515: this.key = null; nicholas@2514: console.error("Could not generate server key. Server responded with error message: \"" + xml.getElementsByTagName("message")[0].textContent + "\""); nicholas@2515: return; nicholas@2498: } nicholas@2498: } nicholas@2515: this.key = null; nicholas@2515: console.error("An unspecified error occured, no server key could be generated"); nicholas@2224: }, nicholas@2510: requestKey: function () { nicholas@2510: // For new servers, request a new key from the server nicholas@2510: var returnURL = ""; nicholas@2510: if (typeof specification.projectReturn == "string") { nicholas@2510: if (specification.projectReturn.substr(0, 4) == "http") { nicholas@2510: returnURL = specification.projectReturn; nicholas@2510: } nicholas@2510: } nicholas@2510: this.request.open("GET", returnURL + "php/requestKey.php", true); nicholas@2510: this.request.addEventListener("load", this); nicholas@2510: this.request.send(); nicholas@2510: }, nicholas@2498: update: function () { nicholas@2678: if (this.key === null) { nicholas@2357: console.log("Cannot save as key == null"); nicholas@2357: return; nicholas@2357: } nicholas@2498: this.parent.root.setAttribute("state", "update"); nicholas@2224: var xmlhttp = new XMLHttpRequest(); nicholas@2302: var returnURL = ""; nicholas@2302: if (typeof specification.projectReturn == "string") { nicholas@2498: if (specification.projectReturn.substr(0, 4) == "http") { nicholas@2302: returnURL = specification.projectReturn; nicholas@2302: } nicholas@2302: } nicholas@2498: xmlhttp.open("POST", returnURL + "php/save.php?key=" + this.key); nicholas@2224: xmlhttp.setRequestHeader('Content-Type', 'text/xml'); nicholas@2498: xmlhttp.onerror = function () { nicholas@2224: console.log('Error updating file to server!'); nicholas@2224: }; nicholas@2224: var hold = document.createElement("div"); nicholas@2224: var clone = this.parent.root.cloneNode(true); nicholas@2224: hold.appendChild(clone); nicholas@2498: xmlhttp.onload = function () { nicholas@2224: if (this.status >= 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@2498: console.log("Intermediate save: OK, written " + file.getAttribute("bytes") + "B"); nicholas@2224: } else { nicholas@2224: var message = response.getElementsByTagName("message"); nicholas@2498: console.log("Intermediate save: Error! " + message.textContent); nicholas@2224: } nicholas@2224: } nicholas@2678: }; nicholas@2224: xmlhttp.send([hold.innerHTML]); nicholas@2224: } nicholas@2678: }; nicholas@2498: nicholas@2498: this.createTestPageStore = function (specification) { nicholas@2498: var store = new this.pageNode(this, specification); nicholas@2498: this.testPages.push(store); nicholas@2498: return this.testPages[this.testPages.length - 1]; nicholas@2498: }; nicholas@2498: nicholas@2498: this.surveyNode = function (parent, root, specification) { nicholas@2498: this.specification = specification; nicholas@2498: this.parent = parent; nicholas@2224: this.state = "empty"; nicholas@2498: this.XMLDOM = this.parent.document.createElement('survey'); nicholas@2498: this.XMLDOM.setAttribute('location', this.specification.location); nicholas@2498: this.XMLDOM.setAttribute("state", this.state); nicholas@2682: this.specification.options.forEach(function (optNode) { nicholas@2498: if (optNode.type != 'statement') { nicholas@2498: var node = this.parent.document.createElement('surveyresult'); nicholas@2498: node.setAttribute("ref", optNode.id); nicholas@2498: node.setAttribute('type', optNode.type); nicholas@2498: this.XMLDOM.appendChild(node); nicholas@2498: } nicholas@2682: }, this); nicholas@2498: root.appendChild(this.XMLDOM); nicholas@2498: nicholas@2498: this.postResult = function (node) { nicholas@2682: function postNumber(doc, value) { nicholas@2682: var child = doc.createElement("response"); nicholas@2682: child.textContent = value; nicholas@2682: return child; nicholas@2682: } nicholas@2682: nicholas@2682: function postRadio(doc, node) { nicholas@2682: var child = doc.createElement('response'); nicholas@2682: if (node.response !== null) { nicholas@2682: child.setAttribute('name', node.response.name); nicholas@2682: child.textContent = node.response.text; nicholas@2682: } nicholas@2682: return child; nicholas@2682: } nicholas@2682: nicholas@2682: function postCheckbox(doc, node) { nicholas@2682: var checkNode = doc.createElement('response'); nicholas@2682: checkNode.setAttribute('name', node.name); nicholas@2682: checkNode.setAttribute('checked', node.checked); nicholas@2682: return checkNode; nicholas@2682: } nicholas@2498: // From popup: node is the popupOption node containing both spec. and results nicholas@2498: // ID is the position nicholas@2498: if (node.specification.type == 'statement') { nicholas@2498: return; nicholas@2498: } nicholas@2498: var surveyresult = this.XMLDOM.firstChild; nicholas@2678: while (surveyresult !== null) { nicholas@2498: if (surveyresult.getAttribute("ref") == node.specification.id) { nicholas@2224: break; nicholas@2224: } nicholas@2224: surveyresult = surveyresult.nextElementSibling; nicholas@2224: } nicholas@2498: switch (node.specification.type) { nicholas@2498: case "number": nicholas@2498: case "question": n@2583: case "slider": nicholas@2682: surveyresult.appendChild(postNumber(this.parent.document, node.response)); nicholas@2464: break; nicholas@2498: case "radio": nicholas@2682: surveyresult.appendChild(postRadio(this.parent.document, node)); nicholas@2498: break; nicholas@2498: case "checkbox": nicholas@2678: if (node.response === undefined) { nicholas@2498: surveyresult.appendChild(this.parent.document.createElement('response')); nicholas@2498: break; nicholas@2498: } nicholas@2498: for (var i = 0; i < node.response.length; i++) { nicholas@2682: surveyresult.appendChild(postCheckbox(this.parent.document, node.response[i])); nicholas@2498: } nicholas@2498: break; nicholas@2498: } nicholas@2498: }; nicholas@2498: this.complete = function () { nicholas@2498: this.state = "complete"; nicholas@2498: this.XMLDOM.setAttribute("state", this.state); nicholas@2678: }; nicholas@2498: }; nicholas@2498: nicholas@2498: this.pageNode = function (parent, specification) { nicholas@2498: // Create one store per test page nicholas@2498: this.specification = specification; nicholas@2498: this.parent = parent; nicholas@2498: this.state = "empty"; nicholas@2498: this.XMLDOM = this.parent.document.createElement('page'); nicholas@2498: this.XMLDOM.setAttribute('ref', specification.id); nicholas@2498: this.XMLDOM.setAttribute('presentedId', specification.presentedId); nicholas@2498: this.XMLDOM.setAttribute("state", this.state); nicholas@2681: if (specification.preTest !== null) { nicholas@2498: this.preTest = new this.parent.surveyNode(this.parent, this.XMLDOM, this.specification.preTest); nicholas@2498: } nicholas@2681: if (specification.postTest !== null) { nicholas@2498: this.postTest = new this.parent.surveyNode(this.parent, this.XMLDOM, this.specification.postTest); nicholas@2498: } nicholas@2498: nicholas@2498: // Add any page metrics nicholas@2498: var page_metric = this.parent.document.createElement('metric'); nicholas@2498: this.XMLDOM.appendChild(page_metric); nicholas@2498: nicholas@2498: // Add the audioelement nicholas@2682: this.specification.audioElements.forEach(function (element) { nicholas@2498: var aeNode = this.parent.document.createElement('audioelement'); nicholas@2498: aeNode.setAttribute('ref', element.id); nicholas@2678: if (element.name !== undefined) { nicholas@2678: aeNode.setAttribute('name', element.name); nicholas@2678: } nicholas@2498: aeNode.setAttribute('type', element.type); nicholas@2498: aeNode.setAttribute('url', element.url); nicholas@2498: aeNode.setAttribute('fqurl', qualifyURL(element.url)); nicholas@2498: aeNode.setAttribute('gain', element.gain); nicholas@2498: if (element.type == 'anchor' || element.type == 'reference') { nicholas@2498: if (element.marker > 0) { nicholas@2498: aeNode.setAttribute('marker', element.marker); nicholas@2464: } nicholas@2498: } nicholas@2498: var ae_metric = this.parent.document.createElement('metric'); nicholas@2498: aeNode.appendChild(ae_metric); nicholas@2498: this.XMLDOM.appendChild(aeNode); nicholas@2682: }, this); nicholas@2498: nicholas@2498: this.parent.root.appendChild(this.XMLDOM); nicholas@2498: nicholas@2498: this.complete = function () { nicholas@2224: this.state = "complete"; nicholas@2498: this.XMLDOM.setAttribute("state", "complete"); nicholas@2678: }; nicholas@2498: }; nicholas@2498: this.update = function () { nicholas@2224: this.SessionKey.update(); nicholas@2678: }; nicholas@2498: this.finish = function () { nicholas@2678: if (this.state === 0) { nicholas@2224: this.update(); nicholas@2498: } nicholas@2498: this.state = 1; nicholas@2498: this.root.setAttribute("state", "complete"); nicholas@2498: return this.root; nicholas@2498: }; nicholas@2224: } nicholas@2384: nicholas@2401: var window_depedancy_callback; nicholas@2498: window_depedancy_callback = window.setInterval(function () { nicholas@2401: if (check_dependancies()) { nicholas@2401: window.clearInterval(window_depedancy_callback); nicholas@2401: onload(); nicholas@2401: } else { nicholas@2401: document.getElementById("topLevelBody").innerHTML = "

Loading Resources

"; nicholas@2401: } nicholas@2498: }, 100);