annotate core.js @ 453:44a6fe06e71a Dev_main

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