annotate core.js @ 1399:5f17d2a88e27

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