annotate core.js @ 901:573810a04b0f

Merge from the default branch
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Mon, 08 Jun 2015 11:01:21 +0100
parents
children 4041e5abcde5
rev   line source
n@901 1 /**
n@901 2 * core.js
n@901 3 *
n@901 4 * Main script to run, calls all other core functions and manages loading/store to backend.
n@901 5 * Also contains all global variables.
n@901 6 */
n@901 7
n@901 8 /* create the web audio API context and store in audioContext*/
n@901 9 var audioContext; // Hold the browser web audio API
n@901 10 var projectXML; // Hold the parsed setup XML
n@901 11 var specification;
n@901 12 var interfaceContext;
n@901 13 var popup; // Hold the interfacePopup object
n@901 14 var testState;
n@901 15 var currentTrackOrder = []; // Hold the current XML tracks in their (randomised) order
n@901 16 var audioEngineContext; // The custome AudioEngine object
n@901 17 var projectReturn; // Hold the URL for the return
n@901 18
n@901 19
n@901 20 // Add a prototype to the bufferSourceNode to reference to the audioObject holding it
n@901 21 AudioBufferSourceNode.prototype.owner = undefined;
n@901 22
n@901 23 window.onload = function() {
n@901 24 // Function called once the browser has loaded all files.
n@901 25 // This should perform any initial commands such as structure / loading documents
n@901 26
n@901 27 // Create a web audio API context
n@901 28 // Fixed for cross-browser support
n@901 29 var AudioContext = window.AudioContext || window.webkitAudioContext;
n@901 30 audioContext = new AudioContext;
n@901 31
n@901 32 // Create test state
n@901 33 testState = new stateMachine();
n@901 34
n@901 35 // Create the audio engine object
n@901 36 audioEngineContext = new AudioEngine();
n@901 37
n@901 38 // Create the popup interface object
n@901 39 popup = new interfacePopup();
n@901 40
n@901 41 // Create the specification object
n@901 42 specification = new Specification();
n@901 43
n@901 44 // Create the interface object
n@901 45 interfaceContext = new Interface(specification);
n@901 46 };
n@901 47
n@901 48 function interfacePopup() {
n@901 49 // Creates an object to manage the popup
n@901 50 this.popup = null;
n@901 51 this.popupContent = null;
n@901 52 this.popupButton = null;
n@901 53 this.popupOptions = null;
n@901 54 this.currentIndex = null;
n@901 55 this.responses = null;
n@901 56
n@901 57 this.createPopup = function(){
n@901 58 // Create popup window interface
n@901 59 var insertPoint = document.getElementById("topLevelBody");
n@901 60 var blank = document.createElement('div');
n@901 61 blank.className = 'testHalt';
n@901 62
n@901 63 this.popup = document.createElement('div');
n@901 64 this.popup.id = 'popupHolder';
n@901 65 this.popup.className = 'popupHolder';
n@901 66 this.popup.style.position = 'absolute';
n@901 67 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
n@901 68 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
n@901 69
n@901 70 this.popupContent = document.createElement('div');
n@901 71 this.popupContent.id = 'popupContent';
n@901 72 this.popupContent.style.marginTop = '25px';
n@901 73 this.popupContent.align = 'center';
n@901 74 this.popup.appendChild(this.popupContent);
n@901 75
n@901 76 this.popupButton = document.createElement('button');
n@901 77 this.popupButton.className = 'popupButton';
n@901 78 this.popupButton.innerHTML = 'Next';
n@901 79 this.popupButton.onclick = function(){popup.buttonClicked();};
n@901 80 this.popup.style.zIndex = -1;
n@901 81 this.popup.style.visibility = 'hidden';
n@901 82 blank.style.zIndex = -2;
n@901 83 blank.style.visibility = 'hidden';
n@901 84 insertPoint.appendChild(this.popup);
n@901 85 insertPoint.appendChild(blank);
n@901 86 };
n@901 87
n@901 88 this.showPopup = function(){
n@901 89 if (this.popup == null) {
n@901 90 this.createPopup();
n@901 91 }
n@901 92 this.popup.style.zIndex = 3;
n@901 93 this.popup.style.visibility = 'visible';
n@901 94 var blank = document.getElementsByClassName('testHalt')[0];
n@901 95 blank.style.zIndex = 2;
n@901 96 blank.style.visibility = 'visible';
n@901 97 };
n@901 98
n@901 99 this.hidePopup = function(){
n@901 100 this.popup.style.zIndex = -1;
n@901 101 this.popup.style.visibility = 'hidden';
n@901 102 var blank = document.getElementsByClassName('testHalt')[0];
n@901 103 blank.style.zIndex = -2;
n@901 104 blank.style.visibility = 'hidden';
n@901 105 };
n@901 106
n@901 107 this.postNode = function() {
n@901 108 // This will take the node from the popupOptions and display it
n@901 109 var node = this.popupOptions[this.currentIndex];
n@901 110 this.popupContent.innerHTML = null;
n@901 111 if (node.type == 'statement') {
n@901 112 var span = document.createElement('span');
n@901 113 span.textContent = node.statement;
n@901 114 this.popupContent.appendChild(span);
n@901 115 } else if (node.type == 'question') {
n@901 116 var span = document.createElement('span');
n@901 117 span.textContent = node.question;
n@901 118 var textArea = document.createElement('textarea');
n@901 119 switch (node.boxsize) {
n@901 120 case 'small':
n@901 121 textArea.cols = "20";
n@901 122 textArea.rows = "1";
n@901 123 break;
n@901 124 case 'normal':
n@901 125 textArea.cols = "30";
n@901 126 textArea.rows = "2";
n@901 127 break;
n@901 128 case 'large':
n@901 129 textArea.cols = "40";
n@901 130 textArea.rows = "5";
n@901 131 break;
n@901 132 case 'huge':
n@901 133 textArea.cols = "50";
n@901 134 textArea.rows = "10";
n@901 135 break;
n@901 136 }
n@901 137 var br = document.createElement('br');
n@901 138 this.popupContent.appendChild(span);
n@901 139 this.popupContent.appendChild(br);
n@901 140 this.popupContent.appendChild(textArea);
n@901 141 this.popupContent.childNodes[2].focus();
n@901 142 } else if (node.type == 'checkbox') {
n@901 143 var span = document.createElement('span');
n@901 144 span.textContent = node.statement;
n@901 145 this.popupContent.appendChild(span);
n@901 146 var optHold = document.createElement('div');
n@901 147 optHold.id = 'option-holder';
n@901 148 optHold.align = 'left';
n@901 149 for (var i=0; i<node.options.length; i++) {
n@901 150 var option = node.options[i];
n@901 151 var input = document.createElement('input');
n@901 152 input.id = option.id;
n@901 153 input.type = 'checkbox';
n@901 154 var span = document.createElement('span');
n@901 155 span.textContent = option.text;
n@901 156 var hold = document.createElement('div');
n@901 157 hold.setAttribute('name','option');
n@901 158 hold.style.float = 'left';
n@901 159 hold.style.padding = '4px';
n@901 160 hold.appendChild(input);
n@901 161 hold.appendChild(span);
n@901 162 optHold.appendChild(hold);
n@901 163 }
n@901 164 this.popupContent.appendChild(optHold);
n@901 165 } else if (node.type == 'radio') {
n@901 166 var span = document.createElement('span');
n@901 167 span.textContent = node.statement;
n@901 168 this.popupContent.appendChild(span);
n@901 169 var optHold = document.createElement('div');
n@901 170 optHold.id = 'option-holder';
n@901 171 optHold.align = 'none';
n@901 172 optHold.style.float = 'left';
n@901 173 optHold.style.width = "100%";
n@901 174 for (var i=0; i<node.options.length; i++) {
n@901 175 var option = node.options[i];
n@901 176 var input = document.createElement('input');
n@901 177 input.id = option.name;
n@901 178 input.type = 'radio';
n@901 179 input.name = node.id;
n@901 180 var span = document.createElement('span');
n@901 181 span.textContent = option.text;
n@901 182 var hold = document.createElement('div');
n@901 183 hold.setAttribute('name','option');
n@901 184 hold.style.padding = '4px';
n@901 185 hold.appendChild(input);
n@901 186 hold.appendChild(span);
n@901 187 optHold.appendChild(hold);
n@901 188 }
n@901 189 this.popupContent.appendChild(optHold);
n@901 190 }
n@901 191 this.popupContent.appendChild(this.popupButton);
n@901 192 };
n@901 193
n@901 194 this.initState = function(node) {
n@901 195 //Call this with your preTest and postTest nodes when needed to
n@901 196 // initialise the popup procedure.
n@901 197 this.popupOptions = node.options;
n@901 198 if (this.popupOptions.length > 0) {
n@901 199 if (node.type == 'pretest') {
n@901 200 this.responses = document.createElement('PreTest');
n@901 201 } else if (node.type == 'posttest') {
n@901 202 this.responses = document.createElement('PostTest');
n@901 203 } else {
n@901 204 console.log ('WARNING - popup node neither pre or post!');
n@901 205 this.responses = document.createElement('responses');
n@901 206 }
n@901 207 this.currentIndex = 0;
n@901 208 this.showPopup();
n@901 209 this.postNode();
n@901 210 } else {
n@901 211 advanceState();
n@901 212 }
n@901 213 };
n@901 214
n@901 215 this.buttonClicked = function() {
n@901 216 // Each time the popup button is clicked!
n@901 217 var node = this.popupOptions[this.currentIndex];
n@901 218 if (node.type == 'question') {
n@901 219 // Must extract the question data
n@901 220 var textArea = $(popup.popupContent).find('textarea')[0];
n@901 221 if (node.mandatory == true && textArea.value.length == 0) {
n@901 222 alert('This question is mandatory');
n@901 223 return;
n@901 224 } else {
n@901 225 // Save the text content
n@901 226 var hold = document.createElement('comment');
n@901 227 hold.id = node.id;
n@901 228 hold.innerHTML = textArea.value;
n@901 229 console.log("Question: "+ node.textContent);
n@901 230 console.log("Question Response: "+ textArea.value);
n@901 231 this.responses.appendChild(hold);
n@901 232 }
n@901 233 } else if (node.type == 'checkbox') {
n@901 234 // Must extract checkbox data
n@901 235 var optHold = document.getElementById('option-holder');
n@901 236 var hold = document.createElement('checkbox');
n@901 237 console.log("Checkbox: "+ node.statement);
n@901 238 hold.id = node.id;
n@901 239 for (var i=0; i<optHold.childElementCount; i++) {
n@901 240 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
n@901 241 var statement = optHold.childNodes[i].getElementsByTagName('span')[0];
n@901 242 var response = document.createElement('option');
n@901 243 response.setAttribute('id',input.id);
n@901 244 response.setAttribute('checked',input.checked);
n@901 245 hold.appendChild(response);
n@901 246 console.log(input.id +': '+ input.checked);
n@901 247 }
n@901 248 this.responses.appendChild(hold);
n@901 249 } else if (node.type == "radio") {
n@901 250 var optHold = document.getElementById('option-holder');
n@901 251 var hold = document.createElement('radio');
n@901 252 var responseID = null;
n@901 253 var i=0;
n@901 254 while(responseID == null) {
n@901 255 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
n@901 256 if (input.checked == true) {
n@901 257 responseID = i;
n@901 258 }
n@901 259 i++;
n@901 260 }
n@901 261 hold.id = node.id;
n@901 262 hold.setAttribute('name',node.options[responseID].name);
n@901 263 hold.textContent = node.options[responseID].text;
n@901 264 this.responses.appendChild(hold);
n@901 265 }
n@901 266 this.currentIndex++;
n@901 267 if (this.currentIndex < this.popupOptions.length) {
n@901 268 this.postNode();
n@901 269 } else {
n@901 270 // Reached the end of the popupOptions
n@901 271 this.hidePopup();
n@901 272 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
n@901 273 testState.stateResults[testState.stateIndex] = this.responses;
n@901 274 } else {
n@901 275 testState.stateResults[testState.stateIndex].appendChild(this.responses);
n@901 276 }
n@901 277 advanceState();
n@901 278 }
n@901 279 };
n@901 280 }
n@901 281
n@901 282 function advanceState()
n@901 283 {
n@901 284 // Just for complete clarity
n@901 285 testState.advanceState();
n@901 286 }
n@901 287
n@901 288 function stateMachine()
n@901 289 {
n@901 290 // Object prototype for tracking and managing the test state
n@901 291 this.stateMap = [];
n@901 292 this.stateIndex = null;
n@901 293 this.currentStateMap = [];
n@901 294 this.currentIndex = null;
n@901 295 this.currentTestId = 0;
n@901 296 this.stateResults = [];
n@901 297 this.timerCallBackHolders = null;
n@901 298 this.initialise = function(){
n@901 299 if (this.stateMap.length > 0) {
n@901 300 if(this.stateIndex != null) {
n@901 301 console.log('NOTE - State already initialise');
n@901 302 }
n@901 303 this.stateIndex = -1;
n@901 304 var that = this;
n@901 305 for (var id=0; id<this.stateMap.length; id++){
n@901 306 var name = this.stateMap[id].type;
n@901 307 var obj = document.createElement(name);
n@901 308 if (name == 'audioHolder') {
n@901 309 obj.id = this.stateMap[id].id;
n@901 310 }
n@901 311 this.stateResults.push(obj);
n@901 312 }
n@901 313 } else {
n@901 314 conolse.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
n@901 315 }
n@901 316 };
n@901 317 this.advanceState = function(){
n@901 318 if (this.stateIndex == null) {
n@901 319 this.initialise();
n@901 320 }
n@901 321 if (this.stateIndex == -1) {
n@901 322 console.log('Starting test...');
n@901 323 }
n@901 324 if (this.currentIndex == null){
n@901 325 if (this.currentStateMap.type == "audioHolder") {
n@901 326 // Save current page
n@901 327 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
n@901 328 this.currentTestId++;
n@901 329 }
n@901 330 this.stateIndex++;
n@901 331 if (this.stateIndex >= this.stateMap.length) {
n@901 332 console.log('Test Completed');
n@901 333 createProjectSave(specification.projectReturn);
n@901 334 } else {
n@901 335 this.currentStateMap = this.stateMap[this.stateIndex];
n@901 336 if (this.currentStateMap.type == "audioHolder") {
n@901 337 console.log('Loading test page');
n@901 338 loadTest(this.currentStateMap);
n@901 339 this.initialiseInnerState(this.currentStateMap);
n@901 340 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") {
n@901 341 if (this.currentStateMap.options.length >= 1) {
n@901 342 popup.initState(this.currentStateMap);
n@901 343 } else {
n@901 344 this.advanceState();
n@901 345 }
n@901 346 } else {
n@901 347 this.advanceState();
n@901 348 }
n@901 349 }
n@901 350 } else {
n@901 351 this.advanceInnerState();
n@901 352 }
n@901 353 };
n@901 354
n@901 355 this.testPageCompleted = function(store, testXML, testId) {
n@901 356 // Function called each time a test page has been completed
n@901 357 // Can be used to over-rule default behaviour
n@901 358
n@901 359 pageXMLSave(store, testXML);
n@901 360 };
n@901 361
n@901 362 this.initialiseInnerState = function(node) {
n@901 363 // Parses the received testXML for pre and post test options
n@901 364 this.currentStateMap = [];
n@901 365 var preTest = node.preTest;
n@901 366 var postTest = node.postTest;
n@901 367 if (preTest == undefined) {preTest = document.createElement("preTest");}
n@901 368 if (postTest == undefined){postTest= document.createElement("postTest");}
n@901 369 this.currentStateMap.push(preTest);
n@901 370 this.currentStateMap.push(node);
n@901 371 this.currentStateMap.push(postTest);
n@901 372 this.currentIndex = -1;
n@901 373 this.advanceInnerState();
n@901 374 };
n@901 375
n@901 376 this.advanceInnerState = function() {
n@901 377 this.currentIndex++;
n@901 378 if (this.currentIndex >= this.currentStateMap.length) {
n@901 379 this.currentIndex = null;
n@901 380 this.currentStateMap = this.stateMap[this.stateIndex];
n@901 381 this.advanceState();
n@901 382 } else {
n@901 383 if (this.currentStateMap[this.currentIndex].type == "audioHolder") {
n@901 384 console.log("Loading test page"+this.currentTestId);
n@901 385 } else if (this.currentStateMap[this.currentIndex].type == "pretest") {
n@901 386 popup.initState(this.currentStateMap[this.currentIndex]);
n@901 387 } else if (this.currentStateMap[this.currentIndex].type == "posttest") {
n@901 388 popup.initState(this.currentStateMap[this.currentIndex]);
n@901 389 } else {
n@901 390 this.advanceInnerState();
n@901 391 }
n@901 392 }
n@901 393 };
n@901 394
n@901 395 this.previousState = function(){};
n@901 396 }
n@901 397
n@901 398 function testEnded(testId)
n@901 399 {
n@901 400 pageXMLSave(testId);
n@901 401 if (testXMLSetups.length-1 > testId)
n@901 402 {
n@901 403 // Yes we have another test to perform
n@901 404 testId = (Number(testId)+1);
n@901 405 currentState = 'testRun-'+testId;
n@901 406 loadTest(testId);
n@901 407 } else {
n@901 408 console.log('Testing Completed!');
n@901 409 currentState = 'postTest';
n@901 410 // Check for any post tests
n@901 411 var xmlSetup = projectXML.find('setup');
n@901 412 var postTest = xmlSetup.find('PostTest')[0];
n@901 413 popup.initState(postTest);
n@901 414 }
n@901 415 }
n@901 416
n@901 417 function loadProjectSpec(url) {
n@901 418 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
n@901 419 // If url is null, request client to upload project XML document
n@901 420 var r = new XMLHttpRequest();
n@901 421 r.open('GET',url,true);
n@901 422 r.onload = function() {
n@901 423 loadProjectSpecCallback(r.response);
n@901 424 };
n@901 425 r.send();
n@901 426 };
n@901 427
n@901 428 function loadProjectSpecCallback(response) {
n@901 429 // Function called after asynchronous download of XML project specification
n@901 430 //var decode = $.parseXML(response);
n@901 431 //projectXML = $(decode);
n@901 432
n@901 433 var parse = new DOMParser();
n@901 434 projectXML = parse.parseFromString(response,'text/xml');
n@901 435
n@901 436 // Build the specification
n@901 437 specification.decode();
n@901 438
n@901 439 testState.stateMap.push(specification.preTest);
n@901 440
n@901 441 // New check if we need to randomise the test order
n@901 442 if (specification.randomiseOrder)
n@901 443 {
n@901 444 specification.audioHolders = randomiseOrder(specification.audioHolders);
n@901 445 }
n@901 446
n@901 447 $(specification.audioHolders).each(function(index,elem){
n@901 448 testState.stateMap.push(elem);
n@901 449 });
n@901 450
n@901 451 testState.stateMap.push(specification.postTest);
n@901 452
n@901 453 // Obtain the metrics enabled
n@901 454 $(specification.metrics).each(function(index,node){
n@901 455 var enabled = node.textContent;
n@901 456 switch(node.enabled)
n@901 457 {
n@901 458 case 'testTimer':
n@901 459 sessionMetrics.prototype.enableTestTimer = true;
n@901 460 break;
n@901 461 case 'elementTimer':
n@901 462 sessionMetrics.prototype.enableElementTimer = true;
n@901 463 break;
n@901 464 case 'elementTracker':
n@901 465 sessionMetrics.prototype.enableElementTracker = true;
n@901 466 break;
n@901 467 case 'elementListenTracker':
n@901 468 sessionMetrics.prototype.enableElementListenTracker = true;
n@901 469 break;
n@901 470 case 'elementInitialPosition':
n@901 471 sessionMetrics.prototype.enableElementInitialPosition = true;
n@901 472 break;
n@901 473 case 'elementFlagListenedTo':
n@901 474 sessionMetrics.prototype.enableFlagListenedTo = true;
n@901 475 break;
n@901 476 case 'elementFlagMoved':
n@901 477 sessionMetrics.prototype.enableFlagMoved = true;
n@901 478 break;
n@901 479 case 'elementFlagComments':
n@901 480 sessionMetrics.prototype.enableFlagComments = true;
n@901 481 break;
n@901 482 }
n@901 483 });
n@901 484
n@901 485
n@901 486
n@901 487 // Detect the interface to use and load the relevant javascripts.
n@901 488 var interfaceJS = document.createElement('script');
n@901 489 interfaceJS.setAttribute("type","text/javascript");
n@901 490 if (specification.interfaceType == 'APE') {
n@901 491 interfaceJS.setAttribute("src","ape.js");
n@901 492
n@901 493 // APE comes with a css file
n@901 494 var css = document.createElement('link');
n@901 495 css.rel = 'stylesheet';
n@901 496 css.type = 'text/css';
n@901 497 css.href = 'ape.css';
n@901 498
n@901 499 document.getElementsByTagName("head")[0].appendChild(css);
n@901 500 }
n@901 501 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
n@901 502
n@901 503 // Define window callbacks for interface
n@901 504 window.onresize = function(event){resizeWindow(event);};
n@901 505 }
n@901 506
n@901 507 function createProjectSave(destURL) {
n@901 508 // Save the data from interface into XML and send to destURL
n@901 509 // If destURL is null then download XML in client
n@901 510 // Now time to render file locally
n@901 511 var xmlDoc = interfaceXMLSave();
n@901 512 var parent = document.createElement("div");
n@901 513 parent.appendChild(xmlDoc);
n@901 514 var file = [parent.innerHTML];
n@901 515 if (destURL == "null" || destURL == undefined) {
n@901 516 var bb = new Blob(file,{type : 'application/xml'});
n@901 517 var dnlk = window.URL.createObjectURL(bb);
n@901 518 var a = document.createElement("a");
n@901 519 a.hidden = '';
n@901 520 a.href = dnlk;
n@901 521 a.download = "save.xml";
n@901 522 a.textContent = "Save File";
n@901 523
n@901 524 popup.showPopup();
n@901 525 popup.popupContent.innerHTML = null;
n@901 526 popup.popupContent.appendChild(a);
n@901 527 } else {
n@901 528 var xmlhttp = new XMLHttpRequest;
n@901 529 xmlhttp.open("POST",destURL,true);
n@901 530 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
n@901 531 xmlhttp.onerror = function(){
n@901 532 console.log('Error saving file to server! Presenting download locally');
n@901 533 createProjectSave(null);
n@901 534 };
n@901 535 xmlhttp.onreadystatechange = function() {
n@901 536 console.log(xmlhttp.status);
n@901 537 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
n@901 538 createProjectSave(null);
n@901 539 }
n@901 540 };
n@901 541 xmlhttp.send(file);
n@901 542 }
n@901 543 }
n@901 544
n@901 545 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
n@901 546 function interfaceXMLSave(){
n@901 547 // Create the XML string to be exported with results
n@901 548 var xmlDoc = document.createElement("BrowserEvaluationResult");
n@901 549 xmlDoc.appendChild(returnDateNode());
n@901 550 for (var i=0; i<testState.stateResults.length; i++)
n@901 551 {
n@901 552 xmlDoc.appendChild(testState.stateResults[i]);
n@901 553 }
n@901 554
n@901 555 return xmlDoc;
n@901 556 }
n@901 557
n@901 558 function AudioEngine() {
n@901 559
n@901 560 // Create two output paths, the main outputGain and fooGain.
n@901 561 // Output gain is default to 1 and any items for playback route here
n@901 562 // Foo gain is used for analysis to ensure paths get processed, but are not heard
n@901 563 // because web audio will optimise and any route which does not go to the destination gets ignored.
n@901 564 this.outputGain = audioContext.createGain();
n@901 565 this.fooGain = audioContext.createGain();
n@901 566 this.fooGain.gain = 0;
n@901 567
n@901 568 // Use this to detect playback state: 0 - stopped, 1 - playing
n@901 569 this.status = 0;
n@901 570 this.audioObjectsReady = false;
n@901 571
n@901 572 // Connect both gains to output
n@901 573 this.outputGain.connect(audioContext.destination);
n@901 574 this.fooGain.connect(audioContext.destination);
n@901 575
n@901 576 // Create the timer Object
n@901 577 this.timer = new timer();
n@901 578 // Create session metrics
n@901 579 this.metric = new sessionMetrics(this);
n@901 580
n@901 581 this.loopPlayback = false;
n@901 582
n@901 583 // Create store for new audioObjects
n@901 584 this.audioObjects = [];
n@901 585
n@901 586 this.play = function() {
n@901 587 // Start the timer and set the audioEngine state to playing (1)
n@901 588 if (this.status == 0) {
n@901 589 // Check if all audioObjects are ready
n@901 590 if (this.audioObjectsReady == false) {
n@901 591 this.audioObjectsReady = this.checkAllReady();
n@901 592 }
n@901 593 if (this.audioObjectsReady == true) {
n@901 594 this.timer.startTest();
n@901 595 if (this.loopPlayback) {
n@901 596 for(var i=0; i<this.audioObjects.length; i++) {
n@901 597 this.audioObjects[i].play(this.timer.getTestTime()+1);
n@901 598 }
n@901 599 }
n@901 600 this.status = 1;
n@901 601 }
n@901 602 }
n@901 603 };
n@901 604
n@901 605 this.stop = function() {
n@901 606 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
n@901 607 if (this.status == 1) {
n@901 608 for (var i=0; i<this.audioObjects.length; i++)
n@901 609 {
n@901 610 this.audioObjects[i].stop();
n@901 611 }
n@901 612 this.status = 0;
n@901 613 }
n@901 614 };
n@901 615
n@901 616
n@901 617 this.newTrack = function(element) {
n@901 618 // Pull data from given URL into new audio buffer
n@901 619 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
n@901 620
n@901 621 // Create the audioObject with ID of the new track length;
n@901 622 audioObjectId = this.audioObjects.length;
n@901 623 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
n@901 624
n@901 625 // AudioObject will get track itself.
n@901 626 this.audioObjects[audioObjectId].specification = element;
n@901 627 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url);
n@901 628 return this.audioObjects[audioObjectId];
n@901 629 };
n@901 630
n@901 631 this.newTestPage = function() {
n@901 632 this.state = 0;
n@901 633 this.audioObjectsReady = false;
n@901 634 this.metric.reset();
n@901 635 this.audioObjects = [];
n@901 636 };
n@901 637
n@901 638 this.checkAllPlayed = function() {
n@901 639 arr = [];
n@901 640 for (var id=0; id<this.audioObjects.length; id++) {
n@901 641 if (this.audioObjects[id].metric.wasListenedTo == false) {
n@901 642 arr.push(this.audioObjects[id].id);
n@901 643 }
n@901 644 }
n@901 645 return arr;
n@901 646 };
n@901 647
n@901 648 this.checkAllReady = function() {
n@901 649 var ready = true;
n@901 650 for (var i=0; i<this.audioObjects.length; i++) {
n@901 651 if (this.audioObjects[i].state == 0) {
n@901 652 // Track not ready
n@901 653 console.log('WAIT -- audioObject '+i+' not ready yet!');
n@901 654 ready = false;
n@901 655 };
n@901 656 }
n@901 657 return ready;
n@901 658 };
n@901 659
n@901 660 }
n@901 661
n@901 662 function audioObject(id) {
n@901 663 // The main buffer object with common control nodes to the AudioEngine
n@901 664
n@901 665 this.specification;
n@901 666 this.id = id;
n@901 667 this.state = 0; // 0 - no data, 1 - ready
n@901 668 this.url = null; // Hold the URL given for the output back to the results.
n@901 669 this.metric = new metricTracker(this);
n@901 670
n@901 671 // Bindings for GUI
n@901 672 this.interfaceDOM = null;
n@901 673 this.commentDOM = null;
n@901 674
n@901 675 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
n@901 676 this.bufferNode = undefined;
n@901 677 this.outputGain = audioContext.createGain();
n@901 678
n@901 679 // Default output gain to be zero
n@901 680 this.outputGain.gain.value = 0.0;
n@901 681
n@901 682 // Connect buffer to the audio graph
n@901 683 this.outputGain.connect(audioEngineContext.outputGain);
n@901 684
n@901 685 // the audiobuffer is not designed for multi-start playback
n@901 686 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
n@901 687 this.buffer;
n@901 688
n@901 689 this.loopStart = function() {
n@901 690 this.outputGain.gain.value = 1.0;
n@901 691 this.metric.startListening(audioEngineContext.timer.getTestTime());
n@901 692 };
n@901 693
n@901 694 this.loopStop = function() {
n@901 695 if (this.outputGain.gain.value != 0.0) {
n@901 696 this.outputGain.gain.value = 0.0;
n@901 697 this.metric.stopListening(audioEngineContext.timer.getTestTime());
n@901 698 }
n@901 699 };
n@901 700
n@901 701 this.play = function(startTime) {
n@901 702 this.bufferNode = audioContext.createBufferSource();
n@901 703 this.bufferNode.owner = this;
n@901 704 this.bufferNode.connect(this.outputGain);
n@901 705 this.bufferNode.buffer = this.buffer;
n@901 706 this.bufferNode.loop = audioEngineContext.loopPlayback;
n@901 707 this.bufferNode.onended = function() {
n@901 708 // Safari does not like using 'this' to reference the calling object!
n@901 709 event.srcElement.owner.metric.stopListening(audioEngineContext.timer.getTestTime());
n@901 710 };
n@901 711 if (this.bufferNode.loop == false) {
n@901 712 this.metric.startListening(audioEngineContext.timer.getTestTime());
n@901 713 }
n@901 714 this.bufferNode.start(startTime);
n@901 715 };
n@901 716
n@901 717 this.stop = function() {
n@901 718 if (this.bufferNode != undefined)
n@901 719 {
n@901 720 this.bufferNode.stop(0);
n@901 721 this.bufferNode = undefined;
n@901 722 this.metric.stopListening(audioEngineContext.timer.getTestTime());
n@901 723 }
n@901 724 };
n@901 725
n@901 726 this.getCurrentPosition = function() {
n@901 727 var time = audioEngineContext.timer.getTestTime();
n@901 728 if (this.bufferNode != undefined) {
n@901 729 if (this.bufferNode.loop == true) {
n@901 730 if (audioEngineContext.status == 1) {
n@901 731 return time%this.buffer.duration;
n@901 732 } else {
n@901 733 return 0;
n@901 734 }
n@901 735 } else {
n@901 736 if (this.metric.listenHold) {
n@901 737 return time - this.metric.listenStart;
n@901 738 } else {
n@901 739 return 0;
n@901 740 }
n@901 741 }
n@901 742 } else {
n@901 743 return 0;
n@901 744 }
n@901 745 };
n@901 746
n@901 747 this.constructTrack = function(url) {
n@901 748 var request = new XMLHttpRequest();
n@901 749 this.url = url;
n@901 750 request.open('GET',url,true);
n@901 751 request.responseType = 'arraybuffer';
n@901 752
n@901 753 var audioObj = this;
n@901 754
n@901 755 // Create callback to decode the data asynchronously
n@901 756 request.onloadend = function() {
n@901 757 audioContext.decodeAudioData(request.response, function(decodedData) {
n@901 758 audioObj.buffer = decodedData;
n@901 759 audioObj.state = 1;
n@901 760 }, function(){
n@901 761 // Should only be called if there was an error, but sometimes gets called continuously
n@901 762 // Check here if the error is genuine
n@901 763 if (audioObj.state == 0 || audioObj.buffer == undefined) {
n@901 764 // Genuine error
n@901 765 console.log('FATAL - Error loading buffer on '+audioObj.id);
n@901 766 }
n@901 767 });
n@901 768 };
n@901 769 request.send();
n@901 770 };
n@901 771
n@901 772 this.exportXMLDOM = function() {
n@901 773 var root = document.createElement('audioElement');
n@901 774 root.id = this.specification.id;
n@901 775 root.setAttribute('url',this.url);
n@901 776 root.appendChild(this.interfaceDOM.exportXMLDOM());
n@901 777 root.appendChild(this.commentDOM.exportXMLDOM());
n@901 778 root.appendChild(this.metric.exportXMLDOM());
n@901 779 return root;
n@901 780 };
n@901 781 }
n@901 782
n@901 783 function timer()
n@901 784 {
n@901 785 /* Timer object used in audioEngine to keep track of session timings
n@901 786 * Uses the timer of the web audio API, so sample resolution
n@901 787 */
n@901 788 this.testStarted = false;
n@901 789 this.testStartTime = 0;
n@901 790 this.testDuration = 0;
n@901 791 this.minimumTestTime = 0; // No minimum test time
n@901 792 this.startTest = function()
n@901 793 {
n@901 794 if (this.testStarted == false)
n@901 795 {
n@901 796 this.testStartTime = audioContext.currentTime;
n@901 797 this.testStarted = true;
n@901 798 this.updateTestTime();
n@901 799 audioEngineContext.metric.initialiseTest();
n@901 800 }
n@901 801 };
n@901 802 this.stopTest = function()
n@901 803 {
n@901 804 if (this.testStarted)
n@901 805 {
n@901 806 this.testDuration = this.getTestTime();
n@901 807 this.testStarted = false;
n@901 808 } else {
n@901 809 console.log('ERR: Test tried to end before beginning');
n@901 810 }
n@901 811 };
n@901 812 this.updateTestTime = function()
n@901 813 {
n@901 814 if (this.testStarted)
n@901 815 {
n@901 816 this.testDuration = audioContext.currentTime - this.testStartTime;
n@901 817 }
n@901 818 };
n@901 819 this.getTestTime = function()
n@901 820 {
n@901 821 this.updateTestTime();
n@901 822 return this.testDuration;
n@901 823 };
n@901 824 }
n@901 825
n@901 826 function sessionMetrics(engine)
n@901 827 {
n@901 828 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
n@901 829 */
n@901 830 this.engine = engine;
n@901 831 this.lastClicked = -1;
n@901 832 this.data = -1;
n@901 833 this.reset = function() {
n@901 834 this.lastClicked = -1;
n@901 835 this.data = -1;
n@901 836 };
n@901 837 this.initialiseTest = function(){};
n@901 838 }
n@901 839
n@901 840 function metricTracker(caller)
n@901 841 {
n@901 842 /* Custom object to track and collect metric data
n@901 843 * Used only inside the audioObjects object.
n@901 844 */
n@901 845
n@901 846 this.listenedTimer = 0;
n@901 847 this.listenStart = 0;
n@901 848 this.listenHold = false;
n@901 849 this.initialPosition = -1;
n@901 850 this.movementTracker = [];
n@901 851 this.listenTracker =[];
n@901 852 this.wasListenedTo = false;
n@901 853 this.wasMoved = false;
n@901 854 this.hasComments = false;
n@901 855 this.parent = caller;
n@901 856
n@901 857 this.initialised = function(position)
n@901 858 {
n@901 859 if (this.initialPosition == -1) {
n@901 860 this.initialPosition = position;
n@901 861 }
n@901 862 };
n@901 863
n@901 864 this.moved = function(time,position)
n@901 865 {
n@901 866 this.wasMoved = true;
n@901 867 this.movementTracker[this.movementTracker.length] = [time, position];
n@901 868 };
n@901 869
n@901 870 this.startListening = function(time)
n@901 871 {
n@901 872 if (this.listenHold == false)
n@901 873 {
n@901 874 this.wasListenedTo = true;
n@901 875 this.listenStart = time;
n@901 876 this.listenHold = true;
n@901 877
n@901 878 var evnt = document.createElement('event');
n@901 879 var testTime = document.createElement('testTime');
n@901 880 testTime.setAttribute('start',time);
n@901 881 var bufferTime = document.createElement('bufferTime');
n@901 882 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
n@901 883 evnt.appendChild(testTime);
n@901 884 evnt.appendChild(bufferTime);
n@901 885 this.listenTracker.push(evnt);
n@901 886
n@901 887 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
n@901 888 }
n@901 889 };
n@901 890
n@901 891 this.stopListening = function(time)
n@901 892 {
n@901 893 if (this.listenHold == true)
n@901 894 {
n@901 895 var diff = time - this.listenStart;
n@901 896 this.listenedTimer += (diff);
n@901 897 this.listenStart = 0;
n@901 898 this.listenHold = false;
n@901 899
n@901 900 var evnt = this.listenTracker[this.listenTracker.length-1];
n@901 901 var testTime = evnt.getElementsByTagName('testTime')[0];
n@901 902 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
n@901 903 testTime.setAttribute('stop',time);
n@901 904 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
n@901 905 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
n@901 906 }
n@901 907 };
n@901 908
n@901 909 this.exportXMLDOM = function() {
n@901 910 var root = document.createElement('metric');
n@901 911 if (audioEngineContext.metric.enableElementTimer) {
n@901 912 var mElementTimer = document.createElement('metricresult');
n@901 913 mElementTimer.setAttribute('name','enableElementTimer');
n@901 914 mElementTimer.textContent = this.listenedTimer;
n@901 915 root.appendChild(mElementTimer);
n@901 916 }
n@901 917 if (audioEngineContext.metric.enableElementTracker) {
n@901 918 var elementTrackerFull = document.createElement('metricResult');
n@901 919 elementTrackerFull.setAttribute('name','elementTrackerFull');
n@901 920 for (var k=0; k<this.movementTracker.length; k++)
n@901 921 {
n@901 922 var timePos = document.createElement('timePos');
n@901 923 timePos.id = k;
n@901 924 var time = document.createElement('time');
n@901 925 time.textContent = this.movementTracker[k][0];
n@901 926 var position = document.createElement('position');
n@901 927 position.textContent = this.movementTracker[k][1];
n@901 928 timePos.appendChild(time);
n@901 929 timePos.appendChild(position);
n@901 930 elementTrackerFull.appendChild(timePos);
n@901 931 }
n@901 932 root.appendChild(elementTrackerFull);
n@901 933 }
n@901 934 if (audioEngineContext.metric.enableElementListenTracker) {
n@901 935 var elementListenTracker = document.createElement('metricResult');
n@901 936 elementListenTracker.setAttribute('name','elementListenTracker');
n@901 937 for (var k=0; k<this.listenTracker.length; k++) {
n@901 938 elementListenTracker.appendChild(this.listenTracker[k]);
n@901 939 }
n@901 940 root.appendChild(elementListenTracker);
n@901 941 }
n@901 942 if (audioEngineContext.metric.enableElementInitialPosition) {
n@901 943 var elementInitial = document.createElement('metricResult');
n@901 944 elementInitial.setAttribute('name','elementInitialPosition');
n@901 945 elementInitial.textContent = this.initialPosition;
n@901 946 root.appendChild(elementInitial);
n@901 947 }
n@901 948 if (audioEngineContext.metric.enableFlagListenedTo) {
n@901 949 var flagListenedTo = document.createElement('metricResult');
n@901 950 flagListenedTo.setAttribute('name','elementFlagListenedTo');
n@901 951 flagListenedTo.textContent = this.wasListenedTo;
n@901 952 root.appendChild(flagListenedTo);
n@901 953 }
n@901 954 if (audioEngineContext.metric.enableFlagMoved) {
n@901 955 var flagMoved = document.createElement('metricResult');
n@901 956 flagMoved.setAttribute('name','elementFlagMoved');
n@901 957 flagMoved.textContent = this.wasMoved;
n@901 958 root.appendChild(flagMoved);
n@901 959 }
n@901 960 if (audioEngineContext.metric.enableFlagComments) {
n@901 961 var flagComments = document.createElement('metricResult');
n@901 962 flagComments.setAttribute('name','elementFlagComments');
n@901 963 if (this.parent.commentDOM == null)
n@901 964 {flag.textContent = 'false';}
n@901 965 else if (this.parent.commentDOM.textContent.length == 0)
n@901 966 {flag.textContent = 'false';}
n@901 967 else
n@901 968 {flag.textContet = 'true';}
n@901 969 root.appendChild(flagComments);
n@901 970 }
n@901 971
n@901 972 return root;
n@901 973 };
n@901 974 }
n@901 975
n@901 976 function randomiseOrder(input)
n@901 977 {
n@901 978 // This takes an array of information and randomises the order
n@901 979 var N = input.length;
n@901 980 var K = N;
n@901 981 var holdArr = [];
n@901 982 for (var n=0; n<N; n++)
n@901 983 {
n@901 984 // First pick a random number
n@901 985 var r = Math.random();
n@901 986 // Multiply and floor by the number of elements left
n@901 987 r = Math.floor(r*input.length);
n@901 988 // Pick out that element and delete from the array
n@901 989 holdArr.push(input.splice(r,1)[0]);
n@901 990 }
n@901 991 return holdArr;
n@901 992 }
n@901 993
n@901 994 function returnDateNode()
n@901 995 {
n@901 996 // Create an XML Node for the Date and Time a test was conducted
n@901 997 // Structure is
n@901 998 // <datetime>
n@901 999 // <date year="##" month="##" day="##">DD/MM/YY</date>
n@901 1000 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
n@901 1001 // </datetime>
n@901 1002 var dateTime = new Date();
n@901 1003 var year = document.createAttribute('year');
n@901 1004 var month = document.createAttribute('month');
n@901 1005 var day = document.createAttribute('day');
n@901 1006 var hour = document.createAttribute('hour');
n@901 1007 var minute = document.createAttribute('minute');
n@901 1008 var secs = document.createAttribute('secs');
n@901 1009
n@901 1010 year.nodeValue = dateTime.getFullYear();
n@901 1011 month.nodeValue = dateTime.getMonth()+1;
n@901 1012 day.nodeValue = dateTime.getDate();
n@901 1013 hour.nodeValue = dateTime.getHours();
n@901 1014 minute.nodeValue = dateTime.getMinutes();
n@901 1015 secs.nodeValue = dateTime.getSeconds();
n@901 1016
n@901 1017 var hold = document.createElement("datetime");
n@901 1018 var date = document.createElement("date");
n@901 1019 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
n@901 1020 var time = document.createElement("time");
n@901 1021 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
n@901 1022
n@901 1023 date.setAttributeNode(year);
n@901 1024 date.setAttributeNode(month);
n@901 1025 date.setAttributeNode(day);
n@901 1026 time.setAttributeNode(hour);
n@901 1027 time.setAttributeNode(minute);
n@901 1028 time.setAttributeNode(secs);
n@901 1029
n@901 1030 hold.appendChild(date);
n@901 1031 hold.appendChild(time);
n@901 1032 return hold
n@901 1033
n@901 1034 }
n@901 1035
n@901 1036 function testWaitIndicator() {
n@901 1037 if (audioEngineContext.checkAllReady() == false) {
n@901 1038 var hold = document.createElement("div");
n@901 1039 hold.id = "testWaitIndicator";
n@901 1040 hold.className = "indicator-box";
n@901 1041 hold.style.zIndex = 3;
n@901 1042 var span = document.createElement("span");
n@901 1043 span.textContent = "Please wait! Elements still loading";
n@901 1044 hold.appendChild(span);
n@901 1045 var blank = document.createElement('div');
n@901 1046 blank.className = 'testHalt';
n@901 1047 blank.id = "testHaltBlank";
n@901 1048 var body = document.getElementsByTagName('body')[0];
n@901 1049 body.appendChild(hold);
n@901 1050 body.appendChild(blank);
n@901 1051 testWaitTimerIntervalHolder = setInterval(function(){
n@901 1052 var ready = audioEngineContext.checkAllReady();
n@901 1053 if (ready) {
n@901 1054 var elem = document.getElementById('testWaitIndicator');
n@901 1055 var blank = document.getElementById('testHaltBlank');
n@901 1056 var body = document.getElementsByTagName('body')[0];
n@901 1057 body.removeChild(elem);
n@901 1058 body.removeChild(blank);
n@901 1059 clearInterval(testWaitTimerIntervalHolder);
n@901 1060 }
n@901 1061 },500,false);
n@901 1062 }
n@901 1063 }
n@901 1064
n@901 1065 var testWaitTimerIntervalHolder = null;
n@901 1066
n@901 1067 function Specification() {
n@901 1068 // Handles the decoding of the project specification XML into a simple JavaScript Object.
n@901 1069
n@901 1070 this.interfaceType;
n@901 1071 this.projectReturn;
n@901 1072 this.randomiseOrder;
n@901 1073 this.collectMetrics;
n@901 1074 this.preTest;
n@901 1075 this.postTest;
n@901 1076 this.metrics =[];
n@901 1077
n@901 1078 this.audioHolders = [];
n@901 1079
n@901 1080 this.decode = function() {
n@901 1081 // projectXML - DOM Parsed document
n@901 1082 var setupNode = projectXML.getElementsByTagName('setup')[0];
n@901 1083 this.interfaceType = setupNode.getAttribute('interface');
n@901 1084 this.projectReturn = setupNode.getAttribute('projectReturn');
n@901 1085 if (setupNode.getAttribute('randomiseOrder') == "true") {
n@901 1086 this.randomiseOrder = true;
n@901 1087 } else {this.randomiseOrder = false;}
n@901 1088 if (setupNode.getAttribute('collectMetrics') == "true") {
n@901 1089 this.collectMetrics = true;
n@901 1090 } else {this.collectMetrics = false;}
n@901 1091 var metricCollection = setupNode.getElementsByTagName('Metric');
n@901 1092
n@901 1093 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
n@901 1094 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
n@901 1095
n@901 1096 if (metricCollection.length > 0) {
n@901 1097 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
n@901 1098 for (var i=0; i<metricCollection.length; i++) {
n@901 1099 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
n@901 1100 }
n@901 1101 }
n@901 1102
n@901 1103 var audioHolders = projectXML.getElementsByTagName('audioHolder');
n@901 1104 for (var i=0; i<audioHolders.length; i++) {
n@901 1105 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
n@901 1106 }
n@901 1107
n@901 1108 };
n@901 1109
n@901 1110 this.prepostNode = function(type,Collection) {
n@901 1111 this.type = type;
n@901 1112 this.options = [];
n@901 1113
n@901 1114 this.OptionNode = function(child) {
n@901 1115
n@901 1116 this.childOption = function(element) {
n@901 1117 this.type = 'option';
n@901 1118 this.id = element.id;
n@901 1119 this.name = element.getAttribute('name');
n@901 1120 this.text = element.textContent;
n@901 1121 };
n@901 1122
n@901 1123 this.type = child.nodeName;
n@901 1124 if (child.nodeName == "question") {
n@901 1125 this.id = child.id;
n@901 1126 this.mandatory;
n@901 1127 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
n@901 1128 else {this.mandatory = false;}
n@901 1129 this.question = child.textContent;
n@901 1130 if (child.getAttribute('boxsize') == null) {
n@901 1131 this.boxsize = 'normal';
n@901 1132 } else {
n@901 1133 this.boxsize = child.getAttribute('boxsize');
n@901 1134 }
n@901 1135 } else if (child.nodeName == "statement") {
n@901 1136 this.statement = child.textContent;
n@901 1137 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
n@901 1138 var element = child.firstElementChild;
n@901 1139 this.id = child.id;
n@901 1140 if (element == null) {
n@901 1141 console.log('Malformed' +child.nodeName+ 'entry');
n@901 1142 this.statement = 'Malformed' +child.nodeName+ 'entry';
n@901 1143 this.type = 'statement';
n@901 1144 } else {
n@901 1145 this.options = [];
n@901 1146 while (element != null) {
n@901 1147 if (element.nodeName == 'statement' && this.statement == undefined){
n@901 1148 this.statement = element.textContent;
n@901 1149 } else if (element.nodeName == 'option') {
n@901 1150 this.options.push(new this.childOption(element));
n@901 1151 }
n@901 1152 element = element.nextElementSibling;
n@901 1153 }
n@901 1154 }
n@901 1155 }
n@901 1156 };
n@901 1157
n@901 1158 // On construction:
n@901 1159 if (Collection.length != 0) {
n@901 1160 Collection = Collection[0];
n@901 1161 if (Collection.childElementCount != 0) {
n@901 1162 var child = Collection.firstElementChild;
n@901 1163 this.options.push(new this.OptionNode(child));
n@901 1164 while (child.nextElementSibling != null) {
n@901 1165 child = child.nextElementSibling;
n@901 1166 this.options.push(new this.OptionNode(child));
n@901 1167 }
n@901 1168 }
n@901 1169 }
n@901 1170 };
n@901 1171
n@901 1172 this.metricNode = function(name) {
n@901 1173 this.enabled = name;
n@901 1174 };
n@901 1175
n@901 1176 this.audioHolderNode = function(parent,xml) {
n@901 1177 this.type = 'audioHolder';
n@901 1178 this.interfaceNode = function(DOM) {
n@901 1179 var title = DOM.getElementsByTagName('title');
n@901 1180 if (title.length == 0) {this.title = null;}
n@901 1181 else {this.title = title[0].textContent;}
n@901 1182
n@901 1183 var scale = DOM.getElementsByTagName('scale');
n@901 1184 this.scale = [];
n@901 1185 for (var i=0; i<scale.length; i++) {
n@901 1186 var arr = [null, null];
n@901 1187 arr[0] = scale[i].getAttribute('position');
n@901 1188 arr[1] = scale[i].textContent;
n@901 1189 this.scale.push(arr);
n@901 1190 }
n@901 1191 };
n@901 1192
n@901 1193 this.audioElementNode = function(parent,xml) {
n@901 1194 this.url = xml.getAttribute('url');
n@901 1195 this.id = xml.id;
n@901 1196 this.parent = parent;
n@901 1197 };
n@901 1198
n@901 1199 this.commentQuestionNode = function(xml) {
n@901 1200 this.childOption = function(element) {
n@901 1201 this.type = 'option';
n@901 1202 this.name = element.getAttribute('name');
n@901 1203 this.text = element.textContent;
n@901 1204 };
n@901 1205 this.id = xml.id;
n@901 1206 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
n@901 1207 else {this.mandatory = false;}
n@901 1208 this.type = xml.getAttribute('type');
n@901 1209 if (this.type == undefined) {this.type = 'text';}
n@901 1210 switch (this.type) {
n@901 1211 case 'text':
n@901 1212 this.question = xml.textContent;
n@901 1213 break;
n@901 1214 case 'radio':
n@901 1215 var child = xml.firstElementChild;
n@901 1216 this.options = [];
n@901 1217 while (child != undefined) {
n@901 1218 if (child.nodeName == 'statement' && this.statement == undefined) {
n@901 1219 this.statement = child.textContent;
n@901 1220 } else if (child.nodeName == 'option') {
n@901 1221 this.options.push(new this.childOption(child));
n@901 1222 }
n@901 1223 child = child.nextElementSibling;
n@901 1224 }
n@901 1225 }
n@901 1226 };
n@901 1227
n@901 1228 this.id = xml.id;
n@901 1229 this.hostURL = xml.getAttribute('hostURL');
n@901 1230 this.sampleRate = xml.getAttribute('sampleRate');
n@901 1231 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
n@901 1232 else {this.randomiseOrder = false;}
n@901 1233 this.repeatCount = xml.getAttribute('repeatCount');
n@901 1234 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
n@901 1235 else {this.loop == false;}
n@901 1236 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
n@901 1237 else {this.elementComments = false;}
n@901 1238
n@901 1239 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
n@901 1240 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
n@901 1241
n@901 1242 this.interfaces = [];
n@901 1243 var interfaceDOM = xml.getElementsByTagName('interface');
n@901 1244 for (var i=0; i<interfaceDOM.length; i++) {
n@901 1245 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
n@901 1246 }
n@901 1247
n@901 1248 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
n@901 1249 if (this.commentBoxPrefix.length != 0) {
n@901 1250 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
n@901 1251 } else {
n@901 1252 this.commentBoxPrefix = "Comment on track";
n@901 1253 }
n@901 1254
n@901 1255 this.audioElements =[];
n@901 1256 var audioElementsDOM = xml.getElementsByTagName('audioElements');
n@901 1257 for (var i=0; i<audioElementsDOM.length; i++) {
n@901 1258 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
n@901 1259 }
n@901 1260
n@901 1261 this.commentQuestions = [];
n@901 1262 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
n@901 1263 for (var i=0; i<commentQuestionsDOM.length; i++) {
n@901 1264 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
n@901 1265 }
n@901 1266 };
n@901 1267 }
n@901 1268
n@901 1269 function Interface(specificationObject) {
n@901 1270 // This handles the bindings between the interface and the audioEngineContext;
n@901 1271 this.specification = specificationObject;
n@901 1272 this.insertPoint = document.getElementById("topLevelBody");
n@901 1273
n@901 1274 // Bounded by interface!!
n@901 1275 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
n@901 1276 // For example, APE returns the slider position normalised in a <value> tag.
n@901 1277 this.interfaceObjects = [];
n@901 1278 this.interfaceObject = function(){};
n@901 1279
n@901 1280 this.commentBoxes = [];
n@901 1281 this.elementCommentBox = function(audioObject) {
n@901 1282 var element = audioObject.specification;
n@901 1283 this.audioObject = audioObject;
n@901 1284 this.id = audioObject.id;
n@901 1285 var audioHolderObject = audioObject.specification.parent;
n@901 1286 // Create document objects to hold the comment boxes
n@901 1287 this.trackComment = document.createElement('div');
n@901 1288 this.trackComment.className = 'comment-div';
n@901 1289 this.trackComment.id = 'comment-div-'+audioObject.id;
n@901 1290 // Create a string next to each comment asking for a comment
n@901 1291 this.trackString = document.createElement('span');
n@901 1292 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
n@901 1293 // Create the HTML5 comment box 'textarea'
n@901 1294 this.trackCommentBox = document.createElement('textarea');
n@901 1295 this.trackCommentBox.rows = '4';
n@901 1296 this.trackCommentBox.cols = '100';
n@901 1297 this.trackCommentBox.name = 'trackComment'+audioObject.id;
n@901 1298 this.trackCommentBox.className = 'trackComment';
n@901 1299 var br = document.createElement('br');
n@901 1300 // Add to the holder.
n@901 1301 this.trackComment.appendChild(this.trackString);
n@901 1302 this.trackComment.appendChild(br);
n@901 1303 this.trackComment.appendChild(this.trackCommentBox);
n@901 1304
n@901 1305 this.exportXMLDOM = function() {
n@901 1306 var root = document.createElement('comment');
n@901 1307 if (this.audioObject.specification.parent.elementComments) {
n@901 1308 var question = document.createElement('question');
n@901 1309 question.textContent = this.trackString.textContent;
n@901 1310 var response = document.createElement('response');
n@901 1311 response.textContent = this.trackCommentBox.value;
n@901 1312 root.appendChild(question);
n@901 1313 root.appendChild(response);
n@901 1314 }
n@901 1315 return root;
n@901 1316 };
n@901 1317 };
n@901 1318
n@901 1319 this.commentQuestions = [];
n@901 1320
n@901 1321 this.commentBox = function(commentQuestion) {
n@901 1322 this.specification = commentQuestion;
n@901 1323 // Create document objects to hold the comment boxes
n@901 1324 this.holder = document.createElement('div');
n@901 1325 this.holder.className = 'comment-div';
n@901 1326 // Create a string next to each comment asking for a comment
n@901 1327 this.string = document.createElement('span');
n@901 1328 this.string.innerHTML = commentQuestion.question;
n@901 1329 // Create the HTML5 comment box 'textarea'
n@901 1330 this.textArea = document.createElement('textarea');
n@901 1331 this.textArea.rows = '4';
n@901 1332 this.textArea.cols = '100';
n@901 1333 this.textArea.className = 'trackComment';
n@901 1334 var br = document.createElement('br');
n@901 1335 // Add to the holder.
n@901 1336 this.holder.appendChild(this.string);
n@901 1337 this.holder.appendChild(br);
n@901 1338 this.holder.appendChild(this.textArea);
n@901 1339
n@901 1340 this.exportXMLDOM = function() {
n@901 1341 var root = document.createElement('comment');
n@901 1342 root.id = this.specification.id;
n@901 1343 root.setAttribute('type',this.specification.type);
n@901 1344 root.textContent = this.textArea.value;
n@901 1345 return root;
n@901 1346 };
n@901 1347 };
n@901 1348
n@901 1349 this.radioBox = function(commentQuestion) {
n@901 1350 this.specification = commentQuestion;
n@901 1351 // Create document objects to hold the comment boxes
n@901 1352 this.holder = document.createElement('div');
n@901 1353 this.holder.className = 'comment-div';
n@901 1354 // Create a string next to each comment asking for a comment
n@901 1355 this.string = document.createElement('span');
n@901 1356 this.string.innerHTML = commentQuestion.statement;
n@901 1357 var br = document.createElement('br');
n@901 1358 // Add to the holder.
n@901 1359 this.holder.appendChild(this.string);
n@901 1360 this.holder.appendChild(br);
n@901 1361 this.options = [];
n@901 1362 this.inputs = document.createElement('div');
n@901 1363 this.span = document.createElement('div');
n@901 1364 this.inputs.align = 'center';
n@901 1365 this.inputs.style.marginLeft = '12px';
n@901 1366 this.span.style.marginLeft = '12px';
n@901 1367 this.span.align = 'center';
n@901 1368 this.span.style.marginTop = '15px';
n@901 1369
n@901 1370 var optCount = commentQuestion.options.length;
n@901 1371 var spanMargin = Math.floor(((600-(optCount*100))/(optCount))/2)+'px';
n@901 1372 console.log(spanMargin);
n@901 1373 for (var i=0; i<optCount; i++)
n@901 1374 {
n@901 1375 var div = document.createElement('div');
n@901 1376 div.style.width = '100px';
n@901 1377 div.style.float = 'left';
n@901 1378 div.style.marginRight = spanMargin;
n@901 1379 div.style.marginLeft = spanMargin;
n@901 1380 var input = document.createElement('input');
n@901 1381 input.type = 'radio';
n@901 1382 input.name = commentQuestion.id;
n@901 1383 input.setAttribute('setvalue',commentQuestion.options[i].name);
n@901 1384 input.className = 'comment-radio';
n@901 1385 div.appendChild(input);
n@901 1386 this.inputs.appendChild(div);
n@901 1387
n@901 1388
n@901 1389 div = document.createElement('div');
n@901 1390 div.style.width = '100px';
n@901 1391 div.style.float = 'left';
n@901 1392 div.style.marginRight = spanMargin;
n@901 1393 div.style.marginLeft = spanMargin;
n@901 1394 div.align = 'center';
n@901 1395 var span = document.createElement('span');
n@901 1396 span.textContent = commentQuestion.options[i].text;
n@901 1397 span.className = 'comment-radio-span';
n@901 1398 div.appendChild(span);
n@901 1399 this.span.appendChild(div);
n@901 1400 this.options.push(input);
n@901 1401 }
n@901 1402 this.holder.appendChild(this.span);
n@901 1403 this.holder.appendChild(this.inputs);
n@901 1404
n@901 1405 this.exportXMLDOM = function() {
n@901 1406 var root = document.createElement('comment');
n@901 1407 root.id = this.specification.id;
n@901 1408 root.setAttribute('type',this.specification.type);
n@901 1409 var question = document.createElement('question');
n@901 1410 question.textContent = this.string.textContent;
n@901 1411 var response = document.createElement('response');
n@901 1412 var i=0;
n@901 1413 while(this.options[i].checked == false) {
n@901 1414 i++;
n@901 1415 if (i >= this.options.length) {
n@901 1416 break;
n@901 1417 }
n@901 1418 }
n@901 1419 if (i >= this.options.length) {
n@901 1420 response.textContent = 'null';
n@901 1421 } else {
n@901 1422 response.textContent = this.options[i].getAttribute('setvalue');
n@901 1423 response.setAttribute('number',i);
n@901 1424 }
n@901 1425 root.appendChild(question);
n@901 1426 root.appendChild(response);
n@901 1427 return root;
n@901 1428 };
n@901 1429 };
n@901 1430
n@901 1431
n@901 1432 this.createCommentBox = function(audioObject) {
n@901 1433 var node = new this.elementCommentBox(audioObject);
n@901 1434 this.commentBoxes.push(node);
n@901 1435 audioObject.commentDOM = node;
n@901 1436 return node;
n@901 1437 };
n@901 1438
n@901 1439 this.sortCommentBoxes = function() {
n@901 1440 var holder = [];
n@901 1441 while (this.commentBoxes.length > 0) {
n@901 1442 var node = this.commentBoxes.pop(0);
n@901 1443 holder[node.id] = node;
n@901 1444 }
n@901 1445 this.commentBoxes = holder;
n@901 1446 };
n@901 1447
n@901 1448 this.showCommentBoxes = function(inject, sort) {
n@901 1449 if (sort) {interfaceContext.sortCommentBoxes();}
n@901 1450 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
n@901 1451 inject.appendChild(this.commentBoxes[i].trackComment);
n@901 1452 }
n@901 1453 };
n@901 1454
n@901 1455 this.createCommentQuestion = function(element) {
n@901 1456 var node;
n@901 1457 if (element.type == 'text') {
n@901 1458 node = new this.commentBox(element);
n@901 1459 } else if (element.type == 'radio') {
n@901 1460 node = new this.radioBox(element);
n@901 1461 }
n@901 1462 this.commentQuestions.push(node);
n@901 1463 return node;
n@901 1464 };
n@901 1465 }
n@901 1466