annotate core.js @ 433:f57ca6e75aec Dev_main

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