annotate core.js @ 1443:bbfcb31c37e4

Bug #1502: Any parser XML errors will show the error and halt the rest of the loading.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Fri, 18 Dec 2015 18:28:06 +0000
parents fd22bb563f85
children 8540d153caec
rev   line source
nickjillings@1404 1 /**
nickjillings@1404 2 * core.js
nickjillings@1404 3 *
nickjillings@1404 4 * Main script to run, calls all other core functions and manages loading/store to backend.
nickjillings@1404 5 * Also contains all global variables.
nickjillings@1404 6 */
nickjillings@1404 7
nickjillings@1404 8 /* create the web audio API context and store in audioContext*/
nickjillings@1404 9 var audioContext; // Hold the browser web audio API
nickjillings@1404 10 var projectXML; // Hold the parsed setup XML
nickjillings@1404 11 var specification;
nickjillings@1404 12 var interfaceContext;
nickjillings@1404 13 var popup; // Hold the interfacePopup object
nickjillings@1404 14 var testState;
nickjillings@1404 15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
nickjillings@1404 16 var audioEngineContext; // The custome AudioEngine object
nickjillings@1404 17 var projectReturn; // Hold the URL for the return
nickjillings@1404 18
nickjillings@1404 19
nickjillings@1404 20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
nickjillings@1404 21 AudioBufferSourceNode.prototype.owner = undefined;
nickjillings@1430 22 // Add a prototype to the bufferNode to hold the desired LINEAR gain
nickjillings@1430 23 AudioBuffer.prototype.gain = undefined;
nickjillings@1430 24 // Add a prototype to the bufferNode to hold the computed LUFS loudness
nickjillings@1430 25 AudioBuffer.prototype.lufs = undefined;
nickjillings@1404 26
nickjillings@1404 27 window.onload = function() {
nickjillings@1404 28 // Function called once the browser has loaded all files.
nickjillings@1404 29 // This should perform any initial commands such as structure / loading documents
nickjillings@1404 30
nickjillings@1404 31 // Create a web audio API context
nickjillings@1404 32 // Fixed for cross-browser support
nickjillings@1404 33 var AudioContext = window.AudioContext || window.webkitAudioContext;
nickjillings@1404 34 audioContext = new AudioContext;
nickjillings@1404 35
nickjillings@1404 36 // Create test state
nickjillings@1404 37 testState = new stateMachine();
nickjillings@1404 38
nickjillings@1404 39 // Create the popup interface object
nickjillings@1404 40 popup = new interfacePopup();
nickjillings@1404 41
nickjillings@1404 42 // Create the specification object
nickjillings@1404 43 specification = new Specification();
nickjillings@1404 44
nickjillings@1404 45 // Create the interface object
nickjillings@1404 46 interfaceContext = new Interface(specification);
nickjillings@1410 47 // Define window callbacks for interface
nickjillings@1410 48 window.onresize = function(event){interfaceContext.resizeWindow(event);};
nickjillings@1404 49 };
nickjillings@1404 50
nickjillings@1408 51 function loadProjectSpec(url) {
nickjillings@1408 52 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nickjillings@1408 53 // If url is null, request client to upload project XML document
nickjillings@1408 54 var r = new XMLHttpRequest();
nickjillings@1408 55 r.open('GET',url,true);
nickjillings@1408 56 r.onload = function() {
nickjillings@1408 57 loadProjectSpecCallback(r.response);
nickjillings@1408 58 };
nickjillings@1408 59 r.send();
nickjillings@1408 60 };
nickjillings@1408 61
nickjillings@1408 62 function loadProjectSpecCallback(response) {
nickjillings@1408 63 // Function called after asynchronous download of XML project specification
nickjillings@1408 64 //var decode = $.parseXML(response);
nickjillings@1408 65 //projectXML = $(decode);
nickjillings@1408 66
nickjillings@1408 67 var parse = new DOMParser();
nickjillings@1408 68 projectXML = parse.parseFromString(response,'text/xml');
nickjillings@1443 69 var errorNode = projectXML.getElementsByTagName('parsererror');
nickjillings@1443 70 if (errorNode.length >= 1)
nickjillings@1443 71 {
nickjillings@1443 72 var msg = document.createElement("h3");
nickjillings@1443 73 msg.textContent = "FATAL ERROR";
nickjillings@1443 74 var span = document.createElement("span");
nickjillings@1443 75 span.textContent = "The XML parser returned the following errors when decoding your XML file";
nickjillings@1443 76 document.getElementsByTagName('body')[0].appendChild(msg);
nickjillings@1443 77 document.getElementsByTagName('body')[0].appendChild(span);
nickjillings@1443 78 document.getElementsByTagName('body')[0].appendChild(errorNode[0]);
nickjillings@1443 79 return;
nickjillings@1443 80 }
nickjillings@1408 81
nickjillings@1408 82 // Build the specification
nickjillings@1408 83 specification.decode(projectXML);
nickjillings@1408 84
nickjillings@1408 85 // Detect the interface to use and load the relevant javascripts.
nickjillings@1408 86 var interfaceJS = document.createElement('script');
nickjillings@1408 87 interfaceJS.setAttribute("type","text/javascript");
nickjillings@1408 88 if (specification.interfaceType == 'APE') {
nickjillings@1408 89 interfaceJS.setAttribute("src","ape.js");
nickjillings@1408 90
nickjillings@1408 91 // APE comes with a css file
nickjillings@1408 92 var css = document.createElement('link');
nickjillings@1408 93 css.rel = 'stylesheet';
nickjillings@1408 94 css.type = 'text/css';
nickjillings@1408 95 css.href = 'ape.css';
nickjillings@1408 96
nickjillings@1408 97 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1408 98 } else if (specification.interfaceType == "MUSHRA")
nickjillings@1408 99 {
nickjillings@1408 100 interfaceJS.setAttribute("src","mushra.js");
nickjillings@1408 101
nickjillings@1408 102 // MUSHRA comes with a css file
nickjillings@1408 103 var css = document.createElement('link');
nickjillings@1408 104 css.rel = 'stylesheet';
nickjillings@1408 105 css.type = 'text/css';
nickjillings@1408 106 css.href = 'mushra.css';
nickjillings@1408 107
nickjillings@1408 108 document.getElementsByTagName("head")[0].appendChild(css);
nickjillings@1408 109 }
nickjillings@1408 110 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
nickjillings@1408 111
nickjillings@1410 112 // Create the audio engine object
nickjillings@1410 113 audioEngineContext = new AudioEngine(specification);
nickjillings@1410 114
nickjillings@1410 115 testState.stateMap.push(specification.preTest);
nickjillings@1410 116
nickjillings@1410 117 $(specification.audioHolders).each(function(index,elem){
nickjillings@1410 118 testState.stateMap.push(elem);
nickjillings@1410 119 $(elem.audioElements).each(function(i,audioElem){
nickjillings@1410 120 var URL = audioElem.parent.hostURL + audioElem.url;
nickjillings@1410 121 var buffer = null;
nickjillings@1410 122 for (var i=0; i<audioEngineContext.buffers.length; i++)
nickjillings@1410 123 {
nickjillings@1410 124 if (URL == audioEngineContext.buffers[i].url)
nickjillings@1410 125 {
nickjillings@1410 126 buffer = audioEngineContext.buffers[i];
nickjillings@1410 127 break;
nickjillings@1410 128 }
nickjillings@1410 129 }
nickjillings@1410 130 if (buffer == null)
nickjillings@1410 131 {
nickjillings@1430 132 buffer = new audioEngineContext.bufferObj();
nickjillings@1430 133 buffer.getMedia(URL);
nickjillings@1410 134 audioEngineContext.buffers.push(buffer);
nickjillings@1410 135 }
nickjillings@1410 136 });
nickjillings@1410 137 });
nickjillings@1410 138
nickjillings@1410 139 testState.stateMap.push(specification.postTest);
nickjillings@1408 140 }
nickjillings@1408 141
nickjillings@1408 142 function createProjectSave(destURL) {
nickjillings@1408 143 // Save the data from interface into XML and send to destURL
nickjillings@1408 144 // If destURL is null then download XML in client
nickjillings@1408 145 // Now time to render file locally
nickjillings@1408 146 var xmlDoc = interfaceXMLSave();
nickjillings@1408 147 var parent = document.createElement("div");
nickjillings@1408 148 parent.appendChild(xmlDoc);
nickjillings@1408 149 var file = [parent.innerHTML];
nickjillings@1408 150 if (destURL == "null" || destURL == undefined) {
nickjillings@1408 151 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1408 152 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1408 153 var a = document.createElement("a");
nickjillings@1408 154 a.hidden = '';
nickjillings@1408 155 a.href = dnlk;
nickjillings@1408 156 a.download = "save.xml";
nickjillings@1408 157 a.textContent = "Save File";
nickjillings@1408 158
nickjillings@1408 159 popup.showPopup();
nickjillings@1408 160 popup.popupContent.innerHTML = null;
nickjillings@1408 161 popup.popupContent.appendChild(a);
nickjillings@1408 162 } else {
nickjillings@1408 163 var xmlhttp = new XMLHttpRequest;
nickjillings@1408 164 xmlhttp.open("POST",destURL,true);
nickjillings@1408 165 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
nickjillings@1408 166 xmlhttp.onerror = function(){
nickjillings@1408 167 console.log('Error saving file to server! Presenting download locally');
nickjillings@1408 168 createProjectSave(null);
nickjillings@1408 169 };
nickjillings@1408 170 xmlhttp.onreadystatechange = function() {
nickjillings@1408 171 console.log(xmlhttp.status);
nickjillings@1408 172 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
nickjillings@1408 173 createProjectSave(null);
nickjillings@1408 174 } else {
nickjillings@1408 175 if (xmlhttp.responseXML == null)
nickjillings@1408 176 {
nickjillings@1408 177 return createProjectSave(null);
nickjillings@1408 178 }
nickjillings@1408 179 var response = xmlhttp.responseXML.childNodes[0];
nickjillings@1408 180 if (response.getAttribute('state') == "OK")
nickjillings@1408 181 {
nickjillings@1408 182 var file = response.getElementsByTagName('file')[0];
nickjillings@1408 183 console.log('Save OK: Filename '+file.textContent+','+file.getAttribute('bytes')+'B');
nickjillings@1408 184 popup.showPopup();
nickjillings@1408 185 popup.popupContent.innerHTML = null;
nickjillings@1408 186 popup.popupContent.textContent = "Thank you!";
nickjillings@1408 187 } else {
nickjillings@1408 188 var message = response.getElementsByTagName('message')[0];
nickjillings@1408 189 errorSessionDump(message.textContent);
nickjillings@1408 190 }
nickjillings@1408 191 }
nickjillings@1408 192 };
nickjillings@1408 193 xmlhttp.send(file);
nickjillings@1408 194 }
nickjillings@1408 195 }
nickjillings@1408 196
nickjillings@1408 197 function errorSessionDump(msg){
nickjillings@1408 198 // Create the partial interface XML save
nickjillings@1408 199 // Include error node with message on why the dump occured
nickjillings@1443 200 popup.showPopup();
nickjillings@1443 201 popup.popupContent.innerHTML = null;
nickjillings@1443 202 var err = document.createElement('error');
nickjillings@1443 203 var parent = document.createElement("div");
nickjillings@1443 204 if (typeof msg === "object")
nickjillings@1443 205 {
nickjillings@1443 206 err.appendChild(msg);
nickjillings@1443 207 popup.popupContent.appendChild(msg);
nickjillings@1443 208
nickjillings@1443 209 } else {
nickjillings@1443 210 err.textContent = msg;
nickjillings@1443 211 popup.popupContent.innerHTML = "ERROR : "+msg;
nickjillings@1443 212 }
nickjillings@1408 213 var xmlDoc = interfaceXMLSave();
nickjillings@1408 214 xmlDoc.appendChild(err);
nickjillings@1408 215 parent.appendChild(xmlDoc);
nickjillings@1408 216 var file = [parent.innerHTML];
nickjillings@1408 217 var bb = new Blob(file,{type : 'application/xml'});
nickjillings@1408 218 var dnlk = window.URL.createObjectURL(bb);
nickjillings@1408 219 var a = document.createElement("a");
nickjillings@1408 220 a.hidden = '';
nickjillings@1408 221 a.href = dnlk;
nickjillings@1408 222 a.download = "save.xml";
nickjillings@1408 223 a.textContent = "Save File";
nickjillings@1408 224
nickjillings@1443 225
nickjillings@1443 226
nickjillings@1408 227 popup.popupContent.appendChild(a);
nickjillings@1408 228 }
nickjillings@1408 229
nickjillings@1408 230 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nickjillings@1408 231 function interfaceXMLSave(){
nickjillings@1408 232 // Create the XML string to be exported with results
nickjillings@1408 233 var xmlDoc = document.createElement("BrowserEvaluationResult");
nickjillings@1408 234 var projectDocument = specification.projectXML;
nickjillings@1408 235 projectDocument.setAttribute('file-name',url);
nickjillings@1408 236 xmlDoc.appendChild(projectDocument);
nickjillings@1408 237 xmlDoc.appendChild(returnDateNode());
nickjillings@1408 238 xmlDoc.appendChild(interfaceContext.returnNavigator());
nickjillings@1408 239 for (var i=0; i<testState.stateResults.length; i++)
nickjillings@1408 240 {
nickjillings@1408 241 xmlDoc.appendChild(testState.stateResults[i]);
nickjillings@1408 242 }
nickjillings@1408 243
nickjillings@1408 244 return xmlDoc;
nickjillings@1408 245 }
nickjillings@1408 246
nickjillings@1426 247 function linearToDecibel(gain)
nickjillings@1426 248 {
nickjillings@1426 249 return 20.0*Math.log10(gain);
nickjillings@1426 250 }
nickjillings@1426 251
nickjillings@1426 252 function decibelToLinear(gain)
nickjillings@1426 253 {
nickjillings@1426 254 return Math.pow(10,gain/20.0);
nickjillings@1426 255 }
nickjillings@1426 256
nickjillings@1404 257 function interfacePopup() {
nickjillings@1404 258 // Creates an object to manage the popup
nickjillings@1404 259 this.popup = null;
nickjillings@1404 260 this.popupContent = null;
nickjillings@1404 261 this.popupTitle = null;
nickjillings@1404 262 this.popupResponse = null;
nickjillings@1404 263 this.buttonProceed = null;
nickjillings@1404 264 this.buttonPrevious = null;
nickjillings@1404 265 this.popupOptions = null;
nickjillings@1404 266 this.currentIndex = null;
nickjillings@1404 267 this.responses = null;
nickjillings@1422 268 $(window).keypress(function(e){
nickjillings@1422 269 if (e.keyCode == 13 && popup.popup.style.visibility == 'visible')
nickjillings@1422 270 {
nickjillings@1422 271 console.log(e);
nickjillings@1422 272 popup.buttonProceed.onclick();
nickjillings@1424 273 e.preventDefault();
nickjillings@1422 274 }
nickjillings@1422 275 });
nickjillings@1404 276
nickjillings@1404 277 this.createPopup = function(){
nickjillings@1404 278 // Create popup window interface
nickjillings@1404 279 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1404 280 var blank = document.createElement('div');
nickjillings@1404 281 blank.className = 'testHalt';
nickjillings@1404 282
nickjillings@1404 283 this.popup = document.createElement('div');
nickjillings@1404 284 this.popup.id = 'popupHolder';
nickjillings@1404 285 this.popup.className = 'popupHolder';
nickjillings@1404 286 this.popup.style.position = 'absolute';
nickjillings@1404 287 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1404 288 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1404 289
nickjillings@1404 290 this.popupContent = document.createElement('div');
nickjillings@1404 291 this.popupContent.id = 'popupContent';
nickjillings@1404 292 this.popupContent.style.marginTop = '20px';
nickjillings@1404 293 this.popupContent.style.marginBottom = '5px';
nickjillings@1404 294 this.popup.appendChild(this.popupContent);
nickjillings@1404 295
nickjillings@1404 296 var titleHolder = document.createElement('div');
nickjillings@1404 297 titleHolder.id = 'popupTitleHolder';
nickjillings@1404 298 titleHolder.align = 'center';
nickjillings@1404 299 titleHolder.style.width = 'inherit';
nickjillings@1404 300 titleHolder.style.minHeight = '25px';
nickjillings@1404 301 titleHolder.style.maxHeight = '250px';
nickjillings@1404 302 titleHolder.style.overflow = 'auto';
nickjillings@1404 303 titleHolder.style.marginBottom = '5px';
nickjillings@1404 304
nickjillings@1404 305 this.popupTitle = document.createElement('span');
nickjillings@1404 306 this.popupTitle.id = 'popupTitle';
nickjillings@1404 307 titleHolder.appendChild(this.popupTitle);
nickjillings@1404 308 this.popupContent.appendChild(titleHolder);
nickjillings@1404 309
nickjillings@1404 310 this.popupResponse = document.createElement('div');
nickjillings@1404 311 this.popupResponse.id = 'popupResponse';
nickjillings@1404 312 this.popupResponse.align = 'center';
nickjillings@1404 313 this.popupResponse.style.width = 'inherit';
nickjillings@1404 314 this.popupResponse.style.minHeight = '50px';
nickjillings@1404 315 this.popupResponse.style.maxHeight = '320px';
nickjillings@1404 316 this.popupResponse.style.overflow = 'auto';
nickjillings@1404 317 this.popupContent.appendChild(this.popupResponse);
nickjillings@1404 318
nickjillings@1404 319 this.buttonProceed = document.createElement('button');
nickjillings@1404 320 this.buttonProceed.className = 'popupButton';
nickjillings@1404 321 this.buttonProceed.position = 'relative';
nickjillings@1404 322 this.buttonProceed.style.left = '390px';
nickjillings@1404 323 this.buttonProceed.innerHTML = 'Next';
nickjillings@1404 324 this.buttonProceed.onclick = function(){popup.proceedClicked();};
nickjillings@1404 325
nickjillings@1404 326 this.buttonPrevious = document.createElement('button');
nickjillings@1404 327 this.buttonPrevious.className = 'popupButton';
nickjillings@1404 328 this.buttonPrevious.position = 'relative';
nickjillings@1404 329 this.buttonPrevious.style.left = '10px';
nickjillings@1404 330 this.buttonPrevious.innerHTML = 'Back';
nickjillings@1404 331 this.buttonPrevious.onclick = function(){popup.previousClick();};
nickjillings@1404 332
nickjillings@1404 333 this.popupContent.appendChild(this.buttonPrevious);
nickjillings@1404 334 this.popupContent.appendChild(this.buttonProceed);
nickjillings@1404 335
nickjillings@1404 336 this.popup.style.zIndex = -1;
nickjillings@1404 337 this.popup.style.visibility = 'hidden';
nickjillings@1404 338 blank.style.zIndex = -2;
nickjillings@1404 339 blank.style.visibility = 'hidden';
nickjillings@1404 340 insertPoint.appendChild(this.popup);
nickjillings@1404 341 insertPoint.appendChild(blank);
nickjillings@1404 342 };
nickjillings@1404 343
nickjillings@1404 344 this.showPopup = function(){
nickjillings@1404 345 if (this.popup == null) {
nickjillings@1404 346 this.createPopup();
nickjillings@1404 347 }
nickjillings@1404 348 this.popup.style.zIndex = 3;
nickjillings@1404 349 this.popup.style.visibility = 'visible';
nickjillings@1404 350 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1404 351 blank.style.zIndex = 2;
nickjillings@1404 352 blank.style.visibility = 'visible';
nickjillings@1404 353 };
nickjillings@1404 354
nickjillings@1404 355 this.hidePopup = function(){
nickjillings@1404 356 this.popup.style.zIndex = -1;
nickjillings@1404 357 this.popup.style.visibility = 'hidden';
nickjillings@1404 358 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1404 359 blank.style.zIndex = -2;
nickjillings@1404 360 blank.style.visibility = 'hidden';
nickjillings@1404 361 this.buttonPrevious.style.visibility = 'inherit';
nickjillings@1404 362 };
nickjillings@1404 363
nickjillings@1404 364 this.postNode = function() {
nickjillings@1404 365 // This will take the node from the popupOptions and display it
nickjillings@1404 366 var node = this.popupOptions[this.currentIndex];
nickjillings@1404 367 this.popupResponse.innerHTML = null;
nickjillings@1404 368 if (node.type == 'statement') {
nickjillings@1404 369 this.popupTitle.textContent = null;
nickjillings@1404 370 var statement = document.createElement('span');
nickjillings@1404 371 statement.textContent = node.statement;
nickjillings@1404 372 this.popupResponse.appendChild(statement);
nickjillings@1404 373 } else if (node.type == 'question') {
nickjillings@1404 374 this.popupTitle.textContent = node.question;
nickjillings@1404 375 var textArea = document.createElement('textarea');
nickjillings@1404 376 switch (node.boxsize) {
nickjillings@1404 377 case 'small':
nickjillings@1404 378 textArea.cols = "20";
nickjillings@1404 379 textArea.rows = "1";
nickjillings@1404 380 break;
nickjillings@1404 381 case 'normal':
nickjillings@1404 382 textArea.cols = "30";
nickjillings@1404 383 textArea.rows = "2";
nickjillings@1404 384 break;
nickjillings@1404 385 case 'large':
nickjillings@1404 386 textArea.cols = "40";
nickjillings@1404 387 textArea.rows = "5";
nickjillings@1404 388 break;
nickjillings@1404 389 case 'huge':
nickjillings@1404 390 textArea.cols = "50";
nickjillings@1404 391 textArea.rows = "10";
nickjillings@1404 392 break;
nickjillings@1404 393 }
nickjillings@1404 394 this.popupResponse.appendChild(textArea);
nickjillings@1404 395 textArea.focus();
nickjillings@1404 396 } else if (node.type == 'checkbox') {
nickjillings@1404 397 this.popupTitle.textContent = node.statement;
nickjillings@1404 398 var optHold = this.popupResponse;
nickjillings@1404 399 for (var i=0; i<node.options.length; i++) {
nickjillings@1404 400 var option = node.options[i];
nickjillings@1404 401 var input = document.createElement('input');
nickjillings@1404 402 input.id = option.id;
nickjillings@1404 403 input.type = 'checkbox';
nickjillings@1404 404 var span = document.createElement('span');
nickjillings@1404 405 span.textContent = option.text;
nickjillings@1404 406 var hold = document.createElement('div');
nickjillings@1404 407 hold.setAttribute('name','option');
nickjillings@1404 408 hold.style.padding = '4px';
nickjillings@1404 409 hold.appendChild(input);
nickjillings@1404 410 hold.appendChild(span);
nickjillings@1404 411 optHold.appendChild(hold);
nickjillings@1404 412 }
nickjillings@1404 413 } else if (node.type == 'radio') {
nickjillings@1404 414 this.popupTitle.textContent = node.statement;
nickjillings@1404 415 var optHold = this.popupResponse;
nickjillings@1404 416 for (var i=0; i<node.options.length; i++) {
nickjillings@1404 417 var option = node.options[i];
nickjillings@1404 418 var input = document.createElement('input');
nickjillings@1404 419 input.id = option.name;
nickjillings@1404 420 input.type = 'radio';
nickjillings@1404 421 input.name = node.id;
nickjillings@1404 422 var span = document.createElement('span');
nickjillings@1404 423 span.textContent = option.text;
nickjillings@1404 424 var hold = document.createElement('div');
nickjillings@1404 425 hold.setAttribute('name','option');
nickjillings@1404 426 hold.style.padding = '4px';
nickjillings@1404 427 hold.appendChild(input);
nickjillings@1404 428 hold.appendChild(span);
nickjillings@1404 429 optHold.appendChild(hold);
nickjillings@1404 430 }
nickjillings@1404 431 } else if (node.type == 'number') {
nickjillings@1404 432 this.popupTitle.textContent = node.statement;
nickjillings@1404 433 var input = document.createElement('input');
nickjillings@1404 434 input.type = 'textarea';
nickjillings@1404 435 if (node.min != null) {input.min = node.min;}
nickjillings@1404 436 if (node.max != null) {input.max = node.max;}
nickjillings@1404 437 if (node.step != null) {input.step = node.step;}
nickjillings@1404 438 this.popupResponse.appendChild(input);
nickjillings@1404 439 }
nickjillings@1404 440 var content_height = Number(this.popup.offsetHeight.toFixed());
nickjillings@1404 441 content_height -= Number(this.popupContent.offsetHeight.toFixed());
nickjillings@1404 442 content_height -=Number(this.buttonProceed.offsetHeight.toFixed());
nickjillings@1404 443 content_height = content_height + "px";
nickjillings@1404 444 this.buttonProceed.style.top = content_height;
nickjillings@1404 445 this.buttonPrevious.style.top = content_height;
nickjillings@1404 446 if(this.currentIndex+1 == this.popupOptions.length) {
nickjillings@1404 447 if (this.responses.nodeName == "PRETEST") {
nickjillings@1404 448 this.buttonProceed.textContent = 'Start';
nickjillings@1404 449 } else {
nickjillings@1404 450 this.buttonProceed.textContent = 'Submit';
nickjillings@1404 451 }
nickjillings@1404 452 } else {
nickjillings@1404 453 this.buttonProceed.textContent = 'Next';
nickjillings@1404 454 }
nickjillings@1404 455 if(this.currentIndex > 0)
nickjillings@1404 456 this.buttonPrevious.style.visibility = 'visible';
nickjillings@1404 457 else
nickjillings@1404 458 this.buttonPrevious.style.visibility = 'hidden';
nickjillings@1404 459 };
nickjillings@1404 460
nickjillings@1404 461 this.initState = function(node) {
nickjillings@1404 462 //Call this with your preTest and postTest nodes when needed to
nickjillings@1404 463 // initialise the popup procedure.
nickjillings@1404 464 this.popupOptions = node.options;
nickjillings@1404 465 if (this.popupOptions.length > 0) {
nickjillings@1404 466 if (node.type == 'pretest') {
nickjillings@1404 467 this.responses = document.createElement('PreTest');
nickjillings@1404 468 } else if (node.type == 'posttest') {
nickjillings@1404 469 this.responses = document.createElement('PostTest');
nickjillings@1404 470 } else {
nickjillings@1404 471 console.log ('WARNING - popup node neither pre or post!');
nickjillings@1404 472 this.responses = document.createElement('responses');
nickjillings@1404 473 }
nickjillings@1404 474 this.currentIndex = 0;
nickjillings@1404 475 this.showPopup();
nickjillings@1404 476 this.postNode();
nickjillings@1404 477 } else {
nickjillings@1404 478 advanceState();
nickjillings@1404 479 }
nickjillings@1404 480 };
nickjillings@1404 481
nickjillings@1404 482 this.proceedClicked = function() {
nickjillings@1404 483 // Each time the popup button is clicked!
nickjillings@1404 484 var node = this.popupOptions[this.currentIndex];
nickjillings@1404 485 if (node.type == 'question') {
nickjillings@1404 486 // Must extract the question data
nickjillings@1404 487 var textArea = $(popup.popupContent).find('textarea')[0];
nickjillings@1404 488 if (node.mandatory == true && textArea.value.length == 0) {
nickjillings@1404 489 alert('This question is mandatory');
nickjillings@1404 490 return;
nickjillings@1404 491 } else {
nickjillings@1404 492 // Save the text content
nickjillings@1404 493 var hold = document.createElement('comment');
nickjillings@1404 494 hold.id = node.id;
nickjillings@1404 495 hold.innerHTML = textArea.value;
nickjillings@1404 496 console.log("Question: "+ node.question);
nickjillings@1404 497 console.log("Question Response: "+ textArea.value);
nickjillings@1404 498 this.responses.appendChild(hold);
nickjillings@1404 499 }
nickjillings@1404 500 } else if (node.type == 'checkbox') {
nickjillings@1404 501 // Must extract checkbox data
nickjillings@1404 502 var optHold = this.popupResponse;
nickjillings@1404 503 var hold = document.createElement('checkbox');
nickjillings@1404 504 console.log("Checkbox: "+ node.statement);
nickjillings@1404 505 hold.id = node.id;
nickjillings@1404 506 for (var i=0; i<optHold.childElementCount; i++) {
nickjillings@1404 507 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nickjillings@1404 508 var statement = optHold.childNodes[i].getElementsByTagName('span')[0];
nickjillings@1404 509 var response = document.createElement('option');
nickjillings@1404 510 response.setAttribute('name',input.id);
nickjillings@1404 511 response.textContent = input.checked;
nickjillings@1404 512 hold.appendChild(response);
nickjillings@1404 513 console.log(input.id +': '+ input.checked);
nickjillings@1404 514 }
nickjillings@1404 515 this.responses.appendChild(hold);
nickjillings@1404 516 } else if (node.type == "radio") {
nickjillings@1404 517 var optHold = this.popupResponse;
nickjillings@1404 518 var hold = document.createElement('radio');
nickjillings@1440 519 console.log("Checkbox: "+ node.statement);
nickjillings@1404 520 var responseID = null;
nickjillings@1404 521 var i=0;
nickjillings@1404 522 while(responseID == null) {
nickjillings@1404 523 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nickjillings@1404 524 if (input.checked == true) {
nickjillings@1404 525 responseID = i;
nickjillings@1440 526 console.log("Selected: "+ node.options[i].name);
nickjillings@1404 527 }
nickjillings@1404 528 i++;
nickjillings@1404 529 }
nickjillings@1404 530 hold.id = node.id;
nickjillings@1404 531 hold.setAttribute('name',node.options[responseID].name);
nickjillings@1404 532 hold.textContent = node.options[responseID].text;
nickjillings@1404 533 this.responses.appendChild(hold);
nickjillings@1404 534 } else if (node.type == "number") {
nickjillings@1404 535 var input = this.popupContent.getElementsByTagName('input')[0];
nickjillings@1404 536 if (node.mandatory == true && input.value.length == 0) {
nickjillings@1404 537 alert('This question is mandatory. Please enter a number');
nickjillings@1404 538 return;
nickjillings@1404 539 }
nickjillings@1404 540 var enteredNumber = Number(input.value);
nickjillings@1404 541 if (isNaN(enteredNumber)) {
nickjillings@1404 542 alert('Please enter a valid number');
nickjillings@1404 543 return;
nickjillings@1404 544 }
nickjillings@1404 545 if (enteredNumber < node.min && node.min != null) {
nickjillings@1404 546 alert('Number is below the minimum value of '+node.min);
nickjillings@1404 547 return;
nickjillings@1404 548 }
nickjillings@1404 549 if (enteredNumber > node.max && node.max != null) {
nickjillings@1404 550 alert('Number is above the maximum value of '+node.max);
nickjillings@1404 551 return;
nickjillings@1404 552 }
nickjillings@1404 553 var hold = document.createElement('number');
nickjillings@1404 554 hold.id = node.id;
nickjillings@1404 555 hold.textContent = input.value;
nickjillings@1404 556 this.responses.appendChild(hold);
nickjillings@1404 557 }
nickjillings@1404 558 this.currentIndex++;
nickjillings@1404 559 if (this.currentIndex < this.popupOptions.length) {
nickjillings@1404 560 this.postNode();
nickjillings@1404 561 } else {
nickjillings@1404 562 // Reached the end of the popupOptions
nickjillings@1404 563 this.hidePopup();
nickjillings@1404 564 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
nickjillings@1404 565 testState.stateResults[testState.stateIndex] = this.responses;
nickjillings@1404 566 } else {
nickjillings@1404 567 testState.stateResults[testState.stateIndex].appendChild(this.responses);
nickjillings@1404 568 }
nickjillings@1404 569 advanceState();
nickjillings@1404 570 }
nickjillings@1404 571 };
nickjillings@1404 572
nickjillings@1404 573 this.previousClick = function() {
nickjillings@1404 574 // Triggered when the 'Back' button is clicked in the survey
nickjillings@1404 575 if (this.currentIndex > 0) {
nickjillings@1404 576 this.currentIndex--;
nickjillings@1404 577 var node = this.popupOptions[this.currentIndex];
nickjillings@1404 578 if (node.type != 'statement') {
nickjillings@1404 579 var prevResp = this.responses.childNodes[this.responses.childElementCount-1];
nickjillings@1404 580 this.responses.removeChild(prevResp);
nickjillings@1404 581 }
nickjillings@1404 582 this.postNode();
nickjillings@1404 583 if (node.type == 'question') {
nickjillings@1404 584 this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent;
nickjillings@1404 585 } else if (node.type == 'checkbox') {
nickjillings@1404 586 var options = this.popupContent.getElementsByTagName('input');
nickjillings@1404 587 var savedOptions = prevResp.getElementsByTagName('option');
nickjillings@1404 588 for (var i=0; i<options.length; i++) {
nickjillings@1404 589 var id = options[i].id;
nickjillings@1404 590 for (var j=0; j<savedOptions.length; j++) {
nickjillings@1404 591 if (savedOptions[j].getAttribute('name') == id) {
nickjillings@1404 592 if (savedOptions[j].textContent == 'true') {options[i].checked = true;}
nickjillings@1404 593 else {options[i].checked = false;}
nickjillings@1404 594 break;
nickjillings@1404 595 }
nickjillings@1404 596 }
nickjillings@1404 597 }
nickjillings@1404 598 } else if (node.type == 'number') {
nickjillings@1404 599 this.popupContent.getElementsByTagName('input')[0].value = prevResp.textContent;
nickjillings@1404 600 } else if (node.type == 'radio') {
nickjillings@1404 601 var options = this.popupContent.getElementsByTagName('input');
nickjillings@1404 602 var name = prevResp.getAttribute('name');
nickjillings@1404 603 for (var i=0; i<options.length; i++) {
nickjillings@1404 604 if (options[i].id == name) {
nickjillings@1404 605 options[i].checked = true;
nickjillings@1404 606 break;
nickjillings@1404 607 }
nickjillings@1404 608 }
nickjillings@1404 609 }
nickjillings@1404 610 }
nickjillings@1404 611 };
nickjillings@1421 612
nickjillings@1421 613 this.resize = function(event)
nickjillings@1421 614 {
nickjillings@1421 615 // Called on window resize;
nickjillings@1421 616 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nickjillings@1421 617 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nickjillings@1421 618 var blank = document.getElementsByClassName('testHalt')[0];
nickjillings@1421 619 blank.style.width = window.innerWidth;
nickjillings@1421 620 blank.style.height = window.innerHeight;
nickjillings@1421 621 };
nickjillings@1404 622 }
nickjillings@1404 623
nickjillings@1404 624 function advanceState()
nickjillings@1404 625 {
nickjillings@1404 626 // Just for complete clarity
nickjillings@1404 627 testState.advanceState();
nickjillings@1404 628 }
nickjillings@1404 629
nickjillings@1404 630 function stateMachine()
nickjillings@1404 631 {
nickjillings@1404 632 // Object prototype for tracking and managing the test state
nickjillings@1404 633 this.stateMap = [];
nickjillings@1404 634 this.stateIndex = null;
nickjillings@1404 635 this.currentStateMap = [];
nickjillings@1404 636 this.currentIndex = null;
nickjillings@1404 637 this.currentTestId = 0;
nickjillings@1404 638 this.stateResults = [];
nickjillings@1404 639 this.timerCallBackHolders = null;
nickjillings@1404 640 this.initialise = function(){
nickjillings@1404 641 if (this.stateMap.length > 0) {
nickjillings@1404 642 if(this.stateIndex != null) {
nickjillings@1404 643 console.log('NOTE - State already initialise');
nickjillings@1404 644 }
nickjillings@1404 645 this.stateIndex = -1;
nickjillings@1404 646 var that = this;
nickjillings@1404 647 var aH_pId = 0;
nickjillings@1404 648 for (var id=0; id<this.stateMap.length; id++){
nickjillings@1404 649 var name = this.stateMap[id].type;
nickjillings@1404 650 var obj = document.createElement(name);
nickjillings@1404 651 if (name == 'audioHolder') {
nickjillings@1404 652 obj.id = this.stateMap[id].id;
nickjillings@1404 653 obj.setAttribute('presentedid',aH_pId);
nickjillings@1404 654 aH_pId+=1;
nickjillings@1404 655 }
nickjillings@1404 656 this.stateResults.push(obj);
nickjillings@1404 657 }
nickjillings@1404 658 } else {
nickjillings@1404 659 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
nickjillings@1404 660 }
nickjillings@1404 661 };
nickjillings@1404 662 this.advanceState = function(){
nickjillings@1404 663 if (this.stateIndex == null) {
nickjillings@1404 664 this.initialise();
nickjillings@1404 665 }
nickjillings@1404 666 if (this.stateIndex == -1) {
nickjillings@1404 667 console.log('Starting test...');
nickjillings@1404 668 }
nickjillings@1404 669 if (this.currentIndex == null){
nickjillings@1404 670 if (this.currentStateMap.type == "audioHolder") {
nickjillings@1404 671 // Save current page
nickjillings@1404 672 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
nickjillings@1404 673 this.currentTestId++;
nickjillings@1404 674 }
nickjillings@1404 675 this.stateIndex++;
nickjillings@1404 676 if (this.stateIndex >= this.stateMap.length) {
nickjillings@1404 677 console.log('Test Completed');
nickjillings@1404 678 createProjectSave(specification.projectReturn);
nickjillings@1404 679 } else {
nickjillings@1404 680 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1404 681 if (this.currentStateMap.type == "audioHolder") {
nickjillings@1404 682 console.log('Loading test page');
nickjillings@1407 683 interfaceContext.newPage(this.currentStateMap);
nickjillings@1404 684 this.initialiseInnerState(this.currentStateMap);
nickjillings@1404 685 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") {
nickjillings@1404 686 if (this.currentStateMap.options.length >= 1) {
nickjillings@1404 687 popup.initState(this.currentStateMap);
nickjillings@1404 688 } else {
nickjillings@1404 689 this.advanceState();
nickjillings@1404 690 }
nickjillings@1404 691 } else {
nickjillings@1404 692 this.advanceState();
nickjillings@1404 693 }
nickjillings@1404 694 }
nickjillings@1404 695 } else {
nickjillings@1404 696 this.advanceInnerState();
nickjillings@1404 697 }
nickjillings@1404 698 };
nickjillings@1404 699
nickjillings@1404 700 this.testPageCompleted = function(store, testXML, testId) {
nickjillings@1404 701 // Function called each time a test page has been completed
nickjillings@1412 702 var metric = document.createElement('metric');
nickjillings@1412 703 if (audioEngineContext.metric.enableTestTimer)
nickjillings@1412 704 {
nickjillings@1412 705 var testTime = document.createElement('metricResult');
nickjillings@1412 706 testTime.id = 'testTime';
nickjillings@1412 707 testTime.textContent = audioEngineContext.timer.testDuration;
nickjillings@1412 708 metric.appendChild(testTime);
nickjillings@1412 709 }
nickjillings@1413 710 store.appendChild(metric);
nickjillings@1412 711 var audioObjects = audioEngineContext.audioObjects;
nickjillings@1412 712 for (var i=0; i<audioObjects.length; i++)
nickjillings@1412 713 {
nickjillings@1412 714 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM();
nickjillings@1412 715 audioElement.setAttribute('presentedId',i);
nickjillings@1413 716 store.appendChild(audioElement);
nickjillings@1412 717 }
nickjillings@1412 718 $(interfaceContext.commentQuestions).each(function(index,element){
nickjillings@1412 719 var node = element.exportXMLDOM();
nickjillings@1413 720 store.appendChild(node);
nickjillings@1412 721 });
nickjillings@1404 722 pageXMLSave(store, testXML);
nickjillings@1404 723 };
nickjillings@1404 724
nickjillings@1404 725 this.initialiseInnerState = function(node) {
nickjillings@1404 726 // Parses the received testXML for pre and post test options
nickjillings@1404 727 this.currentStateMap = [];
nickjillings@1404 728 var preTest = node.preTest;
nickjillings@1404 729 var postTest = node.postTest;
nickjillings@1404 730 if (preTest == undefined) {preTest = document.createElement("preTest");}
nickjillings@1404 731 if (postTest == undefined){postTest= document.createElement("postTest");}
nickjillings@1404 732 this.currentStateMap.push(preTest);
nickjillings@1404 733 this.currentStateMap.push(node);
nickjillings@1404 734 this.currentStateMap.push(postTest);
nickjillings@1404 735 this.currentIndex = -1;
nickjillings@1404 736 this.advanceInnerState();
nickjillings@1404 737 };
nickjillings@1404 738
nickjillings@1404 739 this.advanceInnerState = function() {
nickjillings@1404 740 this.currentIndex++;
nickjillings@1404 741 if (this.currentIndex >= this.currentStateMap.length) {
nickjillings@1404 742 this.currentIndex = null;
nickjillings@1404 743 this.currentStateMap = this.stateMap[this.stateIndex];
nickjillings@1404 744 this.advanceState();
nickjillings@1404 745 } else {
nickjillings@1404 746 if (this.currentStateMap[this.currentIndex].type == "audioHolder") {
nickjillings@1404 747 console.log("Loading test page"+this.currentTestId);
nickjillings@1404 748 } else if (this.currentStateMap[this.currentIndex].type == "pretest") {
nickjillings@1404 749 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1404 750 } else if (this.currentStateMap[this.currentIndex].type == "posttest") {
nickjillings@1404 751 popup.initState(this.currentStateMap[this.currentIndex]);
nickjillings@1404 752 } else {
nickjillings@1404 753 this.advanceInnerState();
nickjillings@1404 754 }
nickjillings@1404 755 }
nickjillings@1404 756 };
nickjillings@1404 757
nickjillings@1404 758 this.previousState = function(){};
nickjillings@1404 759 }
nickjillings@1404 760
nickjillings@1408 761 function AudioEngine(specification) {
nickjillings@1404 762
nickjillings@1404 763 // Create two output paths, the main outputGain and fooGain.
nickjillings@1404 764 // Output gain is default to 1 and any items for playback route here
nickjillings@1404 765 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nickjillings@1404 766 // because web audio will optimise and any route which does not go to the destination gets ignored.
nickjillings@1404 767 this.outputGain = audioContext.createGain();
nickjillings@1404 768 this.fooGain = audioContext.createGain();
nickjillings@1404 769 this.fooGain.gain = 0;
nickjillings@1404 770
nickjillings@1404 771 // Use this to detect playback state: 0 - stopped, 1 - playing
nickjillings@1404 772 this.status = 0;
nickjillings@1404 773
nickjillings@1404 774 // Connect both gains to output
nickjillings@1404 775 this.outputGain.connect(audioContext.destination);
nickjillings@1404 776 this.fooGain.connect(audioContext.destination);
nickjillings@1404 777
nickjillings@1404 778 // Create the timer Object
nickjillings@1404 779 this.timer = new timer();
nickjillings@1404 780 // Create session metrics
nickjillings@1408 781 this.metric = new sessionMetrics(this,specification);
nickjillings@1404 782
nickjillings@1404 783 this.loopPlayback = false;
nickjillings@1404 784
nickjillings@1404 785 // Create store for new audioObjects
nickjillings@1404 786 this.audioObjects = [];
nickjillings@1404 787
nickjillings@1410 788 this.buffers = [];
nickjillings@1430 789 this.bufferObj = function()
nickjillings@1410 790 {
nickjillings@1430 791 this.url = null;
nickjillings@1410 792 this.buffer = null;
nickjillings@1410 793 this.xmlRequest = new XMLHttpRequest();
nickjillings@1396 794 this.xmlRequest.parent = this;
nickjillings@1410 795 this.users = [];
nickjillings@1430 796 this.getMedia = function(url) {
nickjillings@1430 797 this.url = url;
nickjillings@1430 798 this.xmlRequest.open('GET',this.url,true);
nickjillings@1430 799 this.xmlRequest.responseType = 'arraybuffer';
nickjillings@1430 800
nickjillings@1430 801 var bufferObj = this;
nickjillings@1430 802
nickjillings@1430 803 // Create callback to decode the data asynchronously
nickjillings@1430 804 this.xmlRequest.onloadend = function() {
nickjillings@1430 805 audioContext.decodeAudioData(bufferObj.xmlRequest.response, function(decodedData) {
nickjillings@1430 806 bufferObj.buffer = decodedData;
nickjillings@1430 807 for (var i=0; i<bufferObj.users.length; i++)
nickjillings@1410 808 {
nickjillings@1430 809 bufferObj.users[i].state = 1;
nickjillings@1430 810 if (bufferObj.users[i].interfaceDOM != null)
nickjillings@1430 811 {
nickjillings@1434 812 bufferObj.users[i].bufferLoaded(bufferObj);
nickjillings@1430 813 }
nickjillings@1410 814 }
nickjillings@1430 815 calculateLoudness(bufferObj.buffer,"I");
nickjillings@1430 816 }, function(){
nickjillings@1430 817 // Should only be called if there was an error, but sometimes gets called continuously
nickjillings@1430 818 // Check here if the error is genuine
nickjillings@1430 819 if (bufferObj.buffer == undefined) {
nickjillings@1430 820 // Genuine error
nickjillings@1430 821 console.log('FATAL - Error loading buffer on '+audioObj.id);
nickjillings@1430 822 if (request.status == 404)
nickjillings@1430 823 {
nickjillings@1430 824 console.log('FATAL - Fragment '+audioObj.id+' 404 error');
nickjillings@1430 825 console.log('URL: '+audioObj.url);
nickjillings@1430 826 errorSessionDump('Fragment '+audioObj.id+' 404 error');
nickjillings@1430 827 }
nickjillings@1410 828 }
nickjillings@1430 829 });
nickjillings@1430 830 };
nickjillings@1433 831 this.progress = 0;
nickjillings@1433 832 this.progressCallback = function(event){
nickjillings@1433 833 if (event.lengthComputable)
nickjillings@1433 834 {
nickjillings@1396 835 this.parent.progress = event.loaded / event.total;
nickjillings@1396 836 for (var i=0; i<this.parent.users.length; i++)
nickjillings@1396 837 {
nickjillings@1396 838 if(this.parent.users[i].interfaceDOM != null)
nickjillings@1396 839 {
nickjillings@1396 840 if (typeof this.parent.users[i].interfaceDOM.updateLoading === "function")
nickjillings@1396 841 {
nickjillings@1396 842 this.parent.users[i].interfaceDOM.updateLoading(this.parent.progress*100);
nickjillings@1396 843 }
nickjillings@1396 844 }
nickjillings@1396 845 }
nickjillings@1433 846 }
nickjillings@1433 847 };
nickjillings@1433 848 this.xmlRequest.addEventListener("progress", this.progressCallback);
nickjillings@1430 849 this.xmlRequest.send();
nickjillings@1410 850 };
nickjillings@1410 851 };
nickjillings@1410 852
nickjillings@1404 853 this.play = function(id) {
nickjillings@1404 854 // Start the timer and set the audioEngine state to playing (1)
nickjillings@1404 855 if (this.status == 0 && this.loopPlayback) {
nickjillings@1404 856 // Check if all audioObjects are ready
nickjillings@1404 857 if(this.checkAllReady())
nickjillings@1404 858 {
nickjillings@1404 859 this.status = 1;
nickjillings@1404 860 this.setSynchronousLoop();
nickjillings@1404 861 }
nickjillings@1404 862 }
nickjillings@1404 863 else
nickjillings@1404 864 {
nickjillings@1404 865 this.status = 1;
nickjillings@1404 866 }
nickjillings@1404 867 if (this.status== 1) {
nickjillings@1404 868 this.timer.startTest();
nickjillings@1404 869 if (id == undefined) {
nickjillings@1404 870 id = -1;
nickjillings@1404 871 console.log('FATAL - Passed id was undefined - AudioEngineContext.play(id)');
nickjillings@1404 872 return;
nickjillings@1404 873 } else {
nickjillings@1404 874 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
nickjillings@1404 875 }
nickjillings@1404 876 if (this.loopPlayback) {
nickjillings@1404 877 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1404 878 {
nickjillings@1404 879 this.audioObjects[i].play(this.timer.getTestTime()+1);
nickjillings@1404 880 if (id == i) {
nickjillings@1404 881 this.audioObjects[i].loopStart();
nickjillings@1404 882 } else {
nickjillings@1404 883 this.audioObjects[i].loopStop();
nickjillings@1404 884 }
nickjillings@1404 885 }
nickjillings@1404 886 } else {
nickjillings@1404 887 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1404 888 {
nickjillings@1404 889 if (i != id) {
nickjillings@1404 890 this.audioObjects[i].outputGain.gain.value = 0.0;
nickjillings@1404 891 this.audioObjects[i].stop();
nickjillings@1404 892 } else if (i == id) {
nickjillings@1430 893 this.audioObjects[id].outputGain.gain.value = this.audioObjects[id].specification.gain*this.audioObjects[id].buffer.buffer.gain;
nickjillings@1404 894 this.audioObjects[id].play(audioContext.currentTime+0.01);
nickjillings@1404 895 }
nickjillings@1404 896 }
nickjillings@1404 897 }
nickjillings@1404 898 interfaceContext.playhead.start();
nickjillings@1404 899 }
nickjillings@1404 900 };
nickjillings@1404 901
nickjillings@1404 902 this.stop = function() {
nickjillings@1404 903 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nickjillings@1404 904 if (this.status == 1) {
nickjillings@1404 905 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1404 906 {
nickjillings@1404 907 this.audioObjects[i].stop();
nickjillings@1404 908 }
nickjillings@1404 909 interfaceContext.playhead.stop();
nickjillings@1404 910 this.status = 0;
nickjillings@1404 911 }
nickjillings@1404 912 };
nickjillings@1404 913
nickjillings@1404 914 this.newTrack = function(element) {
nickjillings@1404 915 // Pull data from given URL into new audio buffer
nickjillings@1404 916 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nickjillings@1404 917
nickjillings@1404 918 // Create the audioObject with ID of the new track length;
nickjillings@1404 919 audioObjectId = this.audioObjects.length;
nickjillings@1404 920 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nickjillings@1404 921
nickjillings@1410 922 // Check if audioObject buffer is currently stored by full URL
nickjillings@1410 923 var URL = element.parent.hostURL + element.url;
nickjillings@1410 924 var buffer = null;
nickjillings@1410 925 for (var i=0; i<this.buffers.length; i++)
nickjillings@1410 926 {
nickjillings@1410 927 if (URL == this.buffers[i].url)
nickjillings@1410 928 {
nickjillings@1410 929 buffer = this.buffers[i];
nickjillings@1410 930 break;
nickjillings@1410 931 }
nickjillings@1410 932 }
nickjillings@1410 933 if (buffer == null)
nickjillings@1410 934 {
nickjillings@1426 935 console.log("[WARN]: Buffer was not loaded in pre-test! "+URL);
nickjillings@1430 936 buffer = new this.bufferObj();
nickjillings@1430 937 buffer.getMedia(URL);
nickjillings@1410 938 this.buffers.push(buffer);
nickjillings@1410 939 }
nickjillings@1404 940 this.audioObjects[audioObjectId].specification = element;
nickjillings@1426 941 this.audioObjects[audioObjectId].url = URL;
nickjillings@1434 942 buffer.users.push(this.audioObjects[audioObjectId]);
nickjillings@1410 943 if (buffer.buffer != null)
nickjillings@1410 944 {
nickjillings@1434 945 this.audioObjects[audioObjectId].bufferLoaded(buffer);
nickjillings@1410 946 }
nickjillings@1404 947 return this.audioObjects[audioObjectId];
nickjillings@1404 948 };
nickjillings@1404 949
nickjillings@1404 950 this.newTestPage = function() {
nickjillings@1404 951 this.state = 0;
nickjillings@1404 952 this.audioObjectsReady = false;
nickjillings@1404 953 this.metric.reset();
nickjillings@1410 954 for (var i=0; i < this.buffers.length; i++)
nickjillings@1410 955 {
nickjillings@1410 956 this.buffers[i].users = [];
nickjillings@1410 957 }
nickjillings@1404 958 this.audioObjects = [];
nickjillings@1404 959 };
nickjillings@1404 960
nickjillings@1404 961 this.checkAllPlayed = function() {
nickjillings@1404 962 arr = [];
nickjillings@1404 963 for (var id=0; id<this.audioObjects.length; id++) {
nickjillings@1404 964 if (this.audioObjects[id].metric.wasListenedTo == false) {
nickjillings@1404 965 arr.push(this.audioObjects[id].id);
nickjillings@1404 966 }
nickjillings@1404 967 }
nickjillings@1404 968 return arr;
nickjillings@1404 969 };
nickjillings@1404 970
nickjillings@1404 971 this.checkAllReady = function() {
nickjillings@1404 972 var ready = true;
nickjillings@1404 973 for (var i=0; i<this.audioObjects.length; i++) {
nickjillings@1404 974 if (this.audioObjects[i].state == 0) {
nickjillings@1404 975 // Track not ready
nickjillings@1404 976 console.log('WAIT -- audioObject '+i+' not ready yet!');
nickjillings@1404 977 ready = false;
nickjillings@1404 978 };
nickjillings@1404 979 }
nickjillings@1404 980 return ready;
nickjillings@1404 981 };
nickjillings@1404 982
nickjillings@1404 983 this.setSynchronousLoop = function() {
nickjillings@1404 984 // Pads the signals so they are all exactly the same length
nickjillings@1404 985 var length = 0;
nickjillings@1404 986 var maxId;
nickjillings@1404 987 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1404 988 {
nickjillings@1413 989 if (length < this.audioObjects[i].buffer.buffer.length)
nickjillings@1404 990 {
nickjillings@1413 991 length = this.audioObjects[i].buffer.buffer.length;
nickjillings@1404 992 maxId = i;
nickjillings@1404 993 }
nickjillings@1404 994 }
nickjillings@1404 995 // Extract the audio and zero-pad
nickjillings@1430 996 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1404 997 {
nickjillings@1413 998 var orig = this.audioObjects[i].buffer.buffer;
nickjillings@1404 999 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate);
nickjillings@1404 1000 for (var c=0; c<orig.numberOfChannels; c++)
nickjillings@1404 1001 {
nickjillings@1404 1002 var inData = hold.getChannelData(c);
nickjillings@1404 1003 var outData = orig.getChannelData(c);
nickjillings@1404 1004 for (var n=0; n<orig.length; n++)
nickjillings@1404 1005 {inData[n] = outData[n];}
nickjillings@1404 1006 }
nickjillings@1430 1007 hold.gain = orig.gain;
nickjillings@1430 1008 hold.lufs = orig.lufs;
nickjillings@1413 1009 this.audioObjects[i].buffer.buffer = hold;
nickjillings@1404 1010 }
nickjillings@1404 1011 };
nickjillings@1404 1012
nickjillings@1404 1013 }
nickjillings@1404 1014
nickjillings@1404 1015 function audioObject(id) {
nickjillings@1404 1016 // The main buffer object with common control nodes to the AudioEngine
nickjillings@1404 1017
nickjillings@1404 1018 this.specification;
nickjillings@1404 1019 this.id = id;
nickjillings@1404 1020 this.state = 0; // 0 - no data, 1 - ready
nickjillings@1404 1021 this.url = null; // Hold the URL given for the output back to the results.
nickjillings@1404 1022 this.metric = new metricTracker(this);
nickjillings@1404 1023
nickjillings@1404 1024 // Bindings for GUI
nickjillings@1404 1025 this.interfaceDOM = null;
nickjillings@1404 1026 this.commentDOM = null;
nickjillings@1404 1027
nickjillings@1404 1028 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nickjillings@1404 1029 this.bufferNode = undefined;
nickjillings@1404 1030 this.outputGain = audioContext.createGain();
nickjillings@1404 1031
nickjillings@1404 1032 // Default output gain to be zero
nickjillings@1404 1033 this.outputGain.gain.value = 0.0;
nickjillings@1404 1034
nickjillings@1404 1035 // Connect buffer to the audio graph
nickjillings@1404 1036 this.outputGain.connect(audioEngineContext.outputGain);
nickjillings@1404 1037
nickjillings@1404 1038 // the audiobuffer is not designed for multi-start playback
nickjillings@1404 1039 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nickjillings@1404 1040 this.buffer;
nickjillings@1434 1041
nickjillings@1434 1042 this.bufferLoaded = function(callee)
nickjillings@1434 1043 {
nickjillings@1434 1044 // Called by the associated buffer when it has finished loading, will then 'bind' the buffer to the
nickjillings@1434 1045 // audioObject and trigger the interfaceDOM.enable() function for user feedback
nickjillings@1434 1046 if (audioEngineContext.loopPlayback){
nickjillings@1434 1047 // First copy the buffer into this.buffer
nickjillings@1434 1048 this.buffer = new audioEngineContext.bufferObj();
nickjillings@1434 1049 this.buffer.url = callee.url;
nickjillings@1434 1050 this.buffer.buffer = audioContext.createBuffer(callee.buffer.numberOfChannels, callee.buffer.length, callee.buffer.sampleRate);
nickjillings@1434 1051 for (var c=0; c<callee.buffer.numberOfChannels; c++)
nickjillings@1434 1052 {
nickjillings@1434 1053 var src = callee.buffer.getChannelData(c);
nickjillings@1434 1054 var dst = this.buffer.buffer.getChannelData(c);
nickjillings@1434 1055 for (var n=0; n<src.length; n++)
nickjillings@1434 1056 {
nickjillings@1434 1057 dst[n] = src[n];
nickjillings@1434 1058 }
nickjillings@1434 1059 }
nickjillings@1434 1060 } else {
nickjillings@1434 1061 this.buffer = callee;
nickjillings@1434 1062 }
nickjillings@1434 1063 this.state = 1;
nickjillings@1434 1064 this.buffer.buffer.gain = callee.buffer.gain;
nickjillings@1434 1065 this.buffer.buffer.lufs = callee.buffer.lufs;
nickjillings@1434 1066 var targetLUFS = this.specification.parent.loudness;
nickjillings@1434 1067 if (typeof targetLUFS === "number")
nickjillings@1434 1068 {
nickjillings@1434 1069 this.buffer.buffer.gain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs);
nickjillings@1434 1070 } else {
nickjillings@1434 1071 this.buffer.buffer.gain = 1.0;
nickjillings@1434 1072 }
nickjillings@1434 1073 if (this.interfaceDOM != null) {
nickjillings@1434 1074 this.interfaceDOM.enable();
nickjillings@1434 1075 }
nickjillings@1434 1076 };
nickjillings@1404 1077
nickjillings@1404 1078 this.loopStart = function() {
nickjillings@1430 1079 this.outputGain.gain.value = this.specification.gain*this.buffer.buffer.gain;
nickjillings@1404 1080 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1404 1081 };
nickjillings@1404 1082
nickjillings@1404 1083 this.loopStop = function() {
nickjillings@1404 1084 if (this.outputGain.gain.value != 0.0) {
nickjillings@1404 1085 this.outputGain.gain.value = 0.0;
nickjillings@1404 1086 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nickjillings@1404 1087 }
nickjillings@1404 1088 };
nickjillings@1404 1089
nickjillings@1404 1090 this.play = function(startTime) {
nickjillings@1410 1091 if (this.bufferNode == undefined && this.buffer.buffer != undefined) {
nickjillings@1404 1092 this.bufferNode = audioContext.createBufferSource();
nickjillings@1404 1093 this.bufferNode.owner = this;
nickjillings@1404 1094 this.bufferNode.connect(this.outputGain);
nickjillings@1410 1095 this.bufferNode.buffer = this.buffer.buffer;
nickjillings@1404 1096 this.bufferNode.loop = audioEngineContext.loopPlayback;
nickjillings@1404 1097 this.bufferNode.onended = function(event) {
nickjillings@1404 1098 // Safari does not like using 'this' to reference the calling object!
nickjillings@1404 1099 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
nickjillings@1404 1100 event.currentTarget.owner.stop();
nickjillings@1404 1101 };
nickjillings@1404 1102 if (this.bufferNode.loop == false) {
nickjillings@1404 1103 this.metric.startListening(audioEngineContext.timer.getTestTime());
nickjillings@1404 1104 }
nickjillings@1404 1105 this.bufferNode.start(startTime);
nickjillings@1404 1106 }
nickjillings@1404 1107 };
nickjillings@1404 1108
nickjillings@1404 1109 this.stop = function() {
nickjillings@1404 1110 if (this.bufferNode != undefined)
nickjillings@1404 1111 {
nickjillings@1404 1112 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
nickjillings@1404 1113 this.bufferNode.stop(0);
nickjillings@1404 1114 this.bufferNode = undefined;
nickjillings@1404 1115 }
nickjillings@1404 1116 };
nickjillings@1404 1117
nickjillings@1404 1118 this.getCurrentPosition = function() {
nickjillings@1404 1119 var time = audioEngineContext.timer.getTestTime();
nickjillings@1404 1120 if (this.bufferNode != undefined) {
nickjillings@1404 1121 if (this.bufferNode.loop == true) {
nickjillings@1404 1122 if (audioEngineContext.status == 1) {
nickjillings@1410 1123 return (time-this.metric.listenStart)%this.buffer.buffer.duration;
nickjillings@1404 1124 } else {
nickjillings@1404 1125 return 0;
nickjillings@1404 1126 }
nickjillings@1404 1127 } else {
nickjillings@1404 1128 if (this.metric.listenHold) {
nickjillings@1404 1129 return time - this.metric.listenStart;
nickjillings@1404 1130 } else {
nickjillings@1404 1131 return 0;
nickjillings@1404 1132 }
nickjillings@1404 1133 }
nickjillings@1404 1134 } else {
nickjillings@1404 1135 return 0;
nickjillings@1404 1136 }
nickjillings@1404 1137 };
nickjillings@1404 1138
nickjillings@1404 1139 this.exportXMLDOM = function() {
nickjillings@1404 1140 var root = document.createElement('audioElement');
nickjillings@1404 1141 root.id = this.specification.id;
nickjillings@1423 1142 root.setAttribute('url',this.specification.url);
nickjillings@1404 1143 var file = document.createElement('file');
nickjillings@1416 1144 file.setAttribute('sampleRate',this.buffer.buffer.sampleRate);
nickjillings@1416 1145 file.setAttribute('channels',this.buffer.buffer.numberOfChannels);
nickjillings@1416 1146 file.setAttribute('sampleCount',this.buffer.buffer.length);
nickjillings@1416 1147 file.setAttribute('duration',this.buffer.buffer.duration);
nickjillings@1404 1148 root.appendChild(file);
nickjillings@1404 1149 if (this.specification.type != 'outsidereference') {
nickjillings@1413 1150 var interfaceXML = this.interfaceDOM.exportXMLDOM(this);
nickjillings@1413 1151 if (interfaceXML.length == undefined) {
nickjillings@1393 1152 root.appendChild(interfaceXML);
nickjillings@1413 1153 } else {
nickjillings@1413 1154 for (var i=0; i<interfaceXML.length; i++)
nickjillings@1413 1155 {
nickjillings@1413 1156 root.appendChild(interfaceXML[i]);
nickjillings@1413 1157 }
nickjillings@1413 1158 }
nickjillings@1404 1159 root.appendChild(this.commentDOM.exportXMLDOM(this));
nickjillings@1404 1160 if(this.specification.type == 'anchor') {
nickjillings@1404 1161 root.setAttribute('anchor',true);
nickjillings@1404 1162 } else if(this.specification.type == 'reference') {
nickjillings@1404 1163 root.setAttribute('reference',true);
nickjillings@1404 1164 }
nickjillings@1404 1165 }
nickjillings@1404 1166 root.appendChild(this.metric.exportXMLDOM());
nickjillings@1404 1167 return root;
nickjillings@1404 1168 };
nickjillings@1404 1169 }
nickjillings@1404 1170
nickjillings@1404 1171 function timer()
nickjillings@1404 1172 {
nickjillings@1404 1173 /* Timer object used in audioEngine to keep track of session timings
nickjillings@1404 1174 * Uses the timer of the web audio API, so sample resolution
nickjillings@1404 1175 */
nickjillings@1404 1176 this.testStarted = false;
nickjillings@1404 1177 this.testStartTime = 0;
nickjillings@1404 1178 this.testDuration = 0;
nickjillings@1404 1179 this.minimumTestTime = 0; // No minimum test time
nickjillings@1404 1180 this.startTest = function()
nickjillings@1404 1181 {
nickjillings@1404 1182 if (this.testStarted == false)
nickjillings@1404 1183 {
nickjillings@1404 1184 this.testStartTime = audioContext.currentTime;
nickjillings@1404 1185 this.testStarted = true;
nickjillings@1404 1186 this.updateTestTime();
nickjillings@1404 1187 audioEngineContext.metric.initialiseTest();
nickjillings@1404 1188 }
nickjillings@1404 1189 };
nickjillings@1404 1190 this.stopTest = function()
nickjillings@1404 1191 {
nickjillings@1404 1192 if (this.testStarted)
nickjillings@1404 1193 {
nickjillings@1404 1194 this.testDuration = this.getTestTime();
nickjillings@1404 1195 this.testStarted = false;
nickjillings@1404 1196 } else {
nickjillings@1404 1197 console.log('ERR: Test tried to end before beginning');
nickjillings@1404 1198 }
nickjillings@1404 1199 };
nickjillings@1404 1200 this.updateTestTime = function()
nickjillings@1404 1201 {
nickjillings@1404 1202 if (this.testStarted)
nickjillings@1404 1203 {
nickjillings@1404 1204 this.testDuration = audioContext.currentTime - this.testStartTime;
nickjillings@1404 1205 }
nickjillings@1404 1206 };
nickjillings@1404 1207 this.getTestTime = function()
nickjillings@1404 1208 {
nickjillings@1404 1209 this.updateTestTime();
nickjillings@1404 1210 return this.testDuration;
nickjillings@1404 1211 };
nickjillings@1404 1212 }
nickjillings@1404 1213
nickjillings@1408 1214 function sessionMetrics(engine,specification)
nickjillings@1404 1215 {
nickjillings@1404 1216 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nickjillings@1404 1217 */
nickjillings@1404 1218 this.engine = engine;
nickjillings@1404 1219 this.lastClicked = -1;
nickjillings@1404 1220 this.data = -1;
nickjillings@1404 1221 this.reset = function() {
nickjillings@1404 1222 this.lastClicked = -1;
nickjillings@1404 1223 this.data = -1;
nickjillings@1404 1224 };
nickjillings@1408 1225
nickjillings@1408 1226 this.enableElementInitialPosition = false;
nickjillings@1408 1227 this.enableElementListenTracker = false;
nickjillings@1408 1228 this.enableElementTimer = false;
nickjillings@1408 1229 this.enableElementTracker = false;
nickjillings@1408 1230 this.enableFlagListenedTo = false;
nickjillings@1408 1231 this.enableFlagMoved = false;
nickjillings@1408 1232 this.enableTestTimer = false;
nickjillings@1408 1233 // Obtain the metrics enabled
nickjillings@1408 1234 for (var i=0; i<specification.metrics.length; i++)
nickjillings@1408 1235 {
nickjillings@1408 1236 var node = specification.metrics[i];
nickjillings@1408 1237 switch(node.enabled)
nickjillings@1408 1238 {
nickjillings@1408 1239 case 'testTimer':
nickjillings@1408 1240 this.enableTestTimer = true;
nickjillings@1408 1241 break;
nickjillings@1408 1242 case 'elementTimer':
nickjillings@1408 1243 this.enableElementTimer = true;
nickjillings@1408 1244 break;
nickjillings@1408 1245 case 'elementTracker':
nickjillings@1408 1246 this.enableElementTracker = true;
nickjillings@1408 1247 break;
nickjillings@1408 1248 case 'elementListenTracker':
nickjillings@1408 1249 this.enableElementListenTracker = true;
nickjillings@1408 1250 break;
nickjillings@1408 1251 case 'elementInitialPosition':
nickjillings@1408 1252 this.enableElementInitialPosition = true;
nickjillings@1408 1253 break;
nickjillings@1408 1254 case 'elementFlagListenedTo':
nickjillings@1408 1255 this.enableFlagListenedTo = true;
nickjillings@1408 1256 break;
nickjillings@1408 1257 case 'elementFlagMoved':
nickjillings@1408 1258 this.enableFlagMoved = true;
nickjillings@1408 1259 break;
nickjillings@1408 1260 case 'elementFlagComments':
nickjillings@1408 1261 this.enableFlagComments = true;
nickjillings@1408 1262 break;
nickjillings@1408 1263 }
nickjillings@1408 1264 }
nickjillings@1404 1265 this.initialiseTest = function(){};
nickjillings@1404 1266 }
nickjillings@1404 1267
nickjillings@1404 1268 function metricTracker(caller)
nickjillings@1404 1269 {
nickjillings@1404 1270 /* Custom object to track and collect metric data
nickjillings@1404 1271 * Used only inside the audioObjects object.
nickjillings@1404 1272 */
nickjillings@1404 1273
nickjillings@1404 1274 this.listenedTimer = 0;
nickjillings@1404 1275 this.listenStart = 0;
nickjillings@1404 1276 this.listenHold = false;
nickjillings@1404 1277 this.initialPosition = -1;
nickjillings@1404 1278 this.movementTracker = [];
nickjillings@1404 1279 this.listenTracker =[];
nickjillings@1404 1280 this.wasListenedTo = false;
nickjillings@1404 1281 this.wasMoved = false;
nickjillings@1404 1282 this.hasComments = false;
nickjillings@1404 1283 this.parent = caller;
nickjillings@1404 1284
nickjillings@1404 1285 this.initialised = function(position)
nickjillings@1404 1286 {
nickjillings@1404 1287 if (this.initialPosition == -1) {
nickjillings@1404 1288 this.initialPosition = position;
nickjillings@1404 1289 }
nickjillings@1404 1290 };
nickjillings@1404 1291
nickjillings@1404 1292 this.moved = function(time,position)
nickjillings@1404 1293 {
nickjillings@1404 1294 this.wasMoved = true;
nickjillings@1404 1295 this.movementTracker[this.movementTracker.length] = [time, position];
nickjillings@1404 1296 };
nickjillings@1404 1297
nickjillings@1404 1298 this.startListening = function(time)
nickjillings@1404 1299 {
nickjillings@1404 1300 if (this.listenHold == false)
nickjillings@1404 1301 {
nickjillings@1404 1302 this.wasListenedTo = true;
nickjillings@1404 1303 this.listenStart = time;
nickjillings@1404 1304 this.listenHold = true;
nickjillings@1404 1305
nickjillings@1404 1306 var evnt = document.createElement('event');
nickjillings@1404 1307 var testTime = document.createElement('testTime');
nickjillings@1404 1308 testTime.setAttribute('start',time);
nickjillings@1404 1309 var bufferTime = document.createElement('bufferTime');
nickjillings@1404 1310 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
nickjillings@1404 1311 evnt.appendChild(testTime);
nickjillings@1404 1312 evnt.appendChild(bufferTime);
nickjillings@1404 1313 this.listenTracker.push(evnt);
nickjillings@1404 1314
nickjillings@1404 1315 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1404 1316 }
nickjillings@1404 1317 };
nickjillings@1404 1318
nickjillings@1404 1319 this.stopListening = function(time,bufferStopTime)
nickjillings@1404 1320 {
nickjillings@1404 1321 if (this.listenHold == true)
nickjillings@1404 1322 {
nickjillings@1404 1323 var diff = time - this.listenStart;
nickjillings@1404 1324 this.listenedTimer += (diff);
nickjillings@1404 1325 this.listenStart = 0;
nickjillings@1404 1326 this.listenHold = false;
nickjillings@1404 1327
nickjillings@1404 1328 var evnt = this.listenTracker[this.listenTracker.length-1];
nickjillings@1404 1329 var testTime = evnt.getElementsByTagName('testTime')[0];
nickjillings@1404 1330 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
nickjillings@1404 1331 testTime.setAttribute('stop',time);
nickjillings@1404 1332 if (bufferStopTime == undefined) {
nickjillings@1404 1333 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
nickjillings@1404 1334 } else {
nickjillings@1404 1335 bufferTime.setAttribute('stop',bufferStopTime);
nickjillings@1404 1336 }
nickjillings@1404 1337 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1404 1338 }
nickjillings@1404 1339 };
nickjillings@1404 1340
nickjillings@1404 1341 this.exportXMLDOM = function() {
nickjillings@1404 1342 var root = document.createElement('metric');
nickjillings@1404 1343 if (audioEngineContext.metric.enableElementTimer) {
nickjillings@1404 1344 var mElementTimer = document.createElement('metricresult');
nickjillings@1404 1345 mElementTimer.setAttribute('name','enableElementTimer');
nickjillings@1404 1346 mElementTimer.textContent = this.listenedTimer;
nickjillings@1404 1347 root.appendChild(mElementTimer);
nickjillings@1404 1348 }
nickjillings@1404 1349 if (audioEngineContext.metric.enableElementTracker) {
nickjillings@1404 1350 var elementTrackerFull = document.createElement('metricResult');
nickjillings@1404 1351 elementTrackerFull.setAttribute('name','elementTrackerFull');
nickjillings@1404 1352 for (var k=0; k<this.movementTracker.length; k++)
nickjillings@1404 1353 {
nickjillings@1404 1354 var timePos = document.createElement('timePos');
nickjillings@1404 1355 timePos.id = k;
nickjillings@1404 1356 var time = document.createElement('time');
nickjillings@1404 1357 time.textContent = this.movementTracker[k][0];
nickjillings@1404 1358 var position = document.createElement('position');
nickjillings@1404 1359 position.textContent = this.movementTracker[k][1];
nickjillings@1404 1360 timePos.appendChild(time);
nickjillings@1404 1361 timePos.appendChild(position);
nickjillings@1404 1362 elementTrackerFull.appendChild(timePos);
nickjillings@1404 1363 }
nickjillings@1404 1364 root.appendChild(elementTrackerFull);
nickjillings@1404 1365 }
nickjillings@1404 1366 if (audioEngineContext.metric.enableElementListenTracker) {
nickjillings@1404 1367 var elementListenTracker = document.createElement('metricResult');
nickjillings@1404 1368 elementListenTracker.setAttribute('name','elementListenTracker');
nickjillings@1404 1369 for (var k=0; k<this.listenTracker.length; k++) {
nickjillings@1404 1370 elementListenTracker.appendChild(this.listenTracker[k]);
nickjillings@1404 1371 }
nickjillings@1404 1372 root.appendChild(elementListenTracker);
nickjillings@1404 1373 }
nickjillings@1404 1374 if (audioEngineContext.metric.enableElementInitialPosition) {
nickjillings@1404 1375 var elementInitial = document.createElement('metricResult');
nickjillings@1404 1376 elementInitial.setAttribute('name','elementInitialPosition');
nickjillings@1404 1377 elementInitial.textContent = this.initialPosition;
nickjillings@1404 1378 root.appendChild(elementInitial);
nickjillings@1404 1379 }
nickjillings@1404 1380 if (audioEngineContext.metric.enableFlagListenedTo) {
nickjillings@1404 1381 var flagListenedTo = document.createElement('metricResult');
nickjillings@1404 1382 flagListenedTo.setAttribute('name','elementFlagListenedTo');
nickjillings@1404 1383 flagListenedTo.textContent = this.wasListenedTo;
nickjillings@1404 1384 root.appendChild(flagListenedTo);
nickjillings@1404 1385 }
nickjillings@1404 1386 if (audioEngineContext.metric.enableFlagMoved) {
nickjillings@1404 1387 var flagMoved = document.createElement('metricResult');
nickjillings@1404 1388 flagMoved.setAttribute('name','elementFlagMoved');
nickjillings@1404 1389 flagMoved.textContent = this.wasMoved;
nickjillings@1404 1390 root.appendChild(flagMoved);
nickjillings@1404 1391 }
nickjillings@1404 1392 if (audioEngineContext.metric.enableFlagComments) {
nickjillings@1404 1393 var flagComments = document.createElement('metricResult');
nickjillings@1404 1394 flagComments.setAttribute('name','elementFlagComments');
nickjillings@1404 1395 if (this.parent.commentDOM == null)
nickjillings@1404 1396 {flag.textContent = 'false';}
nickjillings@1404 1397 else if (this.parent.commentDOM.textContent.length == 0)
nickjillings@1404 1398 {flag.textContent = 'false';}
nickjillings@1404 1399 else
nickjillings@1404 1400 {flag.textContet = 'true';}
nickjillings@1404 1401 root.appendChild(flagComments);
nickjillings@1404 1402 }
nickjillings@1404 1403
nickjillings@1404 1404 return root;
nickjillings@1404 1405 };
nickjillings@1404 1406 }
nickjillings@1404 1407
nickjillings@1404 1408 function randomiseOrder(input)
nickjillings@1404 1409 {
nickjillings@1404 1410 // This takes an array of information and randomises the order
nickjillings@1404 1411 var N = input.length;
nickjillings@1404 1412
nickjillings@1404 1413 var inputSequence = []; // For safety purposes: keep track of randomisation
nickjillings@1404 1414 for (var counter = 0; counter < N; ++counter)
nickjillings@1404 1415 inputSequence.push(counter) // Fill array
nickjillings@1404 1416 var inputSequenceClone = inputSequence.slice(0);
nickjillings@1404 1417
nickjillings@1404 1418 var holdArr = [];
nickjillings@1404 1419 var outputSequence = [];
nickjillings@1404 1420 for (var n=0; n<N; n++)
nickjillings@1404 1421 {
nickjillings@1404 1422 // First pick a random number
nickjillings@1404 1423 var r = Math.random();
nickjillings@1404 1424 // Multiply and floor by the number of elements left
nickjillings@1404 1425 r = Math.floor(r*input.length);
nickjillings@1404 1426 // Pick out that element and delete from the array
nickjillings@1404 1427 holdArr.push(input.splice(r,1)[0]);
nickjillings@1404 1428 // Do the same with sequence
nickjillings@1404 1429 outputSequence.push(inputSequence.splice(r,1)[0]);
nickjillings@1404 1430 }
nickjillings@1404 1431 console.log(inputSequenceClone.toString()); // print original array to console
nickjillings@1404 1432 console.log(outputSequence.toString()); // print randomised array to console
nickjillings@1404 1433 return holdArr;
nickjillings@1404 1434 }
nickjillings@1404 1435
nickjillings@1404 1436 function returnDateNode()
nickjillings@1404 1437 {
nickjillings@1404 1438 // Create an XML Node for the Date and Time a test was conducted
nickjillings@1404 1439 // Structure is
nickjillings@1404 1440 // <datetime>
nickjillings@1404 1441 // <date year="##" month="##" day="##">DD/MM/YY</date>
nickjillings@1404 1442 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nickjillings@1404 1443 // </datetime>
nickjillings@1404 1444 var dateTime = new Date();
nickjillings@1404 1445 var year = document.createAttribute('year');
nickjillings@1404 1446 var month = document.createAttribute('month');
nickjillings@1404 1447 var day = document.createAttribute('day');
nickjillings@1404 1448 var hour = document.createAttribute('hour');
nickjillings@1404 1449 var minute = document.createAttribute('minute');
nickjillings@1404 1450 var secs = document.createAttribute('secs');
nickjillings@1404 1451
nickjillings@1404 1452 year.nodeValue = dateTime.getFullYear();
nickjillings@1404 1453 month.nodeValue = dateTime.getMonth()+1;
nickjillings@1404 1454 day.nodeValue = dateTime.getDate();
nickjillings@1404 1455 hour.nodeValue = dateTime.getHours();
nickjillings@1404 1456 minute.nodeValue = dateTime.getMinutes();
nickjillings@1404 1457 secs.nodeValue = dateTime.getSeconds();
nickjillings@1404 1458
nickjillings@1404 1459 var hold = document.createElement("datetime");
nickjillings@1404 1460 var date = document.createElement("date");
nickjillings@1404 1461 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nickjillings@1404 1462 var time = document.createElement("time");
nickjillings@1404 1463 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nickjillings@1404 1464
nickjillings@1404 1465 date.setAttributeNode(year);
nickjillings@1404 1466 date.setAttributeNode(month);
nickjillings@1404 1467 date.setAttributeNode(day);
nickjillings@1404 1468 time.setAttributeNode(hour);
nickjillings@1404 1469 time.setAttributeNode(minute);
nickjillings@1404 1470 time.setAttributeNode(secs);
nickjillings@1404 1471
nickjillings@1404 1472 hold.appendChild(date);
nickjillings@1404 1473 hold.appendChild(time);
nickjillings@1408 1474 return hold;
nickjillings@1404 1475
nickjillings@1404 1476 }
nickjillings@1404 1477
nickjillings@1404 1478 function Specification() {
nickjillings@1404 1479 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nickjillings@1404 1480
nickjillings@1404 1481 this.interfaceType = null;
nickjillings@1406 1482 this.commonInterface = new function()
nickjillings@1406 1483 {
nickjillings@1406 1484 this.options = [];
nickjillings@1406 1485 this.optionNode = function(input)
nickjillings@1406 1486 {
nickjillings@1406 1487 var name = input.getAttribute('name');
nickjillings@1406 1488 this.type = name;
nickjillings@1406 1489 if(this.type == "option")
nickjillings@1406 1490 {
nickjillings@1406 1491 this.name = input.id;
nickjillings@1406 1492 } else if (this.type == "check")
nickjillings@1406 1493 {
nickjillings@1406 1494 this.check = input.id;
nickjillings@1406 1495 }
nickjillings@1406 1496 };
nickjillings@1406 1497 };
nickjillings@1411 1498
nickjillings@1411 1499 this.randomiseOrder = function(input)
nickjillings@1411 1500 {
nickjillings@1411 1501 // This takes an array of information and randomises the order
nickjillings@1411 1502 var N = input.length;
nickjillings@1411 1503
nickjillings@1411 1504 var inputSequence = []; // For safety purposes: keep track of randomisation
nickjillings@1411 1505 for (var counter = 0; counter < N; ++counter)
nickjillings@1411 1506 inputSequence.push(counter) // Fill array
nickjillings@1411 1507 var inputSequenceClone = inputSequence.slice(0);
nickjillings@1411 1508
nickjillings@1411 1509 var holdArr = [];
nickjillings@1411 1510 var outputSequence = [];
nickjillings@1411 1511 for (var n=0; n<N; n++)
nickjillings@1411 1512 {
nickjillings@1411 1513 // First pick a random number
nickjillings@1411 1514 var r = Math.random();
nickjillings@1411 1515 // Multiply and floor by the number of elements left
nickjillings@1411 1516 r = Math.floor(r*input.length);
nickjillings@1411 1517 // Pick out that element and delete from the array
nickjillings@1411 1518 holdArr.push(input.splice(r,1)[0]);
nickjillings@1411 1519 // Do the same with sequence
nickjillings@1411 1520 outputSequence.push(inputSequence.splice(r,1)[0]);
nickjillings@1411 1521 }
nickjillings@1411 1522 console.log(inputSequenceClone.toString()); // print original array to console
nickjillings@1411 1523 console.log(outputSequence.toString()); // print randomised array to console
nickjillings@1411 1524 return holdArr;
nickjillings@1411 1525 };
nickjillings@1404 1526 this.projectReturn = null;
nickjillings@1404 1527 this.randomiseOrder = null;
nickjillings@1404 1528 this.collectMetrics = null;
nickjillings@1404 1529 this.testPages = null;
nickjillings@1406 1530 this.audioHolders = [];
nickjillings@1406 1531 this.metrics = [];
nickjillings@1432 1532 this.loudness = null;
nickjillings@1404 1533
nickjillings@1406 1534 this.decode = function(projectXML) {
nickjillings@1404 1535 // projectXML - DOM Parsed document
nickjillings@1404 1536 this.projectXML = projectXML.childNodes[0];
nickjillings@1404 1537 var setupNode = projectXML.getElementsByTagName('setup')[0];
nickjillings@1404 1538 this.interfaceType = setupNode.getAttribute('interface');
nickjillings@1404 1539 this.projectReturn = setupNode.getAttribute('projectReturn');
nickjillings@1404 1540 this.testPages = setupNode.getAttribute('testPages');
nickjillings@1404 1541 if (setupNode.getAttribute('randomiseOrder') == "true") {
nickjillings@1404 1542 this.randomiseOrder = true;
nickjillings@1404 1543 } else {this.randomiseOrder = false;}
nickjillings@1404 1544 if (setupNode.getAttribute('collectMetrics') == "true") {
nickjillings@1404 1545 this.collectMetrics = true;
nickjillings@1404 1546 } else {this.collectMetrics = false;}
nickjillings@1404 1547 if (isNaN(Number(this.testPages)) || this.testPages == undefined)
nickjillings@1404 1548 {
nickjillings@1404 1549 this.testPages = null;
nickjillings@1404 1550 } else {
nickjillings@1404 1551 this.testPages = Number(this.testPages);
nickjillings@1404 1552 if (this.testPages == 0) {this.testPages = null;}
nickjillings@1404 1553 }
nickjillings@1432 1554 if (setupNode.getAttribute('loudness') != null)
nickjillings@1432 1555 {
nickjillings@1432 1556 var XMLloudness = setupNode.getAttribute('loudness');
nickjillings@1432 1557 if (isNaN(Number(XMLloudness)) == false)
nickjillings@1432 1558 {
nickjillings@1432 1559 this.loudness = Number(XMLloudness);
nickjillings@1432 1560 }
nickjillings@1432 1561 }
nickjillings@1404 1562 var metricCollection = setupNode.getElementsByTagName('Metric');
nickjillings@1404 1563
nickjillings@1406 1564 var setupPreTestNode = setupNode.getElementsByTagName('PreTest');
nickjillings@1406 1565 if (setupPreTestNode.length != 0)
nickjillings@1406 1566 {
nickjillings@1406 1567 setupPreTestNode = setupPreTestNode[0];
nickjillings@1406 1568 this.preTest.construct(setupPreTestNode);
nickjillings@1406 1569 }
nickjillings@1406 1570
nickjillings@1406 1571 var setupPostTestNode = setupNode.getElementsByTagName('PostTest');
nickjillings@1406 1572 if (setupPostTestNode.length != 0)
nickjillings@1406 1573 {
nickjillings@1406 1574 setupPostTestNode = setupPostTestNode[0];
nickjillings@1406 1575 this.postTest.construct(setupPostTestNode);
nickjillings@1406 1576 }
nickjillings@1404 1577
nickjillings@1404 1578 if (metricCollection.length > 0) {
nickjillings@1404 1579 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
nickjillings@1404 1580 for (var i=0; i<metricCollection.length; i++) {
nickjillings@1404 1581 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
nickjillings@1404 1582 }
nickjillings@1404 1583 }
nickjillings@1404 1584
nickjillings@1404 1585 var commonInterfaceNode = setupNode.getElementsByTagName('interface');
nickjillings@1404 1586 if (commonInterfaceNode.length > 0) {
nickjillings@1404 1587 commonInterfaceNode = commonInterfaceNode[0];
nickjillings@1404 1588 } else {
nickjillings@1404 1589 commonInterfaceNode = undefined;
nickjillings@1404 1590 }
nickjillings@1404 1591
nickjillings@1404 1592 this.commonInterface = new function() {
nickjillings@1404 1593 this.OptionNode = function(child) {
nickjillings@1404 1594 this.type = child.nodeName;
nickjillings@1404 1595 if (this.type == 'option')
nickjillings@1404 1596 {
nickjillings@1404 1597 this.name = child.getAttribute('name');
nickjillings@1404 1598 }
nickjillings@1404 1599 else if (this.type == 'check') {
nickjillings@1404 1600 this.check = child.getAttribute('name');
nickjillings@1404 1601 if (this.check == 'scalerange') {
nickjillings@1404 1602 this.min = child.getAttribute('min');
nickjillings@1404 1603 this.max = child.getAttribute('max');
nickjillings@1404 1604 if (this.min == null) {this.min = 1;}
nickjillings@1404 1605 else if (Number(this.min) > 1 && this.min != null) {
nickjillings@1404 1606 this.min = Number(this.min)/100;
nickjillings@1404 1607 } else {
nickjillings@1404 1608 this.min = Number(this.min);
nickjillings@1404 1609 }
nickjillings@1404 1610 if (this.max == null) {this.max = 0;}
nickjillings@1404 1611 else if (Number(this.max) > 1 && this.max != null) {
nickjillings@1404 1612 this.max = Number(this.max)/100;
nickjillings@1404 1613 } else {
nickjillings@1404 1614 this.max = Number(this.max);
nickjillings@1404 1615 }
nickjillings@1404 1616 }
nickjillings@1404 1617 } else if (this.type == 'anchor' || this.type == 'reference') {
nickjillings@1406 1618 this.value = Number(child.textContent);
nickjillings@1406 1619 this.enforce = child.getAttribute('enforce');
nickjillings@1406 1620 if (this.enforce == 'true') {this.enforce = true;}
nickjillings@1406 1621 else {this.enforce = false;}
nickjillings@1404 1622 }
nickjillings@1404 1623 };
nickjillings@1404 1624 this.options = [];
nickjillings@1404 1625 if (commonInterfaceNode != undefined) {
nickjillings@1404 1626 var child = commonInterfaceNode.firstElementChild;
nickjillings@1404 1627 while (child != undefined) {
nickjillings@1404 1628 this.options.push(new this.OptionNode(child));
nickjillings@1404 1629 child = child.nextElementSibling;
nickjillings@1404 1630 }
nickjillings@1404 1631 }
nickjillings@1404 1632 };
nickjillings@1404 1633
nickjillings@1404 1634 var audioHolders = projectXML.getElementsByTagName('audioHolder');
nickjillings@1404 1635 for (var i=0; i<audioHolders.length; i++) {
nickjillings@1406 1636 var node = new this.audioHolderNode(this);
nickjillings@1406 1637 node.decode(this,audioHolders[i]);
nickjillings@1406 1638 this.audioHolders.push(node);
nickjillings@1404 1639 }
nickjillings@1404 1640
nickjillings@1404 1641 // New check if we need to randomise the test order
nickjillings@1427 1642 if (this.randomiseOrder && typeof randomiseOrder === "function")
nickjillings@1404 1643 {
nickjillings@1404 1644 this.audioHolders = randomiseOrder(this.audioHolders);
nickjillings@1404 1645 for (var i=0; i<this.audioHolders.length; i++)
nickjillings@1404 1646 {
nickjillings@1404 1647 this.audioHolders[i].presentedId = i;
nickjillings@1404 1648 }
nickjillings@1404 1649 }
nickjillings@1404 1650
nickjillings@1404 1651 if (this.testPages != null || this.testPages != undefined)
nickjillings@1404 1652 {
nickjillings@1404 1653 if (this.testPages > audioHolders.length)
nickjillings@1404 1654 {
nickjillings@1404 1655 console.log('Warning: You have specified '+audioHolders.length+' tests but requested '+this.testPages+' be completed!');
nickjillings@1404 1656 this.testPages = audioHolders.length;
nickjillings@1404 1657 }
nickjillings@1404 1658 var aH = this.audioHolders;
nickjillings@1404 1659 this.audioHolders = [];
nickjillings@1404 1660 for (var i=0; i<this.testPages; i++)
nickjillings@1404 1661 {
nickjillings@1404 1662 this.audioHolders.push(aH[i]);
nickjillings@1404 1663 }
nickjillings@1404 1664 }
nickjillings@1404 1665 };
nickjillings@1404 1666
nickjillings@1406 1667 this.encode = function()
nickjillings@1406 1668 {
nickjillings@1406 1669 var root = document.implementation.createDocument(null,"BrowserEvalProjectDocument");
nickjillings@1406 1670 // First get all the <setup> tag compiled
nickjillings@1406 1671 var setupNode = root.createElement("setup");
nickjillings@1406 1672 setupNode.setAttribute('interface',this.interfaceType);
nickjillings@1406 1673 setupNode.setAttribute('projectReturn',this.projectReturn);
nickjillings@1406 1674 setupNode.setAttribute('randomiseOrder',this.randomiseOrder);
nickjillings@1406 1675 setupNode.setAttribute('collectMetrics',this.collectMetrics);
nickjillings@1406 1676 setupNode.setAttribute('testPages',this.testPages);
nickjillings@1432 1677 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
nickjillings@1406 1678
nickjillings@1406 1679 var setupPreTest = root.createElement("PreTest");
nickjillings@1406 1680 for (var i=0; i<this.preTest.options.length; i++)
nickjillings@1406 1681 {
nickjillings@1406 1682 setupPreTest.appendChild(this.preTest.options[i].exportXML(root));
nickjillings@1406 1683 }
nickjillings@1406 1684
nickjillings@1406 1685 var setupPostTest = root.createElement("PostTest");
nickjillings@1406 1686 for (var i=0; i<this.postTest.options.length; i++)
nickjillings@1406 1687 {
nickjillings@1406 1688 setupPostTest.appendChild(this.postTest.options[i].exportXML(root));
nickjillings@1406 1689 }
nickjillings@1406 1690
nickjillings@1406 1691 setupNode.appendChild(setupPreTest);
nickjillings@1406 1692 setupNode.appendChild(setupPostTest);
nickjillings@1406 1693
nickjillings@1406 1694 // <Metric> tag
nickjillings@1406 1695 var Metric = root.createElement("Metric");
nickjillings@1406 1696 for (var i=0; i<this.metrics.length; i++)
nickjillings@1406 1697 {
nickjillings@1406 1698 var metricEnable = root.createElement("metricEnable");
nickjillings@1406 1699 metricEnable.textContent = this.metrics[i].enabled;
nickjillings@1406 1700 Metric.appendChild(metricEnable);
nickjillings@1406 1701 }
nickjillings@1406 1702 setupNode.appendChild(Metric);
nickjillings@1406 1703
nickjillings@1406 1704 // <interface> tag
nickjillings@1406 1705 var CommonInterface = root.createElement("interface");
nickjillings@1406 1706 for (var i=0; i<this.commonInterface.options.length; i++)
nickjillings@1406 1707 {
nickjillings@1406 1708 var CIObj = this.commonInterface.options[i];
nickjillings@1406 1709 var CINode = root.createElement(CIObj.type);
nickjillings@1406 1710 if (CIObj.type == "check") {CINode.setAttribute("name",CIObj.check);}
nickjillings@1406 1711 else {CINode.setAttribute("name",CIObj.name);}
nickjillings@1406 1712 CommonInterface.appendChild(CINode);
nickjillings@1406 1713 }
nickjillings@1406 1714 setupNode.appendChild(CommonInterface);
nickjillings@1406 1715
nickjillings@1406 1716 root.getElementsByTagName("BrowserEvalProjectDocument")[0].appendChild(setupNode);
nickjillings@1406 1717 // Time for the <audioHolder> tags
nickjillings@1406 1718 for (var ahIndex = 0; ahIndex < this.audioHolders.length; ahIndex++)
nickjillings@1406 1719 {
nickjillings@1406 1720 var node = this.audioHolders[ahIndex].encode(root);
nickjillings@1406 1721 root.getElementsByTagName("BrowserEvalProjectDocument")[0].appendChild(node);
nickjillings@1406 1722 }
nickjillings@1406 1723 return root;
nickjillings@1406 1724 };
nickjillings@1406 1725
nickjillings@1406 1726 this.prepostNode = function(type) {
nickjillings@1404 1727 this.type = type;
nickjillings@1404 1728 this.options = [];
nickjillings@1404 1729
nickjillings@1406 1730 this.OptionNode = function() {
nickjillings@1404 1731
nickjillings@1406 1732 this.childOption = function() {
nickjillings@1404 1733 this.type = 'option';
nickjillings@1406 1734 this.id = null;
nickjillings@1406 1735 this.name = undefined;
nickjillings@1406 1736 this.text = null;
nickjillings@1404 1737 };
nickjillings@1404 1738
nickjillings@1406 1739 this.type = undefined;
nickjillings@1406 1740 this.id = undefined;
nickjillings@1406 1741 this.mandatory = undefined;
nickjillings@1406 1742 this.question = undefined;
nickjillings@1406 1743 this.statement = undefined;
nickjillings@1406 1744 this.boxsize = undefined;
nickjillings@1406 1745 this.options = [];
nickjillings@1406 1746 this.min = undefined;
nickjillings@1406 1747 this.max = undefined;
nickjillings@1406 1748 this.step = undefined;
nickjillings@1406 1749
nickjillings@1406 1750 this.decode = function(child)
nickjillings@1406 1751 {
nickjillings@1406 1752 this.type = child.nodeName;
nickjillings@1406 1753 if (child.nodeName == "question") {
nickjillings@1406 1754 this.id = child.id;
nickjillings@1406 1755 this.mandatory;
nickjillings@1406 1756 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
nickjillings@1406 1757 else {this.mandatory = false;}
nickjillings@1406 1758 this.question = child.textContent;
nickjillings@1406 1759 if (child.getAttribute('boxsize') == null) {
nickjillings@1406 1760 this.boxsize = 'normal';
nickjillings@1406 1761 } else {
nickjillings@1406 1762 this.boxsize = child.getAttribute('boxsize');
nickjillings@1406 1763 }
nickjillings@1406 1764 } else if (child.nodeName == "statement") {
nickjillings@1406 1765 this.statement = child.textContent;
nickjillings@1406 1766 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
nickjillings@1406 1767 var element = child.firstElementChild;
nickjillings@1406 1768 this.id = child.id;
nickjillings@1406 1769 if (element == null) {
nickjillings@1406 1770 console.log('Malformed' +child.nodeName+ 'entry');
nickjillings@1406 1771 this.statement = 'Malformed' +child.nodeName+ 'entry';
nickjillings@1406 1772 this.type = 'statement';
nickjillings@1406 1773 } else {
nickjillings@1406 1774 this.options = [];
nickjillings@1406 1775 while (element != null) {
nickjillings@1406 1776 if (element.nodeName == 'statement' && this.statement == undefined){
nickjillings@1406 1777 this.statement = element.textContent;
nickjillings@1406 1778 } else if (element.nodeName == 'option') {
nickjillings@1406 1779 var node = new this.childOption();
nickjillings@1406 1780 node.id = element.id;
nickjillings@1406 1781 node.name = element.getAttribute('name');
nickjillings@1406 1782 node.text = element.textContent;
nickjillings@1406 1783 this.options.push(node);
nickjillings@1406 1784 }
nickjillings@1406 1785 element = element.nextElementSibling;
nickjillings@1406 1786 }
nickjillings@1406 1787 }
nickjillings@1406 1788 } else if (child.nodeName == "number") {
nickjillings@1406 1789 this.statement = child.textContent;
nickjillings@1406 1790 this.id = child.id;
nickjillings@1406 1791 this.min = child.getAttribute('min');
nickjillings@1406 1792 this.max = child.getAttribute('max');
nickjillings@1406 1793 this.step = child.getAttribute('step');
nickjillings@1404 1794 }
nickjillings@1406 1795 };
nickjillings@1406 1796
nickjillings@1406 1797 this.exportXML = function(root)
nickjillings@1406 1798 {
nickjillings@1406 1799 var node = root.createElement(this.type);
nickjillings@1406 1800 switch(this.type)
nickjillings@1406 1801 {
nickjillings@1406 1802 case "statement":
nickjillings@1406 1803 node.textContent = this.statement;
nickjillings@1406 1804 break;
nickjillings@1406 1805 case "question":
nickjillings@1406 1806 node.id = this.id;
nickjillings@1406 1807 node.setAttribute("mandatory",this.mandatory);
nickjillings@1406 1808 node.setAttribute("boxsize",this.boxsize);
nickjillings@1406 1809 node.textContent = this.question;
nickjillings@1406 1810 break;
nickjillings@1406 1811 case "number":
nickjillings@1406 1812 node.id = this.id;
nickjillings@1406 1813 node.setAttribute("mandatory",this.mandatory);
nickjillings@1406 1814 node.setAttribute("min", this.min);
nickjillings@1406 1815 node.setAttribute("max", this.max);
nickjillings@1406 1816 node.setAttribute("step", this.step);
nickjillings@1406 1817 node.textContent = this.statement;
nickjillings@1406 1818 break;
nickjillings@1406 1819 case "checkbox":
nickjillings@1406 1820 node.id = this.id;
nickjillings@1406 1821 var statement = root.createElement("statement");
nickjillings@1406 1822 statement.textContent = this.statement;
nickjillings@1406 1823 node.appendChild(statement);
nickjillings@1406 1824 for (var i=0; i<this.options.length; i++)
nickjillings@1406 1825 {
nickjillings@1406 1826 var option = this.options[i];
nickjillings@1406 1827 var optionNode = root.createElement("option");
nickjillings@1406 1828 optionNode.id = option.id;
nickjillings@1406 1829 optionNode.textContent = option.text;
nickjillings@1406 1830 node.appendChild(optionNode);
nickjillings@1404 1831 }
nickjillings@1406 1832 break;
nickjillings@1406 1833 case "radio":
nickjillings@1406 1834 node.id = this.id;
nickjillings@1406 1835 var statement = root.createElement("statement");
nickjillings@1406 1836 statement.textContent = this.statement;
nickjillings@1406 1837 node.appendChild(statement);
nickjillings@1406 1838 for (var i=0; i<this.options.length; i++)
nickjillings@1406 1839 {
nickjillings@1406 1840 var option = this.options[i];
nickjillings@1406 1841 var optionNode = root.createElement("option");
nickjillings@1406 1842 optionNode.setAttribute("name",option.name);
nickjillings@1406 1843 optionNode.textContent = option.text;
nickjillings@1406 1844 node.appendChild(optionNode);
nickjillings@1406 1845 }
nickjillings@1406 1846 break;
nickjillings@1404 1847 }
nickjillings@1406 1848 return node;
nickjillings@1406 1849 };
nickjillings@1406 1850 };
nickjillings@1406 1851 this.construct = function(Collection)
nickjillings@1406 1852 {
nickjillings@1406 1853 if (Collection.childElementCount != 0) {
nickjillings@1406 1854 var child = Collection.firstElementChild;
nickjillings@1406 1855 var node = new this.OptionNode();
nickjillings@1406 1856 node.decode(child);
nickjillings@1406 1857 this.options.push(node);
nickjillings@1406 1858 while (child.nextElementSibling != null) {
nickjillings@1406 1859 child = child.nextElementSibling;
nickjillings@1406 1860 node = new this.OptionNode();
nickjillings@1406 1861 node.decode(child);
nickjillings@1406 1862 this.options.push(node);
nickjillings@1406 1863 }
nickjillings@1404 1864 }
nickjillings@1404 1865 };
nickjillings@1404 1866 };
nickjillings@1406 1867 this.preTest = new this.prepostNode("pretest");
nickjillings@1406 1868 this.postTest = new this.prepostNode("posttest");
nickjillings@1404 1869
nickjillings@1404 1870 this.metricNode = function(name) {
nickjillings@1404 1871 this.enabled = name;
nickjillings@1404 1872 };
nickjillings@1404 1873
nickjillings@1406 1874 this.audioHolderNode = function(parent) {
nickjillings@1404 1875 this.type = 'audioHolder';
nickjillings@1406 1876 this.presentedId = undefined;
nickjillings@1406 1877 this.id = undefined;
nickjillings@1406 1878 this.hostURL = undefined;
nickjillings@1406 1879 this.sampleRate = undefined;
nickjillings@1406 1880 this.randomiseOrder = undefined;
nickjillings@1406 1881 this.loop = undefined;
nickjillings@1406 1882 this.elementComments = undefined;
nickjillings@1406 1883 this.outsideReference = null;
nickjillings@1432 1884 this.loudness = null;
nickjillings@1395 1885 this.initialPosition = null;
nickjillings@1406 1886 this.preTest = new parent.prepostNode("pretest");
nickjillings@1406 1887 this.postTest = new parent.prepostNode("pretest");
nickjillings@1406 1888 this.interfaces = [];
nickjillings@1406 1889 this.commentBoxPrefix = "Comment on track";
nickjillings@1406 1890 this.audioElements = [];
nickjillings@1406 1891 this.commentQuestions = [];
nickjillings@1406 1892
nickjillings@1406 1893 this.decode = function(parent,xml)
nickjillings@1406 1894 {
nickjillings@1406 1895 this.presentedId = parent.audioHolders.length;
nickjillings@1406 1896 this.id = xml.id;
nickjillings@1406 1897 this.hostURL = xml.getAttribute('hostURL');
nickjillings@1406 1898 this.sampleRate = xml.getAttribute('sampleRate');
nickjillings@1406 1899 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
nickjillings@1406 1900 else {this.randomiseOrder = false;}
nickjillings@1406 1901 this.repeatCount = xml.getAttribute('repeatCount');
nickjillings@1406 1902 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
nickjillings@1406 1903 else {this.loop == false;}
nickjillings@1406 1904 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
nickjillings@1406 1905 else {this.elementComments = false;}
nickjillings@1432 1906 if (typeof parent.loudness === "number")
nickjillings@1432 1907 {
nickjillings@1432 1908 this.loudness = parent.loudness;
nickjillings@1432 1909 }
nickjillings@1395 1910 if (typeof xml.getAttribute('initial-position') === "string")
nickjillings@1395 1911 {
nickjillings@1395 1912 var xmlInitialPosition = Number(xml.getAttribute('initial-position'));
nickjillings@1395 1913 if (isNaN(xmlInitialPosition) == false)
nickjillings@1395 1914 {
nickjillings@1395 1915 if (xmlInitialPosition > 1)
nickjillings@1395 1916 {
nickjillings@1395 1917 xmlInitialPosition /= 100;
nickjillings@1395 1918 }
nickjillings@1395 1919 this.initialPosition = xmlInitialPosition;
nickjillings@1395 1920 }
nickjillings@1395 1921 }
nickjillings@1432 1922 if (xml.getAttribute('loudness') != null)
nickjillings@1432 1923 {
nickjillings@1432 1924 var XMLloudness = xml.getAttribute('loudness');
nickjillings@1432 1925 if (isNaN(Number(XMLloudness)) == false)
nickjillings@1432 1926 {
nickjillings@1432 1927 this.loudness = Number(XMLloudness);
nickjillings@1432 1928 }
nickjillings@1432 1929 }
nickjillings@1406 1930 var setupPreTestNode = xml.getElementsByTagName('PreTest');
nickjillings@1406 1931 if (setupPreTestNode.length != 0)
nickjillings@1406 1932 {
nickjillings@1406 1933 setupPreTestNode = setupPreTestNode[0];
nickjillings@1406 1934 this.preTest.construct(setupPreTestNode);
nickjillings@1406 1935 }
nickjillings@1406 1936
nickjillings@1406 1937 var setupPostTestNode = xml.getElementsByTagName('PostTest');
nickjillings@1406 1938 if (setupPostTestNode.length != 0)
nickjillings@1406 1939 {
nickjillings@1406 1940 setupPostTestNode = setupPostTestNode[0];
nickjillings@1406 1941 this.postTest.construct(setupPostTestNode);
nickjillings@1406 1942 }
nickjillings@1406 1943
nickjillings@1406 1944 var interfaceDOM = xml.getElementsByTagName('interface');
nickjillings@1406 1945 for (var i=0; i<interfaceDOM.length; i++) {
nickjillings@1406 1946 var node = new this.interfaceNode();
nickjillings@1406 1947 node.decode(interfaceDOM[i]);
nickjillings@1406 1948 this.interfaces.push(node);
nickjillings@1406 1949 }
nickjillings@1406 1950 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
nickjillings@1406 1951 if (this.commentBoxPrefix.length != 0) {
nickjillings@1406 1952 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
nickjillings@1406 1953 } else {
nickjillings@1406 1954 this.commentBoxPrefix = "Comment on track";
nickjillings@1406 1955 }
nickjillings@1406 1956 var audioElementsDOM = xml.getElementsByTagName('audioElements');
nickjillings@1441 1957 var outsideReferenceHolder = null;
nickjillings@1406 1958 for (var i=0; i<audioElementsDOM.length; i++) {
nickjillings@1406 1959 var node = new this.audioElementNode();
nickjillings@1406 1960 node.decode(this,audioElementsDOM[i]);
nickjillings@1406 1961 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') {
nickjillings@1406 1962 if (this.outsideReference == null) {
nickjillings@1441 1963 outsideReferenceHolder = node;
nickjillings@1441 1964 this.outsideReference = i;
nickjillings@1406 1965 } else {
nickjillings@1406 1966 console.log('Error only one audioelement can be of type outsidereference per audioholder');
nickjillings@1406 1967 this.audioElements.push(node);
nickjillings@1406 1968 console.log('Element id '+audioElementsDOM[i].id+' made into normal node');
nickjillings@1406 1969 }
nickjillings@1406 1970 } else {
nickjillings@1406 1971 this.audioElements.push(node);
nickjillings@1406 1972 }
nickjillings@1406 1973 }
nickjillings@1406 1974
nickjillings@1427 1975 if (this.randomiseOrder == true && typeof randomiseOrder === "function")
nickjillings@1411 1976 {
nickjillings@1411 1977 this.audioElements = randomiseOrder(this.audioElements);
nickjillings@1411 1978 }
nickjillings@1441 1979 if (outsideReferenceHolder != null)
nickjillings@1441 1980 {
nickjillings@1441 1981 this.audioElements.push(outsideReferenceHolder);
nickjillings@1441 1982 this.outsideReference = this.audioElements.length-1;
nickjillings@1441 1983 }
nickjillings@1441 1984
nickjillings@1411 1985
nickjillings@1406 1986 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
nickjillings@1406 1987 for (var i=0; i<commentQuestionsDOM.length; i++) {
nickjillings@1406 1988 var node = new this.commentQuestionNode();
nickjillings@1406 1989 node.decode(commentQuestionsDOM[i]);
nickjillings@1406 1990 this.commentQuestions.push(node);
nickjillings@1404 1991 }
nickjillings@1404 1992 };
nickjillings@1404 1993
nickjillings@1406 1994 this.encode = function(root)
nickjillings@1406 1995 {
nickjillings@1406 1996 var AHNode = root.createElement("audioHolder");
nickjillings@1406 1997 AHNode.id = this.id;
nickjillings@1406 1998 AHNode.setAttribute("hostURL",this.hostURL);
nickjillings@1406 1999 AHNode.setAttribute("sampleRate",this.sampleRate);
nickjillings@1406 2000 AHNode.setAttribute("randomiseOrder",this.randomiseOrder);
nickjillings@1406 2001 AHNode.setAttribute("repeatCount",this.repeatCount);
nickjillings@1406 2002 AHNode.setAttribute("loop",this.loop);
nickjillings@1406 2003 AHNode.setAttribute("elementComments",this.elementComments);
nickjillings@1432 2004 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
nickjillings@1395 2005 if(this.initialPosition != null) {
nickjillings@1395 2006 AHNode.setAttribute("loudness",this.initialPosition*100);
nickjillings@1395 2007 }
nickjillings@1406 2008 for (var i=0; i<this.interfaces.length; i++)
nickjillings@1404 2009 {
nickjillings@1406 2010 AHNode.appendChild(this.interfaces[i].encode(root));
nickjillings@1406 2011 }
nickjillings@1406 2012
nickjillings@1406 2013 for (var i=0; i<this.audioElements.length; i++) {
nickjillings@1406 2014 AHNode.appendChild(this.audioElements[i].encode(root));
nickjillings@1406 2015 }
nickjillings@1406 2016 // Create <CommentQuestion>
nickjillings@1406 2017 for (var i=0; i<this.commentQuestions.length; i++)
nickjillings@1406 2018 {
nickjillings@1406 2019 AHNode.appendChild(this.commentQuestions[i].exportXML(root));
nickjillings@1406 2020 }
nickjillings@1406 2021
nickjillings@1406 2022 // Create <PreTest>
nickjillings@1406 2023 var AHPreTest = root.createElement("PreTest");
nickjillings@1406 2024 for (var i=0; i<this.preTest.options.length; i++)
nickjillings@1406 2025 {
nickjillings@1406 2026 AHPreTest.appendChild(this.preTest.options[i].exportXML(root));
nickjillings@1406 2027 }
nickjillings@1406 2028
nickjillings@1406 2029 var AHPostTest = root.createElement("PostTest");
nickjillings@1406 2030 for (var i=0; i<this.postTest.options.length; i++)
nickjillings@1406 2031 {
nickjillings@1406 2032 AHPostTest.appendChild(this.postTest.options[i].exportXML(root));
nickjillings@1406 2033 }
nickjillings@1406 2034 AHNode.appendChild(AHPreTest);
nickjillings@1406 2035 AHNode.appendChild(AHPostTest);
nickjillings@1406 2036 return AHNode;
nickjillings@1406 2037 };
nickjillings@1406 2038
nickjillings@1406 2039 this.interfaceNode = function() {
nickjillings@1406 2040 this.title = undefined;
nickjillings@1406 2041 this.options = [];
nickjillings@1406 2042 this.scale = [];
nickjillings@1416 2043 this.name = undefined;
nickjillings@1406 2044 this.decode = function(DOM)
nickjillings@1406 2045 {
nickjillings@1406 2046 var title = DOM.getElementsByTagName('title');
nickjillings@1406 2047 if (title.length == 0) {this.title = null;}
nickjillings@1406 2048 else {this.title = title[0].textContent;}
nickjillings@1416 2049 var name = DOM.getAttribute("name");
nickjillings@1416 2050 if (name != undefined) {this.name = name;}
nickjillings@1406 2051 this.options = parent.commonInterface.options;
nickjillings@1406 2052 var scale = DOM.getElementsByTagName('scale');
nickjillings@1406 2053 this.scale = [];
nickjillings@1406 2054 for (var i=0; i<scale.length; i++) {
nickjillings@1406 2055 var arr = [null, null];
nickjillings@1406 2056 arr[0] = scale[i].getAttribute('position');
nickjillings@1406 2057 arr[1] = scale[i].textContent;
nickjillings@1406 2058 this.scale.push(arr);
nickjillings@1406 2059 }
nickjillings@1406 2060 };
nickjillings@1406 2061 this.encode = function(root)
nickjillings@1406 2062 {
nickjillings@1406 2063 var node = root.createElement("interface");
nickjillings@1406 2064 if (this.title != undefined)
nickjillings@1404 2065 {
nickjillings@1406 2066 var title = root.createElement("title");
nickjillings@1406 2067 title.textContent = this.title;
nickjillings@1406 2068 node.appendChild(title);
nickjillings@1406 2069 }
nickjillings@1406 2070 for (var i=0; i<this.options.length; i++)
nickjillings@1406 2071 {
nickjillings@1406 2072 var optionNode = root.createElement(this.options[i].type);
nickjillings@1406 2073 if (this.options[i].type == "option")
nickjillings@1404 2074 {
nickjillings@1406 2075 optionNode.setAttribute("name",this.options[i].name);
nickjillings@1406 2076 } else if (this.options[i].type == "check") {
nickjillings@1406 2077 optionNode.setAttribute("check",this.options[i].check);
nickjillings@1406 2078 } else if (this.options[i].type == "scalerange") {
nickjillings@1406 2079 optionNode.setAttribute("min",this.options[i].min*100);
nickjillings@1406 2080 optionNode.setAttribute("max",this.options[i].max*100);
nickjillings@1406 2081 }
nickjillings@1406 2082 node.appendChild(optionNode);
nickjillings@1406 2083 }
nickjillings@1406 2084 for (var i=0; i<this.scale.length; i++) {
nickjillings@1406 2085 var scale = root.createElement("scale");
nickjillings@1406 2086 scale.setAttribute("position",this.scale[i][0]);
nickjillings@1406 2087 scale.textContent = this.scale[i][1];
nickjillings@1406 2088 node.appendChild(scale);
nickjillings@1406 2089 }
nickjillings@1406 2090 return node;
nickjillings@1406 2091 };
nickjillings@1406 2092 };
nickjillings@1406 2093
nickjillings@1406 2094 this.audioElementNode = function() {
nickjillings@1406 2095 this.url = null;
nickjillings@1406 2096 this.id = null;
nickjillings@1406 2097 this.parent = null;
nickjillings@1406 2098 this.type = "normal";
nickjillings@1406 2099 this.marker = false;
nickjillings@1406 2100 this.enforce = false;
nickjillings@1426 2101 this.gain = 1.0;
nickjillings@1406 2102 this.decode = function(parent,xml)
nickjillings@1406 2103 {
nickjillings@1406 2104 this.url = xml.getAttribute('url');
nickjillings@1406 2105 this.id = xml.id;
nickjillings@1406 2106 this.parent = parent;
nickjillings@1406 2107 this.type = xml.getAttribute('type');
nickjillings@1426 2108 var gain = xml.getAttribute('gain');
nickjillings@1426 2109 if (isNaN(gain) == false && gain != null)
nickjillings@1426 2110 {
nickjillings@1426 2111 this.gain = decibelToLinear(Number(gain));
nickjillings@1426 2112 }
nickjillings@1406 2113 if (this.type == null) {this.type = "normal";}
nickjillings@1406 2114 if (this.type == 'anchor') {this.anchor = true;}
nickjillings@1406 2115 else {this.anchor = false;}
nickjillings@1406 2116 if (this.type == 'reference') {this.reference = true;}
nickjillings@1406 2117 else {this.reference = false;}
nickjillings@1406 2118 if (this.anchor == true || this.reference == true)
nickjillings@1406 2119 {
nickjillings@1406 2120 this.marker = xml.getAttribute('marker');
nickjillings@1406 2121 if (this.marker != undefined)
nickjillings@1406 2122 {
nickjillings@1406 2123 this.marker = Number(this.marker);
nickjillings@1406 2124 if (isNaN(this.marker) == false)
nickjillings@1404 2125 {
nickjillings@1406 2126 if (this.marker > 1)
nickjillings@1406 2127 { this.marker /= 100.0;}
nickjillings@1406 2128 if (this.marker >= 0 && this.marker <= 1)
nickjillings@1406 2129 {
nickjillings@1406 2130 this.enforce = true;
nickjillings@1406 2131 return;
nickjillings@1406 2132 } else {
nickjillings@1406 2133 console.log("ERROR - Marker of audioElement "+this.id+" is not between 0 and 1 (float) or 0 and 100 (integer)!");
nickjillings@1406 2134 console.log("ERROR - Marker not enforced!");
nickjillings@1406 2135 }
nickjillings@1404 2136 } else {
nickjillings@1406 2137 console.log("ERROR - Marker of audioElement "+this.id+" is not a number!");
nickjillings@1404 2138 console.log("ERROR - Marker not enforced!");
nickjillings@1404 2139 }
nickjillings@1404 2140 }
nickjillings@1404 2141 }
nickjillings@1406 2142 };
nickjillings@1406 2143 this.encode = function(root)
nickjillings@1406 2144 {
nickjillings@1406 2145 var AENode = root.createElement("audioElements");
nickjillings@1406 2146 AENode.id = this.id;
nickjillings@1406 2147 AENode.setAttribute("url",this.url);
nickjillings@1406 2148 AENode.setAttribute("type",this.type);
nickjillings@1426 2149 AENode.setAttribute("gain",linearToDecibel(this.gain));
nickjillings@1406 2150 if (this.marker != false)
nickjillings@1406 2151 {
nickjillings@1406 2152 AENode.setAttribute("marker",this.marker*100);
nickjillings@1406 2153 }
nickjillings@1406 2154 return AENode;
nickjillings@1406 2155 };
nickjillings@1404 2156 };
nickjillings@1404 2157
nickjillings@1404 2158 this.commentQuestionNode = function(xml) {
nickjillings@1406 2159 this.id = null;
nickjillings@1406 2160 this.type = undefined;
nickjillings@1406 2161 this.question = undefined;
nickjillings@1406 2162 this.options = [];
nickjillings@1406 2163 this.statement = undefined;
nickjillings@1406 2164
nickjillings@1406 2165 this.childOption = function() {
nickjillings@1404 2166 this.type = 'option';
nickjillings@1406 2167 this.name = null;
nickjillings@1406 2168 this.text = null;
nickjillings@1404 2169 };
nickjillings@1406 2170 this.exportXML = function(root)
nickjillings@1406 2171 {
nickjillings@1406 2172 var CQNode = root.createElement("CommentQuestion");
nickjillings@1406 2173 CQNode.id = this.id;
nickjillings@1406 2174 CQNode.setAttribute("type",this.type);
nickjillings@1406 2175 switch(this.type)
nickjillings@1406 2176 {
nickjillings@1406 2177 case "text":
nickjillings@1406 2178 CQNode.textContent = this.question;
nickjillings@1406 2179 break;
nickjillings@1406 2180 case "radio":
nickjillings@1406 2181 var statement = root.createElement("statement");
nickjillings@1406 2182 statement.textContent = this.statement;
nickjillings@1406 2183 CQNode.appendChild(statement);
nickjillings@1406 2184 for (var i=0; i<this.options.length; i++)
nickjillings@1406 2185 {
nickjillings@1406 2186 var optionNode = root.createElement("option");
nickjillings@1406 2187 optionNode.setAttribute("name",this.options[i].name);
nickjillings@1406 2188 optionNode.textContent = this.options[i].text;
nickjillings@1406 2189 CQNode.appendChild(optionNode);
nickjillings@1404 2190 }
nickjillings@1406 2191 break;
nickjillings@1406 2192 case "checkbox":
nickjillings@1406 2193 var statement = root.createElement("statement");
nickjillings@1406 2194 statement.textContent = this.statement;
nickjillings@1406 2195 CQNode.appendChild(statement);
nickjillings@1406 2196 for (var i=0; i<this.options.length; i++)
nickjillings@1406 2197 {
nickjillings@1406 2198 var optionNode = root.createElement("option");
nickjillings@1406 2199 optionNode.setAttribute("name",this.options[i].name);
nickjillings@1406 2200 optionNode.textContent = this.options[i].text;
nickjillings@1406 2201 CQNode.appendChild(optionNode);
nickjillings@1406 2202 }
nickjillings@1406 2203 break;
nickjillings@1404 2204 }
nickjillings@1406 2205 return CQNode;
nickjillings@1406 2206 };
nickjillings@1406 2207 this.decode = function(xml) {
nickjillings@1406 2208 this.id = xml.id;
nickjillings@1406 2209 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
nickjillings@1406 2210 else {this.mandatory = false;}
nickjillings@1406 2211 this.type = xml.getAttribute('type');
nickjillings@1406 2212 if (this.type == undefined) {this.type = 'text';}
nickjillings@1406 2213 switch (this.type) {
nickjillings@1406 2214 case 'text':
nickjillings@1406 2215 this.question = xml.textContent;
nickjillings@1406 2216 break;
nickjillings@1406 2217 case 'radio':
nickjillings@1406 2218 var child = xml.firstElementChild;
nickjillings@1406 2219 this.options = [];
nickjillings@1406 2220 while (child != undefined) {
nickjillings@1406 2221 if (child.nodeName == 'statement' && this.statement == undefined) {
nickjillings@1406 2222 this.statement = child.textContent;
nickjillings@1406 2223 } else if (child.nodeName == 'option') {
nickjillings@1406 2224 var node = new this.childOption();
nickjillings@1406 2225 node.name = child.getAttribute('name');
nickjillings@1406 2226 node.text = child.textContent;
nickjillings@1406 2227 this.options.push(node);
nickjillings@1406 2228 }
nickjillings@1406 2229 child = child.nextElementSibling;
nickjillings@1404 2230 }
nickjillings@1406 2231 break;
nickjillings@1406 2232 case 'checkbox':
nickjillings@1406 2233 var child = xml.firstElementChild;
nickjillings@1406 2234 this.options = [];
nickjillings@1406 2235 while (child != undefined) {
nickjillings@1406 2236 if (child.nodeName == 'statement' && this.statement == undefined) {
nickjillings@1406 2237 this.statement = child.textContent;
nickjillings@1406 2238 } else if (child.nodeName == 'option') {
nickjillings@1406 2239 var node = new this.childOption();
nickjillings@1406 2240 node.name = child.getAttribute('name');
nickjillings@1406 2241 node.text = child.textContent;
nickjillings@1406 2242 this.options.push(node);
nickjillings@1406 2243 }
nickjillings@1406 2244 child = child.nextElementSibling;
nickjillings@1406 2245 }
nickjillings@1406 2246 break;
nickjillings@1404 2247 }
nickjillings@1406 2248 };
nickjillings@1404 2249 };
nickjillings@1404 2250 };
nickjillings@1404 2251 }
nickjillings@1406 2252
nickjillings@1404 2253 function Interface(specificationObject) {
nickjillings@1404 2254 // This handles the bindings between the interface and the audioEngineContext;
nickjillings@1404 2255 this.specification = specificationObject;
nickjillings@1404 2256 this.insertPoint = document.getElementById("topLevelBody");
nickjillings@1404 2257
nickjillings@1407 2258 this.newPage = function(audioHolderObject)
nickjillings@1407 2259 {
nickjillings@1407 2260 audioEngineContext.newTestPage();
nickjillings@1407 2261 /// CHECK FOR SAMPLE RATE COMPATIBILITY
nickjillings@1407 2262 if (audioHolderObject.sampleRate != undefined) {
nickjillings@1407 2263 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
nickjillings@1407 2264 var errStr = 'Sample rates do not match! Requested '+Number(audioHolderObject.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nickjillings@1407 2265 alert(errStr);
nickjillings@1407 2266 return;
nickjillings@1407 2267 }
nickjillings@1407 2268 }
nickjillings@1407 2269
nickjillings@1407 2270 audioEngineContext.loopPlayback = audioHolderObject.loop;
nickjillings@1407 2271 // Delete any previous audioObjects associated with the audioEngine
nickjillings@1407 2272 audioEngineContext.audioObjects = [];
nickjillings@1407 2273 interfaceContext.deleteCommentBoxes();
nickjillings@1407 2274 interfaceContext.deleteCommentQuestions();
nickjillings@1407 2275 loadTest(audioHolderObject);
nickjillings@1407 2276 };
nickjillings@1407 2277
nickjillings@1404 2278 // Bounded by interface!!
nickjillings@1404 2279 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
nickjillings@1404 2280 // For example, APE returns the slider position normalised in a <value> tag.
nickjillings@1404 2281 this.interfaceObjects = [];
nickjillings@1404 2282 this.interfaceObject = function(){};
nickjillings@1404 2283
nickjillings@1404 2284 this.resizeWindow = function(event)
nickjillings@1404 2285 {
nickjillings@1421 2286 popup.resize(event);
nickjillings@1404 2287 for(var i=0; i<this.commentBoxes.length; i++)
nickjillings@1404 2288 {this.commentBoxes[i].resize();}
nickjillings@1404 2289 for(var i=0; i<this.commentQuestions.length; i++)
nickjillings@1404 2290 {this.commentQuestions[i].resize();}
nickjillings@1404 2291 try
nickjillings@1404 2292 {
nickjillings@1404 2293 resizeWindow(event);
nickjillings@1404 2294 }
nickjillings@1404 2295 catch(err)
nickjillings@1404 2296 {
nickjillings@1404 2297 console.log("Warning - Interface does not have Resize option");
nickjillings@1404 2298 console.log(err);
nickjillings@1404 2299 }
nickjillings@1404 2300 };
nickjillings@1404 2301
nickjillings@1404 2302 this.returnNavigator = function()
nickjillings@1404 2303 {
nickjillings@1404 2304 var node = document.createElement("navigator");
nickjillings@1404 2305 var platform = document.createElement("platform");
nickjillings@1404 2306 platform.textContent = navigator.platform;
nickjillings@1404 2307 var vendor = document.createElement("vendor");
nickjillings@1404 2308 vendor.textContent = navigator.vendor;
nickjillings@1404 2309 var userAgent = document.createElement("uagent");
nickjillings@1404 2310 userAgent.textContent = navigator.userAgent;
nickjillings@1404 2311 node.appendChild(platform);
nickjillings@1404 2312 node.appendChild(vendor);
nickjillings@1404 2313 node.appendChild(userAgent);
nickjillings@1404 2314 return node;
nickjillings@1404 2315 };
nickjillings@1404 2316
nickjillings@1404 2317 this.commentBoxes = [];
nickjillings@1404 2318 this.elementCommentBox = function(audioObject) {
nickjillings@1404 2319 var element = audioObject.specification;
nickjillings@1404 2320 this.audioObject = audioObject;
nickjillings@1404 2321 this.id = audioObject.id;
nickjillings@1404 2322 var audioHolderObject = audioObject.specification.parent;
nickjillings@1404 2323 // Create document objects to hold the comment boxes
nickjillings@1404 2324 this.trackComment = document.createElement('div');
nickjillings@1404 2325 this.trackComment.className = 'comment-div';
nickjillings@1404 2326 this.trackComment.id = 'comment-div-'+audioObject.id;
nickjillings@1404 2327 // Create a string next to each comment asking for a comment
nickjillings@1404 2328 this.trackString = document.createElement('span');
nickjillings@1404 2329 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
nickjillings@1404 2330 // Create the HTML5 comment box 'textarea'
nickjillings@1404 2331 this.trackCommentBox = document.createElement('textarea');
nickjillings@1404 2332 this.trackCommentBox.rows = '4';
nickjillings@1404 2333 this.trackCommentBox.cols = '100';
nickjillings@1404 2334 this.trackCommentBox.name = 'trackComment'+audioObject.id;
nickjillings@1404 2335 this.trackCommentBox.className = 'trackComment';
nickjillings@1404 2336 var br = document.createElement('br');
nickjillings@1404 2337 // Add to the holder.
nickjillings@1404 2338 this.trackComment.appendChild(this.trackString);
nickjillings@1404 2339 this.trackComment.appendChild(br);
nickjillings@1404 2340 this.trackComment.appendChild(this.trackCommentBox);
nickjillings@1404 2341
nickjillings@1404 2342 this.exportXMLDOM = function() {
nickjillings@1404 2343 var root = document.createElement('comment');
nickjillings@1404 2344 if (this.audioObject.specification.parent.elementComments) {
nickjillings@1404 2345 var question = document.createElement('question');
nickjillings@1404 2346 question.textContent = this.trackString.textContent;
nickjillings@1404 2347 var response = document.createElement('response');
nickjillings@1404 2348 response.textContent = this.trackCommentBox.value;
nickjillings@1404 2349 console.log("Comment frag-"+this.id+": "+response.textContent);
nickjillings@1404 2350 root.appendChild(question);
nickjillings@1404 2351 root.appendChild(response);
nickjillings@1404 2352 }
nickjillings@1404 2353 return root;
nickjillings@1404 2354 };
nickjillings@1404 2355 this.resize = function()
nickjillings@1404 2356 {
nickjillings@1404 2357 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1404 2358 if (boxwidth >= 600)
nickjillings@1404 2359 {
nickjillings@1404 2360 boxwidth = 600;
nickjillings@1404 2361 }
nickjillings@1404 2362 else if (boxwidth < 400)
nickjillings@1404 2363 {
nickjillings@1404 2364 boxwidth = 400;
nickjillings@1404 2365 }
nickjillings@1404 2366 this.trackComment.style.width = boxwidth+"px";
nickjillings@1404 2367 this.trackCommentBox.style.width = boxwidth-6+"px";
nickjillings@1404 2368 };
nickjillings@1404 2369 this.resize();
nickjillings@1404 2370 };
nickjillings@1404 2371
nickjillings@1404 2372 this.commentQuestions = [];
nickjillings@1404 2373
nickjillings@1404 2374 this.commentBox = function(commentQuestion) {
nickjillings@1404 2375 this.specification = commentQuestion;
nickjillings@1404 2376 // Create document objects to hold the comment boxes
nickjillings@1404 2377 this.holder = document.createElement('div');
nickjillings@1404 2378 this.holder.className = 'comment-div';
nickjillings@1404 2379 // Create a string next to each comment asking for a comment
nickjillings@1404 2380 this.string = document.createElement('span');
nickjillings@1404 2381 this.string.innerHTML = commentQuestion.question;
nickjillings@1404 2382 // Create the HTML5 comment box 'textarea'
nickjillings@1404 2383 this.textArea = document.createElement('textarea');
nickjillings@1404 2384 this.textArea.rows = '4';
nickjillings@1404 2385 this.textArea.cols = '100';
nickjillings@1404 2386 this.textArea.className = 'trackComment';
nickjillings@1404 2387 var br = document.createElement('br');
nickjillings@1404 2388 // Add to the holder.
nickjillings@1404 2389 this.holder.appendChild(this.string);
nickjillings@1404 2390 this.holder.appendChild(br);
nickjillings@1404 2391 this.holder.appendChild(this.textArea);
nickjillings@1404 2392
nickjillings@1404 2393 this.exportXMLDOM = function() {
nickjillings@1404 2394 var root = document.createElement('comment');
nickjillings@1404 2395 root.id = this.specification.id;
nickjillings@1404 2396 root.setAttribute('type',this.specification.type);
nickjillings@1404 2397 root.textContent = this.textArea.value;
nickjillings@1404 2398 console.log("Question: "+this.string.textContent);
nickjillings@1404 2399 console.log("Response: "+root.textContent);
nickjillings@1404 2400 return root;
nickjillings@1404 2401 };
nickjillings@1404 2402 this.resize = function()
nickjillings@1404 2403 {
nickjillings@1404 2404 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1404 2405 if (boxwidth >= 600)
nickjillings@1404 2406 {
nickjillings@1404 2407 boxwidth = 600;
nickjillings@1404 2408 }
nickjillings@1404 2409 else if (boxwidth < 400)
nickjillings@1404 2410 {
nickjillings@1404 2411 boxwidth = 400;
nickjillings@1404 2412 }
nickjillings@1404 2413 this.holder.style.width = boxwidth+"px";
nickjillings@1404 2414 this.textArea.style.width = boxwidth-6+"px";
nickjillings@1404 2415 };
nickjillings@1404 2416 this.resize();
nickjillings@1404 2417 };
nickjillings@1404 2418
nickjillings@1404 2419 this.radioBox = function(commentQuestion) {
nickjillings@1404 2420 this.specification = commentQuestion;
nickjillings@1404 2421 // Create document objects to hold the comment boxes
nickjillings@1404 2422 this.holder = document.createElement('div');
nickjillings@1404 2423 this.holder.className = 'comment-div';
nickjillings@1404 2424 // Create a string next to each comment asking for a comment
nickjillings@1404 2425 this.string = document.createElement('span');
nickjillings@1404 2426 this.string.innerHTML = commentQuestion.statement;
nickjillings@1404 2427 var br = document.createElement('br');
nickjillings@1404 2428 // Add to the holder.
nickjillings@1404 2429 this.holder.appendChild(this.string);
nickjillings@1404 2430 this.holder.appendChild(br);
nickjillings@1404 2431 this.options = [];
nickjillings@1404 2432 this.inputs = document.createElement('div');
nickjillings@1404 2433 this.span = document.createElement('div');
nickjillings@1404 2434 this.inputs.align = 'center';
nickjillings@1404 2435 this.inputs.style.marginLeft = '12px';
nickjillings@1404 2436 this.span.style.marginLeft = '12px';
nickjillings@1404 2437 this.span.align = 'center';
nickjillings@1404 2438 this.span.style.marginTop = '15px';
nickjillings@1404 2439
nickjillings@1404 2440 var optCount = commentQuestion.options.length;
nickjillings@1404 2441 for (var i=0; i<optCount; i++)
nickjillings@1404 2442 {
nickjillings@1404 2443 var div = document.createElement('div');
nickjillings@1404 2444 div.style.width = '80px';
nickjillings@1404 2445 div.style.float = 'left';
nickjillings@1404 2446 var input = document.createElement('input');
nickjillings@1404 2447 input.type = 'radio';
nickjillings@1404 2448 input.name = commentQuestion.id;
nickjillings@1404 2449 input.setAttribute('setvalue',commentQuestion.options[i].name);
nickjillings@1404 2450 input.className = 'comment-radio';
nickjillings@1404 2451 div.appendChild(input);
nickjillings@1404 2452 this.inputs.appendChild(div);
nickjillings@1404 2453
nickjillings@1404 2454
nickjillings@1404 2455 div = document.createElement('div');
nickjillings@1404 2456 div.style.width = '80px';
nickjillings@1404 2457 div.style.float = 'left';
nickjillings@1404 2458 div.align = 'center';
nickjillings@1404 2459 var span = document.createElement('span');
nickjillings@1404 2460 span.textContent = commentQuestion.options[i].text;
nickjillings@1404 2461 span.className = 'comment-radio-span';
nickjillings@1404 2462 div.appendChild(span);
nickjillings@1404 2463 this.span.appendChild(div);
nickjillings@1404 2464 this.options.push(input);
nickjillings@1404 2465 }
nickjillings@1404 2466 this.holder.appendChild(this.span);
nickjillings@1404 2467 this.holder.appendChild(this.inputs);
nickjillings@1404 2468
nickjillings@1404 2469 this.exportXMLDOM = function() {
nickjillings@1404 2470 var root = document.createElement('comment');
nickjillings@1404 2471 root.id = this.specification.id;
nickjillings@1404 2472 root.setAttribute('type',this.specification.type);
nickjillings@1404 2473 var question = document.createElement('question');
nickjillings@1404 2474 question.textContent = this.string.textContent;
nickjillings@1404 2475 var response = document.createElement('response');
nickjillings@1404 2476 var i=0;
nickjillings@1404 2477 while(this.options[i].checked == false) {
nickjillings@1404 2478 i++;
nickjillings@1404 2479 if (i >= this.options.length) {
nickjillings@1404 2480 break;
nickjillings@1404 2481 }
nickjillings@1404 2482 }
nickjillings@1404 2483 if (i >= this.options.length) {
nickjillings@1404 2484 response.textContent = 'null';
nickjillings@1404 2485 } else {
nickjillings@1404 2486 response.textContent = this.options[i].getAttribute('setvalue');
nickjillings@1404 2487 response.setAttribute('number',i);
nickjillings@1404 2488 }
nickjillings@1404 2489 console.log('Comment: '+question.textContent);
nickjillings@1404 2490 console.log('Response: '+response.textContent);
nickjillings@1404 2491 root.appendChild(question);
nickjillings@1404 2492 root.appendChild(response);
nickjillings@1404 2493 return root;
nickjillings@1404 2494 };
nickjillings@1404 2495 this.resize = function()
nickjillings@1404 2496 {
nickjillings@1404 2497 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1404 2498 if (boxwidth >= 600)
nickjillings@1404 2499 {
nickjillings@1404 2500 boxwidth = 600;
nickjillings@1404 2501 }
nickjillings@1404 2502 else if (boxwidth < 400)
nickjillings@1404 2503 {
nickjillings@1404 2504 boxwidth = 400;
nickjillings@1404 2505 }
nickjillings@1404 2506 this.holder.style.width = boxwidth+"px";
nickjillings@1404 2507 var text = this.holder.children[2];
nickjillings@1404 2508 var options = this.holder.children[3];
nickjillings@1404 2509 var optCount = options.children.length;
nickjillings@1404 2510 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
nickjillings@1404 2511 var options = options.firstChild;
nickjillings@1404 2512 var text = text.firstChild;
nickjillings@1404 2513 options.style.marginRight = spanMargin;
nickjillings@1404 2514 options.style.marginLeft = spanMargin;
nickjillings@1404 2515 text.style.marginRight = spanMargin;
nickjillings@1404 2516 text.style.marginLeft = spanMargin;
nickjillings@1404 2517 while(options.nextSibling != undefined)
nickjillings@1404 2518 {
nickjillings@1404 2519 options = options.nextSibling;
nickjillings@1404 2520 text = text.nextSibling;
nickjillings@1404 2521 options.style.marginRight = spanMargin;
nickjillings@1404 2522 options.style.marginLeft = spanMargin;
nickjillings@1404 2523 text.style.marginRight = spanMargin;
nickjillings@1404 2524 text.style.marginLeft = spanMargin;
nickjillings@1404 2525 }
nickjillings@1404 2526 };
nickjillings@1404 2527 this.resize();
nickjillings@1404 2528 };
nickjillings@1404 2529
nickjillings@1404 2530 this.checkboxBox = function(commentQuestion) {
nickjillings@1404 2531 this.specification = commentQuestion;
nickjillings@1404 2532 // Create document objects to hold the comment boxes
nickjillings@1404 2533 this.holder = document.createElement('div');
nickjillings@1404 2534 this.holder.className = 'comment-div';
nickjillings@1404 2535 // Create a string next to each comment asking for a comment
nickjillings@1404 2536 this.string = document.createElement('span');
nickjillings@1404 2537 this.string.innerHTML = commentQuestion.statement;
nickjillings@1404 2538 var br = document.createElement('br');
nickjillings@1404 2539 // Add to the holder.
nickjillings@1404 2540 this.holder.appendChild(this.string);
nickjillings@1404 2541 this.holder.appendChild(br);
nickjillings@1404 2542 this.options = [];
nickjillings@1404 2543 this.inputs = document.createElement('div');
nickjillings@1404 2544 this.span = document.createElement('div');
nickjillings@1404 2545 this.inputs.align = 'center';
nickjillings@1404 2546 this.inputs.style.marginLeft = '12px';
nickjillings@1404 2547 this.span.style.marginLeft = '12px';
nickjillings@1404 2548 this.span.align = 'center';
nickjillings@1404 2549 this.span.style.marginTop = '15px';
nickjillings@1404 2550
nickjillings@1404 2551 var optCount = commentQuestion.options.length;
nickjillings@1404 2552 for (var i=0; i<optCount; i++)
nickjillings@1404 2553 {
nickjillings@1404 2554 var div = document.createElement('div');
nickjillings@1404 2555 div.style.width = '80px';
nickjillings@1404 2556 div.style.float = 'left';
nickjillings@1404 2557 var input = document.createElement('input');
nickjillings@1404 2558 input.type = 'checkbox';
nickjillings@1404 2559 input.name = commentQuestion.id;
nickjillings@1404 2560 input.setAttribute('setvalue',commentQuestion.options[i].name);
nickjillings@1404 2561 input.className = 'comment-radio';
nickjillings@1404 2562 div.appendChild(input);
nickjillings@1404 2563 this.inputs.appendChild(div);
nickjillings@1404 2564
nickjillings@1404 2565
nickjillings@1404 2566 div = document.createElement('div');
nickjillings@1404 2567 div.style.width = '80px';
nickjillings@1404 2568 div.style.float = 'left';
nickjillings@1404 2569 div.align = 'center';
nickjillings@1404 2570 var span = document.createElement('span');
nickjillings@1404 2571 span.textContent = commentQuestion.options[i].text;
nickjillings@1404 2572 span.className = 'comment-radio-span';
nickjillings@1404 2573 div.appendChild(span);
nickjillings@1404 2574 this.span.appendChild(div);
nickjillings@1404 2575 this.options.push(input);
nickjillings@1404 2576 }
nickjillings@1404 2577 this.holder.appendChild(this.span);
nickjillings@1404 2578 this.holder.appendChild(this.inputs);
nickjillings@1404 2579
nickjillings@1404 2580 this.exportXMLDOM = function() {
nickjillings@1404 2581 var root = document.createElement('comment');
nickjillings@1404 2582 root.id = this.specification.id;
nickjillings@1404 2583 root.setAttribute('type',this.specification.type);
nickjillings@1404 2584 var question = document.createElement('question');
nickjillings@1404 2585 question.textContent = this.string.textContent;
nickjillings@1404 2586 root.appendChild(question);
nickjillings@1404 2587 console.log('Comment: '+question.textContent);
nickjillings@1404 2588 for (var i=0; i<this.options.length; i++) {
nickjillings@1404 2589 var response = document.createElement('response');
nickjillings@1404 2590 response.textContent = this.options[i].checked;
nickjillings@1404 2591 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
nickjillings@1404 2592 root.appendChild(response);
nickjillings@1404 2593 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
nickjillings@1404 2594 }
nickjillings@1404 2595 return root;
nickjillings@1404 2596 };
nickjillings@1404 2597 this.resize = function()
nickjillings@1404 2598 {
nickjillings@1404 2599 var boxwidth = (window.innerWidth-100)/2;
nickjillings@1404 2600 if (boxwidth >= 600)
nickjillings@1404 2601 {
nickjillings@1404 2602 boxwidth = 600;
nickjillings@1404 2603 }
nickjillings@1404 2604 else if (boxwidth < 400)
nickjillings@1404 2605 {
nickjillings@1404 2606 boxwidth = 400;
nickjillings@1404 2607 }
nickjillings@1404 2608 this.holder.style.width = boxwidth+"px";
nickjillings@1404 2609 var text = this.holder.children[2];
nickjillings@1404 2610 var options = this.holder.children[3];
nickjillings@1404 2611 var optCount = options.children.length;
nickjillings@1404 2612 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
nickjillings@1404 2613 var options = options.firstChild;
nickjillings@1404 2614 var text = text.firstChild;
nickjillings@1404 2615 options.style.marginRight = spanMargin;
nickjillings@1404 2616 options.style.marginLeft = spanMargin;
nickjillings@1404 2617 text.style.marginRight = spanMargin;
nickjillings@1404 2618 text.style.marginLeft = spanMargin;
nickjillings@1404 2619 while(options.nextSibling != undefined)
nickjillings@1404 2620 {
nickjillings@1404 2621 options = options.nextSibling;
nickjillings@1404 2622 text = text.nextSibling;
nickjillings@1404 2623 options.style.marginRight = spanMargin;
nickjillings@1404 2624 options.style.marginLeft = spanMargin;
nickjillings@1404 2625 text.style.marginRight = spanMargin;
nickjillings@1404 2626 text.style.marginLeft = spanMargin;
nickjillings@1404 2627 }
nickjillings@1404 2628 };
nickjillings@1404 2629 this.resize();
nickjillings@1404 2630 };
nickjillings@1404 2631
nickjillings@1404 2632 this.createCommentBox = function(audioObject) {
nickjillings@1404 2633 var node = new this.elementCommentBox(audioObject);
nickjillings@1404 2634 this.commentBoxes.push(node);
nickjillings@1404 2635 audioObject.commentDOM = node;
nickjillings@1404 2636 return node;
nickjillings@1404 2637 };
nickjillings@1404 2638
nickjillings@1404 2639 this.sortCommentBoxes = function() {
nickjillings@1404 2640 var holder = [];
nickjillings@1404 2641 while (this.commentBoxes.length > 0) {
nickjillings@1404 2642 var node = this.commentBoxes.pop(0);
nickjillings@1404 2643 holder[node.id] = node;
nickjillings@1404 2644 }
nickjillings@1404 2645 this.commentBoxes = holder;
nickjillings@1404 2646 };
nickjillings@1404 2647
nickjillings@1404 2648 this.showCommentBoxes = function(inject, sort) {
nickjillings@1404 2649 if (sort) {interfaceContext.sortCommentBoxes();}
nickjillings@1404 2650 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
nickjillings@1404 2651 inject.appendChild(this.commentBoxes[i].trackComment);
nickjillings@1404 2652 }
nickjillings@1404 2653 };
nickjillings@1404 2654
nickjillings@1404 2655 this.deleteCommentBoxes = function() {
nickjillings@1404 2656 this.commentBoxes = [];
nickjillings@1404 2657 };
nickjillings@1404 2658
nickjillings@1404 2659 this.createCommentQuestion = function(element) {
nickjillings@1404 2660 var node;
nickjillings@1404 2661 if (element.type == 'text') {
nickjillings@1404 2662 node = new this.commentBox(element);
nickjillings@1404 2663 } else if (element.type == 'radio') {
nickjillings@1404 2664 node = new this.radioBox(element);
nickjillings@1404 2665 } else if (element.type == 'checkbox') {
nickjillings@1404 2666 node = new this.checkboxBox(element);
nickjillings@1404 2667 }
nickjillings@1404 2668 this.commentQuestions.push(node);
nickjillings@1404 2669 return node;
nickjillings@1404 2670 };
nickjillings@1404 2671
nickjillings@1404 2672 this.deleteCommentQuestions = function()
nickjillings@1404 2673 {
nickjillings@1404 2674 this.commentQuestions = [];
nickjillings@1404 2675 };
nickjillings@1404 2676
nickjillings@1404 2677 this.playhead = new function()
nickjillings@1404 2678 {
nickjillings@1404 2679 this.object = document.createElement('div');
nickjillings@1404 2680 this.object.className = 'playhead';
nickjillings@1404 2681 this.object.align = 'left';
nickjillings@1404 2682 var curTime = document.createElement('div');
nickjillings@1404 2683 curTime.style.width = '50px';
nickjillings@1404 2684 this.curTimeSpan = document.createElement('span');
nickjillings@1404 2685 this.curTimeSpan.textContent = '00:00';
nickjillings@1404 2686 curTime.appendChild(this.curTimeSpan);
nickjillings@1404 2687 this.object.appendChild(curTime);
nickjillings@1404 2688 this.scrubberTrack = document.createElement('div');
nickjillings@1404 2689 this.scrubberTrack.className = 'playhead-scrub-track';
nickjillings@1404 2690
nickjillings@1404 2691 this.scrubberHead = document.createElement('div');
nickjillings@1404 2692 this.scrubberHead.id = 'playhead-scrubber';
nickjillings@1404 2693 this.scrubberTrack.appendChild(this.scrubberHead);
nickjillings@1404 2694 this.object.appendChild(this.scrubberTrack);
nickjillings@1404 2695
nickjillings@1404 2696 this.timePerPixel = 0;
nickjillings@1404 2697 this.maxTime = 0;
nickjillings@1404 2698
nickjillings@1404 2699 this.playbackObject;
nickjillings@1404 2700
nickjillings@1404 2701 this.setTimePerPixel = function(audioObject) {
nickjillings@1404 2702 //maxTime must be in seconds
nickjillings@1404 2703 this.playbackObject = audioObject;
nickjillings@1410 2704 this.maxTime = audioObject.buffer.buffer.duration;
nickjillings@1404 2705 var width = 490; //500 - 10, 5 each side of the tracker head
nickjillings@1404 2706 this.timePerPixel = this.maxTime/490;
nickjillings@1404 2707 if (this.maxTime < 60) {
nickjillings@1404 2708 this.curTimeSpan.textContent = '0.00';
nickjillings@1404 2709 } else {
nickjillings@1404 2710 this.curTimeSpan.textContent = '00:00';
nickjillings@1404 2711 }
nickjillings@1404 2712 };
nickjillings@1404 2713
nickjillings@1404 2714 this.update = function() {
nickjillings@1404 2715 // Update the playhead position, startPlay must be called
nickjillings@1404 2716 if (this.timePerPixel > 0) {
nickjillings@1404 2717 var time = this.playbackObject.getCurrentPosition();
nickjillings@1404 2718 if (time > 0) {
nickjillings@1404 2719 var width = 490;
nickjillings@1404 2720 var pix = Math.floor(time/this.timePerPixel);
nickjillings@1404 2721 this.scrubberHead.style.left = pix+'px';
nickjillings@1404 2722 if (this.maxTime > 60.0) {
nickjillings@1404 2723 var secs = time%60;
nickjillings@1404 2724 var mins = Math.floor((time-secs)/60);
nickjillings@1404 2725 secs = secs.toString();
nickjillings@1404 2726 secs = secs.substr(0,2);
nickjillings@1404 2727 mins = mins.toString();
nickjillings@1404 2728 this.curTimeSpan.textContent = mins+':'+secs;
nickjillings@1404 2729 } else {
nickjillings@1404 2730 time = time.toString();
nickjillings@1404 2731 this.curTimeSpan.textContent = time.substr(0,4);
nickjillings@1404 2732 }
nickjillings@1404 2733 } else {
nickjillings@1404 2734 this.scrubberHead.style.left = '0px';
nickjillings@1404 2735 if (this.maxTime < 60) {
nickjillings@1404 2736 this.curTimeSpan.textContent = '0.00';
nickjillings@1404 2737 } else {
nickjillings@1404 2738 this.curTimeSpan.textContent = '00:00';
nickjillings@1404 2739 }
nickjillings@1404 2740 }
nickjillings@1404 2741 }
nickjillings@1404 2742 };
nickjillings@1404 2743
nickjillings@1404 2744 this.interval = undefined;
nickjillings@1404 2745
nickjillings@1404 2746 this.start = function() {
nickjillings@1404 2747 if (this.playbackObject != undefined && this.interval == undefined) {
nickjillings@1404 2748 if (this.maxTime < 60) {
nickjillings@1404 2749 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
nickjillings@1404 2750 } else {
nickjillings@1404 2751 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
nickjillings@1404 2752 }
nickjillings@1404 2753 }
nickjillings@1404 2754 };
nickjillings@1404 2755 this.stop = function() {
nickjillings@1404 2756 clearInterval(this.interval);
nickjillings@1404 2757 this.interval = undefined;
nickjillings@1404 2758 if (this.maxTime < 60) {
nickjillings@1404 2759 this.curTimeSpan.textContent = '0.00';
nickjillings@1404 2760 } else {
nickjillings@1404 2761 this.curTimeSpan.textContent = '00:00';
nickjillings@1404 2762 }
nickjillings@1404 2763 };
nickjillings@1404 2764 };
nickjillings@1404 2765
nickjillings@1404 2766 // Global Checkers
nickjillings@1404 2767 // These functions will help enforce the checkers
nickjillings@1404 2768 this.checkHiddenAnchor = function()
nickjillings@1404 2769 {
nickjillings@1404 2770 var audioHolder = testState.currentStateMap[testState.currentIndex];
nickjillings@1404 2771 if (audioHolder.anchorId != null)
nickjillings@1404 2772 {
nickjillings@1404 2773 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId];
nickjillings@1404 2774 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
nickjillings@1404 2775 {
nickjillings@1404 2776 // Anchor is not set below
nickjillings@1404 2777 console.log('Anchor node not below marker value');
nickjillings@1404 2778 alert('Please keep listening');
nickjillings@1404 2779 return false;
nickjillings@1404 2780 }
nickjillings@1404 2781 }
nickjillings@1404 2782 return true;
nickjillings@1404 2783 };
nickjillings@1404 2784
nickjillings@1404 2785 this.checkHiddenReference = function()
nickjillings@1404 2786 {
nickjillings@1404 2787 var audioHolder = testState.currentStateMap[testState.currentIndex];
nickjillings@1404 2788 if (audioHolder.referenceId != null)
nickjillings@1404 2789 {
nickjillings@1404 2790 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId];
nickjillings@1404 2791 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
nickjillings@1404 2792 {
nickjillings@1404 2793 // Anchor is not set below
nickjillings@1404 2794 console.log('Reference node not above marker value');
nickjillings@1404 2795 alert('Please keep listening');
nickjillings@1404 2796 return false;
nickjillings@1404 2797 }
nickjillings@1404 2798 }
nickjillings@1404 2799 return true;
nickjillings@1404 2800 };
nickjillings@1404 2801
nickjillings@1404 2802 this.checkFragmentsFullyPlayed = function ()
nickjillings@1404 2803 {
nickjillings@1404 2804 // Checks the entire file has been played back
nickjillings@1404 2805 // NOTE ! This will return true IF playback is Looped!!!
nickjillings@1404 2806 if (audioEngineContext.loopPlayback)
nickjillings@1404 2807 {
nickjillings@1404 2808 console.log("WARNING - Looped source: Cannot check fragments are fully played");
nickjillings@1404 2809 return true;
nickjillings@1404 2810 }
nickjillings@1404 2811 var check_pass = true;
nickjillings@1404 2812 var error_obj = [];
nickjillings@1404 2813 for (var i = 0; i<audioEngineContext.audioObjects.length; i++)
nickjillings@1404 2814 {
nickjillings@1404 2815 var object = audioEngineContext.audioObjects[i];
nickjillings@1393 2816 var time = object.buffer.buffer.duration;
nickjillings@1404 2817 var metric = object.metric;
nickjillings@1404 2818 var passed = false;
nickjillings@1404 2819 for (var j=0; j<metric.listenTracker.length; j++)
nickjillings@1404 2820 {
nickjillings@1404 2821 var bt = metric.listenTracker[j].getElementsByTagName('buffertime');
nickjillings@1404 2822 var start_time = Number(bt[0].getAttribute('start'));
nickjillings@1404 2823 var stop_time = Number(bt[0].getAttribute('stop'));
nickjillings@1404 2824 var delta = stop_time - start_time;
nickjillings@1404 2825 if (delta >= time)
nickjillings@1404 2826 {
nickjillings@1404 2827 passed = true;
nickjillings@1404 2828 break;
nickjillings@1404 2829 }
nickjillings@1404 2830 }
nickjillings@1404 2831 if (passed == false)
nickjillings@1404 2832 {
nickjillings@1404 2833 check_pass = false;
nickjillings@1404 2834 console.log("Continue listening to track-"+i);
nickjillings@1404 2835 error_obj.push(i);
nickjillings@1404 2836 }
nickjillings@1404 2837 }
nickjillings@1404 2838 if (check_pass == false)
nickjillings@1404 2839 {
nickjillings@1393 2840 var str_start = "You have not completely listened to fragments ";
nickjillings@1404 2841 for (var i=0; i<error_obj.length; i++)
nickjillings@1404 2842 {
nickjillings@1404 2843 str_start += error_obj[i];
nickjillings@1404 2844 if (i != error_obj.length-1)
nickjillings@1404 2845 {
nickjillings@1404 2846 str_start += ', ';
nickjillings@1404 2847 }
nickjillings@1404 2848 }
nickjillings@1404 2849 str_start += ". Please keep listening";
nickjillings@1404 2850 console.log("[ALERT]: "+str_start);
nickjillings@1404 2851 alert(str_start);
nickjillings@1404 2852 }
nickjillings@1404 2853 };
nickjillings@1399 2854 this.checkAllMoved = function()
nickjillings@1399 2855 {
nickjillings@1399 2856 var str = "You have not moved ";
nickjillings@1399 2857 var failed = [];
nickjillings@1399 2858 for (var i in audioEngineContext.audioObjects)
nickjillings@1399 2859 {
nickjillings@1399 2860 if(audioEngineContext.audioObjects[i].metric.wasMoved == false)
nickjillings@1399 2861 {
nickjillings@1399 2862 failed.push(audioEngineContext.audioObjects[i].id);
nickjillings@1399 2863 }
nickjillings@1399 2864 }
nickjillings@1399 2865 if (failed.length == 0)
nickjillings@1399 2866 {
nickjillings@1399 2867 return true;
nickjillings@1399 2868 } else if (failed.length == 1)
nickjillings@1399 2869 {
nickjillings@1399 2870 str += 'track '+failed[0];
nickjillings@1399 2871 } else {
nickjillings@1399 2872 str += 'tracks ';
nickjillings@1399 2873 for (var i=0; i<failed.length-1; i++)
nickjillings@1399 2874 {
nickjillings@1399 2875 str += failed[i]+', ';
nickjillings@1399 2876 }
nickjillings@1399 2877 str += 'and '+failed[i];
nickjillings@1399 2878 }
nickjillings@1399 2879 str +='.';
nickjillings@1399 2880 alert(str);
nickjillings@1399 2881 console.log(str);
nickjillings@1399 2882 return false;
nickjillings@1399 2883 };
nickjillings@1399 2884 this.checkAllPlayed = function()
nickjillings@1399 2885 {
nickjillings@1399 2886 var str = "You have not played ";
nickjillings@1399 2887 var failed = [];
nickjillings@1399 2888 for (var i in audioEngineContext.audioObjects)
nickjillings@1399 2889 {
nickjillings@1399 2890 if(audioEngineContext.audioObjects[i].metric.wasListenedTo == false)
nickjillings@1399 2891 {
nickjillings@1399 2892 failed.push(audioEngineContext.audioObjects[i].id);
nickjillings@1399 2893 }
nickjillings@1399 2894 }
nickjillings@1399 2895 if (failed.length == 0)
nickjillings@1399 2896 {
nickjillings@1399 2897 return true;
nickjillings@1399 2898 } else if (failed.length == 1)
nickjillings@1399 2899 {
nickjillings@1399 2900 str += 'track '+failed[0];
nickjillings@1399 2901 } else {
nickjillings@1399 2902 str += 'tracks ';
nickjillings@1399 2903 for (var i=0; i<failed.length-1; i++)
nickjillings@1399 2904 {
nickjillings@1399 2905 str += failed[i]+', ';
nickjillings@1399 2906 }
nickjillings@1399 2907 str += 'and '+failed[i];
nickjillings@1399 2908 }
nickjillings@1399 2909 str +='.';
nickjillings@1399 2910 alert(str);
nickjillings@1399 2911 console.log(str);
nickjillings@1399 2912 return false;
nickjillings@1399 2913 };
nickjillings@1404 2914 }