annotate core.js @ 185:8665e532fa52 Dev_main

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