annotate core.js @ 771:0796d28701ae

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