annotate core.js @ 1408:8e19255b85b3

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