annotate core.js @ 472:23f77550b842 Dev_main

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