annotate core.js @ 365:8d9c19f97dc1 Dev_main

Feature #1246: Playhead starts from 0 when test stopped and restarted for synchronous loop tests.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 02 Dec 2015 11:26:45 +0000
parents 30742d67704f
children ed22a47dcacd
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;
n@303 52 this.popupTitle = null;
n@303 53 this.popupResponse = null;
n@197 54 this.buttonProceed = null;
n@199 55 this.buttonPrevious = null;
nicholas@116 56 this.popupOptions = null;
nicholas@116 57 this.currentIndex = null;
nicholas@116 58 this.responses = null;
n@181 59
nicholas@116 60 this.createPopup = function(){
nicholas@116 61 // Create popup window interface
nicholas@116 62 var insertPoint = document.getElementById("topLevelBody");
nicholas@116 63 var blank = document.createElement('div');
nicholas@116 64 blank.className = 'testHalt';
nicholas@116 65
nicholas@116 66 this.popup = document.createElement('div');
nicholas@116 67 this.popup.id = 'popupHolder';
nicholas@116 68 this.popup.className = 'popupHolder';
nicholas@116 69 this.popup.style.position = 'absolute';
nicholas@116 70 this.popup.style.left = (window.innerWidth/2)-250 + 'px';
nicholas@116 71 this.popup.style.top = (window.innerHeight/2)-125 + 'px';
nicholas@116 72
nicholas@116 73 this.popupContent = document.createElement('div');
nicholas@116 74 this.popupContent.id = 'popupContent';
n@303 75 this.popupContent.style.marginTop = '20px';
nicholas@364 76 this.popupContent.style.marginBottom = '5px';
nicholas@116 77 this.popup.appendChild(this.popupContent);
nicholas@116 78
n@303 79 var titleHolder = document.createElement('div');
n@303 80 titleHolder.id = 'popupTitleHolder';
nicholas@364 81 titleHolder.align = 'center';
n@303 82 titleHolder.style.width = 'inherit';
nicholas@364 83 titleHolder.style.minHeight = '25px';
nicholas@364 84 titleHolder.style.maxHeight = '250px';
nicholas@364 85 titleHolder.style.overflow = 'auto';
n@303 86 titleHolder.style.marginBottom = '5px';
n@303 87
n@303 88 this.popupTitle = document.createElement('span');
n@303 89 this.popupTitle.id = 'popupTitle';
n@303 90 titleHolder.appendChild(this.popupTitle);
n@303 91 this.popupContent.appendChild(titleHolder);
n@303 92
n@303 93 this.popupResponse = document.createElement('div');
n@303 94 this.popupResponse.id = 'popupResponse';
nicholas@364 95 this.popupResponse.align = 'center';
n@303 96 this.popupResponse.style.width = 'inherit';
nicholas@364 97 this.popupResponse.style.minHeight = '50px';
n@303 98 this.popupResponse.style.maxHeight = '320px';
n@303 99 this.popupResponse.style.overflow = 'auto';
n@303 100 this.popupContent.appendChild(this.popupResponse);
n@303 101
n@197 102 this.buttonProceed = document.createElement('button');
n@197 103 this.buttonProceed.className = 'popupButton';
nicholas@364 104 this.buttonProceed.position = 'relative';
n@303 105 this.buttonProceed.style.left = '390px';
n@197 106 this.buttonProceed.innerHTML = 'Next';
n@197 107 this.buttonProceed.onclick = function(){popup.proceedClicked();};
n@199 108
n@199 109 this.buttonPrevious = document.createElement('button');
n@199 110 this.buttonPrevious.className = 'popupButton';
nicholas@364 111 this.buttonPrevious.position = 'relative';
n@199 112 this.buttonPrevious.style.left = '10px';
n@199 113 this.buttonPrevious.innerHTML = 'Back';
n@199 114 this.buttonPrevious.onclick = function(){popup.previousClick();};
n@199 115
nicholas@364 116 this.popupContent.appendChild(this.buttonPrevious);
nicholas@364 117 this.popupContent.appendChild(this.buttonProceed);
n@303 118
n@181 119 this.popup.style.zIndex = -1;
n@181 120 this.popup.style.visibility = 'hidden';
n@181 121 blank.style.zIndex = -2;
n@181 122 blank.style.visibility = 'hidden';
nicholas@116 123 insertPoint.appendChild(this.popup);
nicholas@116 124 insertPoint.appendChild(blank);
nicholas@116 125 };
nicholas@114 126
nicholas@116 127 this.showPopup = function(){
n@181 128 if (this.popup == null) {
nicholas@116 129 this.createPopup();
nicholas@116 130 }
nicholas@116 131 this.popup.style.zIndex = 3;
nicholas@116 132 this.popup.style.visibility = 'visible';
nicholas@116 133 var blank = document.getElementsByClassName('testHalt')[0];
nicholas@116 134 blank.style.zIndex = 2;
nicholas@116 135 blank.style.visibility = 'visible';
nicholas@276 136 $(window).keypress(function(e){
nicholas@276 137 if (e.keyCode == 13 && popup.popup.style.visibility == 'visible')
nicholas@276 138 {
nicholas@276 139 popup.buttonProceed.onclick();
nicholas@276 140 }
nicholas@276 141 });
nicholas@116 142 };
nicholas@116 143
nicholas@116 144 this.hidePopup = function(){
nicholas@116 145 this.popup.style.zIndex = -1;
nicholas@116 146 this.popup.style.visibility = 'hidden';
nicholas@116 147 var blank = document.getElementsByClassName('testHalt')[0];
nicholas@116 148 blank.style.zIndex = -2;
nicholas@116 149 blank.style.visibility = 'hidden';
n@303 150 this.buttonPrevious.style.visibility = 'inherit';
n@362 151 $(window).keypress(function(e){});
nicholas@116 152 };
nicholas@116 153
nicholas@116 154 this.postNode = function() {
nicholas@116 155 // This will take the node from the popupOptions and display it
nicholas@116 156 var node = this.popupOptions[this.currentIndex];
n@303 157 this.popupResponse.innerHTML = null;
n@181 158 if (node.type == 'statement') {
n@304 159 this.popupTitle.textContent = null;
n@304 160 var statement = document.createElement('span');
n@304 161 statement.textContent = node.statement;
n@304 162 this.popupResponse.appendChild(statement);
n@181 163 } else if (node.type == 'question') {
n@303 164 this.popupTitle.textContent = node.question;
nicholas@116 165 var textArea = document.createElement('textarea');
n@191 166 switch (node.boxsize) {
n@191 167 case 'small':
n@191 168 textArea.cols = "20";
n@191 169 textArea.rows = "1";
n@191 170 break;
n@191 171 case 'normal':
n@191 172 textArea.cols = "30";
n@191 173 textArea.rows = "2";
n@191 174 break;
n@191 175 case 'large':
n@191 176 textArea.cols = "40";
n@191 177 textArea.rows = "5";
n@191 178 break;
n@191 179 case 'huge':
n@191 180 textArea.cols = "50";
n@191 181 textArea.rows = "10";
n@191 182 break;
n@191 183 }
n@303 184 this.popupResponse.appendChild(textArea);
n@303 185 textArea.focus();
nicholas@188 186 } else if (node.type == 'checkbox') {
n@303 187 this.popupTitle.textContent = node.statement;
n@303 188 var optHold = this.popupResponse;
nicholas@188 189 for (var i=0; i<node.options.length; i++) {
nicholas@188 190 var option = node.options[i];
nicholas@188 191 var input = document.createElement('input');
nicholas@188 192 input.id = option.id;
nicholas@188 193 input.type = 'checkbox';
nicholas@188 194 var span = document.createElement('span');
nicholas@188 195 span.textContent = option.text;
nicholas@188 196 var hold = document.createElement('div');
nicholas@188 197 hold.setAttribute('name','option');
nicholas@188 198 hold.style.padding = '4px';
nicholas@188 199 hold.appendChild(input);
nicholas@188 200 hold.appendChild(span);
nicholas@188 201 optHold.appendChild(hold);
nicholas@188 202 }
nicholas@189 203 } else if (node.type == 'radio') {
n@303 204 this.popupTitle.textContent = node.statement;
n@303 205 var optHold = this.popupResponse;
nicholas@189 206 for (var i=0; i<node.options.length; i++) {
nicholas@189 207 var option = node.options[i];
nicholas@189 208 var input = document.createElement('input');
nicholas@189 209 input.id = option.name;
nicholas@189 210 input.type = 'radio';
nicholas@189 211 input.name = node.id;
nicholas@189 212 var span = document.createElement('span');
nicholas@189 213 span.textContent = option.text;
nicholas@189 214 var hold = document.createElement('div');
nicholas@189 215 hold.setAttribute('name','option');
nicholas@189 216 hold.style.padding = '4px';
nicholas@189 217 hold.appendChild(input);
nicholas@189 218 hold.appendChild(span);
nicholas@189 219 optHold.appendChild(hold);
nicholas@189 220 }
n@196 221 } else if (node.type == 'number') {
n@303 222 this.popupTitle.textContent = node.statement;
n@196 223 var input = document.createElement('input');
nicholas@224 224 input.type = 'textarea';
n@196 225 if (node.min != null) {input.min = node.min;}
n@196 226 if (node.max != null) {input.max = node.max;}
n@196 227 if (node.step != null) {input.step = node.step;}
n@303 228 this.popupResponse.appendChild(input);
nicholas@116 229 }
nicholas@364 230 var content_height = Number(this.popup.offsetHeight.toFixed());
nicholas@364 231 content_height -= Number(this.popupContent.offsetHeight.toFixed());
nicholas@364 232 content_height -=Number(this.buttonProceed.offsetHeight.toFixed());
nicholas@364 233 content_height = content_height + "px";
nicholas@364 234 this.buttonProceed.style.top = content_height;
nicholas@364 235 this.buttonPrevious.style.top = content_height;
n@199 236 if(this.currentIndex+1 == this.popupOptions.length) {
nicholas@268 237 if (this.responses.nodeName == "PRETEST") {
nicholas@268 238 this.buttonProceed.textContent = 'Start';
nicholas@268 239 } else {
nicholas@268 240 this.buttonProceed.textContent = 'Submit';
nicholas@268 241 }
n@199 242 } else {
n@199 243 this.buttonProceed.textContent = 'Next';
n@199 244 }
n@199 245 if(this.currentIndex > 0)
n@303 246 this.buttonPrevious.style.visibility = 'visible';
n@303 247 else
n@303 248 this.buttonPrevious.style.visibility = 'hidden';
n@155 249 };
nicholas@116 250
nicholas@116 251 this.initState = function(node) {
nicholas@116 252 //Call this with your preTest and postTest nodes when needed to
nicholas@116 253 // initialise the popup procedure.
n@181 254 this.popupOptions = node.options;
nicholas@116 255 if (this.popupOptions.length > 0) {
n@181 256 if (node.type == 'pretest') {
nicholas@116 257 this.responses = document.createElement('PreTest');
n@181 258 } else if (node.type == 'posttest') {
nicholas@116 259 this.responses = document.createElement('PostTest');
nicholas@116 260 } else {
nicholas@116 261 console.log ('WARNING - popup node neither pre or post!');
nicholas@116 262 this.responses = document.createElement('responses');
nicholas@116 263 }
nicholas@116 264 this.currentIndex = 0;
nicholas@116 265 this.showPopup();
nicholas@116 266 this.postNode();
n@181 267 } else {
n@181 268 advanceState();
nicholas@116 269 }
n@155 270 };
nicholas@116 271
n@197 272 this.proceedClicked = function() {
nicholas@116 273 // Each time the popup button is clicked!
nicholas@116 274 var node = this.popupOptions[this.currentIndex];
n@181 275 if (node.type == 'question') {
nicholas@116 276 // Must extract the question data
nicholas@116 277 var textArea = $(popup.popupContent).find('textarea')[0];
n@181 278 if (node.mandatory == true && textArea.value.length == 0) {
nicholas@116 279 alert('This question is mandatory');
nicholas@116 280 return;
nicholas@116 281 } else {
nicholas@116 282 // Save the text content
nicholas@116 283 var hold = document.createElement('comment');
n@181 284 hold.id = node.id;
nicholas@116 285 hold.innerHTML = textArea.value;
n@195 286 console.log("Question: "+ node.question);
nicholas@117 287 console.log("Question Response: "+ textArea.value);
nicholas@116 288 this.responses.appendChild(hold);
nicholas@116 289 }
nicholas@188 290 } else if (node.type == 'checkbox') {
nicholas@188 291 // Must extract checkbox data
n@303 292 var optHold = this.popupResponse;
nicholas@188 293 var hold = document.createElement('checkbox');
nicholas@188 294 console.log("Checkbox: "+ node.statement);
nicholas@189 295 hold.id = node.id;
nicholas@188 296 for (var i=0; i<optHold.childElementCount; i++) {
nicholas@188 297 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nicholas@188 298 var statement = optHold.childNodes[i].getElementsByTagName('span')[0];
nicholas@188 299 var response = document.createElement('option');
n@195 300 response.setAttribute('name',input.id);
n@195 301 response.textContent = input.checked;
nicholas@188 302 hold.appendChild(response);
nicholas@188 303 console.log(input.id +': '+ input.checked);
nicholas@188 304 }
nicholas@188 305 this.responses.appendChild(hold);
nicholas@189 306 } else if (node.type == "radio") {
n@303 307 var optHold = this.popupResponse;
nicholas@189 308 var hold = document.createElement('radio');
nicholas@189 309 var responseID = null;
nicholas@189 310 var i=0;
nicholas@189 311 while(responseID == null) {
nicholas@189 312 var input = optHold.childNodes[i].getElementsByTagName('input')[0];
nicholas@189 313 if (input.checked == true) {
nicholas@189 314 responseID = i;
nicholas@189 315 }
nicholas@189 316 i++;
nicholas@189 317 }
nicholas@189 318 hold.id = node.id;
nicholas@189 319 hold.setAttribute('name',node.options[responseID].name);
nicholas@189 320 hold.textContent = node.options[responseID].text;
nicholas@189 321 this.responses.appendChild(hold);
n@196 322 } else if (node.type == "number") {
n@196 323 var input = this.popupContent.getElementsByTagName('input')[0];
n@196 324 if (node.mandatory == true && input.value.length == 0) {
n@197 325 alert('This question is mandatory. Please enter a number');
n@197 326 return;
n@197 327 }
n@197 328 var enteredNumber = Number(input.value);
nicholas@224 329 if (isNaN(enteredNumber)) {
n@197 330 alert('Please enter a valid number');
n@197 331 return;
n@197 332 }
n@197 333 if (enteredNumber < node.min && node.min != null) {
n@197 334 alert('Number is below the minimum value of '+node.min);
n@197 335 return;
n@197 336 }
n@197 337 if (enteredNumber > node.max && node.max != null) {
n@197 338 alert('Number is above the maximum value of '+node.max);
n@196 339 return;
n@196 340 }
n@196 341 var hold = document.createElement('number');
n@196 342 hold.id = node.id;
n@196 343 hold.textContent = input.value;
n@196 344 this.responses.appendChild(hold);
nicholas@116 345 }
nicholas@116 346 this.currentIndex++;
nicholas@116 347 if (this.currentIndex < this.popupOptions.length) {
nicholas@116 348 this.postNode();
nicholas@116 349 } else {
nicholas@116 350 // Reached the end of the popupOptions
nicholas@116 351 this.hidePopup();
nicholas@129 352 if (this.responses.nodeName == testState.stateResults[testState.stateIndex].nodeName) {
nicholas@129 353 testState.stateResults[testState.stateIndex] = this.responses;
nicholas@129 354 } else {
nicholas@129 355 testState.stateResults[testState.stateIndex].appendChild(this.responses);
nicholas@129 356 }
nicholas@116 357 advanceState();
nicholas@116 358 }
n@155 359 };
n@199 360
n@199 361 this.previousClick = function() {
n@199 362 // Triggered when the 'Back' button is clicked in the survey
n@199 363 if (this.currentIndex > 0) {
n@199 364 this.currentIndex--;
n@199 365 var node = this.popupOptions[this.currentIndex];
n@199 366 if (node.type != 'statement') {
n@199 367 var prevResp = this.responses.childNodes[this.responses.childElementCount-1];
n@199 368 this.responses.removeChild(prevResp);
n@199 369 }
n@199 370 this.postNode();
n@199 371 if (node.type == 'question') {
n@199 372 this.popupContent.getElementsByTagName('textarea')[0].value = prevResp.textContent;
n@199 373 } else if (node.type == 'checkbox') {
n@199 374 var options = this.popupContent.getElementsByTagName('input');
n@199 375 var savedOptions = prevResp.getElementsByTagName('option');
n@199 376 for (var i=0; i<options.length; i++) {
n@199 377 var id = options[i].id;
n@199 378 for (var j=0; j<savedOptions.length; j++) {
n@199 379 if (savedOptions[j].getAttribute('name') == id) {
n@199 380 if (savedOptions[j].textContent == 'true') {options[i].checked = true;}
n@199 381 else {options[i].checked = false;}
n@199 382 break;
n@199 383 }
n@199 384 }
n@199 385 }
n@199 386 } else if (node.type == 'number') {
n@199 387 this.popupContent.getElementsByTagName('input')[0].value = prevResp.textContent;
n@199 388 } else if (node.type == 'radio') {
n@199 389 var options = this.popupContent.getElementsByTagName('input');
n@199 390 var name = prevResp.getAttribute('name');
n@199 391 for (var i=0; i<options.length; i++) {
n@199 392 if (options[i].id == name) {
n@199 393 options[i].checked = true;
n@199 394 break;
n@199 395 }
n@199 396 }
n@199 397 }
n@199 398 }
n@199 399 };
nicholas@114 400 }
nicholas@114 401
nicholas@116 402 function advanceState()
nicholas@114 403 {
nicholas@129 404 // Just for complete clarity
nicholas@129 405 testState.advanceState();
nicholas@129 406 }
nicholas@129 407
nicholas@129 408 function stateMachine()
nicholas@129 409 {
nicholas@129 410 // Object prototype for tracking and managing the test state
nicholas@129 411 this.stateMap = [];
nicholas@129 412 this.stateIndex = null;
nicholas@129 413 this.currentStateMap = [];
nicholas@129 414 this.currentIndex = null;
nicholas@129 415 this.currentTestId = 0;
nicholas@129 416 this.stateResults = [];
nicholas@135 417 this.timerCallBackHolders = null;
nicholas@129 418 this.initialise = function(){
nicholas@129 419 if (this.stateMap.length > 0) {
nicholas@129 420 if(this.stateIndex != null) {
nicholas@129 421 console.log('NOTE - State already initialise');
nicholas@129 422 }
nicholas@129 423 this.stateIndex = -1;
nicholas@129 424 var that = this;
nicholas@250 425 var aH_pId = 0;
nicholas@129 426 for (var id=0; id<this.stateMap.length; id++){
n@181 427 var name = this.stateMap[id].type;
nicholas@129 428 var obj = document.createElement(name);
nicholas@187 429 if (name == 'audioHolder') {
nicholas@187 430 obj.id = this.stateMap[id].id;
nicholas@250 431 obj.setAttribute('presentedid',aH_pId);
nicholas@250 432 aH_pId+=1;
nicholas@187 433 }
nicholas@129 434 this.stateResults.push(obj);
nicholas@129 435 }
nicholas@129 436 } else {
b@254 437 console.log('FATAL - StateMap not correctly constructed. EMPTY_STATE_MAP');
nicholas@116 438 }
nicholas@129 439 };
nicholas@129 440 this.advanceState = function(){
nicholas@129 441 if (this.stateIndex == null) {
nicholas@129 442 this.initialise();
nicholas@129 443 }
nicholas@129 444 if (this.stateIndex == -1) {
nicholas@129 445 console.log('Starting test...');
nicholas@129 446 }
nicholas@129 447 if (this.currentIndex == null){
n@181 448 if (this.currentStateMap.type == "audioHolder") {
nicholas@129 449 // Save current page
nicholas@129 450 this.testPageCompleted(this.stateResults[this.stateIndex],this.currentStateMap,this.currentTestId);
nicholas@129 451 this.currentTestId++;
nicholas@129 452 }
nicholas@129 453 this.stateIndex++;
nicholas@129 454 if (this.stateIndex >= this.stateMap.length) {
nicholas@129 455 console.log('Test Completed');
n@182 456 createProjectSave(specification.projectReturn);
nicholas@129 457 } else {
nicholas@129 458 this.currentStateMap = this.stateMap[this.stateIndex];
n@181 459 if (this.currentStateMap.type == "audioHolder") {
nicholas@129 460 console.log('Loading test page');
nicholas@129 461 loadTest(this.currentStateMap);
nicholas@129 462 this.initialiseInnerState(this.currentStateMap);
n@181 463 } else if (this.currentStateMap.type == "pretest" || this.currentStateMap.type == "posttest") {
n@181 464 if (this.currentStateMap.options.length >= 1) {
nicholas@129 465 popup.initState(this.currentStateMap);
nicholas@129 466 } else {
nicholas@129 467 this.advanceState();
nicholas@129 468 }
nicholas@129 469 } else {
nicholas@129 470 this.advanceState();
nicholas@129 471 }
nicholas@129 472 }
nicholas@129 473 } else {
nicholas@129 474 this.advanceInnerState();
nicholas@129 475 }
nicholas@129 476 };
nicholas@129 477
nicholas@129 478 this.testPageCompleted = function(store, testXML, testId) {
nicholas@129 479 // Function called each time a test page has been completed
nicholas@129 480 // Can be used to over-rule default behaviour
nicholas@129 481
n@181 482 pageXMLSave(store, testXML);
n@176 483 };
nicholas@129 484
n@181 485 this.initialiseInnerState = function(node) {
nicholas@129 486 // Parses the received testXML for pre and post test options
nicholas@129 487 this.currentStateMap = [];
n@181 488 var preTest = node.preTest;
n@181 489 var postTest = node.postTest;
nicholas@129 490 if (preTest == undefined) {preTest = document.createElement("preTest");}
nicholas@129 491 if (postTest == undefined){postTest= document.createElement("postTest");}
nicholas@129 492 this.currentStateMap.push(preTest);
n@181 493 this.currentStateMap.push(node);
nicholas@129 494 this.currentStateMap.push(postTest);
nicholas@129 495 this.currentIndex = -1;
nicholas@129 496 this.advanceInnerState();
n@176 497 };
nicholas@129 498
nicholas@129 499 this.advanceInnerState = function() {
nicholas@129 500 this.currentIndex++;
nicholas@129 501 if (this.currentIndex >= this.currentStateMap.length) {
nicholas@129 502 this.currentIndex = null;
nicholas@129 503 this.currentStateMap = this.stateMap[this.stateIndex];
nicholas@129 504 this.advanceState();
nicholas@129 505 } else {
n@181 506 if (this.currentStateMap[this.currentIndex].type == "audioHolder") {
nicholas@129 507 console.log("Loading test page"+this.currentTestId);
n@181 508 } else if (this.currentStateMap[this.currentIndex].type == "pretest") {
nicholas@129 509 popup.initState(this.currentStateMap[this.currentIndex]);
n@181 510 } else if (this.currentStateMap[this.currentIndex].type == "posttest") {
nicholas@129 511 popup.initState(this.currentStateMap[this.currentIndex]);
nicholas@129 512 } else {
nicholas@129 513 this.advanceInnerState();
nicholas@129 514 }
nicholas@116 515 }
n@176 516 };
nicholas@129 517
nicholas@129 518 this.previousState = function(){};
nicholas@114 519 }
nicholas@114 520
nicholas@116 521 function testEnded(testId)
nicholas@114 522 {
nicholas@116 523 pageXMLSave(testId);
nicholas@116 524 if (testXMLSetups.length-1 > testId)
nicholas@116 525 {
nicholas@116 526 // Yes we have another test to perform
nicholas@116 527 testId = (Number(testId)+1);
nicholas@116 528 currentState = 'testRun-'+testId;
nicholas@116 529 loadTest(testId);
nicholas@116 530 } else {
nicholas@116 531 console.log('Testing Completed!');
nicholas@116 532 currentState = 'postTest';
nicholas@116 533 // Check for any post tests
nicholas@116 534 var xmlSetup = projectXML.find('setup');
nicholas@116 535 var postTest = xmlSetup.find('PostTest')[0];
nicholas@116 536 popup.initState(postTest);
nicholas@116 537 }
nicholas@114 538 }
nicholas@114 539
nicholas@1 540 function loadProjectSpec(url) {
nicholas@1 541 // Load the project document from the given URL, decode the XML and instruct audioEngine to get audio data
nicholas@1 542 // If url is null, request client to upload project XML document
nicholas@2 543 var r = new XMLHttpRequest();
nicholas@2 544 r.open('GET',url,true);
nicholas@2 545 r.onload = function() {
nicholas@2 546 loadProjectSpecCallback(r.response);
n@16 547 };
nicholas@2 548 r.send();
n@16 549 };
nicholas@2 550
nicholas@2 551 function loadProjectSpecCallback(response) {
nicholas@2 552 // Function called after asynchronous download of XML project specification
n@180 553 //var decode = $.parseXML(response);
n@180 554 //projectXML = $(decode);
n@180 555
n@180 556 var parse = new DOMParser();
n@180 557 projectXML = parse.parseFromString(response,'text/xml');
nicholas@2 558
n@181 559 // Build the specification
n@181 560 specification.decode();
n@176 561
n@181 562 testState.stateMap.push(specification.preTest);
n@176 563
n@181 564 $(specification.audioHolders).each(function(index,elem){
n@176 565 testState.stateMap.push(elem);
n@176 566 });
n@176 567
n@181 568 testState.stateMap.push(specification.postTest);
n@176 569
n@176 570 // Obtain the metrics enabled
n@181 571 $(specification.metrics).each(function(index,node){
n@176 572 var enabled = node.textContent;
n@181 573 switch(node.enabled)
n@176 574 {
n@176 575 case 'testTimer':
n@176 576 sessionMetrics.prototype.enableTestTimer = true;
n@176 577 break;
n@176 578 case 'elementTimer':
n@176 579 sessionMetrics.prototype.enableElementTimer = true;
n@176 580 break;
n@176 581 case 'elementTracker':
n@176 582 sessionMetrics.prototype.enableElementTracker = true;
n@176 583 break;
n@176 584 case 'elementListenTracker':
n@176 585 sessionMetrics.prototype.enableElementListenTracker = true;
n@176 586 break;
n@176 587 case 'elementInitialPosition':
n@176 588 sessionMetrics.prototype.enableElementInitialPosition = true;
n@176 589 break;
n@176 590 case 'elementFlagListenedTo':
n@176 591 sessionMetrics.prototype.enableFlagListenedTo = true;
n@176 592 break;
n@176 593 case 'elementFlagMoved':
n@176 594 sessionMetrics.prototype.enableFlagMoved = true;
n@176 595 break;
n@176 596 case 'elementFlagComments':
n@176 597 sessionMetrics.prototype.enableFlagComments = true;
n@176 598 break;
n@176 599 }
n@176 600 });
n@176 601
n@176 602
n@176 603
n@16 604 // Detect the interface to use and load the relevant javascripts.
nicholas@2 605 var interfaceJS = document.createElement('script');
nicholas@2 606 interfaceJS.setAttribute("type","text/javascript");
n@181 607 if (specification.interfaceType == 'APE') {
nicholas@2 608 interfaceJS.setAttribute("src","ape.js");
n@33 609
n@33 610 // APE comes with a css file
n@33 611 var css = document.createElement('link');
n@33 612 css.rel = 'stylesheet';
n@33 613 css.type = 'text/css';
n@33 614 css.href = 'ape.css';
n@33 615
n@33 616 document.getElementsByTagName("head")[0].appendChild(css);
n@281 617 } else if (specification.interfaceType == "MUSHRA")
n@281 618 {
n@281 619 interfaceJS.setAttribute("src","mushra.js");
n@281 620
n@281 621 // MUSHRA comes with a css file
n@281 622 var css = document.createElement('link');
n@281 623 css.rel = 'stylesheet';
n@281 624 css.type = 'text/css';
n@281 625 css.href = 'mushra.css';
n@281 626
n@281 627 document.getElementsByTagName("head")[0].appendChild(css);
nicholas@2 628 }
nicholas@2 629 document.getElementsByTagName("head")[0].appendChild(interfaceJS);
n@127 630
n@127 631 // Define window callbacks for interface
n@302 632 window.onresize = function(event){interfaceContext.resizeWindow(event);};
nicholas@1 633 }
nicholas@1 634
nicholas@1 635 function createProjectSave(destURL) {
nicholas@1 636 // Save the data from interface into XML and send to destURL
nicholas@1 637 // If destURL is null then download XML in client
nicholas@7 638 // Now time to render file locally
nicholas@7 639 var xmlDoc = interfaceXMLSave();
nicholas@122 640 var parent = document.createElement("div");
nicholas@122 641 parent.appendChild(xmlDoc);
nicholas@122 642 var file = [parent.innerHTML];
nicholas@7 643 if (destURL == "null" || destURL == undefined) {
nicholas@7 644 var bb = new Blob(file,{type : 'application/xml'});
nicholas@7 645 var dnlk = window.URL.createObjectURL(bb);
nicholas@7 646 var a = document.createElement("a");
nicholas@7 647 a.hidden = '';
nicholas@7 648 a.href = dnlk;
nicholas@7 649 a.download = "save.xml";
nicholas@7 650 a.textContent = "Save File";
nicholas@7 651
nicholas@118 652 popup.showPopup();
nicholas@118 653 popup.popupContent.innerHTML = null;
n@182 654 popup.popupContent.appendChild(a);
nicholas@122 655 } else {
nicholas@122 656 var xmlhttp = new XMLHttpRequest;
nicholas@122 657 xmlhttp.open("POST",destURL,true);
nicholas@122 658 xmlhttp.setRequestHeader('Content-Type', 'text/xml');
n@123 659 xmlhttp.onerror = function(){
n@123 660 console.log('Error saving file to server! Presenting download locally');
n@123 661 createProjectSave(null);
n@123 662 };
n@160 663 xmlhttp.onreadystatechange = function() {
n@160 664 console.log(xmlhttp.status);
n@160 665 if (xmlhttp.status != 200 && xmlhttp.readyState == 4) {
n@160 666 createProjectSave(null);
nicholas@275 667 } else {
nicholas@345 668 if (xmlhttp.responseXML == null)
nicholas@345 669 {
nicholas@345 670 return createProjectSave(null);
nicholas@345 671 }
nicholas@345 672 var response = xmlhttp.responseXML.childNodes[0];
nicholas@345 673 if (response.getAttribute('state') == "OK")
nicholas@345 674 {
nicholas@345 675 var file = response.getElementsByTagName('file')[0];
nicholas@345 676 console.log('Save OK: Filename '+file.textContent+','+file.getAttribute('bytes')+'B');
nicholas@345 677 popup.showPopup();
nicholas@345 678 popup.popupContent.innerHTML = null;
nicholas@345 679 popup.popupContent.textContent = "Thank you!";
nicholas@345 680 } else {
nicholas@345 681 var message = response.getElementsByTagName('message')[0];
nicholas@345 682 errorSessionDump(message.textContent);
nicholas@345 683 }
n@160 684 }
n@160 685 };
nicholas@122 686 xmlhttp.send(file);
nicholas@7 687 }
nicholas@1 688 }
nicholas@1 689
nicholas@252 690 function errorSessionDump(msg){
nicholas@252 691 // Create the partial interface XML save
nicholas@252 692 // Include error node with message on why the dump occured
nicholas@252 693 var xmlDoc = interfaceXMLSave();
nicholas@252 694 var err = document.createElement('error');
nicholas@252 695 err.textContent = msg;
nicholas@252 696 xmlDoc.appendChild(err);
nicholas@252 697 var parent = document.createElement("div");
nicholas@252 698 parent.appendChild(xmlDoc);
nicholas@252 699 var file = [parent.innerHTML];
nicholas@252 700 var bb = new Blob(file,{type : 'application/xml'});
nicholas@252 701 var dnlk = window.URL.createObjectURL(bb);
nicholas@252 702 var a = document.createElement("a");
nicholas@252 703 a.hidden = '';
nicholas@252 704 a.href = dnlk;
nicholas@252 705 a.download = "save.xml";
nicholas@252 706 a.textContent = "Save File";
nicholas@252 707
nicholas@252 708 popup.showPopup();
nicholas@252 709 popup.popupContent.innerHTML = "ERROR : "+msg;
nicholas@252 710 popup.popupContent.appendChild(a);
nicholas@252 711 }
nicholas@252 712
nicholas@129 713 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
nicholas@129 714 function interfaceXMLSave(){
nicholas@129 715 // Create the XML string to be exported with results
nicholas@129 716 var xmlDoc = document.createElement("BrowserEvaluationResult");
nicholas@243 717 var projectDocument = specification.projectXML;
nicholas@243 718 projectDocument.setAttribute('file-name',url);
nicholas@243 719 xmlDoc.appendChild(projectDocument);
nicholas@129 720 xmlDoc.appendChild(returnDateNode());
n@356 721 xmlDoc.appendChild(interfaceContext.returnNavigator());
nicholas@129 722 for (var i=0; i<testState.stateResults.length; i++)
nicholas@129 723 {
nicholas@129 724 xmlDoc.appendChild(testState.stateResults[i]);
nicholas@129 725 }
nicholas@129 726
nicholas@129 727 return xmlDoc;
nicholas@129 728 }
nicholas@129 729
nicholas@1 730 function AudioEngine() {
nicholas@1 731
nicholas@1 732 // Create two output paths, the main outputGain and fooGain.
nicholas@1 733 // Output gain is default to 1 and any items for playback route here
nicholas@1 734 // Foo gain is used for analysis to ensure paths get processed, but are not heard
nicholas@1 735 // because web audio will optimise and any route which does not go to the destination gets ignored.
nicholas@1 736 this.outputGain = audioContext.createGain();
nicholas@1 737 this.fooGain = audioContext.createGain();
nicholas@1 738 this.fooGain.gain = 0;
nicholas@1 739
nicholas@7 740 // Use this to detect playback state: 0 - stopped, 1 - playing
nicholas@7 741 this.status = 0;
nicholas@7 742
nicholas@1 743 // Connect both gains to output
nicholas@1 744 this.outputGain.connect(audioContext.destination);
nicholas@1 745 this.fooGain.connect(audioContext.destination);
nicholas@1 746
n@49 747 // Create the timer Object
n@49 748 this.timer = new timer();
n@49 749 // Create session metrics
n@49 750 this.metric = new sessionMetrics(this);
n@49 751
n@57 752 this.loopPlayback = false;
n@57 753
nicholas@1 754 // Create store for new audioObjects
nicholas@1 755 this.audioObjects = [];
nicholas@1 756
n@202 757 this.play = function(id) {
n@113 758 // Start the timer and set the audioEngine state to playing (1)
n@300 759 if (this.status == 0 && this.loopPlayback) {
n@113 760 // Check if all audioObjects are ready
n@300 761 if(this.checkAllReady())
n@300 762 {
n@202 763 this.status = 1;
n@300 764 this.setSynchronousLoop();
n@202 765 }
n@202 766 }
n@300 767 else
n@300 768 {
n@300 769 this.status = 1;
n@300 770 }
n@202 771 if (this.status== 1) {
n@300 772 this.timer.startTest();
n@204 773 if (id == undefined) {
n@204 774 id = -1;
n@300 775 console.log('FATAL - Passed id was undefined - AudioEngineContext.play(id)');
n@300 776 return;
n@204 777 } else {
n@204 778 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
n@204 779 }
n@202 780 if (this.loopPlayback) {
n@202 781 for (var i=0; i<this.audioObjects.length; i++)
n@202 782 {
n@202 783 this.audioObjects[i].play(this.timer.getTestTime()+1);
n@202 784 if (id == i) {
n@202 785 this.audioObjects[i].loopStart();
n@202 786 } else {
n@202 787 this.audioObjects[i].loopStop();
nicholas@131 788 }
nicholas@131 789 }
n@202 790 } else {
n@202 791 for (var i=0; i<this.audioObjects.length; i++)
n@202 792 {
n@202 793 if (i != id) {
n@202 794 this.audioObjects[i].outputGain.gain.value = 0.0;
n@202 795 this.audioObjects[i].stop();
n@202 796 } else if (i == id) {
n@202 797 this.audioObjects[id].outputGain.gain.value = 1.0;
n@202 798 this.audioObjects[id].play(audioContext.currentTime+0.01);
n@202 799 }
n@202 800 }
n@113 801 }
n@204 802 interfaceContext.playhead.start();
n@113 803 }
n@113 804 };
nicholas@1 805
n@113 806 this.stop = function() {
n@113 807 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
n@113 808 if (this.status == 1) {
n@113 809 for (var i=0; i<this.audioObjects.length; i++)
n@113 810 {
n@113 811 this.audioObjects[i].stop();
n@113 812 }
n@204 813 interfaceContext.playhead.stop();
n@113 814 this.status = 0;
n@113 815 }
n@113 816 };
nicholas@8 817
n@182 818 this.newTrack = function(element) {
nicholas@1 819 // Pull data from given URL into new audio buffer
nicholas@1 820 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nicholas@7 821
nicholas@1 822 // Create the audioObject with ID of the new track length;
n@49 823 audioObjectId = this.audioObjects.length;
nicholas@1 824 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nicholas@7 825
nicholas@7 826 // AudioObject will get track itself.
n@182 827 this.audioObjects[audioObjectId].specification = element;
n@182 828 this.audioObjects[audioObjectId].constructTrack(element.parent.hostURL + element.url);
n@179 829 return this.audioObjects[audioObjectId];
n@16 830 };
nicholas@1 831
n@113 832 this.newTestPage = function() {
n@113 833 this.state = 0;
n@113 834 this.audioObjectsReady = false;
n@113 835 this.metric.reset();
n@113 836 this.audioObjects = [];
n@113 837 };
n@113 838
nicholas@107 839 this.checkAllPlayed = function() {
nicholas@107 840 arr = [];
nicholas@107 841 for (var id=0; id<this.audioObjects.length; id++) {
nicholas@142 842 if (this.audioObjects[id].metric.wasListenedTo == false) {
nicholas@107 843 arr.push(this.audioObjects[id].id);
nicholas@107 844 }
nicholas@107 845 }
nicholas@107 846 return arr;
nicholas@107 847 };
nicholas@107 848
n@113 849 this.checkAllReady = function() {
n@113 850 var ready = true;
n@113 851 for (var i=0; i<this.audioObjects.length; i++) {
n@113 852 if (this.audioObjects[i].state == 0) {
n@113 853 // Track not ready
n@113 854 console.log('WAIT -- audioObject '+i+' not ready yet!');
n@113 855 ready = false;
n@113 856 };
n@113 857 }
n@113 858 return ready;
n@113 859 };
n@113 860
nicholas@272 861 this.setSynchronousLoop = function() {
nicholas@272 862 // Pads the signals so they are all exactly the same length
n@300 863 var length = 0;
n@300 864 var lens = [];
n@300 865 var maxId;
n@300 866 for (var i=0; i<this.audioObjects.length; i++)
nicholas@272 867 {
n@300 868 lens.push(this.audioObjects[i].buffer.length);
n@300 869 if (length < this.audioObjects[i].buffer.length)
nicholas@272 870 {
n@300 871 length = this.audioObjects[i].buffer.length;
n@300 872 maxId = i;
nicholas@272 873 }
n@300 874 }
n@300 875 // Perform difference
n@300 876 for (var i=0; i<lens.length; i++)
n@300 877 {
n@300 878 lens[i] = length - lens[i];
n@300 879 }
n@300 880 // Extract the audio and zero-pad
n@300 881 for (var i=0; i<lens.length; i++)
n@300 882 {
n@300 883 var orig = this.audioObjects[i].buffer;
n@300 884 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate);
n@300 885 for (var c=0; c<orig.numberOfChannels; c++)
nicholas@272 886 {
n@300 887 var inData = hold.getChannelData(c);
n@300 888 var outData = orig.getChannelData(c);
n@300 889 for (var n=0; n<orig.length; n++)
n@300 890 {inData[n] = outData[n];}
nicholas@272 891 }
n@300 892 this.audioObjects[i].buffer = hold;
n@300 893 delete orig;
nicholas@272 894 }
nicholas@272 895 };
nicholas@272 896
nicholas@1 897 }
nicholas@1 898
nicholas@1 899 function audioObject(id) {
nicholas@1 900 // The main buffer object with common control nodes to the AudioEngine
nicholas@1 901
n@182 902 this.specification;
nicholas@1 903 this.id = id;
nicholas@1 904 this.state = 0; // 0 - no data, 1 - ready
n@24 905 this.url = null; // Hold the URL given for the output back to the results.
n@139 906 this.metric = new metricTracker(this);
nicholas@1 907
n@177 908 // Bindings for GUI
n@183 909 this.interfaceDOM = null;
n@177 910 this.commentDOM = null;
n@177 911
nicholas@1 912 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
n@57 913 this.bufferNode = undefined;
nicholas@1 914 this.outputGain = audioContext.createGain();
nicholas@1 915
nicholas@8 916 // Default output gain to be zero
nicholas@8 917 this.outputGain.gain.value = 0.0;
nicholas@8 918
nicholas@1 919 // Connect buffer to the audio graph
nicholas@1 920 this.outputGain.connect(audioEngineContext.outputGain);
nicholas@1 921
nicholas@1 922 // the audiobuffer is not designed for multi-start playback
nicholas@1 923 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nicholas@1 924 this.buffer;
b@134 925
nicholas@132 926 this.loopStart = function() {
nicholas@132 927 this.outputGain.gain.value = 1.0;
nicholas@132 928 this.metric.startListening(audioEngineContext.timer.getTestTime());
n@177 929 };
nicholas@132 930
nicholas@132 931 this.loopStop = function() {
nicholas@132 932 if (this.outputGain.gain.value != 0.0) {
nicholas@132 933 this.outputGain.gain.value = 0.0;
nicholas@132 934 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nicholas@132 935 }
n@177 936 };
nicholas@132 937
nicholas@1 938 this.play = function(startTime) {
n@202 939 if (this.bufferNode == undefined) {
n@202 940 this.bufferNode = audioContext.createBufferSource();
n@202 941 this.bufferNode.owner = this;
n@202 942 this.bufferNode.connect(this.outputGain);
n@202 943 this.bufferNode.buffer = this.buffer;
n@202 944 this.bufferNode.loop = audioEngineContext.loopPlayback;
n@299 945 this.bufferNode.onended = function(event) {
n@202 946 // Safari does not like using 'this' to reference the calling object!
n@347 947 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
n@347 948 event.currentTarget.owner.stop();
n@202 949 };
n@202 950 if (this.bufferNode.loop == false) {
n@202 951 this.metric.startListening(audioEngineContext.timer.getTestTime());
n@202 952 }
n@202 953 this.bufferNode.start(startTime);
nicholas@110 954 }
n@16 955 };
nicholas@1 956
nicholas@1 957 this.stop = function() {
n@97 958 if (this.bufferNode != undefined)
n@97 959 {
n@203 960 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
n@97 961 this.bufferNode.stop(0);
n@97 962 this.bufferNode = undefined;
n@97 963 }
n@16 964 };
n@164 965
n@164 966 this.getCurrentPosition = function() {
n@164 967 var time = audioEngineContext.timer.getTestTime();
n@164 968 if (this.bufferNode != undefined) {
n@164 969 if (this.bufferNode.loop == true) {
n@164 970 if (audioEngineContext.status == 1) {
n@365 971 return (time-this.metric.listenStart)%this.buffer.duration;
n@164 972 } else {
n@164 973 return 0;
n@164 974 }
n@164 975 } else {
n@164 976 if (this.metric.listenHold) {
n@164 977 return time - this.metric.listenStart;
n@164 978 } else {
n@164 979 return 0;
n@164 980 }
n@164 981 }
n@164 982 } else {
n@164 983 return 0;
n@164 984 }
n@164 985 };
nicholas@8 986
nicholas@7 987 this.constructTrack = function(url) {
nicholas@7 988 var request = new XMLHttpRequest();
n@24 989 this.url = url;
nicholas@7 990 request.open('GET',url,true);
nicholas@7 991 request.responseType = 'arraybuffer';
nicholas@7 992
nicholas@7 993 var audioObj = this;
nicholas@7 994
nicholas@7 995 // Create callback to decode the data asynchronously
nicholas@7 996 request.onloadend = function() {
nicholas@7 997 audioContext.decodeAudioData(request.response, function(decodedData) {
nicholas@7 998 audioObj.buffer = decodedData;
nicholas@7 999 audioObj.state = 1;
nicholas@271 1000 if (audioObj.specification.type != 'outsidereference')
nicholas@271 1001 {audioObj.interfaceDOM.enable();}
nicholas@7 1002 }, function(){
nicholas@7 1003 // Should only be called if there was an error, but sometimes gets called continuously
nicholas@7 1004 // Check here if the error is genuine
nicholas@7 1005 if (audioObj.state == 0 || audioObj.buffer == undefined) {
nicholas@7 1006 // Genuine error
nicholas@7 1007 console.log('FATAL - Error loading buffer on '+audioObj.id);
nicholas@252 1008 if (request.status == 404)
nicholas@252 1009 {
nicholas@252 1010 console.log('FATAL - Fragment '+audioObj.id+' 404 error');
nicholas@252 1011 console.log('URL: '+audioObj.url);
nicholas@252 1012 errorSessionDump('Fragment '+audioObj.id+' 404 error');
nicholas@252 1013 }
nicholas@7 1014 }
nicholas@7 1015 });
n@16 1016 };
nicholas@7 1017 request.send();
n@16 1018 };
n@183 1019
n@183 1020 this.exportXMLDOM = function() {
n@183 1021 var root = document.createElement('audioElement');
n@183 1022 root.id = this.specification.id;
n@183 1023 root.setAttribute('url',this.url);
nicholas@234 1024 var file = document.createElement('file');
nicholas@234 1025 file.setAttribute('sampleRate',this.buffer.sampleRate);
nicholas@236 1026 file.setAttribute('channels',this.buffer.numberOfChannels);
nicholas@234 1027 file.setAttribute('sampleCount',this.buffer.length);
nicholas@234 1028 file.setAttribute('duration',this.buffer.duration);
nicholas@234 1029 root.appendChild(file);
nicholas@236 1030 if (this.specification.type != 'outsidereference') {
nicholas@236 1031 root.appendChild(this.interfaceDOM.exportXMLDOM(this));
nicholas@236 1032 root.appendChild(this.commentDOM.exportXMLDOM(this));
nicholas@266 1033 if(this.specification.type == 'anchor') {
nicholas@266 1034 root.setAttribute('anchor',true);
nicholas@266 1035 } else if(this.specification.type == 'reference') {
nicholas@266 1036 root.setAttribute('reference',true);
nicholas@266 1037 }
nicholas@236 1038 }
n@183 1039 root.appendChild(this.metric.exportXMLDOM());
n@183 1040 return root;
n@183 1041 };
n@49 1042 }
n@49 1043
n@49 1044 function timer()
n@49 1045 {
n@49 1046 /* Timer object used in audioEngine to keep track of session timings
n@49 1047 * Uses the timer of the web audio API, so sample resolution
n@49 1048 */
n@49 1049 this.testStarted = false;
n@49 1050 this.testStartTime = 0;
n@49 1051 this.testDuration = 0;
n@49 1052 this.minimumTestTime = 0; // No minimum test time
n@49 1053 this.startTest = function()
n@49 1054 {
n@49 1055 if (this.testStarted == false)
n@49 1056 {
n@49 1057 this.testStartTime = audioContext.currentTime;
n@49 1058 this.testStarted = true;
n@49 1059 this.updateTestTime();
n@52 1060 audioEngineContext.metric.initialiseTest();
n@49 1061 }
n@49 1062 };
n@49 1063 this.stopTest = function()
n@49 1064 {
n@49 1065 if (this.testStarted)
n@49 1066 {
n@49 1067 this.testDuration = this.getTestTime();
n@49 1068 this.testStarted = false;
n@49 1069 } else {
n@49 1070 console.log('ERR: Test tried to end before beginning');
n@49 1071 }
n@49 1072 };
n@49 1073 this.updateTestTime = function()
n@49 1074 {
n@49 1075 if (this.testStarted)
n@49 1076 {
n@49 1077 this.testDuration = audioContext.currentTime - this.testStartTime;
n@49 1078 }
n@49 1079 };
n@49 1080 this.getTestTime = function()
n@49 1081 {
n@49 1082 this.updateTestTime();
n@49 1083 return this.testDuration;
n@49 1084 };
n@49 1085 }
n@49 1086
n@49 1087 function sessionMetrics(engine)
n@49 1088 {
n@49 1089 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
n@49 1090 */
n@49 1091 this.engine = engine;
n@49 1092 this.lastClicked = -1;
n@49 1093 this.data = -1;
n@113 1094 this.reset = function() {
n@113 1095 this.lastClicked = -1;
n@113 1096 this.data = -1;
n@113 1097 };
n@52 1098 this.initialiseTest = function(){};
n@49 1099 }
n@49 1100
n@139 1101 function metricTracker(caller)
n@49 1102 {
n@49 1103 /* Custom object to track and collect metric data
n@49 1104 * Used only inside the audioObjects object.
n@49 1105 */
n@49 1106
n@49 1107 this.listenedTimer = 0;
n@49 1108 this.listenStart = 0;
nicholas@110 1109 this.listenHold = false;
n@51 1110 this.initialPosition = -1;
n@49 1111 this.movementTracker = [];
n@164 1112 this.listenTracker =[];
n@49 1113 this.wasListenedTo = false;
n@49 1114 this.wasMoved = false;
n@49 1115 this.hasComments = false;
n@139 1116 this.parent = caller;
n@49 1117
n@49 1118 this.initialised = function(position)
n@49 1119 {
n@51 1120 if (this.initialPosition == -1) {
n@51 1121 this.initialPosition = position;
n@51 1122 }
n@49 1123 };
n@49 1124
n@49 1125 this.moved = function(time,position)
n@49 1126 {
n@49 1127 this.wasMoved = true;
n@49 1128 this.movementTracker[this.movementTracker.length] = [time, position];
n@49 1129 };
n@49 1130
nicholas@132 1131 this.startListening = function(time)
n@49 1132 {
nicholas@110 1133 if (this.listenHold == false)
n@49 1134 {
n@49 1135 this.wasListenedTo = true;
n@49 1136 this.listenStart = time;
nicholas@110 1137 this.listenHold = true;
n@164 1138
n@164 1139 var evnt = document.createElement('event');
n@164 1140 var testTime = document.createElement('testTime');
n@164 1141 testTime.setAttribute('start',time);
n@164 1142 var bufferTime = document.createElement('bufferTime');
n@164 1143 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
n@164 1144 evnt.appendChild(testTime);
n@164 1145 evnt.appendChild(bufferTime);
n@164 1146 this.listenTracker.push(evnt);
n@164 1147
n@139 1148 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
n@139 1149 }
n@139 1150 };
nicholas@132 1151
n@203 1152 this.stopListening = function(time,bufferStopTime)
nicholas@132 1153 {
nicholas@132 1154 if (this.listenHold == true)
nicholas@132 1155 {
n@164 1156 var diff = time - this.listenStart;
n@164 1157 this.listenedTimer += (diff);
n@49 1158 this.listenStart = 0;
nicholas@110 1159 this.listenHold = false;
n@164 1160
n@164 1161 var evnt = this.listenTracker[this.listenTracker.length-1];
n@164 1162 var testTime = evnt.getElementsByTagName('testTime')[0];
n@164 1163 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
n@164 1164 testTime.setAttribute('stop',time);
n@203 1165 if (bufferStopTime == undefined) {
n@203 1166 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
n@203 1167 } else {
n@203 1168 bufferTime.setAttribute('stop',bufferStopTime);
n@203 1169 }
n@164 1170 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
n@49 1171 }
n@49 1172 };
n@177 1173
n@177 1174 this.exportXMLDOM = function() {
n@177 1175 var root = document.createElement('metric');
n@177 1176 if (audioEngineContext.metric.enableElementTimer) {
n@177 1177 var mElementTimer = document.createElement('metricresult');
n@177 1178 mElementTimer.setAttribute('name','enableElementTimer');
n@177 1179 mElementTimer.textContent = this.listenedTimer;
n@177 1180 root.appendChild(mElementTimer);
n@177 1181 }
n@177 1182 if (audioEngineContext.metric.enableElementTracker) {
n@177 1183 var elementTrackerFull = document.createElement('metricResult');
n@177 1184 elementTrackerFull.setAttribute('name','elementTrackerFull');
n@177 1185 for (var k=0; k<this.movementTracker.length; k++)
n@177 1186 {
n@177 1187 var timePos = document.createElement('timePos');
n@177 1188 timePos.id = k;
n@177 1189 var time = document.createElement('time');
n@177 1190 time.textContent = this.movementTracker[k][0];
n@177 1191 var position = document.createElement('position');
n@177 1192 position.textContent = this.movementTracker[k][1];
n@177 1193 timePos.appendChild(time);
n@177 1194 timePos.appendChild(position);
n@177 1195 elementTrackerFull.appendChild(timePos);
n@177 1196 }
n@177 1197 root.appendChild(elementTrackerFull);
n@177 1198 }
n@177 1199 if (audioEngineContext.metric.enableElementListenTracker) {
n@177 1200 var elementListenTracker = document.createElement('metricResult');
n@177 1201 elementListenTracker.setAttribute('name','elementListenTracker');
n@177 1202 for (var k=0; k<this.listenTracker.length; k++) {
n@177 1203 elementListenTracker.appendChild(this.listenTracker[k]);
n@177 1204 }
n@177 1205 root.appendChild(elementListenTracker);
n@177 1206 }
n@177 1207 if (audioEngineContext.metric.enableElementInitialPosition) {
n@177 1208 var elementInitial = document.createElement('metricResult');
n@177 1209 elementInitial.setAttribute('name','elementInitialPosition');
n@177 1210 elementInitial.textContent = this.initialPosition;
n@177 1211 root.appendChild(elementInitial);
n@177 1212 }
n@177 1213 if (audioEngineContext.metric.enableFlagListenedTo) {
n@177 1214 var flagListenedTo = document.createElement('metricResult');
n@177 1215 flagListenedTo.setAttribute('name','elementFlagListenedTo');
n@177 1216 flagListenedTo.textContent = this.wasListenedTo;
n@177 1217 root.appendChild(flagListenedTo);
n@177 1218 }
n@177 1219 if (audioEngineContext.metric.enableFlagMoved) {
n@177 1220 var flagMoved = document.createElement('metricResult');
n@177 1221 flagMoved.setAttribute('name','elementFlagMoved');
n@177 1222 flagMoved.textContent = this.wasMoved;
n@177 1223 root.appendChild(flagMoved);
n@177 1224 }
n@177 1225 if (audioEngineContext.metric.enableFlagComments) {
n@177 1226 var flagComments = document.createElement('metricResult');
n@177 1227 flagComments.setAttribute('name','elementFlagComments');
n@177 1228 if (this.parent.commentDOM == null)
n@177 1229 {flag.textContent = 'false';}
n@177 1230 else if (this.parent.commentDOM.textContent.length == 0)
n@177 1231 {flag.textContent = 'false';}
n@177 1232 else
n@177 1233 {flag.textContet = 'true';}
n@177 1234 root.appendChild(flagComments);
n@177 1235 }
n@177 1236
n@177 1237 return root;
n@177 1238 };
n@54 1239 }
n@54 1240
n@54 1241 function randomiseOrder(input)
n@54 1242 {
n@54 1243 // This takes an array of information and randomises the order
n@54 1244 var N = input.length;
b@207 1245
b@207 1246 var inputSequence = []; // For safety purposes: keep track of randomisation
b@207 1247 for (var counter = 0; counter < N; ++counter)
b@207 1248 inputSequence.push(counter) // Fill array
b@207 1249 var inputSequenceClone = inputSequence.slice(0);
b@207 1250
n@54 1251 var holdArr = [];
b@207 1252 var outputSequence = [];
n@54 1253 for (var n=0; n<N; n++)
n@54 1254 {
n@54 1255 // First pick a random number
n@54 1256 var r = Math.random();
n@54 1257 // Multiply and floor by the number of elements left
n@54 1258 r = Math.floor(r*input.length);
n@54 1259 // Pick out that element and delete from the array
n@54 1260 holdArr.push(input.splice(r,1)[0]);
b@207 1261 // Do the same with sequence
b@207 1262 outputSequence.push(inputSequence.splice(r,1)[0]);
n@54 1263 }
b@207 1264 console.log(inputSequenceClone.toString()); // print original array to console
b@207 1265 console.log(outputSequence.toString()); // print randomised array to console
n@54 1266 return holdArr;
n@125 1267 }
n@125 1268
n@125 1269 function returnDateNode()
n@125 1270 {
n@125 1271 // Create an XML Node for the Date and Time a test was conducted
n@125 1272 // Structure is
n@125 1273 // <datetime>
n@125 1274 // <date year="##" month="##" day="##">DD/MM/YY</date>
n@125 1275 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
n@125 1276 // </datetime>
n@125 1277 var dateTime = new Date();
n@125 1278 var year = document.createAttribute('year');
n@125 1279 var month = document.createAttribute('month');
n@125 1280 var day = document.createAttribute('day');
n@125 1281 var hour = document.createAttribute('hour');
n@125 1282 var minute = document.createAttribute('minute');
n@125 1283 var secs = document.createAttribute('secs');
n@125 1284
n@125 1285 year.nodeValue = dateTime.getFullYear();
n@125 1286 month.nodeValue = dateTime.getMonth()+1;
n@125 1287 day.nodeValue = dateTime.getDate();
n@125 1288 hour.nodeValue = dateTime.getHours();
n@125 1289 minute.nodeValue = dateTime.getMinutes();
n@125 1290 secs.nodeValue = dateTime.getSeconds();
n@125 1291
n@125 1292 var hold = document.createElement("datetime");
n@125 1293 var date = document.createElement("date");
n@125 1294 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
n@125 1295 var time = document.createElement("time");
n@125 1296 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
n@125 1297
n@125 1298 date.setAttributeNode(year);
n@125 1299 date.setAttributeNode(month);
n@125 1300 date.setAttributeNode(day);
n@125 1301 time.setAttributeNode(hour);
n@125 1302 time.setAttributeNode(minute);
n@125 1303 time.setAttributeNode(secs);
n@125 1304
n@125 1305 hold.appendChild(date);
n@125 1306 hold.appendChild(time);
n@125 1307 return hold
n@125 1308
nicholas@135 1309 }
nicholas@135 1310
n@180 1311 function Specification() {
n@180 1312 // Handles the decoding of the project specification XML into a simple JavaScript Object.
n@180 1313
n@311 1314 this.interfaceType = null;
n@311 1315 this.commonInterface = null;
n@311 1316 this.projectReturn = null;
n@311 1317 this.randomiseOrder = null;
n@311 1318 this.collectMetrics = null;
n@311 1319 this.testPages = null;
n@311 1320 this.preTest = null;
n@311 1321 this.postTest = null;
n@180 1322 this.metrics =[];
n@180 1323
n@180 1324 this.audioHolders = [];
n@180 1325
n@180 1326 this.decode = function() {
n@180 1327 // projectXML - DOM Parsed document
nicholas@240 1328 this.projectXML = projectXML.childNodes[0];
n@180 1329 var setupNode = projectXML.getElementsByTagName('setup')[0];
n@180 1330 this.interfaceType = setupNode.getAttribute('interface');
n@180 1331 this.projectReturn = setupNode.getAttribute('projectReturn');
n@297 1332 this.testPages = setupNode.getAttribute('testPages');
n@180 1333 if (setupNode.getAttribute('randomiseOrder') == "true") {
n@180 1334 this.randomiseOrder = true;
n@184 1335 } else {this.randomiseOrder = false;}
n@180 1336 if (setupNode.getAttribute('collectMetrics') == "true") {
n@180 1337 this.collectMetrics = true;
n@184 1338 } else {this.collectMetrics = false;}
n@297 1339 if (isNaN(Number(this.testPages)) || this.testPages == undefined)
n@297 1340 {
n@297 1341 this.testPages = null;
n@297 1342 } else {
n@297 1343 this.testPages = Number(this.testPages);
n@297 1344 if (this.testPages == 0) {this.testPages = null;}
n@297 1345 }
n@180 1346 var metricCollection = setupNode.getElementsByTagName('Metric');
n@180 1347
n@181 1348 this.preTest = new this.prepostNode('pretest',setupNode.getElementsByTagName('PreTest'));
n@181 1349 this.postTest = new this.prepostNode('posttest',setupNode.getElementsByTagName('PostTest'));
n@180 1350
n@180 1351 if (metricCollection.length > 0) {
n@180 1352 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
n@180 1353 for (var i=0; i<metricCollection.length; i++) {
n@181 1354 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
n@180 1355 }
n@180 1356 }
n@180 1357
nicholas@213 1358 var commonInterfaceNode = setupNode.getElementsByTagName('interface');
nicholas@213 1359 if (commonInterfaceNode.length > 0) {
nicholas@213 1360 commonInterfaceNode = commonInterfaceNode[0];
nicholas@213 1361 } else {
nicholas@213 1362 commonInterfaceNode = undefined;
nicholas@213 1363 }
nicholas@213 1364
nicholas@213 1365 this.commonInterface = new function() {
nicholas@213 1366 this.OptionNode = function(child) {
nicholas@213 1367 this.type = child.nodeName;
nicholas@256 1368 if (this.type == 'option')
nicholas@256 1369 {
nicholas@256 1370 this.name = child.getAttribute('name');
nicholas@256 1371 }
nicholas@256 1372 else if (this.type == 'check') {
nicholas@213 1373 this.check = child.getAttribute('name');
nicholas@231 1374 if (this.check == 'scalerange') {
nicholas@231 1375 this.min = child.getAttribute('min');
nicholas@231 1376 this.max = child.getAttribute('max');
nicholas@231 1377 if (this.min == null) {this.min = 1;}
nicholas@231 1378 else if (Number(this.min) > 1 && this.min != null) {
nicholas@231 1379 this.min = Number(this.min)/100;
nicholas@231 1380 } else {
nicholas@231 1381 this.min = Number(this.min);
nicholas@231 1382 }
nicholas@231 1383 if (this.max == null) {this.max = 0;}
nicholas@231 1384 else if (Number(this.max) > 1 && this.max != null) {
nicholas@231 1385 this.max = Number(this.max)/100;
nicholas@231 1386 } else {
nicholas@231 1387 this.max = Number(this.max);
nicholas@231 1388 }
nicholas@231 1389 }
nicholas@218 1390 } else if (this.type == 'anchor' || this.type == 'reference') {
n@324 1391 Console.log("WARNING: Anchor and Reference tags in the <interface> node are depricated");
nicholas@213 1392 }
nicholas@218 1393 };
nicholas@213 1394 this.options = [];
nicholas@213 1395 if (commonInterfaceNode != undefined) {
nicholas@213 1396 var child = commonInterfaceNode.firstElementChild;
nicholas@213 1397 while (child != undefined) {
nicholas@213 1398 this.options.push(new this.OptionNode(child));
nicholas@213 1399 child = child.nextElementSibling;
nicholas@213 1400 }
nicholas@213 1401 }
nicholas@213 1402 };
nicholas@213 1403
n@180 1404 var audioHolders = projectXML.getElementsByTagName('audioHolder');
n@180 1405 for (var i=0; i<audioHolders.length; i++) {
n@180 1406 this.audioHolders.push(new this.audioHolderNode(this,audioHolders[i]));
n@180 1407 }
n@180 1408
n@297 1409 // New check if we need to randomise the test order
n@297 1410 if (this.randomiseOrder)
n@297 1411 {
n@297 1412 this.audioHolders = randomiseOrder(this.audioHolders);
n@297 1413 for (var i=0; i<this.audioHolders.length; i++)
n@297 1414 {
n@297 1415 this.audioHolders[i].presentedId = i;
n@297 1416 }
n@297 1417 }
n@297 1418
n@297 1419 if (this.testPages != null || this.testPages != undefined)
n@297 1420 {
n@297 1421 if (this.testPages > audioHolders.length)
n@297 1422 {
n@297 1423 console.log('Warning: You have specified '+audioHolders.length+' tests but requested '+this.testPages+' be completed!');
n@297 1424 this.testPages = audioHolders.length;
n@297 1425 }
n@297 1426 var aH = this.audioHolders;
n@297 1427 this.audioHolders = [];
n@297 1428 for (var i=0; i<this.testPages; i++)
n@297 1429 {
n@297 1430 this.audioHolders.push(aH[i]);
n@297 1431 }
n@297 1432 }
n@180 1433 };
n@180 1434
n@180 1435 this.prepostNode = function(type,Collection) {
n@180 1436 this.type = type;
n@180 1437 this.options = [];
n@180 1438
n@180 1439 this.OptionNode = function(child) {
nicholas@188 1440
nicholas@188 1441 this.childOption = function(element) {
nicholas@188 1442 this.type = 'option';
nicholas@188 1443 this.id = element.id;
nicholas@189 1444 this.name = element.getAttribute('name');
nicholas@188 1445 this.text = element.textContent;
n@191 1446 };
nicholas@188 1447
n@180 1448 this.type = child.nodeName;
n@180 1449 if (child.nodeName == "question") {
n@180 1450 this.id = child.id;
n@180 1451 this.mandatory;
n@180 1452 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
n@180 1453 else {this.mandatory = false;}
n@180 1454 this.question = child.textContent;
n@191 1455 if (child.getAttribute('boxsize') == null) {
n@191 1456 this.boxsize = 'normal';
n@191 1457 } else {
n@191 1458 this.boxsize = child.getAttribute('boxsize');
n@191 1459 }
n@180 1460 } else if (child.nodeName == "statement") {
n@181 1461 this.statement = child.textContent;
nicholas@189 1462 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
nicholas@188 1463 var element = child.firstElementChild;
nicholas@189 1464 this.id = child.id;
nicholas@188 1465 if (element == null) {
nicholas@189 1466 console.log('Malformed' +child.nodeName+ 'entry');
nicholas@189 1467 this.statement = 'Malformed' +child.nodeName+ 'entry';
nicholas@188 1468 this.type = 'statement';
nicholas@188 1469 } else {
nicholas@188 1470 this.options = [];
nicholas@188 1471 while (element != null) {
nicholas@188 1472 if (element.nodeName == 'statement' && this.statement == undefined){
nicholas@188 1473 this.statement = element.textContent;
nicholas@188 1474 } else if (element.nodeName == 'option') {
nicholas@188 1475 this.options.push(new this.childOption(element));
nicholas@188 1476 }
nicholas@188 1477 element = element.nextElementSibling;
nicholas@188 1478 }
nicholas@188 1479 }
n@196 1480 } else if (child.nodeName == "number") {
n@196 1481 this.statement = child.textContent;
n@196 1482 this.id = child.id;
n@196 1483 this.min = child.getAttribute('min');
n@196 1484 this.max = child.getAttribute('max');
n@196 1485 this.step = child.getAttribute('step');
n@180 1486 }
n@180 1487 };
n@180 1488
n@180 1489 // On construction:
n@180 1490 if (Collection.length != 0) {
n@180 1491 Collection = Collection[0];
nicholas@186 1492 if (Collection.childElementCount != 0) {
nicholas@186 1493 var child = Collection.firstElementChild;
n@180 1494 this.options.push(new this.OptionNode(child));
nicholas@186 1495 while (child.nextElementSibling != null) {
nicholas@186 1496 child = child.nextElementSibling;
nicholas@186 1497 this.options.push(new this.OptionNode(child));
nicholas@186 1498 }
n@180 1499 }
n@180 1500 }
n@180 1501 };
n@180 1502
n@180 1503 this.metricNode = function(name) {
n@180 1504 this.enabled = name;
n@180 1505 };
n@180 1506
n@180 1507 this.audioHolderNode = function(parent,xml) {
n@181 1508 this.type = 'audioHolder';
nicholas@274 1509 this.presentedId = parent.audioHolders.length;
n@180 1510 this.interfaceNode = function(DOM) {
n@180 1511 var title = DOM.getElementsByTagName('title');
n@180 1512 if (title.length == 0) {this.title = null;}
n@180 1513 else {this.title = title[0].textContent;}
nicholas@231 1514 this.options = parent.commonInterface.options;
n@180 1515 var scale = DOM.getElementsByTagName('scale');
n@180 1516 this.scale = [];
n@180 1517 for (var i=0; i<scale.length; i++) {
n@180 1518 var arr = [null, null];
n@180 1519 arr[0] = scale[i].getAttribute('position');
n@180 1520 arr[1] = scale[i].textContent;
n@180 1521 this.scale.push(arr);
n@180 1522 }
n@180 1523 };
n@180 1524
n@182 1525 this.audioElementNode = function(parent,xml) {
n@180 1526 this.url = xml.getAttribute('url');
n@180 1527 this.id = xml.id;
n@182 1528 this.parent = parent;
nicholas@235 1529 this.type = xml.getAttribute('type');
nicholas@235 1530 if (this.type == null) {this.type = "normal";}
nicholas@235 1531 if (this.type == 'anchor') {this.anchor = true;}
nicholas@215 1532 else {this.anchor = false;}
nicholas@235 1533 if (this.type == 'reference') {this.reference = true;}
nicholas@215 1534 else {this.reference = false;}
nicholas@215 1535
n@324 1536 if (this.anchor == true || this.reference == true)
n@324 1537 {
n@324 1538 this.marker = xml.getAttribute('marker');
n@324 1539 if (this.marker != undefined)
n@324 1540 {
n@324 1541 this.marker = Number(this.marker);
n@324 1542 if (isNaN(this.marker) == false)
n@324 1543 {
n@324 1544 if (this.marker > 1)
n@324 1545 { this.marker /= 100.0;}
n@324 1546 if (this.marker >= 0 && this.marker <= 1)
n@324 1547 {
n@324 1548 this.enforce = true;
n@324 1549 return;
n@324 1550 } else {
n@324 1551 console.log("ERROR - Marker of audioElement "+this.id+" is not between 0 and 1 (float) or 0 and 100 (integer)!");
n@324 1552 console.log("ERROR - Marker not enforced!");
n@324 1553 }
n@324 1554 } else {
n@324 1555 console.log("ERROR - Marker of audioElement "+this.id+" is not a number!");
n@324 1556 console.log("ERROR - Marker not enforced!");
n@324 1557 }
n@324 1558 }
nicholas@219 1559 }
n@324 1560 this.marker = false;
n@324 1561 this.enforce = false;
n@180 1562 };
n@180 1563
n@180 1564 this.commentQuestionNode = function(xml) {
n@193 1565 this.childOption = function(element) {
n@193 1566 this.type = 'option';
n@193 1567 this.name = element.getAttribute('name');
n@193 1568 this.text = element.textContent;
n@193 1569 };
n@180 1570 this.id = xml.id;
n@180 1571 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
n@180 1572 else {this.mandatory = false;}
n@193 1573 this.type = xml.getAttribute('type');
n@193 1574 if (this.type == undefined) {this.type = 'text';}
n@193 1575 switch (this.type) {
n@193 1576 case 'text':
n@193 1577 this.question = xml.textContent;
n@193 1578 break;
n@193 1579 case 'radio':
n@193 1580 var child = xml.firstElementChild;
n@193 1581 this.options = [];
n@193 1582 while (child != undefined) {
n@193 1583 if (child.nodeName == 'statement' && this.statement == undefined) {
n@193 1584 this.statement = child.textContent;
n@193 1585 } else if (child.nodeName == 'option') {
n@193 1586 this.options.push(new this.childOption(child));
n@193 1587 }
n@193 1588 child = child.nextElementSibling;
n@193 1589 }
n@195 1590 break;
n@195 1591 case 'checkbox':
n@195 1592 var child = xml.firstElementChild;
n@195 1593 this.options = [];
n@195 1594 while (child != undefined) {
n@195 1595 if (child.nodeName == 'statement' && this.statement == undefined) {
n@195 1596 this.statement = child.textContent;
n@195 1597 } else if (child.nodeName == 'option') {
n@195 1598 this.options.push(new this.childOption(child));
n@195 1599 }
n@195 1600 child = child.nextElementSibling;
n@195 1601 }
n@195 1602 break;
n@193 1603 }
n@180 1604 };
n@180 1605
n@180 1606 this.id = xml.id;
n@180 1607 this.hostURL = xml.getAttribute('hostURL');
n@180 1608 this.sampleRate = xml.getAttribute('sampleRate');
n@180 1609 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
n@180 1610 else {this.randomiseOrder = false;}
n@180 1611 this.repeatCount = xml.getAttribute('repeatCount');
n@180 1612 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
n@180 1613 else {this.loop == false;}
n@180 1614 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
n@180 1615 else {this.elementComments = false;}
n@180 1616
n@181 1617 this.preTest = new parent.prepostNode('pretest',xml.getElementsByTagName('PreTest'));
n@181 1618 this.postTest = new parent.prepostNode('posttest',xml.getElementsByTagName('PostTest'));
n@180 1619
n@180 1620 this.interfaces = [];
n@180 1621 var interfaceDOM = xml.getElementsByTagName('interface');
n@180 1622 for (var i=0; i<interfaceDOM.length; i++) {
n@180 1623 this.interfaces.push(new this.interfaceNode(interfaceDOM[i]));
n@180 1624 }
n@180 1625
n@180 1626 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
n@180 1627 if (this.commentBoxPrefix.length != 0) {
n@180 1628 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
n@180 1629 } else {
n@180 1630 this.commentBoxPrefix = "Comment on track";
n@180 1631 }
n@180 1632
n@180 1633 this.audioElements =[];
n@180 1634 var audioElementsDOM = xml.getElementsByTagName('audioElements');
nicholas@236 1635 this.outsideReference = null;
n@180 1636 for (var i=0; i<audioElementsDOM.length; i++) {
nicholas@236 1637 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') {
nicholas@236 1638 if (this.outsideReference == null) {
nicholas@236 1639 this.outsideReference = new this.audioElementNode(this,audioElementsDOM[i]);
nicholas@236 1640 } else {
nicholas@236 1641 console.log('Error only one audioelement can be of type outsidereference per audioholder');
nicholas@236 1642 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
nicholas@236 1643 console.log('Element id '+audioElementsDOM[i].id+' made into normal node');
nicholas@236 1644 }
nicholas@236 1645 } else {
nicholas@236 1646 this.audioElements.push(new this.audioElementNode(this,audioElementsDOM[i]));
nicholas@236 1647 }
n@180 1648 }
n@180 1649
nicholas@235 1650 if (this.randomiseOrder) {
nicholas@235 1651 this.audioElements = randomiseOrder(this.audioElements);
nicholas@235 1652 }
nicholas@235 1653
n@180 1654 this.commentQuestions = [];
n@180 1655 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
n@180 1656 for (var i=0; i<commentQuestionsDOM.length; i++) {
n@180 1657 this.commentQuestions.push(new this.commentQuestionNode(commentQuestionsDOM[i]));
n@180 1658 }
n@180 1659 };
n@180 1660 }
n@180 1661
n@182 1662 function Interface(specificationObject) {
n@180 1663 // This handles the bindings between the interface and the audioEngineContext;
n@182 1664 this.specification = specificationObject;
n@182 1665 this.insertPoint = document.getElementById("topLevelBody");
n@180 1666
n@182 1667 // Bounded by interface!!
n@182 1668 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
n@182 1669 // For example, APE returns the slider position normalised in a <value> tag.
n@182 1670 this.interfaceObjects = [];
n@182 1671 this.interfaceObject = function(){};
n@182 1672
n@302 1673 this.resizeWindow = function(event)
n@302 1674 {
n@302 1675 for(var i=0; i<this.commentBoxes.length; i++)
n@302 1676 {this.commentBoxes[i].resize();}
n@302 1677 for(var i=0; i<this.commentQuestions.length; i++)
n@302 1678 {this.commentQuestions[i].resize();}
n@302 1679 try
n@302 1680 {
n@302 1681 resizeWindow(event);
n@302 1682 }
n@302 1683 catch(err)
n@302 1684 {
n@302 1685 console.log("Warning - Interface does not have Resize option");
n@302 1686 console.log(err);
n@302 1687 }
n@302 1688 };
n@302 1689
n@356 1690 this.returnNavigator = function()
n@356 1691 {
n@356 1692 var node = document.createElement("navigator");
n@356 1693 var platform = document.createElement("platform");
n@356 1694 platform.textContent = navigator.platform;
n@356 1695 var vendor = document.createElement("vendor");
n@356 1696 vendor.textContent = navigator.vendor;
n@356 1697 var userAgent = document.createElement("uagent");
n@356 1698 userAgent.textContent = navigator.userAgent;
n@356 1699 node.appendChild(platform);
n@356 1700 node.appendChild(vendor);
n@356 1701 node.appendChild(userAgent);
n@356 1702 return node;
n@356 1703 };
n@356 1704
n@182 1705 this.commentBoxes = [];
n@193 1706 this.elementCommentBox = function(audioObject) {
n@182 1707 var element = audioObject.specification;
n@183 1708 this.audioObject = audioObject;
n@182 1709 this.id = audioObject.id;
n@182 1710 var audioHolderObject = audioObject.specification.parent;
n@182 1711 // Create document objects to hold the comment boxes
n@182 1712 this.trackComment = document.createElement('div');
n@182 1713 this.trackComment.className = 'comment-div';
n@182 1714 this.trackComment.id = 'comment-div-'+audioObject.id;
n@182 1715 // Create a string next to each comment asking for a comment
n@183 1716 this.trackString = document.createElement('span');
n@183 1717 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
n@182 1718 // Create the HTML5 comment box 'textarea'
n@183 1719 this.trackCommentBox = document.createElement('textarea');
n@183 1720 this.trackCommentBox.rows = '4';
n@183 1721 this.trackCommentBox.cols = '100';
n@183 1722 this.trackCommentBox.name = 'trackComment'+audioObject.id;
n@183 1723 this.trackCommentBox.className = 'trackComment';
n@182 1724 var br = document.createElement('br');
n@182 1725 // Add to the holder.
n@183 1726 this.trackComment.appendChild(this.trackString);
n@182 1727 this.trackComment.appendChild(br);
n@183 1728 this.trackComment.appendChild(this.trackCommentBox);
n@183 1729
n@183 1730 this.exportXMLDOM = function() {
n@183 1731 var root = document.createElement('comment');
n@183 1732 if (this.audioObject.specification.parent.elementComments) {
n@183 1733 var question = document.createElement('question');
n@183 1734 question.textContent = this.trackString.textContent;
n@183 1735 var response = document.createElement('response');
n@183 1736 response.textContent = this.trackCommentBox.value;
nicholas@249 1737 console.log("Comment frag-"+this.id+": "+response.textContent);
n@183 1738 root.appendChild(question);
n@183 1739 root.appendChild(response);
n@183 1740 }
n@183 1741 return root;
n@183 1742 };
n@302 1743 this.resize = function()
n@302 1744 {
n@302 1745 var boxwidth = (window.innerWidth-100)/2;
n@302 1746 if (boxwidth >= 600)
n@302 1747 {
n@302 1748 boxwidth = 600;
n@302 1749 }
n@302 1750 else if (boxwidth < 400)
n@302 1751 {
n@302 1752 boxwidth = 400;
n@302 1753 }
n@302 1754 this.trackComment.style.width = boxwidth+"px";
n@302 1755 this.trackCommentBox.style.width = boxwidth-6+"px";
n@302 1756 };
n@302 1757 this.resize();
n@182 1758 };
n@182 1759
n@193 1760 this.commentQuestions = [];
n@193 1761
n@193 1762 this.commentBox = function(commentQuestion) {
n@193 1763 this.specification = commentQuestion;
n@193 1764 // Create document objects to hold the comment boxes
n@193 1765 this.holder = document.createElement('div');
n@193 1766 this.holder.className = 'comment-div';
n@193 1767 // Create a string next to each comment asking for a comment
n@193 1768 this.string = document.createElement('span');
n@193 1769 this.string.innerHTML = commentQuestion.question;
n@193 1770 // Create the HTML5 comment box 'textarea'
n@193 1771 this.textArea = document.createElement('textarea');
n@193 1772 this.textArea.rows = '4';
n@193 1773 this.textArea.cols = '100';
n@193 1774 this.textArea.className = 'trackComment';
n@193 1775 var br = document.createElement('br');
n@193 1776 // Add to the holder.
n@193 1777 this.holder.appendChild(this.string);
n@193 1778 this.holder.appendChild(br);
n@193 1779 this.holder.appendChild(this.textArea);
n@193 1780
n@193 1781 this.exportXMLDOM = function() {
n@193 1782 var root = document.createElement('comment');
n@193 1783 root.id = this.specification.id;
n@193 1784 root.setAttribute('type',this.specification.type);
n@193 1785 root.textContent = this.textArea.value;
b@254 1786 console.log("Question: "+this.string.textContent);
b@254 1787 console.log("Response: "+root.textContent);
n@193 1788 return root;
n@193 1789 };
n@302 1790 this.resize = function()
n@302 1791 {
n@302 1792 var boxwidth = (window.innerWidth-100)/2;
n@302 1793 if (boxwidth >= 600)
n@302 1794 {
n@302 1795 boxwidth = 600;
n@302 1796 }
n@302 1797 else if (boxwidth < 400)
n@302 1798 {
n@302 1799 boxwidth = 400;
n@302 1800 }
n@302 1801 this.holder.style.width = boxwidth+"px";
n@302 1802 this.textArea.style.width = boxwidth-6+"px";
n@302 1803 };
n@302 1804 this.resize();
n@193 1805 };
n@193 1806
n@193 1807 this.radioBox = function(commentQuestion) {
n@193 1808 this.specification = commentQuestion;
n@193 1809 // Create document objects to hold the comment boxes
n@193 1810 this.holder = document.createElement('div');
n@193 1811 this.holder.className = 'comment-div';
n@193 1812 // Create a string next to each comment asking for a comment
n@193 1813 this.string = document.createElement('span');
n@193 1814 this.string.innerHTML = commentQuestion.statement;
n@193 1815 var br = document.createElement('br');
n@193 1816 // Add to the holder.
n@193 1817 this.holder.appendChild(this.string);
n@193 1818 this.holder.appendChild(br);
n@193 1819 this.options = [];
n@193 1820 this.inputs = document.createElement('div');
n@193 1821 this.span = document.createElement('div');
n@193 1822 this.inputs.align = 'center';
n@193 1823 this.inputs.style.marginLeft = '12px';
n@193 1824 this.span.style.marginLeft = '12px';
n@193 1825 this.span.align = 'center';
n@193 1826 this.span.style.marginTop = '15px';
n@193 1827
n@193 1828 var optCount = commentQuestion.options.length;
n@193 1829 for (var i=0; i<optCount; i++)
n@193 1830 {
n@193 1831 var div = document.createElement('div');
n@301 1832 div.style.width = '80px';
n@193 1833 div.style.float = 'left';
n@193 1834 var input = document.createElement('input');
n@193 1835 input.type = 'radio';
n@193 1836 input.name = commentQuestion.id;
n@193 1837 input.setAttribute('setvalue',commentQuestion.options[i].name);
n@193 1838 input.className = 'comment-radio';
n@193 1839 div.appendChild(input);
n@193 1840 this.inputs.appendChild(div);
n@193 1841
n@193 1842
n@193 1843 div = document.createElement('div');
n@301 1844 div.style.width = '80px';
n@193 1845 div.style.float = 'left';
n@193 1846 div.align = 'center';
n@193 1847 var span = document.createElement('span');
n@193 1848 span.textContent = commentQuestion.options[i].text;
n@193 1849 span.className = 'comment-radio-span';
n@193 1850 div.appendChild(span);
n@193 1851 this.span.appendChild(div);
n@193 1852 this.options.push(input);
n@193 1853 }
n@193 1854 this.holder.appendChild(this.span);
n@193 1855 this.holder.appendChild(this.inputs);
n@193 1856
n@193 1857 this.exportXMLDOM = function() {
n@193 1858 var root = document.createElement('comment');
n@193 1859 root.id = this.specification.id;
n@193 1860 root.setAttribute('type',this.specification.type);
n@193 1861 var question = document.createElement('question');
n@193 1862 question.textContent = this.string.textContent;
n@193 1863 var response = document.createElement('response');
n@193 1864 var i=0;
n@193 1865 while(this.options[i].checked == false) {
n@193 1866 i++;
n@193 1867 if (i >= this.options.length) {
n@193 1868 break;
n@193 1869 }
n@193 1870 }
n@193 1871 if (i >= this.options.length) {
n@193 1872 response.textContent = 'null';
n@193 1873 } else {
n@193 1874 response.textContent = this.options[i].getAttribute('setvalue');
n@193 1875 response.setAttribute('number',i);
n@193 1876 }
n@195 1877 console.log('Comment: '+question.textContent);
n@195 1878 console.log('Response: '+response.textContent);
n@193 1879 root.appendChild(question);
n@193 1880 root.appendChild(response);
n@193 1881 return root;
n@193 1882 };
n@302 1883 this.resize = function()
n@302 1884 {
n@302 1885 var boxwidth = (window.innerWidth-100)/2;
n@302 1886 if (boxwidth >= 600)
n@302 1887 {
n@302 1888 boxwidth = 600;
n@302 1889 }
n@302 1890 else if (boxwidth < 400)
n@302 1891 {
n@302 1892 boxwidth = 400;
n@302 1893 }
n@302 1894 this.holder.style.width = boxwidth+"px";
n@302 1895 var text = this.holder.children[2];
n@302 1896 var options = this.holder.children[3];
n@302 1897 var optCount = options.children.length;
n@302 1898 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
n@302 1899 var options = options.firstChild;
n@302 1900 var text = text.firstChild;
n@302 1901 options.style.marginRight = spanMargin;
n@302 1902 options.style.marginLeft = spanMargin;
n@302 1903 text.style.marginRight = spanMargin;
n@302 1904 text.style.marginLeft = spanMargin;
n@302 1905 while(options.nextSibling != undefined)
n@302 1906 {
n@302 1907 options = options.nextSibling;
n@302 1908 text = text.nextSibling;
n@302 1909 options.style.marginRight = spanMargin;
n@302 1910 options.style.marginLeft = spanMargin;
n@302 1911 text.style.marginRight = spanMargin;
n@302 1912 text.style.marginLeft = spanMargin;
n@302 1913 }
n@302 1914 };
n@302 1915 this.resize();
n@193 1916 };
n@193 1917
n@195 1918 this.checkboxBox = function(commentQuestion) {
n@195 1919 this.specification = commentQuestion;
n@195 1920 // Create document objects to hold the comment boxes
n@195 1921 this.holder = document.createElement('div');
n@195 1922 this.holder.className = 'comment-div';
n@195 1923 // Create a string next to each comment asking for a comment
n@195 1924 this.string = document.createElement('span');
n@195 1925 this.string.innerHTML = commentQuestion.statement;
n@195 1926 var br = document.createElement('br');
n@195 1927 // Add to the holder.
n@195 1928 this.holder.appendChild(this.string);
n@195 1929 this.holder.appendChild(br);
n@195 1930 this.options = [];
n@195 1931 this.inputs = document.createElement('div');
n@195 1932 this.span = document.createElement('div');
n@195 1933 this.inputs.align = 'center';
n@195 1934 this.inputs.style.marginLeft = '12px';
n@195 1935 this.span.style.marginLeft = '12px';
n@195 1936 this.span.align = 'center';
n@195 1937 this.span.style.marginTop = '15px';
n@195 1938
n@195 1939 var optCount = commentQuestion.options.length;
n@195 1940 for (var i=0; i<optCount; i++)
n@195 1941 {
n@195 1942 var div = document.createElement('div');
n@301 1943 div.style.width = '80px';
n@195 1944 div.style.float = 'left';
n@195 1945 var input = document.createElement('input');
n@195 1946 input.type = 'checkbox';
n@195 1947 input.name = commentQuestion.id;
n@195 1948 input.setAttribute('setvalue',commentQuestion.options[i].name);
n@195 1949 input.className = 'comment-radio';
n@195 1950 div.appendChild(input);
n@195 1951 this.inputs.appendChild(div);
n@195 1952
n@195 1953
n@195 1954 div = document.createElement('div');
n@301 1955 div.style.width = '80px';
n@195 1956 div.style.float = 'left';
n@195 1957 div.align = 'center';
n@195 1958 var span = document.createElement('span');
n@195 1959 span.textContent = commentQuestion.options[i].text;
n@195 1960 span.className = 'comment-radio-span';
n@195 1961 div.appendChild(span);
n@195 1962 this.span.appendChild(div);
n@195 1963 this.options.push(input);
n@195 1964 }
n@195 1965 this.holder.appendChild(this.span);
n@195 1966 this.holder.appendChild(this.inputs);
n@195 1967
n@195 1968 this.exportXMLDOM = function() {
n@195 1969 var root = document.createElement('comment');
n@195 1970 root.id = this.specification.id;
n@195 1971 root.setAttribute('type',this.specification.type);
n@195 1972 var question = document.createElement('question');
n@195 1973 question.textContent = this.string.textContent;
n@195 1974 root.appendChild(question);
n@195 1975 console.log('Comment: '+question.textContent);
n@195 1976 for (var i=0; i<this.options.length; i++) {
n@195 1977 var response = document.createElement('response');
n@195 1978 response.textContent = this.options[i].checked;
n@195 1979 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
n@195 1980 root.appendChild(response);
n@195 1981 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
n@195 1982 }
n@195 1983 return root;
n@195 1984 };
n@302 1985 this.resize = function()
n@302 1986 {
n@302 1987 var boxwidth = (window.innerWidth-100)/2;
n@302 1988 if (boxwidth >= 600)
n@302 1989 {
n@302 1990 boxwidth = 600;
n@302 1991 }
n@302 1992 else if (boxwidth < 400)
n@302 1993 {
n@302 1994 boxwidth = 400;
n@302 1995 }
n@302 1996 this.holder.style.width = boxwidth+"px";
n@302 1997 var text = this.holder.children[2];
n@302 1998 var options = this.holder.children[3];
n@302 1999 var optCount = options.children.length;
n@302 2000 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
n@302 2001 var options = options.firstChild;
n@302 2002 var text = text.firstChild;
n@302 2003 options.style.marginRight = spanMargin;
n@302 2004 options.style.marginLeft = spanMargin;
n@302 2005 text.style.marginRight = spanMargin;
n@302 2006 text.style.marginLeft = spanMargin;
n@302 2007 while(options.nextSibling != undefined)
n@302 2008 {
n@302 2009 options = options.nextSibling;
n@302 2010 text = text.nextSibling;
n@302 2011 options.style.marginRight = spanMargin;
n@302 2012 options.style.marginLeft = spanMargin;
n@302 2013 text.style.marginRight = spanMargin;
n@302 2014 text.style.marginLeft = spanMargin;
n@302 2015 }
n@302 2016 };
n@302 2017 this.resize();
n@195 2018 };
n@193 2019
n@182 2020 this.createCommentBox = function(audioObject) {
n@193 2021 var node = new this.elementCommentBox(audioObject);
n@182 2022 this.commentBoxes.push(node);
n@182 2023 audioObject.commentDOM = node;
n@182 2024 return node;
n@182 2025 };
n@182 2026
n@182 2027 this.sortCommentBoxes = function() {
n@182 2028 var holder = [];
n@182 2029 while (this.commentBoxes.length > 0) {
n@182 2030 var node = this.commentBoxes.pop(0);
n@182 2031 holder[node.id] = node;
n@182 2032 }
n@182 2033 this.commentBoxes = holder;
n@182 2034 };
n@182 2035
n@182 2036 this.showCommentBoxes = function(inject, sort) {
n@182 2037 if (sort) {interfaceContext.sortCommentBoxes();}
n@182 2038 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
n@182 2039 inject.appendChild(this.commentBoxes[i].trackComment);
n@182 2040 }
n@182 2041 };
n@193 2042
nicholas@211 2043 this.deleteCommentBoxes = function() {
nicholas@211 2044 this.commentBoxes = [];
nicholas@237 2045 };
nicholas@211 2046
n@193 2047 this.createCommentQuestion = function(element) {
n@193 2048 var node;
n@193 2049 if (element.type == 'text') {
n@193 2050 node = new this.commentBox(element);
n@193 2051 } else if (element.type == 'radio') {
n@193 2052 node = new this.radioBox(element);
n@195 2053 } else if (element.type == 'checkbox') {
n@195 2054 node = new this.checkboxBox(element);
n@193 2055 }
n@193 2056 this.commentQuestions.push(node);
n@193 2057 return node;
n@193 2058 };
n@201 2059
nicholas@237 2060 this.deleteCommentQuestions = function()
nicholas@237 2061 {
nicholas@237 2062 this.commentQuestions = [];
nicholas@237 2063 };
nicholas@237 2064
n@201 2065 this.playhead = new function()
n@201 2066 {
n@201 2067 this.object = document.createElement('div');
n@201 2068 this.object.className = 'playhead';
n@201 2069 this.object.align = 'left';
n@201 2070 var curTime = document.createElement('div');
n@201 2071 curTime.style.width = '50px';
n@201 2072 this.curTimeSpan = document.createElement('span');
n@201 2073 this.curTimeSpan.textContent = '00:00';
n@201 2074 curTime.appendChild(this.curTimeSpan);
n@201 2075 this.object.appendChild(curTime);
n@201 2076 this.scrubberTrack = document.createElement('div');
n@201 2077 this.scrubberTrack.className = 'playhead-scrub-track';
n@201 2078
n@201 2079 this.scrubberHead = document.createElement('div');
n@201 2080 this.scrubberHead.id = 'playhead-scrubber';
n@201 2081 this.scrubberTrack.appendChild(this.scrubberHead);
n@201 2082 this.object.appendChild(this.scrubberTrack);
n@201 2083
n@201 2084 this.timePerPixel = 0;
n@201 2085 this.maxTime = 0;
n@201 2086
n@204 2087 this.playbackObject;
n@204 2088
n@204 2089 this.setTimePerPixel = function(audioObject) {
n@201 2090 //maxTime must be in seconds
n@204 2091 this.playbackObject = audioObject;
n@204 2092 this.maxTime = audioObject.buffer.duration;
n@201 2093 var width = 490; //500 - 10, 5 each side of the tracker head
n@204 2094 this.timePerPixel = this.maxTime/490;
n@204 2095 if (this.maxTime < 60) {
n@201 2096 this.curTimeSpan.textContent = '0.00';
n@201 2097 } else {
n@201 2098 this.curTimeSpan.textContent = '00:00';
n@201 2099 }
n@201 2100 };
n@201 2101
n@204 2102 this.update = function() {
n@201 2103 // Update the playhead position, startPlay must be called
n@201 2104 if (this.timePerPixel > 0) {
n@204 2105 var time = this.playbackObject.getCurrentPosition();
nicholas@267 2106 if (time > 0) {
nicholas@267 2107 var width = 490;
nicholas@267 2108 var pix = Math.floor(time/this.timePerPixel);
nicholas@267 2109 this.scrubberHead.style.left = pix+'px';
nicholas@267 2110 if (this.maxTime > 60.0) {
nicholas@267 2111 var secs = time%60;
nicholas@267 2112 var mins = Math.floor((time-secs)/60);
nicholas@267 2113 secs = secs.toString();
nicholas@267 2114 secs = secs.substr(0,2);
nicholas@267 2115 mins = mins.toString();
nicholas@267 2116 this.curTimeSpan.textContent = mins+':'+secs;
nicholas@267 2117 } else {
nicholas@267 2118 time = time.toString();
nicholas@267 2119 this.curTimeSpan.textContent = time.substr(0,4);
nicholas@267 2120 }
n@201 2121 } else {
nicholas@267 2122 this.scrubberHead.style.left = '0px';
nicholas@267 2123 if (this.maxTime < 60) {
nicholas@267 2124 this.curTimeSpan.textContent = '0.00';
nicholas@267 2125 } else {
nicholas@267 2126 this.curTimeSpan.textContent = '00:00';
nicholas@267 2127 }
n@201 2128 }
n@201 2129 }
n@201 2130 };
n@204 2131
n@204 2132 this.interval = undefined;
n@204 2133
n@204 2134 this.start = function() {
n@204 2135 if (this.playbackObject != undefined && this.interval == undefined) {
nicholas@267 2136 if (this.maxTime < 60) {
nicholas@267 2137 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
nicholas@267 2138 } else {
nicholas@267 2139 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
nicholas@267 2140 }
n@204 2141 }
n@204 2142 };
n@204 2143 this.stop = function() {
n@204 2144 clearInterval(this.interval);
n@204 2145 this.interval = undefined;
nicholas@267 2146 if (this.maxTime < 60) {
nicholas@267 2147 this.curTimeSpan.textContent = '0.00';
nicholas@267 2148 } else {
nicholas@267 2149 this.curTimeSpan.textContent = '00:00';
nicholas@267 2150 }
n@204 2151 };
n@201 2152 };
nicholas@235 2153
nicholas@235 2154 // Global Checkers
nicholas@235 2155 // These functions will help enforce the checkers
nicholas@235 2156 this.checkHiddenAnchor = function()
nicholas@235 2157 {
nicholas@235 2158 var audioHolder = testState.currentStateMap[testState.currentIndex];
nicholas@235 2159 if (audioHolder.anchorId != null)
nicholas@235 2160 {
nicholas@235 2161 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId];
nicholas@269 2162 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
nicholas@235 2163 {
nicholas@235 2164 // Anchor is not set below
nicholas@235 2165 console.log('Anchor node not below marker value');
nicholas@235 2166 alert('Please keep listening');
nicholas@235 2167 return false;
nicholas@235 2168 }
nicholas@235 2169 }
nicholas@235 2170 return true;
nicholas@235 2171 };
nicholas@235 2172
nicholas@235 2173 this.checkHiddenReference = function()
nicholas@235 2174 {
nicholas@235 2175 var audioHolder = testState.currentStateMap[testState.currentIndex];
nicholas@235 2176 if (audioHolder.referenceId != null)
nicholas@235 2177 {
nicholas@235 2178 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId];
nicholas@269 2179 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
nicholas@235 2180 {
nicholas@235 2181 // Anchor is not set below
nicholas@235 2182 console.log('Reference node not above marker value');
nicholas@235 2183 alert('Please keep listening');
nicholas@235 2184 return false;
nicholas@235 2185 }
nicholas@235 2186 }
nicholas@235 2187 return true;
nicholas@235 2188 };
nicholas@252 2189 }