annotate core.js @ 1393:0d78b1204f37

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