annotate core.js @ 444:9c9fd68693b1

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