annotate core.js @ 459:f6c9f9e5400e Dev_main

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