annotate core.js @ 1023:907abe027ebc

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