annotate core.js @ 470:1330c77d212c Dev_main

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