annotate core.js @ 1121:595511282fa7

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