annotate core.js @ 759:801e1977ab55

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