annotate core.js @ 758:57a3cbf90e01

MUSHRA: Set the initial position of sliders using the <audioHolder> attribute initial-position using integer 0 to 100
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Thu, 17 Dec 2015 16:10:58 +0000
parents 46acb0963059
children 801e1977ab55
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@755 770 this.users = [];
nicholas@755 771 this.getMedia = function(url) {
nicholas@755 772 this.url = url;
nicholas@755 773 this.xmlRequest.open('GET',this.url,true);
nicholas@755 774 this.xmlRequest.responseType = 'arraybuffer';
nicholas@755 775
nicholas@755 776 var bufferObj = this;
nicholas@755 777
nicholas@755 778 // Create callback to decode the data asynchronously
nicholas@755 779 this.xmlRequest.onloadend = function() {
nicholas@755 780 audioContext.decodeAudioData(bufferObj.xmlRequest.response, function(decodedData) {
nicholas@755 781 bufferObj.buffer = decodedData;
nicholas@755 782 for (var i=0; i<bufferObj.users.length; i++)
nicholas@755 783 {
nicholas@755 784 bufferObj.users[i].state = 1;
nicholas@755 785 if (bufferObj.users[i].interfaceDOM != null)
nicholas@755 786 {
nicholas@755 787 bufferObj.users[i].bufferLoaded(bufferObj);
nicholas@755 788 }
nicholas@755 789 }
nicholas@755 790 calculateLoudness(bufferObj.buffer,"I");
nicholas@755 791 }, function(){
nicholas@755 792 // Should only be called if there was an error, but sometimes gets called continuously
nicholas@755 793 // Check here if the error is genuine
nicholas@755 794 if (bufferObj.buffer == undefined) {
nicholas@755 795 // Genuine error
nicholas@755 796 console.log('FATAL - Error loading buffer on '+audioObj.id);
nicholas@755 797 if (request.status == 404)
nicholas@755 798 {
nicholas@755 799 console.log('FATAL - Fragment '+audioObj.id+' 404 error');
nicholas@755 800 console.log('URL: '+audioObj.url);
nicholas@755 801 errorSessionDump('Fragment '+audioObj.id+' 404 error');
nicholas@755 802 }
nicholas@755 803 }
nicholas@755 804 });
nicholas@755 805 };
nicholas@755 806 this.progress = 0;
nicholas@755 807 this.progressCallback = function(event){
nicholas@755 808 if (event.lengthComputable)
nicholas@755 809 {
nicholas@755 810 this.progress = event.loaded / event.total;
nicholas@755 811 }
nicholas@755 812 };
nicholas@755 813 this.xmlRequest.addEventListener("progress", this.progressCallback);
nicholas@755 814 this.xmlRequest.send();
nicholas@755 815 };
nicholas@755 816 };
nicholas@755 817
nicholas@755 818 this.play = function(id) {
nicholas@755 819 // Start the timer and set the audioEngine state to playing (1)
nicholas@755 820 if (this.status == 0 && this.loopPlayback) {
nicholas@755 821 // Check if all audioObjects are ready
nicholas@755 822 if(this.checkAllReady())
nicholas@755 823 {
nicholas@755 824 this.status = 1;
nicholas@755 825 this.setSynchronousLoop();
nicholas@755 826 }
nicholas@755 827 }
nicholas@755 828 else
nicholas@755 829 {
nicholas@755 830 this.status = 1;
nicholas@755 831 }
nicholas@755 832 if (this.status== 1) {
nicholas@755 833 this.timer.startTest();
nicholas@755 834 if (id == undefined) {
nicholas@755 835 id = -1;
nicholas@755 836 console.log('FATAL - Passed id was undefined - AudioEngineContext.play(id)');
nicholas@755 837 return;
nicholas@755 838 } else {
nicholas@755 839 interfaceContext.playhead.setTimePerPixel(this.audioObjects[id]);
nicholas@755 840 }
nicholas@755 841 if (this.loopPlayback) {
nicholas@755 842 for (var i=0; i<this.audioObjects.length; i++)
nicholas@755 843 {
nicholas@755 844 this.audioObjects[i].play(this.timer.getTestTime()+1);
nicholas@755 845 if (id == i) {
nicholas@755 846 this.audioObjects[i].loopStart();
nicholas@755 847 } else {
nicholas@755 848 this.audioObjects[i].loopStop();
nicholas@755 849 }
nicholas@755 850 }
nicholas@755 851 } else {
nicholas@755 852 for (var i=0; i<this.audioObjects.length; i++)
nicholas@755 853 {
nicholas@755 854 if (i != id) {
nicholas@755 855 this.audioObjects[i].outputGain.gain.value = 0.0;
nicholas@755 856 this.audioObjects[i].stop();
nicholas@755 857 } else if (i == id) {
nicholas@755 858 this.audioObjects[id].outputGain.gain.value = this.audioObjects[id].specification.gain*this.audioObjects[id].buffer.buffer.gain;
nicholas@755 859 this.audioObjects[id].play(audioContext.currentTime+0.01);
nicholas@755 860 }
nicholas@755 861 }
nicholas@755 862 }
nicholas@755 863 interfaceContext.playhead.start();
nicholas@755 864 }
nicholas@755 865 };
nicholas@755 866
nicholas@755 867 this.stop = function() {
nicholas@755 868 // Send stop and reset command to all playback buffers and set audioEngine state to stopped (1)
nicholas@755 869 if (this.status == 1) {
nicholas@755 870 for (var i=0; i<this.audioObjects.length; i++)
nicholas@755 871 {
nicholas@755 872 this.audioObjects[i].stop();
nicholas@755 873 }
nicholas@755 874 interfaceContext.playhead.stop();
nicholas@755 875 this.status = 0;
nicholas@755 876 }
nicholas@755 877 };
nicholas@755 878
nicholas@755 879 this.newTrack = function(element) {
nicholas@755 880 // Pull data from given URL into new audio buffer
nicholas@755 881 // URLs must either be from the same source OR be setup to 'Access-Control-Allow-Origin'
nicholas@755 882
nicholas@755 883 // Create the audioObject with ID of the new track length;
nicholas@755 884 audioObjectId = this.audioObjects.length;
nicholas@755 885 this.audioObjects[audioObjectId] = new audioObject(audioObjectId);
nicholas@755 886
nicholas@755 887 // Check if audioObject buffer is currently stored by full URL
nicholas@755 888 var URL = element.parent.hostURL + element.url;
nicholas@755 889 var buffer = null;
nicholas@755 890 for (var i=0; i<this.buffers.length; i++)
nicholas@755 891 {
nicholas@755 892 if (URL == this.buffers[i].url)
nicholas@755 893 {
nicholas@755 894 buffer = this.buffers[i];
nicholas@755 895 break;
nicholas@755 896 }
nicholas@755 897 }
nicholas@755 898 if (buffer == null)
nicholas@755 899 {
nicholas@755 900 console.log("[WARN]: Buffer was not loaded in pre-test! "+URL);
nicholas@755 901 buffer = new this.bufferObj();
nicholas@755 902 buffer.getMedia(URL);
nicholas@755 903 this.buffers.push(buffer);
nicholas@755 904 }
nicholas@755 905 this.audioObjects[audioObjectId].specification = element;
nicholas@755 906 this.audioObjects[audioObjectId].url = URL;
nicholas@755 907 buffer.users.push(this.audioObjects[audioObjectId]);
nicholas@755 908 if (buffer.buffer != null)
nicholas@755 909 {
nicholas@755 910 this.audioObjects[audioObjectId].bufferLoaded(buffer);
nicholas@755 911 }
nicholas@755 912 return this.audioObjects[audioObjectId];
nicholas@755 913 };
nicholas@755 914
nicholas@755 915 this.newTestPage = function() {
nicholas@755 916 this.state = 0;
nicholas@755 917 this.audioObjectsReady = false;
nicholas@755 918 this.metric.reset();
nicholas@755 919 for (var i=0; i < this.buffers.length; i++)
nicholas@755 920 {
nicholas@755 921 this.buffers[i].users = [];
nicholas@755 922 }
nicholas@755 923 this.audioObjects = [];
nicholas@755 924 };
nicholas@755 925
nicholas@755 926 this.checkAllPlayed = function() {
nicholas@755 927 arr = [];
nicholas@755 928 for (var id=0; id<this.audioObjects.length; id++) {
nicholas@755 929 if (this.audioObjects[id].metric.wasListenedTo == false) {
nicholas@755 930 arr.push(this.audioObjects[id].id);
nicholas@755 931 }
nicholas@755 932 }
nicholas@755 933 return arr;
nicholas@755 934 };
nicholas@755 935
nicholas@755 936 this.checkAllReady = function() {
nicholas@755 937 var ready = true;
nicholas@755 938 for (var i=0; i<this.audioObjects.length; i++) {
nicholas@755 939 if (this.audioObjects[i].state == 0) {
nicholas@755 940 // Track not ready
nicholas@755 941 console.log('WAIT -- audioObject '+i+' not ready yet!');
nicholas@755 942 ready = false;
nicholas@755 943 };
nicholas@755 944 }
nicholas@755 945 return ready;
nicholas@755 946 };
nicholas@755 947
nicholas@755 948 this.setSynchronousLoop = function() {
nicholas@755 949 // Pads the signals so they are all exactly the same length
nicholas@755 950 var length = 0;
nicholas@755 951 var maxId;
nicholas@755 952 for (var i=0; i<this.audioObjects.length; i++)
nicholas@755 953 {
nicholas@755 954 if (length < this.audioObjects[i].buffer.buffer.length)
nicholas@755 955 {
nicholas@755 956 length = this.audioObjects[i].buffer.buffer.length;
nicholas@755 957 maxId = i;
nicholas@755 958 }
nicholas@755 959 }
nicholas@755 960 // Extract the audio and zero-pad
nicholas@755 961 for (var i=0; i<this.audioObjects.length; i++)
nicholas@755 962 {
nicholas@755 963 var orig = this.audioObjects[i].buffer.buffer;
nicholas@755 964 var hold = audioContext.createBuffer(orig.numberOfChannels,length,orig.sampleRate);
nicholas@755 965 for (var c=0; c<orig.numberOfChannels; c++)
nicholas@755 966 {
nicholas@755 967 var inData = hold.getChannelData(c);
nicholas@755 968 var outData = orig.getChannelData(c);
nicholas@755 969 for (var n=0; n<orig.length; n++)
nicholas@755 970 {inData[n] = outData[n];}
nicholas@755 971 }
nicholas@755 972 hold.gain = orig.gain;
nicholas@755 973 hold.lufs = orig.lufs;
nicholas@755 974 this.audioObjects[i].buffer.buffer = hold;
nicholas@755 975 }
nicholas@755 976 };
nicholas@755 977
nicholas@755 978 }
nicholas@755 979
nicholas@755 980 function audioObject(id) {
nicholas@755 981 // The main buffer object with common control nodes to the AudioEngine
nicholas@755 982
nicholas@755 983 this.specification;
nicholas@755 984 this.id = id;
nicholas@755 985 this.state = 0; // 0 - no data, 1 - ready
nicholas@755 986 this.url = null; // Hold the URL given for the output back to the results.
nicholas@755 987 this.metric = new metricTracker(this);
nicholas@755 988
nicholas@755 989 // Bindings for GUI
nicholas@755 990 this.interfaceDOM = null;
nicholas@755 991 this.commentDOM = null;
nicholas@755 992
nicholas@755 993 // Create a buffer and external gain control to allow internal patching of effects and volume leveling.
nicholas@755 994 this.bufferNode = undefined;
nicholas@755 995 this.outputGain = audioContext.createGain();
nicholas@755 996
nicholas@755 997 // Default output gain to be zero
nicholas@755 998 this.outputGain.gain.value = 0.0;
nicholas@755 999
nicholas@755 1000 // Connect buffer to the audio graph
nicholas@755 1001 this.outputGain.connect(audioEngineContext.outputGain);
nicholas@755 1002
nicholas@755 1003 // the audiobuffer is not designed for multi-start playback
nicholas@755 1004 // When stopeed, the buffer node is deleted and recreated with the stored buffer.
nicholas@755 1005 this.buffer;
nicholas@755 1006
nicholas@755 1007 this.bufferLoaded = function(callee)
nicholas@755 1008 {
nicholas@755 1009 // Called by the associated buffer when it has finished loading, will then 'bind' the buffer to the
nicholas@755 1010 // audioObject and trigger the interfaceDOM.enable() function for user feedback
nicholas@755 1011 if (audioEngineContext.loopPlayback){
nicholas@755 1012 // First copy the buffer into this.buffer
nicholas@755 1013 this.buffer = new audioEngineContext.bufferObj();
nicholas@755 1014 this.buffer.url = callee.url;
nicholas@755 1015 this.buffer.buffer = audioContext.createBuffer(callee.buffer.numberOfChannels, callee.buffer.length, callee.buffer.sampleRate);
nicholas@755 1016 for (var c=0; c<callee.buffer.numberOfChannels; c++)
nicholas@755 1017 {
nicholas@755 1018 var src = callee.buffer.getChannelData(c);
nicholas@755 1019 var dst = this.buffer.buffer.getChannelData(c);
nicholas@755 1020 for (var n=0; n<src.length; n++)
nicholas@755 1021 {
nicholas@755 1022 dst[n] = src[n];
nicholas@755 1023 }
nicholas@755 1024 }
nicholas@755 1025 } else {
nicholas@755 1026 this.buffer = callee;
nicholas@755 1027 }
nicholas@755 1028 this.state = 1;
nicholas@755 1029 this.buffer.buffer.gain = callee.buffer.gain;
nicholas@755 1030 this.buffer.buffer.lufs = callee.buffer.lufs;
nicholas@755 1031 var targetLUFS = this.specification.parent.loudness;
nicholas@755 1032 if (typeof targetLUFS === "number")
nicholas@755 1033 {
nicholas@755 1034 this.buffer.buffer.gain = decibelToLinear(targetLUFS - this.buffer.buffer.lufs);
nicholas@755 1035 } else {
nicholas@755 1036 this.buffer.buffer.gain = 1.0;
nicholas@755 1037 }
nicholas@755 1038 if (this.interfaceDOM != null) {
nicholas@755 1039 this.interfaceDOM.enable();
nicholas@755 1040 }
nicholas@755 1041 };
nicholas@755 1042
nicholas@755 1043 this.loopStart = function() {
nicholas@755 1044 this.outputGain.gain.value = this.specification.gain*this.buffer.buffer.gain;
nicholas@755 1045 this.metric.startListening(audioEngineContext.timer.getTestTime());
nicholas@755 1046 };
nicholas@755 1047
nicholas@755 1048 this.loopStop = function() {
nicholas@755 1049 if (this.outputGain.gain.value != 0.0) {
nicholas@755 1050 this.outputGain.gain.value = 0.0;
nicholas@755 1051 this.metric.stopListening(audioEngineContext.timer.getTestTime());
nicholas@755 1052 }
nicholas@755 1053 };
nicholas@755 1054
nicholas@755 1055 this.play = function(startTime) {
nicholas@755 1056 if (this.bufferNode == undefined && this.buffer.buffer != undefined) {
nicholas@755 1057 this.bufferNode = audioContext.createBufferSource();
nicholas@755 1058 this.bufferNode.owner = this;
nicholas@755 1059 this.bufferNode.connect(this.outputGain);
nicholas@755 1060 this.bufferNode.buffer = this.buffer.buffer;
nicholas@755 1061 this.bufferNode.loop = audioEngineContext.loopPlayback;
nicholas@755 1062 this.bufferNode.onended = function(event) {
nicholas@755 1063 // Safari does not like using 'this' to reference the calling object!
nicholas@755 1064 //event.currentTarget.owner.metric.stopListening(audioEngineContext.timer.getTestTime(),event.currentTarget.owner.getCurrentPosition());
nicholas@755 1065 event.currentTarget.owner.stop();
nicholas@755 1066 };
nicholas@755 1067 if (this.bufferNode.loop == false) {
nicholas@755 1068 this.metric.startListening(audioEngineContext.timer.getTestTime());
nicholas@755 1069 }
nicholas@755 1070 this.bufferNode.start(startTime);
nicholas@755 1071 }
nicholas@755 1072 };
nicholas@755 1073
nicholas@755 1074 this.stop = function() {
nicholas@755 1075 if (this.bufferNode != undefined)
nicholas@755 1076 {
nicholas@755 1077 this.metric.stopListening(audioEngineContext.timer.getTestTime(),this.getCurrentPosition());
nicholas@755 1078 this.bufferNode.stop(0);
nicholas@755 1079 this.bufferNode = undefined;
nicholas@755 1080 }
nicholas@755 1081 };
nicholas@755 1082
nicholas@755 1083 this.getCurrentPosition = function() {
nicholas@755 1084 var time = audioEngineContext.timer.getTestTime();
nicholas@755 1085 if (this.bufferNode != undefined) {
nicholas@755 1086 if (this.bufferNode.loop == true) {
nicholas@755 1087 if (audioEngineContext.status == 1) {
nicholas@755 1088 return (time-this.metric.listenStart)%this.buffer.buffer.duration;
nicholas@755 1089 } else {
nicholas@755 1090 return 0;
nicholas@755 1091 }
nicholas@755 1092 } else {
nicholas@755 1093 if (this.metric.listenHold) {
nicholas@755 1094 return time - this.metric.listenStart;
nicholas@755 1095 } else {
nicholas@755 1096 return 0;
nicholas@755 1097 }
nicholas@755 1098 }
nicholas@755 1099 } else {
nicholas@755 1100 return 0;
nicholas@755 1101 }
nicholas@755 1102 };
nicholas@755 1103
nicholas@755 1104 this.exportXMLDOM = function() {
nicholas@755 1105 var root = document.createElement('audioElement');
nicholas@755 1106 root.id = this.specification.id;
nicholas@755 1107 root.setAttribute('url',this.specification.url);
nicholas@755 1108 var file = document.createElement('file');
nicholas@755 1109 file.setAttribute('sampleRate',this.buffer.buffer.sampleRate);
nicholas@755 1110 file.setAttribute('channels',this.buffer.buffer.numberOfChannels);
nicholas@755 1111 file.setAttribute('sampleCount',this.buffer.buffer.length);
nicholas@755 1112 file.setAttribute('duration',this.buffer.buffer.duration);
nicholas@755 1113 root.appendChild(file);
nicholas@755 1114 if (this.specification.type != 'outsidereference') {
nicholas@755 1115 var interfaceXML = this.interfaceDOM.exportXMLDOM(this);
nicholas@755 1116 if (interfaceXML.length == undefined) {
nicholas@756 1117 root.appendChild(interfaceXML);
nicholas@755 1118 } else {
nicholas@755 1119 for (var i=0; i<interfaceXML.length; i++)
nicholas@755 1120 {
nicholas@755 1121 root.appendChild(interfaceXML[i]);
nicholas@755 1122 }
nicholas@755 1123 }
nicholas@755 1124 root.appendChild(this.commentDOM.exportXMLDOM(this));
nicholas@755 1125 if(this.specification.type == 'anchor') {
nicholas@755 1126 root.setAttribute('anchor',true);
nicholas@755 1127 } else if(this.specification.type == 'reference') {
nicholas@755 1128 root.setAttribute('reference',true);
nicholas@755 1129 }
nicholas@755 1130 }
nicholas@755 1131 root.appendChild(this.metric.exportXMLDOM());
nicholas@755 1132 return root;
nicholas@755 1133 };
nicholas@755 1134 }
nicholas@755 1135
nicholas@755 1136 function timer()
nicholas@755 1137 {
nicholas@755 1138 /* Timer object used in audioEngine to keep track of session timings
nicholas@755 1139 * Uses the timer of the web audio API, so sample resolution
nicholas@755 1140 */
nicholas@755 1141 this.testStarted = false;
nicholas@755 1142 this.testStartTime = 0;
nicholas@755 1143 this.testDuration = 0;
nicholas@755 1144 this.minimumTestTime = 0; // No minimum test time
nicholas@755 1145 this.startTest = function()
nicholas@755 1146 {
nicholas@755 1147 if (this.testStarted == false)
nicholas@755 1148 {
nicholas@755 1149 this.testStartTime = audioContext.currentTime;
nicholas@755 1150 this.testStarted = true;
nicholas@755 1151 this.updateTestTime();
nicholas@755 1152 audioEngineContext.metric.initialiseTest();
nicholas@755 1153 }
nicholas@755 1154 };
nicholas@755 1155 this.stopTest = function()
nicholas@755 1156 {
nicholas@755 1157 if (this.testStarted)
nicholas@755 1158 {
nicholas@755 1159 this.testDuration = this.getTestTime();
nicholas@755 1160 this.testStarted = false;
nicholas@755 1161 } else {
nicholas@755 1162 console.log('ERR: Test tried to end before beginning');
nicholas@755 1163 }
nicholas@755 1164 };
nicholas@755 1165 this.updateTestTime = function()
nicholas@755 1166 {
nicholas@755 1167 if (this.testStarted)
nicholas@755 1168 {
nicholas@755 1169 this.testDuration = audioContext.currentTime - this.testStartTime;
nicholas@755 1170 }
nicholas@755 1171 };
nicholas@755 1172 this.getTestTime = function()
nicholas@755 1173 {
nicholas@755 1174 this.updateTestTime();
nicholas@755 1175 return this.testDuration;
nicholas@755 1176 };
nicholas@755 1177 }
nicholas@755 1178
nicholas@755 1179 function sessionMetrics(engine,specification)
nicholas@755 1180 {
nicholas@755 1181 /* Used by audioEngine to link to audioObjects to minimise the timer call timers;
nicholas@755 1182 */
nicholas@755 1183 this.engine = engine;
nicholas@755 1184 this.lastClicked = -1;
nicholas@755 1185 this.data = -1;
nicholas@755 1186 this.reset = function() {
nicholas@755 1187 this.lastClicked = -1;
nicholas@755 1188 this.data = -1;
nicholas@755 1189 };
nicholas@755 1190
nicholas@755 1191 this.enableElementInitialPosition = false;
nicholas@755 1192 this.enableElementListenTracker = false;
nicholas@755 1193 this.enableElementTimer = false;
nicholas@755 1194 this.enableElementTracker = false;
nicholas@755 1195 this.enableFlagListenedTo = false;
nicholas@755 1196 this.enableFlagMoved = false;
nicholas@755 1197 this.enableTestTimer = false;
nicholas@755 1198 // Obtain the metrics enabled
nicholas@755 1199 for (var i=0; i<specification.metrics.length; i++)
nicholas@755 1200 {
nicholas@755 1201 var node = specification.metrics[i];
nicholas@755 1202 switch(node.enabled)
nicholas@755 1203 {
nicholas@755 1204 case 'testTimer':
nicholas@755 1205 this.enableTestTimer = true;
nicholas@755 1206 break;
nicholas@755 1207 case 'elementTimer':
nicholas@755 1208 this.enableElementTimer = true;
nicholas@755 1209 break;
nicholas@755 1210 case 'elementTracker':
nicholas@755 1211 this.enableElementTracker = true;
nicholas@755 1212 break;
nicholas@755 1213 case 'elementListenTracker':
nicholas@755 1214 this.enableElementListenTracker = true;
nicholas@755 1215 break;
nicholas@755 1216 case 'elementInitialPosition':
nicholas@755 1217 this.enableElementInitialPosition = true;
nicholas@755 1218 break;
nicholas@755 1219 case 'elementFlagListenedTo':
nicholas@755 1220 this.enableFlagListenedTo = true;
nicholas@755 1221 break;
nicholas@755 1222 case 'elementFlagMoved':
nicholas@755 1223 this.enableFlagMoved = true;
nicholas@755 1224 break;
nicholas@755 1225 case 'elementFlagComments':
nicholas@755 1226 this.enableFlagComments = true;
nicholas@755 1227 break;
nicholas@755 1228 }
nicholas@755 1229 }
nicholas@755 1230 this.initialiseTest = function(){};
nicholas@755 1231 }
nicholas@755 1232
nicholas@755 1233 function metricTracker(caller)
nicholas@755 1234 {
nicholas@755 1235 /* Custom object to track and collect metric data
nicholas@755 1236 * Used only inside the audioObjects object.
nicholas@755 1237 */
nicholas@755 1238
nicholas@755 1239 this.listenedTimer = 0;
nicholas@755 1240 this.listenStart = 0;
nicholas@755 1241 this.listenHold = false;
nicholas@755 1242 this.initialPosition = -1;
nicholas@755 1243 this.movementTracker = [];
nicholas@755 1244 this.listenTracker =[];
nicholas@755 1245 this.wasListenedTo = false;
nicholas@755 1246 this.wasMoved = false;
nicholas@755 1247 this.hasComments = false;
nicholas@755 1248 this.parent = caller;
nicholas@755 1249
nicholas@755 1250 this.initialised = function(position)
nicholas@755 1251 {
nicholas@755 1252 if (this.initialPosition == -1) {
nicholas@755 1253 this.initialPosition = position;
nicholas@755 1254 }
nicholas@755 1255 };
nicholas@755 1256
nicholas@755 1257 this.moved = function(time,position)
nicholas@755 1258 {
nicholas@755 1259 this.wasMoved = true;
nicholas@755 1260 this.movementTracker[this.movementTracker.length] = [time, position];
nicholas@755 1261 };
nicholas@755 1262
nicholas@755 1263 this.startListening = function(time)
nicholas@755 1264 {
nicholas@755 1265 if (this.listenHold == false)
nicholas@755 1266 {
nicholas@755 1267 this.wasListenedTo = true;
nicholas@755 1268 this.listenStart = time;
nicholas@755 1269 this.listenHold = true;
nicholas@755 1270
nicholas@755 1271 var evnt = document.createElement('event');
nicholas@755 1272 var testTime = document.createElement('testTime');
nicholas@755 1273 testTime.setAttribute('start',time);
nicholas@755 1274 var bufferTime = document.createElement('bufferTime');
nicholas@755 1275 bufferTime.setAttribute('start',this.parent.getCurrentPosition());
nicholas@755 1276 evnt.appendChild(testTime);
nicholas@755 1277 evnt.appendChild(bufferTime);
nicholas@755 1278 this.listenTracker.push(evnt);
nicholas@755 1279
nicholas@755 1280 console.log('slider ' + this.parent.id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nicholas@755 1281 }
nicholas@755 1282 };
nicholas@755 1283
nicholas@755 1284 this.stopListening = function(time,bufferStopTime)
nicholas@755 1285 {
nicholas@755 1286 if (this.listenHold == true)
nicholas@755 1287 {
nicholas@755 1288 var diff = time - this.listenStart;
nicholas@755 1289 this.listenedTimer += (diff);
nicholas@755 1290 this.listenStart = 0;
nicholas@755 1291 this.listenHold = false;
nicholas@755 1292
nicholas@755 1293 var evnt = this.listenTracker[this.listenTracker.length-1];
nicholas@755 1294 var testTime = evnt.getElementsByTagName('testTime')[0];
nicholas@755 1295 var bufferTime = evnt.getElementsByTagName('bufferTime')[0];
nicholas@755 1296 testTime.setAttribute('stop',time);
nicholas@755 1297 if (bufferStopTime == undefined) {
nicholas@755 1298 bufferTime.setAttribute('stop',this.parent.getCurrentPosition());
nicholas@755 1299 } else {
nicholas@755 1300 bufferTime.setAttribute('stop',bufferStopTime);
nicholas@755 1301 }
nicholas@755 1302 console.log('slider ' + this.parent.id + ' played for (' + diff + ')'); // DEBUG/SAFETY: show played slider id
nicholas@755 1303 }
nicholas@755 1304 };
nicholas@755 1305
nicholas@755 1306 this.exportXMLDOM = function() {
nicholas@755 1307 var root = document.createElement('metric');
nicholas@755 1308 if (audioEngineContext.metric.enableElementTimer) {
nicholas@755 1309 var mElementTimer = document.createElement('metricresult');
nicholas@755 1310 mElementTimer.setAttribute('name','enableElementTimer');
nicholas@755 1311 mElementTimer.textContent = this.listenedTimer;
nicholas@755 1312 root.appendChild(mElementTimer);
nicholas@755 1313 }
nicholas@755 1314 if (audioEngineContext.metric.enableElementTracker) {
nicholas@755 1315 var elementTrackerFull = document.createElement('metricResult');
nicholas@755 1316 elementTrackerFull.setAttribute('name','elementTrackerFull');
nicholas@755 1317 for (var k=0; k<this.movementTracker.length; k++)
nicholas@755 1318 {
nicholas@755 1319 var timePos = document.createElement('timePos');
nicholas@755 1320 timePos.id = k;
nicholas@755 1321 var time = document.createElement('time');
nicholas@755 1322 time.textContent = this.movementTracker[k][0];
nicholas@755 1323 var position = document.createElement('position');
nicholas@755 1324 position.textContent = this.movementTracker[k][1];
nicholas@755 1325 timePos.appendChild(time);
nicholas@755 1326 timePos.appendChild(position);
nicholas@755 1327 elementTrackerFull.appendChild(timePos);
nicholas@755 1328 }
nicholas@755 1329 root.appendChild(elementTrackerFull);
nicholas@755 1330 }
nicholas@755 1331 if (audioEngineContext.metric.enableElementListenTracker) {
nicholas@755 1332 var elementListenTracker = document.createElement('metricResult');
nicholas@755 1333 elementListenTracker.setAttribute('name','elementListenTracker');
nicholas@755 1334 for (var k=0; k<this.listenTracker.length; k++) {
nicholas@755 1335 elementListenTracker.appendChild(this.listenTracker[k]);
nicholas@755 1336 }
nicholas@755 1337 root.appendChild(elementListenTracker);
nicholas@755 1338 }
nicholas@755 1339 if (audioEngineContext.metric.enableElementInitialPosition) {
nicholas@755 1340 var elementInitial = document.createElement('metricResult');
nicholas@755 1341 elementInitial.setAttribute('name','elementInitialPosition');
nicholas@755 1342 elementInitial.textContent = this.initialPosition;
nicholas@755 1343 root.appendChild(elementInitial);
nicholas@755 1344 }
nicholas@755 1345 if (audioEngineContext.metric.enableFlagListenedTo) {
nicholas@755 1346 var flagListenedTo = document.createElement('metricResult');
nicholas@755 1347 flagListenedTo.setAttribute('name','elementFlagListenedTo');
nicholas@755 1348 flagListenedTo.textContent = this.wasListenedTo;
nicholas@755 1349 root.appendChild(flagListenedTo);
nicholas@755 1350 }
nicholas@755 1351 if (audioEngineContext.metric.enableFlagMoved) {
nicholas@755 1352 var flagMoved = document.createElement('metricResult');
nicholas@755 1353 flagMoved.setAttribute('name','elementFlagMoved');
nicholas@755 1354 flagMoved.textContent = this.wasMoved;
nicholas@755 1355 root.appendChild(flagMoved);
nicholas@755 1356 }
nicholas@755 1357 if (audioEngineContext.metric.enableFlagComments) {
nicholas@755 1358 var flagComments = document.createElement('metricResult');
nicholas@755 1359 flagComments.setAttribute('name','elementFlagComments');
nicholas@755 1360 if (this.parent.commentDOM == null)
nicholas@755 1361 {flag.textContent = 'false';}
nicholas@755 1362 else if (this.parent.commentDOM.textContent.length == 0)
nicholas@755 1363 {flag.textContent = 'false';}
nicholas@755 1364 else
nicholas@755 1365 {flag.textContet = 'true';}
nicholas@755 1366 root.appendChild(flagComments);
nicholas@755 1367 }
nicholas@755 1368
nicholas@755 1369 return root;
nicholas@755 1370 };
nicholas@755 1371 }
nicholas@755 1372
nicholas@755 1373 function randomiseOrder(input)
nicholas@755 1374 {
nicholas@755 1375 // This takes an array of information and randomises the order
nicholas@755 1376 var N = input.length;
nicholas@755 1377
nicholas@755 1378 var inputSequence = []; // For safety purposes: keep track of randomisation
nicholas@755 1379 for (var counter = 0; counter < N; ++counter)
nicholas@755 1380 inputSequence.push(counter) // Fill array
nicholas@755 1381 var inputSequenceClone = inputSequence.slice(0);
nicholas@755 1382
nicholas@755 1383 var holdArr = [];
nicholas@755 1384 var outputSequence = [];
nicholas@755 1385 for (var n=0; n<N; n++)
nicholas@755 1386 {
nicholas@755 1387 // First pick a random number
nicholas@755 1388 var r = Math.random();
nicholas@755 1389 // Multiply and floor by the number of elements left
nicholas@755 1390 r = Math.floor(r*input.length);
nicholas@755 1391 // Pick out that element and delete from the array
nicholas@755 1392 holdArr.push(input.splice(r,1)[0]);
nicholas@755 1393 // Do the same with sequence
nicholas@755 1394 outputSequence.push(inputSequence.splice(r,1)[0]);
nicholas@755 1395 }
nicholas@755 1396 console.log(inputSequenceClone.toString()); // print original array to console
nicholas@755 1397 console.log(outputSequence.toString()); // print randomised array to console
nicholas@755 1398 return holdArr;
nicholas@755 1399 }
nicholas@755 1400
nicholas@755 1401 function returnDateNode()
nicholas@755 1402 {
nicholas@755 1403 // Create an XML Node for the Date and Time a test was conducted
nicholas@755 1404 // Structure is
nicholas@755 1405 // <datetime>
nicholas@755 1406 // <date year="##" month="##" day="##">DD/MM/YY</date>
nicholas@755 1407 // <time hour="##" minute="##" sec="##">HH:MM:SS</time>
nicholas@755 1408 // </datetime>
nicholas@755 1409 var dateTime = new Date();
nicholas@755 1410 var year = document.createAttribute('year');
nicholas@755 1411 var month = document.createAttribute('month');
nicholas@755 1412 var day = document.createAttribute('day');
nicholas@755 1413 var hour = document.createAttribute('hour');
nicholas@755 1414 var minute = document.createAttribute('minute');
nicholas@755 1415 var secs = document.createAttribute('secs');
nicholas@755 1416
nicholas@755 1417 year.nodeValue = dateTime.getFullYear();
nicholas@755 1418 month.nodeValue = dateTime.getMonth()+1;
nicholas@755 1419 day.nodeValue = dateTime.getDate();
nicholas@755 1420 hour.nodeValue = dateTime.getHours();
nicholas@755 1421 minute.nodeValue = dateTime.getMinutes();
nicholas@755 1422 secs.nodeValue = dateTime.getSeconds();
nicholas@755 1423
nicholas@755 1424 var hold = document.createElement("datetime");
nicholas@755 1425 var date = document.createElement("date");
nicholas@755 1426 date.textContent = year.nodeValue+'/'+month.nodeValue+'/'+day.nodeValue;
nicholas@755 1427 var time = document.createElement("time");
nicholas@755 1428 time.textContent = hour.nodeValue+':'+minute.nodeValue+':'+secs.nodeValue;
nicholas@755 1429
nicholas@755 1430 date.setAttributeNode(year);
nicholas@755 1431 date.setAttributeNode(month);
nicholas@755 1432 date.setAttributeNode(day);
nicholas@755 1433 time.setAttributeNode(hour);
nicholas@755 1434 time.setAttributeNode(minute);
nicholas@755 1435 time.setAttributeNode(secs);
nicholas@755 1436
nicholas@755 1437 hold.appendChild(date);
nicholas@755 1438 hold.appendChild(time);
nicholas@755 1439 return hold;
nicholas@755 1440
nicholas@755 1441 }
nicholas@755 1442
nicholas@755 1443 function Specification() {
nicholas@755 1444 // Handles the decoding of the project specification XML into a simple JavaScript Object.
nicholas@755 1445
nicholas@755 1446 this.interfaceType = null;
nicholas@755 1447 this.commonInterface = new function()
nicholas@755 1448 {
nicholas@755 1449 this.options = [];
nicholas@755 1450 this.optionNode = function(input)
nicholas@755 1451 {
nicholas@755 1452 var name = input.getAttribute('name');
nicholas@755 1453 this.type = name;
nicholas@755 1454 if(this.type == "option")
nicholas@755 1455 {
nicholas@755 1456 this.name = input.id;
nicholas@755 1457 } else if (this.type == "check")
nicholas@755 1458 {
nicholas@755 1459 this.check = input.id;
nicholas@755 1460 }
nicholas@755 1461 };
nicholas@755 1462 };
nicholas@755 1463
nicholas@755 1464 this.randomiseOrder = function(input)
nicholas@755 1465 {
nicholas@755 1466 // This takes an array of information and randomises the order
nicholas@755 1467 var N = input.length;
nicholas@755 1468
nicholas@755 1469 var inputSequence = []; // For safety purposes: keep track of randomisation
nicholas@755 1470 for (var counter = 0; counter < N; ++counter)
nicholas@755 1471 inputSequence.push(counter) // Fill array
nicholas@755 1472 var inputSequenceClone = inputSequence.slice(0);
nicholas@755 1473
nicholas@755 1474 var holdArr = [];
nicholas@755 1475 var outputSequence = [];
nicholas@755 1476 for (var n=0; n<N; n++)
nicholas@755 1477 {
nicholas@755 1478 // First pick a random number
nicholas@755 1479 var r = Math.random();
nicholas@755 1480 // Multiply and floor by the number of elements left
nicholas@755 1481 r = Math.floor(r*input.length);
nicholas@755 1482 // Pick out that element and delete from the array
nicholas@755 1483 holdArr.push(input.splice(r,1)[0]);
nicholas@755 1484 // Do the same with sequence
nicholas@755 1485 outputSequence.push(inputSequence.splice(r,1)[0]);
nicholas@755 1486 }
nicholas@755 1487 console.log(inputSequenceClone.toString()); // print original array to console
nicholas@755 1488 console.log(outputSequence.toString()); // print randomised array to console
nicholas@755 1489 return holdArr;
nicholas@755 1490 };
nicholas@755 1491 this.projectReturn = null;
nicholas@755 1492 this.randomiseOrder = null;
nicholas@755 1493 this.collectMetrics = null;
nicholas@755 1494 this.testPages = null;
nicholas@755 1495 this.audioHolders = [];
nicholas@755 1496 this.metrics = [];
nicholas@755 1497 this.loudness = null;
nicholas@755 1498
nicholas@755 1499 this.decode = function(projectXML) {
nicholas@755 1500 // projectXML - DOM Parsed document
nicholas@755 1501 this.projectXML = projectXML.childNodes[0];
nicholas@755 1502 var setupNode = projectXML.getElementsByTagName('setup')[0];
nicholas@755 1503 this.interfaceType = setupNode.getAttribute('interface');
nicholas@755 1504 this.projectReturn = setupNode.getAttribute('projectReturn');
nicholas@755 1505 this.testPages = setupNode.getAttribute('testPages');
nicholas@755 1506 if (setupNode.getAttribute('randomiseOrder') == "true") {
nicholas@755 1507 this.randomiseOrder = true;
nicholas@755 1508 } else {this.randomiseOrder = false;}
nicholas@755 1509 if (setupNode.getAttribute('collectMetrics') == "true") {
nicholas@755 1510 this.collectMetrics = true;
nicholas@755 1511 } else {this.collectMetrics = false;}
nicholas@755 1512 if (isNaN(Number(this.testPages)) || this.testPages == undefined)
nicholas@755 1513 {
nicholas@755 1514 this.testPages = null;
nicholas@755 1515 } else {
nicholas@755 1516 this.testPages = Number(this.testPages);
nicholas@755 1517 if (this.testPages == 0) {this.testPages = null;}
nicholas@755 1518 }
nicholas@755 1519 if (setupNode.getAttribute('loudness') != null)
nicholas@755 1520 {
nicholas@755 1521 var XMLloudness = setupNode.getAttribute('loudness');
nicholas@755 1522 if (isNaN(Number(XMLloudness)) == false)
nicholas@755 1523 {
nicholas@755 1524 this.loudness = Number(XMLloudness);
nicholas@755 1525 }
nicholas@755 1526 }
nicholas@755 1527 var metricCollection = setupNode.getElementsByTagName('Metric');
nicholas@755 1528
nicholas@755 1529 var setupPreTestNode = setupNode.getElementsByTagName('PreTest');
nicholas@755 1530 if (setupPreTestNode.length != 0)
nicholas@755 1531 {
nicholas@755 1532 setupPreTestNode = setupPreTestNode[0];
nicholas@755 1533 this.preTest.construct(setupPreTestNode);
nicholas@755 1534 }
nicholas@755 1535
nicholas@755 1536 var setupPostTestNode = setupNode.getElementsByTagName('PostTest');
nicholas@755 1537 if (setupPostTestNode.length != 0)
nicholas@755 1538 {
nicholas@755 1539 setupPostTestNode = setupPostTestNode[0];
nicholas@755 1540 this.postTest.construct(setupPostTestNode);
nicholas@755 1541 }
nicholas@755 1542
nicholas@755 1543 if (metricCollection.length > 0) {
nicholas@755 1544 metricCollection = metricCollection[0].getElementsByTagName('metricEnable');
nicholas@755 1545 for (var i=0; i<metricCollection.length; i++) {
nicholas@755 1546 this.metrics.push(new this.metricNode(metricCollection[i].textContent));
nicholas@755 1547 }
nicholas@755 1548 }
nicholas@755 1549
nicholas@755 1550 var commonInterfaceNode = setupNode.getElementsByTagName('interface');
nicholas@755 1551 if (commonInterfaceNode.length > 0) {
nicholas@755 1552 commonInterfaceNode = commonInterfaceNode[0];
nicholas@755 1553 } else {
nicholas@755 1554 commonInterfaceNode = undefined;
nicholas@755 1555 }
nicholas@755 1556
nicholas@755 1557 this.commonInterface = new function() {
nicholas@755 1558 this.OptionNode = function(child) {
nicholas@755 1559 this.type = child.nodeName;
nicholas@755 1560 if (this.type == 'option')
nicholas@755 1561 {
nicholas@755 1562 this.name = child.getAttribute('name');
nicholas@755 1563 }
nicholas@755 1564 else if (this.type == 'check') {
nicholas@755 1565 this.check = child.getAttribute('name');
nicholas@755 1566 if (this.check == 'scalerange') {
nicholas@755 1567 this.min = child.getAttribute('min');
nicholas@755 1568 this.max = child.getAttribute('max');
nicholas@755 1569 if (this.min == null) {this.min = 1;}
nicholas@755 1570 else if (Number(this.min) > 1 && this.min != null) {
nicholas@755 1571 this.min = Number(this.min)/100;
nicholas@755 1572 } else {
nicholas@755 1573 this.min = Number(this.min);
nicholas@755 1574 }
nicholas@755 1575 if (this.max == null) {this.max = 0;}
nicholas@755 1576 else if (Number(this.max) > 1 && this.max != null) {
nicholas@755 1577 this.max = Number(this.max)/100;
nicholas@755 1578 } else {
nicholas@755 1579 this.max = Number(this.max);
nicholas@755 1580 }
nicholas@755 1581 }
nicholas@755 1582 } else if (this.type == 'anchor' || this.type == 'reference') {
nicholas@755 1583 this.value = Number(child.textContent);
nicholas@755 1584 this.enforce = child.getAttribute('enforce');
nicholas@755 1585 if (this.enforce == 'true') {this.enforce = true;}
nicholas@755 1586 else {this.enforce = false;}
nicholas@755 1587 }
nicholas@755 1588 };
nicholas@755 1589 this.options = [];
nicholas@755 1590 if (commonInterfaceNode != undefined) {
nicholas@755 1591 var child = commonInterfaceNode.firstElementChild;
nicholas@755 1592 while (child != undefined) {
nicholas@755 1593 this.options.push(new this.OptionNode(child));
nicholas@755 1594 child = child.nextElementSibling;
nicholas@755 1595 }
nicholas@755 1596 }
nicholas@755 1597 };
nicholas@755 1598
nicholas@755 1599 var audioHolders = projectXML.getElementsByTagName('audioHolder');
nicholas@755 1600 for (var i=0; i<audioHolders.length; i++) {
nicholas@755 1601 var node = new this.audioHolderNode(this);
nicholas@755 1602 node.decode(this,audioHolders[i]);
nicholas@755 1603 this.audioHolders.push(node);
nicholas@755 1604 }
nicholas@755 1605
nicholas@755 1606 // New check if we need to randomise the test order
nicholas@755 1607 if (this.randomiseOrder && typeof randomiseOrder === "function")
nicholas@755 1608 {
nicholas@755 1609 this.audioHolders = randomiseOrder(this.audioHolders);
nicholas@755 1610 for (var i=0; i<this.audioHolders.length; i++)
nicholas@755 1611 {
nicholas@755 1612 this.audioHolders[i].presentedId = i;
nicholas@755 1613 }
nicholas@755 1614 }
nicholas@755 1615
nicholas@755 1616 if (this.testPages != null || this.testPages != undefined)
nicholas@755 1617 {
nicholas@755 1618 if (this.testPages > audioHolders.length)
nicholas@755 1619 {
nicholas@755 1620 console.log('Warning: You have specified '+audioHolders.length+' tests but requested '+this.testPages+' be completed!');
nicholas@755 1621 this.testPages = audioHolders.length;
nicholas@755 1622 }
nicholas@755 1623 var aH = this.audioHolders;
nicholas@755 1624 this.audioHolders = [];
nicholas@755 1625 for (var i=0; i<this.testPages; i++)
nicholas@755 1626 {
nicholas@755 1627 this.audioHolders.push(aH[i]);
nicholas@755 1628 }
nicholas@755 1629 }
nicholas@755 1630 };
nicholas@755 1631
nicholas@755 1632 this.encode = function()
nicholas@755 1633 {
nicholas@755 1634 var root = document.implementation.createDocument(null,"BrowserEvalProjectDocument");
nicholas@755 1635 // First get all the <setup> tag compiled
nicholas@755 1636 var setupNode = root.createElement("setup");
nicholas@755 1637 setupNode.setAttribute('interface',this.interfaceType);
nicholas@755 1638 setupNode.setAttribute('projectReturn',this.projectReturn);
nicholas@755 1639 setupNode.setAttribute('randomiseOrder',this.randomiseOrder);
nicholas@755 1640 setupNode.setAttribute('collectMetrics',this.collectMetrics);
nicholas@755 1641 setupNode.setAttribute('testPages',this.testPages);
nicholas@755 1642 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
nicholas@755 1643
nicholas@755 1644 var setupPreTest = root.createElement("PreTest");
nicholas@755 1645 for (var i=0; i<this.preTest.options.length; i++)
nicholas@755 1646 {
nicholas@755 1647 setupPreTest.appendChild(this.preTest.options[i].exportXML(root));
nicholas@755 1648 }
nicholas@755 1649
nicholas@755 1650 var setupPostTest = root.createElement("PostTest");
nicholas@755 1651 for (var i=0; i<this.postTest.options.length; i++)
nicholas@755 1652 {
nicholas@755 1653 setupPostTest.appendChild(this.postTest.options[i].exportXML(root));
nicholas@755 1654 }
nicholas@755 1655
nicholas@755 1656 setupNode.appendChild(setupPreTest);
nicholas@755 1657 setupNode.appendChild(setupPostTest);
nicholas@755 1658
nicholas@755 1659 // <Metric> tag
nicholas@755 1660 var Metric = root.createElement("Metric");
nicholas@755 1661 for (var i=0; i<this.metrics.length; i++)
nicholas@755 1662 {
nicholas@755 1663 var metricEnable = root.createElement("metricEnable");
nicholas@755 1664 metricEnable.textContent = this.metrics[i].enabled;
nicholas@755 1665 Metric.appendChild(metricEnable);
nicholas@755 1666 }
nicholas@755 1667 setupNode.appendChild(Metric);
nicholas@755 1668
nicholas@755 1669 // <interface> tag
nicholas@755 1670 var CommonInterface = root.createElement("interface");
nicholas@755 1671 for (var i=0; i<this.commonInterface.options.length; i++)
nicholas@755 1672 {
nicholas@755 1673 var CIObj = this.commonInterface.options[i];
nicholas@755 1674 var CINode = root.createElement(CIObj.type);
nicholas@755 1675 if (CIObj.type == "check") {CINode.setAttribute("name",CIObj.check);}
nicholas@755 1676 else {CINode.setAttribute("name",CIObj.name);}
nicholas@755 1677 CommonInterface.appendChild(CINode);
nicholas@755 1678 }
nicholas@755 1679 setupNode.appendChild(CommonInterface);
nicholas@755 1680
nicholas@755 1681 root.getElementsByTagName("BrowserEvalProjectDocument")[0].appendChild(setupNode);
nicholas@755 1682 // Time for the <audioHolder> tags
nicholas@755 1683 for (var ahIndex = 0; ahIndex < this.audioHolders.length; ahIndex++)
nicholas@755 1684 {
nicholas@755 1685 var node = this.audioHolders[ahIndex].encode(root);
nicholas@755 1686 root.getElementsByTagName("BrowserEvalProjectDocument")[0].appendChild(node);
nicholas@755 1687 }
nicholas@755 1688 return root;
nicholas@755 1689 };
nicholas@755 1690
nicholas@755 1691 this.prepostNode = function(type) {
nicholas@755 1692 this.type = type;
nicholas@755 1693 this.options = [];
nicholas@755 1694
nicholas@755 1695 this.OptionNode = function() {
nicholas@755 1696
nicholas@755 1697 this.childOption = function() {
nicholas@755 1698 this.type = 'option';
nicholas@755 1699 this.id = null;
nicholas@755 1700 this.name = undefined;
nicholas@755 1701 this.text = null;
nicholas@755 1702 };
nicholas@755 1703
nicholas@755 1704 this.type = undefined;
nicholas@755 1705 this.id = undefined;
nicholas@755 1706 this.mandatory = undefined;
nicholas@755 1707 this.question = undefined;
nicholas@755 1708 this.statement = undefined;
nicholas@755 1709 this.boxsize = undefined;
nicholas@755 1710 this.options = [];
nicholas@755 1711 this.min = undefined;
nicholas@755 1712 this.max = undefined;
nicholas@755 1713 this.step = undefined;
nicholas@755 1714
nicholas@755 1715 this.decode = function(child)
nicholas@755 1716 {
nicholas@755 1717 this.type = child.nodeName;
nicholas@755 1718 if (child.nodeName == "question") {
nicholas@755 1719 this.id = child.id;
nicholas@755 1720 this.mandatory;
nicholas@755 1721 if (child.getAttribute('mandatory') == "true") {this.mandatory = true;}
nicholas@755 1722 else {this.mandatory = false;}
nicholas@755 1723 this.question = child.textContent;
nicholas@755 1724 if (child.getAttribute('boxsize') == null) {
nicholas@755 1725 this.boxsize = 'normal';
nicholas@755 1726 } else {
nicholas@755 1727 this.boxsize = child.getAttribute('boxsize');
nicholas@755 1728 }
nicholas@755 1729 } else if (child.nodeName == "statement") {
nicholas@755 1730 this.statement = child.textContent;
nicholas@755 1731 } else if (child.nodeName == "checkbox" || child.nodeName == "radio") {
nicholas@755 1732 var element = child.firstElementChild;
nicholas@755 1733 this.id = child.id;
nicholas@755 1734 if (element == null) {
nicholas@755 1735 console.log('Malformed' +child.nodeName+ 'entry');
nicholas@755 1736 this.statement = 'Malformed' +child.nodeName+ 'entry';
nicholas@755 1737 this.type = 'statement';
nicholas@755 1738 } else {
nicholas@755 1739 this.options = [];
nicholas@755 1740 while (element != null) {
nicholas@755 1741 if (element.nodeName == 'statement' && this.statement == undefined){
nicholas@755 1742 this.statement = element.textContent;
nicholas@755 1743 } else if (element.nodeName == 'option') {
nicholas@755 1744 var node = new this.childOption();
nicholas@755 1745 node.id = element.id;
nicholas@755 1746 node.name = element.getAttribute('name');
nicholas@755 1747 node.text = element.textContent;
nicholas@755 1748 this.options.push(node);
nicholas@755 1749 }
nicholas@755 1750 element = element.nextElementSibling;
nicholas@755 1751 }
nicholas@755 1752 }
nicholas@755 1753 } else if (child.nodeName == "number") {
nicholas@755 1754 this.statement = child.textContent;
nicholas@755 1755 this.id = child.id;
nicholas@755 1756 this.min = child.getAttribute('min');
nicholas@755 1757 this.max = child.getAttribute('max');
nicholas@755 1758 this.step = child.getAttribute('step');
nicholas@755 1759 }
nicholas@755 1760 };
nicholas@755 1761
nicholas@755 1762 this.exportXML = function(root)
nicholas@755 1763 {
nicholas@755 1764 var node = root.createElement(this.type);
nicholas@755 1765 switch(this.type)
nicholas@755 1766 {
nicholas@755 1767 case "statement":
nicholas@755 1768 node.textContent = this.statement;
nicholas@755 1769 break;
nicholas@755 1770 case "question":
nicholas@755 1771 node.id = this.id;
nicholas@755 1772 node.setAttribute("mandatory",this.mandatory);
nicholas@755 1773 node.setAttribute("boxsize",this.boxsize);
nicholas@755 1774 node.textContent = this.question;
nicholas@755 1775 break;
nicholas@755 1776 case "number":
nicholas@755 1777 node.id = this.id;
nicholas@755 1778 node.setAttribute("mandatory",this.mandatory);
nicholas@755 1779 node.setAttribute("min", this.min);
nicholas@755 1780 node.setAttribute("max", this.max);
nicholas@755 1781 node.setAttribute("step", this.step);
nicholas@755 1782 node.textContent = this.statement;
nicholas@755 1783 break;
nicholas@755 1784 case "checkbox":
nicholas@755 1785 node.id = this.id;
nicholas@755 1786 var statement = root.createElement("statement");
nicholas@755 1787 statement.textContent = this.statement;
nicholas@755 1788 node.appendChild(statement);
nicholas@755 1789 for (var i=0; i<this.options.length; i++)
nicholas@755 1790 {
nicholas@755 1791 var option = this.options[i];
nicholas@755 1792 var optionNode = root.createElement("option");
nicholas@755 1793 optionNode.id = option.id;
nicholas@755 1794 optionNode.textContent = option.text;
nicholas@755 1795 node.appendChild(optionNode);
nicholas@755 1796 }
nicholas@755 1797 break;
nicholas@755 1798 case "radio":
nicholas@755 1799 node.id = this.id;
nicholas@755 1800 var statement = root.createElement("statement");
nicholas@755 1801 statement.textContent = this.statement;
nicholas@755 1802 node.appendChild(statement);
nicholas@755 1803 for (var i=0; i<this.options.length; i++)
nicholas@755 1804 {
nicholas@755 1805 var option = this.options[i];
nicholas@755 1806 var optionNode = root.createElement("option");
nicholas@755 1807 optionNode.setAttribute("name",option.name);
nicholas@755 1808 optionNode.textContent = option.text;
nicholas@755 1809 node.appendChild(optionNode);
nicholas@755 1810 }
nicholas@755 1811 break;
nicholas@755 1812 }
nicholas@755 1813 return node;
nicholas@755 1814 };
nicholas@755 1815 };
nicholas@755 1816 this.construct = function(Collection)
nicholas@755 1817 {
nicholas@755 1818 if (Collection.childElementCount != 0) {
nicholas@755 1819 var child = Collection.firstElementChild;
nicholas@755 1820 var node = new this.OptionNode();
nicholas@755 1821 node.decode(child);
nicholas@755 1822 this.options.push(node);
nicholas@755 1823 while (child.nextElementSibling != null) {
nicholas@755 1824 child = child.nextElementSibling;
nicholas@755 1825 node = new this.OptionNode();
nicholas@755 1826 node.decode(child);
nicholas@755 1827 this.options.push(node);
nicholas@755 1828 }
nicholas@755 1829 }
nicholas@755 1830 };
nicholas@755 1831 };
nicholas@755 1832 this.preTest = new this.prepostNode("pretest");
nicholas@755 1833 this.postTest = new this.prepostNode("posttest");
nicholas@755 1834
nicholas@755 1835 this.metricNode = function(name) {
nicholas@755 1836 this.enabled = name;
nicholas@755 1837 };
nicholas@755 1838
nicholas@755 1839 this.audioHolderNode = function(parent) {
nicholas@755 1840 this.type = 'audioHolder';
nicholas@755 1841 this.presentedId = undefined;
nicholas@755 1842 this.id = undefined;
nicholas@755 1843 this.hostURL = undefined;
nicholas@755 1844 this.sampleRate = undefined;
nicholas@755 1845 this.randomiseOrder = undefined;
nicholas@755 1846 this.loop = undefined;
nicholas@755 1847 this.elementComments = undefined;
nicholas@755 1848 this.outsideReference = null;
nicholas@755 1849 this.loudness = null;
nicholas@758 1850 this.initialPosition = null;
nicholas@755 1851 this.preTest = new parent.prepostNode("pretest");
nicholas@755 1852 this.postTest = new parent.prepostNode("pretest");
nicholas@755 1853 this.interfaces = [];
nicholas@755 1854 this.commentBoxPrefix = "Comment on track";
nicholas@755 1855 this.audioElements = [];
nicholas@755 1856 this.commentQuestions = [];
nicholas@755 1857
nicholas@755 1858 this.decode = function(parent,xml)
nicholas@755 1859 {
nicholas@755 1860 this.presentedId = parent.audioHolders.length;
nicholas@755 1861 this.id = xml.id;
nicholas@755 1862 this.hostURL = xml.getAttribute('hostURL');
nicholas@755 1863 this.sampleRate = xml.getAttribute('sampleRate');
nicholas@755 1864 if (xml.getAttribute('randomiseOrder') == "true") {this.randomiseOrder = true;}
nicholas@755 1865 else {this.randomiseOrder = false;}
nicholas@755 1866 this.repeatCount = xml.getAttribute('repeatCount');
nicholas@755 1867 if (xml.getAttribute('loop') == 'true') {this.loop = true;}
nicholas@755 1868 else {this.loop == false;}
nicholas@755 1869 if (xml.getAttribute('elementComments') == "true") {this.elementComments = true;}
nicholas@755 1870 else {this.elementComments = false;}
nicholas@755 1871 if (typeof parent.loudness === "number")
nicholas@755 1872 {
nicholas@755 1873 this.loudness = parent.loudness;
nicholas@755 1874 }
nicholas@758 1875 if (typeof xml.getAttribute('initial-position') === "string")
nicholas@758 1876 {
nicholas@758 1877 var xmlInitialPosition = Number(xml.getAttribute('initial-position'));
nicholas@758 1878 if (isNaN(xmlInitialPosition) == false)
nicholas@758 1879 {
nicholas@758 1880 if (xmlInitialPosition > 1)
nicholas@758 1881 {
nicholas@758 1882 xmlInitialPosition /= 100;
nicholas@758 1883 }
nicholas@758 1884 this.initialPosition = xmlInitialPosition;
nicholas@758 1885 }
nicholas@758 1886 }
nicholas@755 1887 if (xml.getAttribute('loudness') != null)
nicholas@755 1888 {
nicholas@755 1889 var XMLloudness = xml.getAttribute('loudness');
nicholas@755 1890 if (isNaN(Number(XMLloudness)) == false)
nicholas@755 1891 {
nicholas@755 1892 this.loudness = Number(XMLloudness);
nicholas@755 1893 }
nicholas@755 1894 }
nicholas@755 1895 var setupPreTestNode = xml.getElementsByTagName('PreTest');
nicholas@755 1896 if (setupPreTestNode.length != 0)
nicholas@755 1897 {
nicholas@755 1898 setupPreTestNode = setupPreTestNode[0];
nicholas@755 1899 this.preTest.construct(setupPreTestNode);
nicholas@755 1900 }
nicholas@755 1901
nicholas@755 1902 var setupPostTestNode = xml.getElementsByTagName('PostTest');
nicholas@755 1903 if (setupPostTestNode.length != 0)
nicholas@755 1904 {
nicholas@755 1905 setupPostTestNode = setupPostTestNode[0];
nicholas@755 1906 this.postTest.construct(setupPostTestNode);
nicholas@755 1907 }
nicholas@755 1908
nicholas@755 1909 var interfaceDOM = xml.getElementsByTagName('interface');
nicholas@755 1910 for (var i=0; i<interfaceDOM.length; i++) {
nicholas@755 1911 var node = new this.interfaceNode();
nicholas@755 1912 node.decode(interfaceDOM[i]);
nicholas@755 1913 this.interfaces.push(node);
nicholas@755 1914 }
nicholas@755 1915 this.commentBoxPrefix = xml.getElementsByTagName('commentBoxPrefix');
nicholas@755 1916 if (this.commentBoxPrefix.length != 0) {
nicholas@755 1917 this.commentBoxPrefix = this.commentBoxPrefix[0].textContent;
nicholas@755 1918 } else {
nicholas@755 1919 this.commentBoxPrefix = "Comment on track";
nicholas@755 1920 }
nicholas@755 1921 var audioElementsDOM = xml.getElementsByTagName('audioElements');
nicholas@755 1922 for (var i=0; i<audioElementsDOM.length; i++) {
nicholas@755 1923 var node = new this.audioElementNode();
nicholas@755 1924 node.decode(this,audioElementsDOM[i]);
nicholas@755 1925 if (audioElementsDOM[i].getAttribute('type') == 'outsidereference') {
nicholas@755 1926 if (this.outsideReference == null) {
nicholas@755 1927 this.outsideReference = node;
nicholas@755 1928 } else {
nicholas@755 1929 console.log('Error only one audioelement can be of type outsidereference per audioholder');
nicholas@755 1930 this.audioElements.push(node);
nicholas@755 1931 console.log('Element id '+audioElementsDOM[i].id+' made into normal node');
nicholas@755 1932 }
nicholas@755 1933 } else {
nicholas@755 1934 this.audioElements.push(node);
nicholas@755 1935 }
nicholas@755 1936 }
nicholas@755 1937
nicholas@755 1938 if (this.randomiseOrder == true && typeof randomiseOrder === "function")
nicholas@755 1939 {
nicholas@755 1940 this.audioElements = randomiseOrder(this.audioElements);
nicholas@755 1941 }
nicholas@755 1942
nicholas@755 1943 var commentQuestionsDOM = xml.getElementsByTagName('CommentQuestion');
nicholas@755 1944 for (var i=0; i<commentQuestionsDOM.length; i++) {
nicholas@755 1945 var node = new this.commentQuestionNode();
nicholas@755 1946 node.decode(commentQuestionsDOM[i]);
nicholas@755 1947 this.commentQuestions.push(node);
nicholas@755 1948 }
nicholas@755 1949 };
nicholas@755 1950
nicholas@755 1951 this.encode = function(root)
nicholas@755 1952 {
nicholas@755 1953 var AHNode = root.createElement("audioHolder");
nicholas@755 1954 AHNode.id = this.id;
nicholas@755 1955 AHNode.setAttribute("hostURL",this.hostURL);
nicholas@755 1956 AHNode.setAttribute("sampleRate",this.sampleRate);
nicholas@755 1957 AHNode.setAttribute("randomiseOrder",this.randomiseOrder);
nicholas@755 1958 AHNode.setAttribute("repeatCount",this.repeatCount);
nicholas@755 1959 AHNode.setAttribute("loop",this.loop);
nicholas@755 1960 AHNode.setAttribute("elementComments",this.elementComments);
nicholas@755 1961 if(this.loudness != null) {AHNode.setAttribute("loudness",this.loudness);}
nicholas@758 1962 if(this.initialPosition != null) {
nicholas@758 1963 AHNode.setAttribute("loudness",this.initialPosition*100);
nicholas@758 1964 }
nicholas@755 1965 for (var i=0; i<this.interfaces.length; i++)
nicholas@755 1966 {
nicholas@755 1967 AHNode.appendChild(this.interfaces[i].encode(root));
nicholas@755 1968 }
nicholas@755 1969
nicholas@755 1970 for (var i=0; i<this.audioElements.length; i++) {
nicholas@755 1971 AHNode.appendChild(this.audioElements[i].encode(root));
nicholas@755 1972 }
nicholas@755 1973 // Create <CommentQuestion>
nicholas@755 1974 for (var i=0; i<this.commentQuestions.length; i++)
nicholas@755 1975 {
nicholas@755 1976 AHNode.appendChild(this.commentQuestions[i].exportXML(root));
nicholas@755 1977 }
nicholas@755 1978
nicholas@755 1979 // Create <PreTest>
nicholas@755 1980 var AHPreTest = root.createElement("PreTest");
nicholas@755 1981 for (var i=0; i<this.preTest.options.length; i++)
nicholas@755 1982 {
nicholas@755 1983 AHPreTest.appendChild(this.preTest.options[i].exportXML(root));
nicholas@755 1984 }
nicholas@755 1985
nicholas@755 1986 var AHPostTest = root.createElement("PostTest");
nicholas@755 1987 for (var i=0; i<this.postTest.options.length; i++)
nicholas@755 1988 {
nicholas@755 1989 AHPostTest.appendChild(this.postTest.options[i].exportXML(root));
nicholas@755 1990 }
nicholas@755 1991 AHNode.appendChild(AHPreTest);
nicholas@755 1992 AHNode.appendChild(AHPostTest);
nicholas@755 1993 return AHNode;
nicholas@755 1994 };
nicholas@755 1995
nicholas@755 1996 this.interfaceNode = function() {
nicholas@755 1997 this.title = undefined;
nicholas@755 1998 this.options = [];
nicholas@755 1999 this.scale = [];
nicholas@755 2000 this.name = undefined;
nicholas@755 2001 this.decode = function(DOM)
nicholas@755 2002 {
nicholas@755 2003 var title = DOM.getElementsByTagName('title');
nicholas@755 2004 if (title.length == 0) {this.title = null;}
nicholas@755 2005 else {this.title = title[0].textContent;}
nicholas@755 2006 var name = DOM.getAttribute("name");
nicholas@755 2007 if (name != undefined) {this.name = name;}
nicholas@755 2008 this.options = parent.commonInterface.options;
nicholas@755 2009 var scale = DOM.getElementsByTagName('scale');
nicholas@755 2010 this.scale = [];
nicholas@755 2011 for (var i=0; i<scale.length; i++) {
nicholas@755 2012 var arr = [null, null];
nicholas@755 2013 arr[0] = scale[i].getAttribute('position');
nicholas@755 2014 arr[1] = scale[i].textContent;
nicholas@755 2015 this.scale.push(arr);
nicholas@755 2016 }
nicholas@755 2017 };
nicholas@755 2018 this.encode = function(root)
nicholas@755 2019 {
nicholas@755 2020 var node = root.createElement("interface");
nicholas@755 2021 if (this.title != undefined)
nicholas@755 2022 {
nicholas@755 2023 var title = root.createElement("title");
nicholas@755 2024 title.textContent = this.title;
nicholas@755 2025 node.appendChild(title);
nicholas@755 2026 }
nicholas@755 2027 for (var i=0; i<this.options.length; i++)
nicholas@755 2028 {
nicholas@755 2029 var optionNode = root.createElement(this.options[i].type);
nicholas@755 2030 if (this.options[i].type == "option")
nicholas@755 2031 {
nicholas@755 2032 optionNode.setAttribute("name",this.options[i].name);
nicholas@755 2033 } else if (this.options[i].type == "check") {
nicholas@755 2034 optionNode.setAttribute("check",this.options[i].check);
nicholas@755 2035 } else if (this.options[i].type == "scalerange") {
nicholas@755 2036 optionNode.setAttribute("min",this.options[i].min*100);
nicholas@755 2037 optionNode.setAttribute("max",this.options[i].max*100);
nicholas@755 2038 }
nicholas@755 2039 node.appendChild(optionNode);
nicholas@755 2040 }
nicholas@755 2041 for (var i=0; i<this.scale.length; i++) {
nicholas@755 2042 var scale = root.createElement("scale");
nicholas@755 2043 scale.setAttribute("position",this.scale[i][0]);
nicholas@755 2044 scale.textContent = this.scale[i][1];
nicholas@755 2045 node.appendChild(scale);
nicholas@755 2046 }
nicholas@755 2047 return node;
nicholas@755 2048 };
nicholas@755 2049 };
nicholas@755 2050
nicholas@755 2051 this.audioElementNode = function() {
nicholas@755 2052 this.url = null;
nicholas@755 2053 this.id = null;
nicholas@755 2054 this.parent = null;
nicholas@755 2055 this.type = "normal";
nicholas@755 2056 this.marker = false;
nicholas@755 2057 this.enforce = false;
nicholas@755 2058 this.gain = 1.0;
nicholas@755 2059 this.decode = function(parent,xml)
nicholas@755 2060 {
nicholas@755 2061 this.url = xml.getAttribute('url');
nicholas@755 2062 this.id = xml.id;
nicholas@755 2063 this.parent = parent;
nicholas@755 2064 this.type = xml.getAttribute('type');
nicholas@755 2065 var gain = xml.getAttribute('gain');
nicholas@755 2066 if (isNaN(gain) == false && gain != null)
nicholas@755 2067 {
nicholas@755 2068 this.gain = decibelToLinear(Number(gain));
nicholas@755 2069 }
nicholas@755 2070 if (this.type == null) {this.type = "normal";}
nicholas@755 2071 if (this.type == 'anchor') {this.anchor = true;}
nicholas@755 2072 else {this.anchor = false;}
nicholas@755 2073 if (this.type == 'reference') {this.reference = true;}
nicholas@755 2074 else {this.reference = false;}
nicholas@755 2075 if (this.anchor == true || this.reference == true)
nicholas@755 2076 {
nicholas@755 2077 this.marker = xml.getAttribute('marker');
nicholas@755 2078 if (this.marker != undefined)
nicholas@755 2079 {
nicholas@755 2080 this.marker = Number(this.marker);
nicholas@755 2081 if (isNaN(this.marker) == false)
nicholas@755 2082 {
nicholas@755 2083 if (this.marker > 1)
nicholas@755 2084 { this.marker /= 100.0;}
nicholas@755 2085 if (this.marker >= 0 && this.marker <= 1)
nicholas@755 2086 {
nicholas@755 2087 this.enforce = true;
nicholas@755 2088 return;
nicholas@755 2089 } else {
nicholas@755 2090 console.log("ERROR - Marker of audioElement "+this.id+" is not between 0 and 1 (float) or 0 and 100 (integer)!");
nicholas@755 2091 console.log("ERROR - Marker not enforced!");
nicholas@755 2092 }
nicholas@755 2093 } else {
nicholas@755 2094 console.log("ERROR - Marker of audioElement "+this.id+" is not a number!");
nicholas@755 2095 console.log("ERROR - Marker not enforced!");
nicholas@755 2096 }
nicholas@755 2097 }
nicholas@755 2098 }
nicholas@755 2099 };
nicholas@755 2100 this.encode = function(root)
nicholas@755 2101 {
nicholas@755 2102 var AENode = root.createElement("audioElements");
nicholas@755 2103 AENode.id = this.id;
nicholas@755 2104 AENode.setAttribute("url",this.url);
nicholas@755 2105 AENode.setAttribute("type",this.type);
nicholas@755 2106 AENode.setAttribute("gain",linearToDecibel(this.gain));
nicholas@755 2107 if (this.marker != false)
nicholas@755 2108 {
nicholas@755 2109 AENode.setAttribute("marker",this.marker*100);
nicholas@755 2110 }
nicholas@755 2111 return AENode;
nicholas@755 2112 };
nicholas@755 2113 };
nicholas@755 2114
nicholas@755 2115 this.commentQuestionNode = function(xml) {
nicholas@755 2116 this.id = null;
nicholas@755 2117 this.type = undefined;
nicholas@755 2118 this.question = undefined;
nicholas@755 2119 this.options = [];
nicholas@755 2120 this.statement = undefined;
nicholas@755 2121
nicholas@755 2122 this.childOption = function() {
nicholas@755 2123 this.type = 'option';
nicholas@755 2124 this.name = null;
nicholas@755 2125 this.text = null;
nicholas@755 2126 };
nicholas@755 2127 this.exportXML = function(root)
nicholas@755 2128 {
nicholas@755 2129 var CQNode = root.createElement("CommentQuestion");
nicholas@755 2130 CQNode.id = this.id;
nicholas@755 2131 CQNode.setAttribute("type",this.type);
nicholas@755 2132 switch(this.type)
nicholas@755 2133 {
nicholas@755 2134 case "text":
nicholas@755 2135 CQNode.textContent = this.question;
nicholas@755 2136 break;
nicholas@755 2137 case "radio":
nicholas@755 2138 var statement = root.createElement("statement");
nicholas@755 2139 statement.textContent = this.statement;
nicholas@755 2140 CQNode.appendChild(statement);
nicholas@755 2141 for (var i=0; i<this.options.length; i++)
nicholas@755 2142 {
nicholas@755 2143 var optionNode = root.createElement("option");
nicholas@755 2144 optionNode.setAttribute("name",this.options[i].name);
nicholas@755 2145 optionNode.textContent = this.options[i].text;
nicholas@755 2146 CQNode.appendChild(optionNode);
nicholas@755 2147 }
nicholas@755 2148 break;
nicholas@755 2149 case "checkbox":
nicholas@755 2150 var statement = root.createElement("statement");
nicholas@755 2151 statement.textContent = this.statement;
nicholas@755 2152 CQNode.appendChild(statement);
nicholas@755 2153 for (var i=0; i<this.options.length; i++)
nicholas@755 2154 {
nicholas@755 2155 var optionNode = root.createElement("option");
nicholas@755 2156 optionNode.setAttribute("name",this.options[i].name);
nicholas@755 2157 optionNode.textContent = this.options[i].text;
nicholas@755 2158 CQNode.appendChild(optionNode);
nicholas@755 2159 }
nicholas@755 2160 break;
nicholas@755 2161 }
nicholas@755 2162 return CQNode;
nicholas@755 2163 };
nicholas@755 2164 this.decode = function(xml) {
nicholas@755 2165 this.id = xml.id;
nicholas@755 2166 if (xml.getAttribute('mandatory') == 'true') {this.mandatory = true;}
nicholas@755 2167 else {this.mandatory = false;}
nicholas@755 2168 this.type = xml.getAttribute('type');
nicholas@755 2169 if (this.type == undefined) {this.type = 'text';}
nicholas@755 2170 switch (this.type) {
nicholas@755 2171 case 'text':
nicholas@755 2172 this.question = xml.textContent;
nicholas@755 2173 break;
nicholas@755 2174 case 'radio':
nicholas@755 2175 var child = xml.firstElementChild;
nicholas@755 2176 this.options = [];
nicholas@755 2177 while (child != undefined) {
nicholas@755 2178 if (child.nodeName == 'statement' && this.statement == undefined) {
nicholas@755 2179 this.statement = child.textContent;
nicholas@755 2180 } else if (child.nodeName == 'option') {
nicholas@755 2181 var node = new this.childOption();
nicholas@755 2182 node.name = child.getAttribute('name');
nicholas@755 2183 node.text = child.textContent;
nicholas@755 2184 this.options.push(node);
nicholas@755 2185 }
nicholas@755 2186 child = child.nextElementSibling;
nicholas@755 2187 }
nicholas@755 2188 break;
nicholas@755 2189 case 'checkbox':
nicholas@755 2190 var child = xml.firstElementChild;
nicholas@755 2191 this.options = [];
nicholas@755 2192 while (child != undefined) {
nicholas@755 2193 if (child.nodeName == 'statement' && this.statement == undefined) {
nicholas@755 2194 this.statement = child.textContent;
nicholas@755 2195 } else if (child.nodeName == 'option') {
nicholas@755 2196 var node = new this.childOption();
nicholas@755 2197 node.name = child.getAttribute('name');
nicholas@755 2198 node.text = child.textContent;
nicholas@755 2199 this.options.push(node);
nicholas@755 2200 }
nicholas@755 2201 child = child.nextElementSibling;
nicholas@755 2202 }
nicholas@755 2203 break;
nicholas@755 2204 }
nicholas@755 2205 };
nicholas@755 2206 };
nicholas@755 2207 };
nicholas@755 2208 }
nicholas@755 2209
nicholas@755 2210 function Interface(specificationObject) {
nicholas@755 2211 // This handles the bindings between the interface and the audioEngineContext;
nicholas@755 2212 this.specification = specificationObject;
nicholas@755 2213 this.insertPoint = document.getElementById("topLevelBody");
nicholas@755 2214
nicholas@755 2215 this.newPage = function(audioHolderObject)
nicholas@755 2216 {
nicholas@755 2217 audioEngineContext.newTestPage();
nicholas@755 2218 /// CHECK FOR SAMPLE RATE COMPATIBILITY
nicholas@755 2219 if (audioHolderObject.sampleRate != undefined) {
nicholas@755 2220 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
nicholas@755 2221 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 2222 alert(errStr);
nicholas@755 2223 return;
nicholas@755 2224 }
nicholas@755 2225 }
nicholas@755 2226
nicholas@755 2227 audioEngineContext.loopPlayback = audioHolderObject.loop;
nicholas@755 2228 // Delete any previous audioObjects associated with the audioEngine
nicholas@755 2229 audioEngineContext.audioObjects = [];
nicholas@755 2230 interfaceContext.deleteCommentBoxes();
nicholas@755 2231 interfaceContext.deleteCommentQuestions();
nicholas@755 2232 loadTest(audioHolderObject);
nicholas@755 2233 };
nicholas@755 2234
nicholas@755 2235 // Bounded by interface!!
nicholas@755 2236 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
nicholas@755 2237 // For example, APE returns the slider position normalised in a <value> tag.
nicholas@755 2238 this.interfaceObjects = [];
nicholas@755 2239 this.interfaceObject = function(){};
nicholas@755 2240
nicholas@755 2241 this.resizeWindow = function(event)
nicholas@755 2242 {
nicholas@755 2243 popup.resize(event);
nicholas@755 2244 for(var i=0; i<this.commentBoxes.length; i++)
nicholas@755 2245 {this.commentBoxes[i].resize();}
nicholas@755 2246 for(var i=0; i<this.commentQuestions.length; i++)
nicholas@755 2247 {this.commentQuestions[i].resize();}
nicholas@755 2248 try
nicholas@755 2249 {
nicholas@755 2250 resizeWindow(event);
nicholas@755 2251 }
nicholas@755 2252 catch(err)
nicholas@755 2253 {
nicholas@755 2254 console.log("Warning - Interface does not have Resize option");
nicholas@755 2255 console.log(err);
nicholas@755 2256 }
nicholas@755 2257 };
nicholas@755 2258
nicholas@755 2259 this.returnNavigator = function()
nicholas@755 2260 {
nicholas@755 2261 var node = document.createElement("navigator");
nicholas@755 2262 var platform = document.createElement("platform");
nicholas@755 2263 platform.textContent = navigator.platform;
nicholas@755 2264 var vendor = document.createElement("vendor");
nicholas@755 2265 vendor.textContent = navigator.vendor;
nicholas@755 2266 var userAgent = document.createElement("uagent");
nicholas@755 2267 userAgent.textContent = navigator.userAgent;
nicholas@755 2268 node.appendChild(platform);
nicholas@755 2269 node.appendChild(vendor);
nicholas@755 2270 node.appendChild(userAgent);
nicholas@755 2271 return node;
nicholas@755 2272 };
nicholas@755 2273
nicholas@755 2274 this.commentBoxes = [];
nicholas@755 2275 this.elementCommentBox = function(audioObject) {
nicholas@755 2276 var element = audioObject.specification;
nicholas@755 2277 this.audioObject = audioObject;
nicholas@755 2278 this.id = audioObject.id;
nicholas@755 2279 var audioHolderObject = audioObject.specification.parent;
nicholas@755 2280 // Create document objects to hold the comment boxes
nicholas@755 2281 this.trackComment = document.createElement('div');
nicholas@755 2282 this.trackComment.className = 'comment-div';
nicholas@755 2283 this.trackComment.id = 'comment-div-'+audioObject.id;
nicholas@755 2284 // Create a string next to each comment asking for a comment
nicholas@755 2285 this.trackString = document.createElement('span');
nicholas@755 2286 this.trackString.innerHTML = audioHolderObject.commentBoxPrefix+' '+audioObject.id;
nicholas@755 2287 // Create the HTML5 comment box 'textarea'
nicholas@755 2288 this.trackCommentBox = document.createElement('textarea');
nicholas@755 2289 this.trackCommentBox.rows = '4';
nicholas@755 2290 this.trackCommentBox.cols = '100';
nicholas@755 2291 this.trackCommentBox.name = 'trackComment'+audioObject.id;
nicholas@755 2292 this.trackCommentBox.className = 'trackComment';
nicholas@755 2293 var br = document.createElement('br');
nicholas@755 2294 // Add to the holder.
nicholas@755 2295 this.trackComment.appendChild(this.trackString);
nicholas@755 2296 this.trackComment.appendChild(br);
nicholas@755 2297 this.trackComment.appendChild(this.trackCommentBox);
nicholas@755 2298
nicholas@755 2299 this.exportXMLDOM = function() {
nicholas@755 2300 var root = document.createElement('comment');
nicholas@755 2301 if (this.audioObject.specification.parent.elementComments) {
nicholas@755 2302 var question = document.createElement('question');
nicholas@755 2303 question.textContent = this.trackString.textContent;
nicholas@755 2304 var response = document.createElement('response');
nicholas@755 2305 response.textContent = this.trackCommentBox.value;
nicholas@755 2306 console.log("Comment frag-"+this.id+": "+response.textContent);
nicholas@755 2307 root.appendChild(question);
nicholas@755 2308 root.appendChild(response);
nicholas@755 2309 }
nicholas@755 2310 return root;
nicholas@755 2311 };
nicholas@755 2312 this.resize = function()
nicholas@755 2313 {
nicholas@755 2314 var boxwidth = (window.innerWidth-100)/2;
nicholas@755 2315 if (boxwidth >= 600)
nicholas@755 2316 {
nicholas@755 2317 boxwidth = 600;
nicholas@755 2318 }
nicholas@755 2319 else if (boxwidth < 400)
nicholas@755 2320 {
nicholas@755 2321 boxwidth = 400;
nicholas@755 2322 }
nicholas@755 2323 this.trackComment.style.width = boxwidth+"px";
nicholas@755 2324 this.trackCommentBox.style.width = boxwidth-6+"px";
nicholas@755 2325 };
nicholas@755 2326 this.resize();
nicholas@755 2327 };
nicholas@755 2328
nicholas@755 2329 this.commentQuestions = [];
nicholas@755 2330
nicholas@755 2331 this.commentBox = function(commentQuestion) {
nicholas@755 2332 this.specification = commentQuestion;
nicholas@755 2333 // Create document objects to hold the comment boxes
nicholas@755 2334 this.holder = document.createElement('div');
nicholas@755 2335 this.holder.className = 'comment-div';
nicholas@755 2336 // Create a string next to each comment asking for a comment
nicholas@755 2337 this.string = document.createElement('span');
nicholas@755 2338 this.string.innerHTML = commentQuestion.question;
nicholas@755 2339 // Create the HTML5 comment box 'textarea'
nicholas@755 2340 this.textArea = document.createElement('textarea');
nicholas@755 2341 this.textArea.rows = '4';
nicholas@755 2342 this.textArea.cols = '100';
nicholas@755 2343 this.textArea.className = 'trackComment';
nicholas@755 2344 var br = document.createElement('br');
nicholas@755 2345 // Add to the holder.
nicholas@755 2346 this.holder.appendChild(this.string);
nicholas@755 2347 this.holder.appendChild(br);
nicholas@755 2348 this.holder.appendChild(this.textArea);
nicholas@755 2349
nicholas@755 2350 this.exportXMLDOM = function() {
nicholas@755 2351 var root = document.createElement('comment');
nicholas@755 2352 root.id = this.specification.id;
nicholas@755 2353 root.setAttribute('type',this.specification.type);
nicholas@755 2354 root.textContent = this.textArea.value;
nicholas@755 2355 console.log("Question: "+this.string.textContent);
nicholas@755 2356 console.log("Response: "+root.textContent);
nicholas@755 2357 return root;
nicholas@755 2358 };
nicholas@755 2359 this.resize = function()
nicholas@755 2360 {
nicholas@755 2361 var boxwidth = (window.innerWidth-100)/2;
nicholas@755 2362 if (boxwidth >= 600)
nicholas@755 2363 {
nicholas@755 2364 boxwidth = 600;
nicholas@755 2365 }
nicholas@755 2366 else if (boxwidth < 400)
nicholas@755 2367 {
nicholas@755 2368 boxwidth = 400;
nicholas@755 2369 }
nicholas@755 2370 this.holder.style.width = boxwidth+"px";
nicholas@755 2371 this.textArea.style.width = boxwidth-6+"px";
nicholas@755 2372 };
nicholas@755 2373 this.resize();
nicholas@755 2374 };
nicholas@755 2375
nicholas@755 2376 this.radioBox = function(commentQuestion) {
nicholas@755 2377 this.specification = commentQuestion;
nicholas@755 2378 // Create document objects to hold the comment boxes
nicholas@755 2379 this.holder = document.createElement('div');
nicholas@755 2380 this.holder.className = 'comment-div';
nicholas@755 2381 // Create a string next to each comment asking for a comment
nicholas@755 2382 this.string = document.createElement('span');
nicholas@755 2383 this.string.innerHTML = commentQuestion.statement;
nicholas@755 2384 var br = document.createElement('br');
nicholas@755 2385 // Add to the holder.
nicholas@755 2386 this.holder.appendChild(this.string);
nicholas@755 2387 this.holder.appendChild(br);
nicholas@755 2388 this.options = [];
nicholas@755 2389 this.inputs = document.createElement('div');
nicholas@755 2390 this.span = document.createElement('div');
nicholas@755 2391 this.inputs.align = 'center';
nicholas@755 2392 this.inputs.style.marginLeft = '12px';
nicholas@755 2393 this.span.style.marginLeft = '12px';
nicholas@755 2394 this.span.align = 'center';
nicholas@755 2395 this.span.style.marginTop = '15px';
nicholas@755 2396
nicholas@755 2397 var optCount = commentQuestion.options.length;
nicholas@755 2398 for (var i=0; i<optCount; i++)
nicholas@755 2399 {
nicholas@755 2400 var div = document.createElement('div');
nicholas@755 2401 div.style.width = '80px';
nicholas@755 2402 div.style.float = 'left';
nicholas@755 2403 var input = document.createElement('input');
nicholas@755 2404 input.type = 'radio';
nicholas@755 2405 input.name = commentQuestion.id;
nicholas@755 2406 input.setAttribute('setvalue',commentQuestion.options[i].name);
nicholas@755 2407 input.className = 'comment-radio';
nicholas@755 2408 div.appendChild(input);
nicholas@755 2409 this.inputs.appendChild(div);
nicholas@755 2410
nicholas@755 2411
nicholas@755 2412 div = document.createElement('div');
nicholas@755 2413 div.style.width = '80px';
nicholas@755 2414 div.style.float = 'left';
nicholas@755 2415 div.align = 'center';
nicholas@755 2416 var span = document.createElement('span');
nicholas@755 2417 span.textContent = commentQuestion.options[i].text;
nicholas@755 2418 span.className = 'comment-radio-span';
nicholas@755 2419 div.appendChild(span);
nicholas@755 2420 this.span.appendChild(div);
nicholas@755 2421 this.options.push(input);
nicholas@755 2422 }
nicholas@755 2423 this.holder.appendChild(this.span);
nicholas@755 2424 this.holder.appendChild(this.inputs);
nicholas@755 2425
nicholas@755 2426 this.exportXMLDOM = function() {
nicholas@755 2427 var root = document.createElement('comment');
nicholas@755 2428 root.id = this.specification.id;
nicholas@755 2429 root.setAttribute('type',this.specification.type);
nicholas@755 2430 var question = document.createElement('question');
nicholas@755 2431 question.textContent = this.string.textContent;
nicholas@755 2432 var response = document.createElement('response');
nicholas@755 2433 var i=0;
nicholas@755 2434 while(this.options[i].checked == false) {
nicholas@755 2435 i++;
nicholas@755 2436 if (i >= this.options.length) {
nicholas@755 2437 break;
nicholas@755 2438 }
nicholas@755 2439 }
nicholas@755 2440 if (i >= this.options.length) {
nicholas@755 2441 response.textContent = 'null';
nicholas@755 2442 } else {
nicholas@755 2443 response.textContent = this.options[i].getAttribute('setvalue');
nicholas@755 2444 response.setAttribute('number',i);
nicholas@755 2445 }
nicholas@755 2446 console.log('Comment: '+question.textContent);
nicholas@755 2447 console.log('Response: '+response.textContent);
nicholas@755 2448 root.appendChild(question);
nicholas@755 2449 root.appendChild(response);
nicholas@755 2450 return root;
nicholas@755 2451 };
nicholas@755 2452 this.resize = function()
nicholas@755 2453 {
nicholas@755 2454 var boxwidth = (window.innerWidth-100)/2;
nicholas@755 2455 if (boxwidth >= 600)
nicholas@755 2456 {
nicholas@755 2457 boxwidth = 600;
nicholas@755 2458 }
nicholas@755 2459 else if (boxwidth < 400)
nicholas@755 2460 {
nicholas@755 2461 boxwidth = 400;
nicholas@755 2462 }
nicholas@755 2463 this.holder.style.width = boxwidth+"px";
nicholas@755 2464 var text = this.holder.children[2];
nicholas@755 2465 var options = this.holder.children[3];
nicholas@755 2466 var optCount = options.children.length;
nicholas@755 2467 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
nicholas@755 2468 var options = options.firstChild;
nicholas@755 2469 var text = text.firstChild;
nicholas@755 2470 options.style.marginRight = spanMargin;
nicholas@755 2471 options.style.marginLeft = spanMargin;
nicholas@755 2472 text.style.marginRight = spanMargin;
nicholas@755 2473 text.style.marginLeft = spanMargin;
nicholas@755 2474 while(options.nextSibling != undefined)
nicholas@755 2475 {
nicholas@755 2476 options = options.nextSibling;
nicholas@755 2477 text = text.nextSibling;
nicholas@755 2478 options.style.marginRight = spanMargin;
nicholas@755 2479 options.style.marginLeft = spanMargin;
nicholas@755 2480 text.style.marginRight = spanMargin;
nicholas@755 2481 text.style.marginLeft = spanMargin;
nicholas@755 2482 }
nicholas@755 2483 };
nicholas@755 2484 this.resize();
nicholas@755 2485 };
nicholas@755 2486
nicholas@755 2487 this.checkboxBox = function(commentQuestion) {
nicholas@755 2488 this.specification = commentQuestion;
nicholas@755 2489 // Create document objects to hold the comment boxes
nicholas@755 2490 this.holder = document.createElement('div');
nicholas@755 2491 this.holder.className = 'comment-div';
nicholas@755 2492 // Create a string next to each comment asking for a comment
nicholas@755 2493 this.string = document.createElement('span');
nicholas@755 2494 this.string.innerHTML = commentQuestion.statement;
nicholas@755 2495 var br = document.createElement('br');
nicholas@755 2496 // Add to the holder.
nicholas@755 2497 this.holder.appendChild(this.string);
nicholas@755 2498 this.holder.appendChild(br);
nicholas@755 2499 this.options = [];
nicholas@755 2500 this.inputs = document.createElement('div');
nicholas@755 2501 this.span = document.createElement('div');
nicholas@755 2502 this.inputs.align = 'center';
nicholas@755 2503 this.inputs.style.marginLeft = '12px';
nicholas@755 2504 this.span.style.marginLeft = '12px';
nicholas@755 2505 this.span.align = 'center';
nicholas@755 2506 this.span.style.marginTop = '15px';
nicholas@755 2507
nicholas@755 2508 var optCount = commentQuestion.options.length;
nicholas@755 2509 for (var i=0; i<optCount; i++)
nicholas@755 2510 {
nicholas@755 2511 var div = document.createElement('div');
nicholas@755 2512 div.style.width = '80px';
nicholas@755 2513 div.style.float = 'left';
nicholas@755 2514 var input = document.createElement('input');
nicholas@755 2515 input.type = 'checkbox';
nicholas@755 2516 input.name = commentQuestion.id;
nicholas@755 2517 input.setAttribute('setvalue',commentQuestion.options[i].name);
nicholas@755 2518 input.className = 'comment-radio';
nicholas@755 2519 div.appendChild(input);
nicholas@755 2520 this.inputs.appendChild(div);
nicholas@755 2521
nicholas@755 2522
nicholas@755 2523 div = document.createElement('div');
nicholas@755 2524 div.style.width = '80px';
nicholas@755 2525 div.style.float = 'left';
nicholas@755 2526 div.align = 'center';
nicholas@755 2527 var span = document.createElement('span');
nicholas@755 2528 span.textContent = commentQuestion.options[i].text;
nicholas@755 2529 span.className = 'comment-radio-span';
nicholas@755 2530 div.appendChild(span);
nicholas@755 2531 this.span.appendChild(div);
nicholas@755 2532 this.options.push(input);
nicholas@755 2533 }
nicholas@755 2534 this.holder.appendChild(this.span);
nicholas@755 2535 this.holder.appendChild(this.inputs);
nicholas@755 2536
nicholas@755 2537 this.exportXMLDOM = function() {
nicholas@755 2538 var root = document.createElement('comment');
nicholas@755 2539 root.id = this.specification.id;
nicholas@755 2540 root.setAttribute('type',this.specification.type);
nicholas@755 2541 var question = document.createElement('question');
nicholas@755 2542 question.textContent = this.string.textContent;
nicholas@755 2543 root.appendChild(question);
nicholas@755 2544 console.log('Comment: '+question.textContent);
nicholas@755 2545 for (var i=0; i<this.options.length; i++) {
nicholas@755 2546 var response = document.createElement('response');
nicholas@755 2547 response.textContent = this.options[i].checked;
nicholas@755 2548 response.setAttribute('name',this.options[i].getAttribute('setvalue'));
nicholas@755 2549 root.appendChild(response);
nicholas@755 2550 console.log('Response '+response.getAttribute('name') +': '+response.textContent);
nicholas@755 2551 }
nicholas@755 2552 return root;
nicholas@755 2553 };
nicholas@755 2554 this.resize = function()
nicholas@755 2555 {
nicholas@755 2556 var boxwidth = (window.innerWidth-100)/2;
nicholas@755 2557 if (boxwidth >= 600)
nicholas@755 2558 {
nicholas@755 2559 boxwidth = 600;
nicholas@755 2560 }
nicholas@755 2561 else if (boxwidth < 400)
nicholas@755 2562 {
nicholas@755 2563 boxwidth = 400;
nicholas@755 2564 }
nicholas@755 2565 this.holder.style.width = boxwidth+"px";
nicholas@755 2566 var text = this.holder.children[2];
nicholas@755 2567 var options = this.holder.children[3];
nicholas@755 2568 var optCount = options.children.length;
nicholas@755 2569 var spanMargin = Math.floor(((boxwidth-20-(optCount*80))/(optCount))/2)+'px';
nicholas@755 2570 var options = options.firstChild;
nicholas@755 2571 var text = text.firstChild;
nicholas@755 2572 options.style.marginRight = spanMargin;
nicholas@755 2573 options.style.marginLeft = spanMargin;
nicholas@755 2574 text.style.marginRight = spanMargin;
nicholas@755 2575 text.style.marginLeft = spanMargin;
nicholas@755 2576 while(options.nextSibling != undefined)
nicholas@755 2577 {
nicholas@755 2578 options = options.nextSibling;
nicholas@755 2579 text = text.nextSibling;
nicholas@755 2580 options.style.marginRight = spanMargin;
nicholas@755 2581 options.style.marginLeft = spanMargin;
nicholas@755 2582 text.style.marginRight = spanMargin;
nicholas@755 2583 text.style.marginLeft = spanMargin;
nicholas@755 2584 }
nicholas@755 2585 };
nicholas@755 2586 this.resize();
nicholas@755 2587 };
nicholas@755 2588
nicholas@755 2589 this.createCommentBox = function(audioObject) {
nicholas@755 2590 var node = new this.elementCommentBox(audioObject);
nicholas@755 2591 this.commentBoxes.push(node);
nicholas@755 2592 audioObject.commentDOM = node;
nicholas@755 2593 return node;
nicholas@755 2594 };
nicholas@755 2595
nicholas@755 2596 this.sortCommentBoxes = function() {
nicholas@755 2597 var holder = [];
nicholas@755 2598 while (this.commentBoxes.length > 0) {
nicholas@755 2599 var node = this.commentBoxes.pop(0);
nicholas@755 2600 holder[node.id] = node;
nicholas@755 2601 }
nicholas@755 2602 this.commentBoxes = holder;
nicholas@755 2603 };
nicholas@755 2604
nicholas@755 2605 this.showCommentBoxes = function(inject, sort) {
nicholas@755 2606 if (sort) {interfaceContext.sortCommentBoxes();}
nicholas@755 2607 for (var i=0; i<interfaceContext.commentBoxes.length; i++) {
nicholas@755 2608 inject.appendChild(this.commentBoxes[i].trackComment);
nicholas@755 2609 }
nicholas@755 2610 };
nicholas@755 2611
nicholas@755 2612 this.deleteCommentBoxes = function() {
nicholas@755 2613 this.commentBoxes = [];
nicholas@755 2614 };
nicholas@755 2615
nicholas@755 2616 this.createCommentQuestion = function(element) {
nicholas@755 2617 var node;
nicholas@755 2618 if (element.type == 'text') {
nicholas@755 2619 node = new this.commentBox(element);
nicholas@755 2620 } else if (element.type == 'radio') {
nicholas@755 2621 node = new this.radioBox(element);
nicholas@755 2622 } else if (element.type == 'checkbox') {
nicholas@755 2623 node = new this.checkboxBox(element);
nicholas@755 2624 }
nicholas@755 2625 this.commentQuestions.push(node);
nicholas@755 2626 return node;
nicholas@755 2627 };
nicholas@755 2628
nicholas@755 2629 this.deleteCommentQuestions = function()
nicholas@755 2630 {
nicholas@755 2631 this.commentQuestions = [];
nicholas@755 2632 };
nicholas@755 2633
nicholas@755 2634 this.playhead = new function()
nicholas@755 2635 {
nicholas@755 2636 this.object = document.createElement('div');
nicholas@755 2637 this.object.className = 'playhead';
nicholas@755 2638 this.object.align = 'left';
nicholas@755 2639 var curTime = document.createElement('div');
nicholas@755 2640 curTime.style.width = '50px';
nicholas@755 2641 this.curTimeSpan = document.createElement('span');
nicholas@755 2642 this.curTimeSpan.textContent = '00:00';
nicholas@755 2643 curTime.appendChild(this.curTimeSpan);
nicholas@755 2644 this.object.appendChild(curTime);
nicholas@755 2645 this.scrubberTrack = document.createElement('div');
nicholas@755 2646 this.scrubberTrack.className = 'playhead-scrub-track';
nicholas@755 2647
nicholas@755 2648 this.scrubberHead = document.createElement('div');
nicholas@755 2649 this.scrubberHead.id = 'playhead-scrubber';
nicholas@755 2650 this.scrubberTrack.appendChild(this.scrubberHead);
nicholas@755 2651 this.object.appendChild(this.scrubberTrack);
nicholas@755 2652
nicholas@755 2653 this.timePerPixel = 0;
nicholas@755 2654 this.maxTime = 0;
nicholas@755 2655
nicholas@755 2656 this.playbackObject;
nicholas@755 2657
nicholas@755 2658 this.setTimePerPixel = function(audioObject) {
nicholas@755 2659 //maxTime must be in seconds
nicholas@755 2660 this.playbackObject = audioObject;
nicholas@755 2661 this.maxTime = audioObject.buffer.buffer.duration;
nicholas@755 2662 var width = 490; //500 - 10, 5 each side of the tracker head
nicholas@755 2663 this.timePerPixel = this.maxTime/490;
nicholas@755 2664 if (this.maxTime < 60) {
nicholas@755 2665 this.curTimeSpan.textContent = '0.00';
nicholas@755 2666 } else {
nicholas@755 2667 this.curTimeSpan.textContent = '00:00';
nicholas@755 2668 }
nicholas@755 2669 };
nicholas@755 2670
nicholas@755 2671 this.update = function() {
nicholas@755 2672 // Update the playhead position, startPlay must be called
nicholas@755 2673 if (this.timePerPixel > 0) {
nicholas@755 2674 var time = this.playbackObject.getCurrentPosition();
nicholas@755 2675 if (time > 0) {
nicholas@755 2676 var width = 490;
nicholas@755 2677 var pix = Math.floor(time/this.timePerPixel);
nicholas@755 2678 this.scrubberHead.style.left = pix+'px';
nicholas@755 2679 if (this.maxTime > 60.0) {
nicholas@755 2680 var secs = time%60;
nicholas@755 2681 var mins = Math.floor((time-secs)/60);
nicholas@755 2682 secs = secs.toString();
nicholas@755 2683 secs = secs.substr(0,2);
nicholas@755 2684 mins = mins.toString();
nicholas@755 2685 this.curTimeSpan.textContent = mins+':'+secs;
nicholas@755 2686 } else {
nicholas@755 2687 time = time.toString();
nicholas@755 2688 this.curTimeSpan.textContent = time.substr(0,4);
nicholas@755 2689 }
nicholas@755 2690 } else {
nicholas@755 2691 this.scrubberHead.style.left = '0px';
nicholas@755 2692 if (this.maxTime < 60) {
nicholas@755 2693 this.curTimeSpan.textContent = '0.00';
nicholas@755 2694 } else {
nicholas@755 2695 this.curTimeSpan.textContent = '00:00';
nicholas@755 2696 }
nicholas@755 2697 }
nicholas@755 2698 }
nicholas@755 2699 };
nicholas@755 2700
nicholas@755 2701 this.interval = undefined;
nicholas@755 2702
nicholas@755 2703 this.start = function() {
nicholas@755 2704 if (this.playbackObject != undefined && this.interval == undefined) {
nicholas@755 2705 if (this.maxTime < 60) {
nicholas@755 2706 this.interval = setInterval(function(){interfaceContext.playhead.update();},10);
nicholas@755 2707 } else {
nicholas@755 2708 this.interval = setInterval(function(){interfaceContext.playhead.update();},100);
nicholas@755 2709 }
nicholas@755 2710 }
nicholas@755 2711 };
nicholas@755 2712 this.stop = function() {
nicholas@755 2713 clearInterval(this.interval);
nicholas@755 2714 this.interval = undefined;
nicholas@755 2715 if (this.maxTime < 60) {
nicholas@755 2716 this.curTimeSpan.textContent = '0.00';
nicholas@755 2717 } else {
nicholas@755 2718 this.curTimeSpan.textContent = '00:00';
nicholas@755 2719 }
nicholas@755 2720 };
nicholas@755 2721 };
nicholas@755 2722
nicholas@755 2723 // Global Checkers
nicholas@755 2724 // These functions will help enforce the checkers
nicholas@755 2725 this.checkHiddenAnchor = function()
nicholas@755 2726 {
nicholas@755 2727 var audioHolder = testState.currentStateMap[testState.currentIndex];
nicholas@755 2728 if (audioHolder.anchorId != null)
nicholas@755 2729 {
nicholas@755 2730 var audioObject = audioEngineContext.audioObjects[audioHolder.anchorId];
nicholas@755 2731 if (audioObject.interfaceDOM.getValue() > audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
nicholas@755 2732 {
nicholas@755 2733 // Anchor is not set below
nicholas@755 2734 console.log('Anchor node not below marker value');
nicholas@755 2735 alert('Please keep listening');
nicholas@755 2736 return false;
nicholas@755 2737 }
nicholas@755 2738 }
nicholas@755 2739 return true;
nicholas@755 2740 };
nicholas@755 2741
nicholas@755 2742 this.checkHiddenReference = function()
nicholas@755 2743 {
nicholas@755 2744 var audioHolder = testState.currentStateMap[testState.currentIndex];
nicholas@755 2745 if (audioHolder.referenceId != null)
nicholas@755 2746 {
nicholas@755 2747 var audioObject = audioEngineContext.audioObjects[audioHolder.referenceId];
nicholas@755 2748 if (audioObject.interfaceDOM.getValue() < audioObject.specification.marker && audioObject.interfaceDOM.enforce == true)
nicholas@755 2749 {
nicholas@755 2750 // Anchor is not set below
nicholas@755 2751 console.log('Reference node not above marker value');
nicholas@755 2752 alert('Please keep listening');
nicholas@755 2753 return false;
nicholas@755 2754 }
nicholas@755 2755 }
nicholas@755 2756 return true;
nicholas@755 2757 };
nicholas@755 2758
nicholas@755 2759 this.checkFragmentsFullyPlayed = function ()
nicholas@755 2760 {
nicholas@755 2761 // Checks the entire file has been played back
nicholas@755 2762 // NOTE ! This will return true IF playback is Looped!!!
nicholas@755 2763 if (audioEngineContext.loopPlayback)
nicholas@755 2764 {
nicholas@755 2765 console.log("WARNING - Looped source: Cannot check fragments are fully played");
nicholas@755 2766 return true;
nicholas@755 2767 }
nicholas@755 2768 var check_pass = true;
nicholas@755 2769 var error_obj = [];
nicholas@755 2770 for (var i = 0; i<audioEngineContext.audioObjects.length; i++)
nicholas@755 2771 {
nicholas@755 2772 var object = audioEngineContext.audioObjects[i];
nicholas@756 2773 var time = object.buffer.buffer.duration;
nicholas@755 2774 var metric = object.metric;
nicholas@755 2775 var passed = false;
nicholas@755 2776 for (var j=0; j<metric.listenTracker.length; j++)
nicholas@755 2777 {
nicholas@755 2778 var bt = metric.listenTracker[j].getElementsByTagName('buffertime');
nicholas@755 2779 var start_time = Number(bt[0].getAttribute('start'));
nicholas@755 2780 var stop_time = Number(bt[0].getAttribute('stop'));
nicholas@755 2781 var delta = stop_time - start_time;
nicholas@755 2782 if (delta >= time)
nicholas@755 2783 {
nicholas@755 2784 passed = true;
nicholas@755 2785 break;
nicholas@755 2786 }
nicholas@755 2787 }
nicholas@755 2788 if (passed == false)
nicholas@755 2789 {
nicholas@755 2790 check_pass = false;
nicholas@755 2791 console.log("Continue listening to track-"+i);
nicholas@755 2792 error_obj.push(i);
nicholas@755 2793 }
nicholas@755 2794 }
nicholas@755 2795 if (check_pass == false)
nicholas@755 2796 {
nicholas@756 2797 var str_start = "You have not completely listened to fragments ";
nicholas@755 2798 for (var i=0; i<error_obj.length; i++)
nicholas@755 2799 {
nicholas@755 2800 str_start += error_obj[i];
nicholas@755 2801 if (i != error_obj.length-1)
nicholas@755 2802 {
nicholas@755 2803 str_start += ', ';
nicholas@755 2804 }
nicholas@755 2805 }
nicholas@755 2806 str_start += ". Please keep listening";
nicholas@755 2807 console.log("[ALERT]: "+str_start);
nicholas@755 2808 alert(str_start);
nicholas@755 2809 }
nicholas@755 2810 };
nicholas@755 2811 }