annotate ape.js @ 1602:503ba0ff72c2

Bug #1258 Fixed. audioObject.metric.startListening now writes to console.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Fri, 29 May 2015 16:27:05 +0100
parents
children 1265e2aebf17
rev   line source
nickjillings@1602 1 /**
nickjillings@1602 2 * ape.js
nickjillings@1602 3 * Create the APE interface
nickjillings@1602 4 */
nickjillings@1602 5
nickjillings@1602 6 // preTest - In preTest state
nickjillings@1602 7 // testRun-ID - In test running, test Id number at the end 'testRun-2'
nickjillings@1602 8 // testRunPost-ID - Post test of test ID
nickjillings@1602 9 // testRunPre-ID - Pre-test of test ID
nickjillings@1602 10 // postTest - End of test, final submission!
nickjillings@1602 11
nickjillings@1602 12
nickjillings@1602 13 // Once this is loaded and parsed, begin execution
nickjillings@1602 14 loadInterface(projectXML);
nickjillings@1602 15
nickjillings@1602 16 function loadInterface(xmlDoc) {
nickjillings@1602 17
nickjillings@1602 18 // Get the dimensions of the screen available to the page
nickjillings@1602 19 var width = window.innerWidth;
nickjillings@1602 20 var height = window.innerHeight;
nickjillings@1602 21
nickjillings@1602 22 // The injection point into the HTML page
nickjillings@1602 23 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1602 24 var testContent = document.createElement('div');
nickjillings@1602 25
nickjillings@1602 26 testContent.id = 'testContent';
nickjillings@1602 27
nickjillings@1602 28
nickjillings@1602 29 // Decode parts of the xmlDoc that are needed
nickjillings@1602 30 // xmlDoc MUST already be parsed by jQuery!
nickjillings@1602 31 var xmlSetup = xmlDoc.find('setup');
nickjillings@1602 32 // Should put in an error function here incase of malprocessed or malformed XML
nickjillings@1602 33
nickjillings@1602 34 // Create pre and post test questions
nickjillings@1602 35
nickjillings@1602 36 var preTest = xmlSetup.find('PreTest');
nickjillings@1602 37 var postTest = xmlSetup.find('PostTest');
nickjillings@1602 38 preTest = preTest[0];
nickjillings@1602 39 postTest = postTest[0];
nickjillings@1602 40
nickjillings@1602 41 if (preTest == undefined) {preTest = document.createElement("preTest");}
nickjillings@1602 42 if (postTest == undefined){postTest= document.createElement("postTest");}
nickjillings@1602 43
nickjillings@1602 44 testState.stateMap.push(preTest);
nickjillings@1602 45
nickjillings@1602 46 // Extract the different test XML DOM trees
nickjillings@1602 47 var audioHolders = xmlDoc.find('audioHolder');
nickjillings@1602 48 var testXMLSetups = [];
nickjillings@1602 49 audioHolders.each(function(index,element) {
nickjillings@1602 50 var repeatN = element.attributes['repeatCount'].value;
nickjillings@1602 51 for (var r=0; r<=repeatN; r++) {
nickjillings@1602 52 testXMLSetups.push(element);
nickjillings@1602 53 }
nickjillings@1602 54 });
nickjillings@1602 55
nickjillings@1602 56 // New check if we need to randomise the test order
nickjillings@1602 57 var randomise = xmlSetup[0].attributes['randomiseOrder'];
nickjillings@1602 58 if (randomise != undefined) {
nickjillings@1602 59 if (randomise.value === 'true'){
nickjillings@1602 60 randomise = true;
nickjillings@1602 61 } else {
nickjillings@1602 62 randomise = false;
nickjillings@1602 63 }
nickjillings@1602 64 } else {
nickjillings@1602 65 randomise = false;
nickjillings@1602 66 }
nickjillings@1602 67
nickjillings@1602 68 if (randomise)
nickjillings@1602 69 {
nickjillings@1602 70 testXMLSetups = randomiseOrder(testXMLSetups);
nickjillings@1602 71 }
nickjillings@1602 72
nickjillings@1602 73 $(testXMLSetups).each(function(index,elem){
nickjillings@1602 74 testState.stateMap.push(elem);
nickjillings@1602 75 })
nickjillings@1602 76
nickjillings@1602 77 testState.stateMap.push(postTest);
nickjillings@1602 78
nickjillings@1602 79 // Obtain the metrics enabled
nickjillings@1602 80 var metricNode = xmlSetup.find('Metric');
nickjillings@1602 81 var metricNode = metricNode.find('metricEnable');
nickjillings@1602 82 metricNode.each(function(index,node){
nickjillings@1602 83 var enabled = node.textContent;
nickjillings@1602 84 switch(enabled)
nickjillings@1602 85 {
nickjillings@1602 86 case 'testTimer':
nickjillings@1602 87 sessionMetrics.prototype.enableTestTimer = true;
nickjillings@1602 88 break;
nickjillings@1602 89 case 'elementTimer':
nickjillings@1602 90 sessionMetrics.prototype.enableElementTimer = true;
nickjillings@1602 91 break;
nickjillings@1602 92 case 'elementTracker':
nickjillings@1602 93 sessionMetrics.prototype.enableElementTracker = true;
nickjillings@1602 94 break;
nickjillings@1602 95 case 'elementInitalPosition':
nickjillings@1602 96 sessionMetrics.prototype.enableElementInitialPosition = true;
nickjillings@1602 97 break;
nickjillings@1602 98 case 'elementFlagListenedTo':
nickjillings@1602 99 sessionMetrics.prototype.enableFlagListenedTo = true;
nickjillings@1602 100 break;
nickjillings@1602 101 case 'elementFlagMoved':
nickjillings@1602 102 sessionMetrics.prototype.enableFlagMoved = true;
nickjillings@1602 103 break;
nickjillings@1602 104 case 'elementFlagComments':
nickjillings@1602 105 sessionMetrics.prototype.enableFlagComments = true;
nickjillings@1602 106 break;
nickjillings@1602 107 }
nickjillings@1602 108 });
nickjillings@1602 109
nickjillings@1602 110 // Create APE specific metric functions
nickjillings@1602 111 audioEngineContext.metric.initialiseTest = function()
nickjillings@1602 112 {
nickjillings@1602 113 var sliders = document.getElementsByClassName('track-slider');
nickjillings@1602 114 for (var i=0; i<sliders.length; i++)
nickjillings@1602 115 {
nickjillings@1602 116 audioEngineContext.audioObjects[i].metric.initialised(convSliderPosToRate(i));
nickjillings@1602 117 }
nickjillings@1602 118 };
nickjillings@1602 119
nickjillings@1602 120 audioEngineContext.metric.sliderMoveStart = function(id)
nickjillings@1602 121 {
nickjillings@1602 122 if (this.data == -1)
nickjillings@1602 123 {
nickjillings@1602 124 this.data = id;
nickjillings@1602 125 } else {
nickjillings@1602 126 console.log('ERROR: Metric tracker detecting two moves!');
nickjillings@1602 127 this.data = -1;
nickjillings@1602 128 }
nickjillings@1602 129 };
nickjillings@1602 130 audioEngineContext.metric.sliderMoved = function()
nickjillings@1602 131 {
nickjillings@1602 132 var time = audioEngineContext.timer.getTestTime();
nickjillings@1602 133 var id = this.data;
nickjillings@1602 134 this.data = -1;
nickjillings@1602 135 var position = convSliderPosToRate(id);
nickjillings@1602 136 console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id
nickjillings@1602 137 if (audioEngineContext.timer.testStarted)
nickjillings@1602 138 {
nickjillings@1602 139 audioEngineContext.audioObjects[id].metric.moved(time,position);
nickjillings@1602 140 }
nickjillings@1602 141 };
nickjillings@1602 142
nickjillings@1602 143 audioEngineContext.metric.sliderPlayed = function(id)
nickjillings@1602 144 {
nickjillings@1602 145 var time = audioEngineContext.timer.getTestTime();
nickjillings@1602 146 if (audioEngineContext.timer.testStarted)
nickjillings@1602 147 {
nickjillings@1602 148 if (this.lastClicked >= 0)
nickjillings@1602 149 {
nickjillings@1602 150 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
nickjillings@1602 151 }
nickjillings@1602 152 this.lastClicked = id;
nickjillings@1602 153 audioEngineContext.audioObjects[id].metric.listening(time);
nickjillings@1602 154 }
nickjillings@1602 155 console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1602 156 };
nickjillings@1602 157
nickjillings@1602 158 // Create the top div for the Title element
nickjillings@1602 159 var titleAttr = xmlSetup[0].attributes['title'];
nickjillings@1602 160 var title = document.createElement('div');
nickjillings@1602 161 title.className = "title";
nickjillings@1602 162 title.align = "center";
nickjillings@1602 163 var titleSpan = document.createElement('span');
nickjillings@1602 164
nickjillings@1602 165 // Set title to that defined in XML, else set to default
nickjillings@1602 166 if (titleAttr != undefined) {
nickjillings@1602 167 titleSpan.innerHTML = titleAttr.value;
nickjillings@1602 168 } else {
nickjillings@1602 169 titleSpan.innerHTML = 'Listening test';
nickjillings@1602 170 }
nickjillings@1602 171 // Insert the titleSpan element into the title div element.
nickjillings@1602 172 title.appendChild(titleSpan);
nickjillings@1602 173
nickjillings@1602 174 var pagetitle = document.createElement('div');
nickjillings@1602 175 pagetitle.className = "pageTitle";
nickjillings@1602 176 pagetitle.align = "center";
nickjillings@1602 177 var titleSpan = document.createElement('span');
nickjillings@1602 178 titleSpan.id = "pageTitle";
nickjillings@1602 179 pagetitle.appendChild(titleSpan);
nickjillings@1602 180
nickjillings@1602 181 // Store the return URL path in global projectReturn
nickjillings@1602 182 projectReturn = xmlSetup[0].attributes['projectReturn'].value;
nickjillings@1602 183
nickjillings@1602 184 // Create Interface buttons!
nickjillings@1602 185 var interfaceButtons = document.createElement('div');
nickjillings@1602 186 interfaceButtons.id = 'interface-buttons';
nickjillings@1602 187
nickjillings@1602 188 // MANUAL DOWNLOAD POINT
nickjillings@1602 189 // If project return is null, this MUST be specified as the location to create the download link
nickjillings@1602 190 var downloadPoint = document.createElement('div');
nickjillings@1602 191 downloadPoint.id = 'download-point';
nickjillings@1602 192
nickjillings@1602 193 // Create playback start/stop points
nickjillings@1602 194 var playback = document.createElement("button");
nickjillings@1602 195 playback.innerHTML = 'Stop';
nickjillings@1602 196 playback.id = 'playback-button';
nickjillings@1602 197 // onclick function. Check if it is playing or not, call the correct function in the
nickjillings@1602 198 // audioEngine, change the button text to reflect the next state.
nickjillings@1602 199 playback.onclick = function() {
nickjillings@1602 200 if (audioEngineContext.status == 1) {
nickjillings@1602 201 audioEngineContext.stop();
nickjillings@1602 202 this.innerHTML = 'Stop';
nickjillings@1602 203 var time = audioEngineContext.timer.getTestTime();
nickjillings@1602 204 console.log('Stopped at ' + time); // DEBUG/SAFETY
nickjillings@1602 205 }
nickjillings@1602 206 };
nickjillings@1602 207 // Create Submit (save) button
nickjillings@1602 208 var submit = document.createElement("button");
nickjillings@1602 209 submit.innerHTML = 'Submit';
nickjillings@1602 210 submit.onclick = buttonSubmitClick;
nickjillings@1602 211 submit.id = 'submit-button';
nickjillings@1602 212 // Append the interface buttons into the interfaceButtons object.
nickjillings@1602 213 interfaceButtons.appendChild(playback);
nickjillings@1602 214 interfaceButtons.appendChild(submit);
nickjillings@1602 215 interfaceButtons.appendChild(downloadPoint);
nickjillings@1602 216
nickjillings@1602 217 // Now create the slider and HTML5 canvas boxes
nickjillings@1602 218
nickjillings@1602 219 // Create the div box to center align
nickjillings@1602 220 var sliderBox = document.createElement('div');
nickjillings@1602 221 sliderBox.className = 'sliderCanvasDiv';
nickjillings@1602 222 sliderBox.id = 'sliderCanvasHolder';
nickjillings@1602 223
nickjillings@1602 224 // Create the slider box to hold the slider elements
nickjillings@1602 225 var canvas = document.createElement('div');
nickjillings@1602 226 canvas.id = 'slider';
nickjillings@1602 227 canvas.align = "left";
nickjillings@1602 228 canvas.addEventListener('dragover',function(event){
nickjillings@1602 229 event.preventDefault();
nickjillings@1602 230 return false;
nickjillings@1602 231 },false);
nickjillings@1602 232 var sliderMargin = document.createAttribute('marginsize');
nickjillings@1602 233 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
nickjillings@1602 234 // Must have a known EXACT width, as this is used later to determine the ratings
nickjillings@1602 235 var w = (Number(sliderMargin.nodeValue)+8)*2;
nickjillings@1602 236 canvas.style.width = width - w +"px";
nickjillings@1602 237 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
nickjillings@1602 238 canvas.setAttributeNode(sliderMargin);
nickjillings@1602 239 sliderBox.appendChild(canvas);
nickjillings@1602 240
nickjillings@1602 241 // Create the div to hold any scale objects
nickjillings@1602 242 var scale = document.createElement('div');
nickjillings@1602 243 scale.className = 'sliderScale';
nickjillings@1602 244 scale.id = 'sliderScaleHolder';
nickjillings@1602 245 scale.align = 'left';
nickjillings@1602 246 sliderBox.appendChild(scale);
nickjillings@1602 247
nickjillings@1602 248 // Global parent for the comment boxes on the page
nickjillings@1602 249 var feedbackHolder = document.createElement('div');
nickjillings@1602 250 feedbackHolder.id = 'feedbackHolder';
nickjillings@1602 251
nickjillings@1602 252 testContent.style.zIndex = 1;
nickjillings@1602 253 insertPoint.innerHTML = null; // Clear the current schema
nickjillings@1602 254
nickjillings@1602 255 currentState = 'preTest';
nickjillings@1602 256
nickjillings@1602 257 // Inject into HTML
nickjillings@1602 258 testContent.appendChild(title); // Insert the title
nickjillings@1602 259 testContent.appendChild(pagetitle);
nickjillings@1602 260 testContent.appendChild(interfaceButtons);
nickjillings@1602 261 testContent.appendChild(sliderBox);
nickjillings@1602 262 testContent.appendChild(feedbackHolder);
nickjillings@1602 263 insertPoint.appendChild(testContent);
nickjillings@1602 264
nickjillings@1602 265 // Load the full interface
nickjillings@1602 266 testState.initialise();
nickjillings@1602 267 testState.advanceState();
nickjillings@1602 268
nickjillings@1602 269 testWaitIndicator();
nickjillings@1602 270 }
nickjillings@1602 271
nickjillings@1602 272 function loadTest(textXML)
nickjillings@1602 273 {
nickjillings@1602 274
nickjillings@1602 275 // Reset audioEngineContext.Metric globals for new test
nickjillings@1602 276 audioEngineContext.newTestPage();
nickjillings@1602 277
nickjillings@1602 278 var id = textXML.id;
nickjillings@1602 279
nickjillings@1602 280 var feedbackHolder = document.getElementById('feedbackHolder');
nickjillings@1602 281 var canvas = document.getElementById('slider');
nickjillings@1602 282 feedbackHolder.innerHTML = null;
nickjillings@1602 283 canvas.innerHTML = null;
nickjillings@1602 284
nickjillings@1602 285 // Setup question title
nickjillings@1602 286 var interfaceObj = $(textXML).find('interface');
nickjillings@1602 287 var titleNode = interfaceObj.find('title');
nickjillings@1602 288 if (titleNode[0] != undefined)
nickjillings@1602 289 {
nickjillings@1602 290 document.getElementById('pageTitle').textContent = titleNode[0].textContent;
nickjillings@1602 291 }
nickjillings@1602 292 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
nickjillings@1602 293 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
nickjillings@1602 294 var scale = document.getElementById('sliderScaleHolder');
nickjillings@1602 295 scale.innerHTML = null;
nickjillings@1602 296 interfaceObj.find('scale').each(function(index,scaleObj){
nickjillings@1602 297 var value = document.createAttribute('value');
nickjillings@1602 298 var position = Number(scaleObj.attributes['position'].value)*0.01;
nickjillings@1602 299 value.nodeValue = position;
nickjillings@1602 300 var pixelPosition = (position*positionScale)+offset;
nickjillings@1602 301 var scaleDOM = document.createElement('span');
nickjillings@1602 302 scaleDOM.textContent = scaleObj.textContent;
nickjillings@1602 303 scale.appendChild(scaleDOM);
nickjillings@1602 304 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
nickjillings@1602 305 scaleDOM.setAttributeNode(value);
nickjillings@1602 306 });
nickjillings@1602 307
nickjillings@1602 308 // Extract the hostURL attribute. If not set, create an empty string.
nickjillings@1602 309 var hostURL = textXML.attributes['hostURL'];
nickjillings@1602 310 if (hostURL == undefined) {
nickjillings@1602 311 hostURL = "";
nickjillings@1602 312 } else {
nickjillings@1602 313 hostURL = hostURL.value;
nickjillings@1602 314 }
nickjillings@1602 315 // Extract the sampleRate. If set, convert the string to a Number.
nickjillings@1602 316 var hostFs = textXML.attributes['sampleRate'];
nickjillings@1602 317 if (hostFs != undefined) {
nickjillings@1602 318 hostFs = Number(hostFs.value);
nickjillings@1602 319 }
nickjillings@1602 320
nickjillings@1602 321 /// CHECK FOR SAMPLE RATE COMPATIBILITY
nickjillings@1602 322 if (hostFs != undefined) {
nickjillings@1602 323 if (Number(hostFs) != audioContext.sampleRate) {
nickjillings@1602 324 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nickjillings@1602 325 alert(errStr);
nickjillings@1602 326 return;
nickjillings@1602 327 }
nickjillings@1602 328 }
nickjillings@1602 329
nickjillings@1602 330 var commentShow = textXML.attributes['elementComments'];
nickjillings@1602 331 if (commentShow != undefined) {
nickjillings@1602 332 if (commentShow.value == 'false') {commentShow = false;}
nickjillings@1602 333 else {commentShow = true;}
nickjillings@1602 334 } else {commentShow = true;}
nickjillings@1602 335
nickjillings@1602 336 var loopPlayback = textXML.attributes['loop'];
nickjillings@1602 337 if (loopPlayback != undefined)
nickjillings@1602 338 {
nickjillings@1602 339 loopPlayback = loopPlayback.value;
nickjillings@1602 340 if (loopPlayback == 'true') {
nickjillings@1602 341 loopPlayback = true;
nickjillings@1602 342 } else {
nickjillings@1602 343 loopPlayback = false;
nickjillings@1602 344 }
nickjillings@1602 345 } else {
nickjillings@1602 346 loopPlayback = false;
nickjillings@1602 347 }
nickjillings@1602 348 audioEngineContext.loopPlayback = loopPlayback;
nickjillings@1602 349 // Create AudioEngine bindings for playback
nickjillings@1602 350 if (loopPlayback) {
nickjillings@1602 351 audioEngineContext.selectedTrack = function(id) {
nickjillings@1602 352 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1602 353 {
nickjillings@1602 354 if (id == i) {
nickjillings@1602 355 this.audioObjects[i].loopStart();
nickjillings@1602 356 } else {
nickjillings@1602 357 this.audioObjects[i].loopStop();
nickjillings@1602 358 }
nickjillings@1602 359 }
nickjillings@1602 360 };
nickjillings@1602 361 } else {
nickjillings@1602 362 audioEngineContext.selectedTrack = function(id) {
nickjillings@1602 363 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1602 364 {
nickjillings@1602 365 this.audioObjects[i].outputGain.gain.value = 0.0;
nickjillings@1602 366 this.audioObjects[i].stop();
nickjillings@1602 367 }
nickjillings@1602 368 if (this.status == 1) {
nickjillings@1602 369 this.audioObjects[id].outputGain.gain.value = 1.0;
nickjillings@1602 370 this.audioObjects[id].play(audioContext.currentTime+0.01);
nickjillings@1602 371 this.audioObjects[id].flagAsPlayed();
nickjillings@1602 372 }
nickjillings@1602 373 };
nickjillings@1602 374 }
nickjillings@1602 375
nickjillings@1602 376 currentTestHolder = document.createElement('audioHolder');
nickjillings@1602 377 currentTestHolder.id = textXML.id;
nickjillings@1602 378 currentTestHolder.repeatCount = textXML.attributes['repeatCount'].value;
nickjillings@1602 379
nickjillings@1602 380 var randomise = textXML.attributes['randomiseOrder'];
nickjillings@1602 381 if (randomise != undefined) {randomise = randomise.value;}
nickjillings@1602 382 else {randomise = false;}
nickjillings@1602 383
nickjillings@1602 384 var audioElements = $(textXML).find('audioElements');
nickjillings@1602 385 currentTrackOrder = [];
nickjillings@1602 386 audioElements.each(function(index,element){
nickjillings@1602 387 // Find any blind-repeats
nickjillings@1602 388 // Not implemented yet, but just in case
nickjillings@1602 389 currentTrackOrder[index] = element;
nickjillings@1602 390 });
nickjillings@1602 391 if (randomise) {
nickjillings@1602 392 currentTrackOrder = randomiseOrder(currentTrackOrder);
nickjillings@1602 393 }
nickjillings@1602 394
nickjillings@1602 395 // Delete any previous audioObjects associated with the audioEngine
nickjillings@1602 396 audioEngineContext.audioObjects = [];
nickjillings@1602 397
nickjillings@1602 398 // Find all the audioElements from the audioHolder
nickjillings@1602 399 $(currentTrackOrder).each(function(index,element){
nickjillings@1602 400 // Find URL of track
nickjillings@1602 401 // In this jQuery loop, variable 'this' holds the current audioElement.
nickjillings@1602 402
nickjillings@1602 403 // Now load each audio sample. First create the new track by passing the full URL
nickjillings@1602 404 var trackURL = hostURL + this.attributes['url'].value;
nickjillings@1602 405 audioEngineContext.newTrack(trackURL);
nickjillings@1602 406
nickjillings@1602 407 if (commentShow) {
nickjillings@1602 408 // Create document objects to hold the comment boxes
nickjillings@1602 409 var trackComment = document.createElement('div');
nickjillings@1602 410 trackComment.className = 'comment-div';
nickjillings@1602 411 // Create a string next to each comment asking for a comment
nickjillings@1602 412 var trackString = document.createElement('span');
nickjillings@1602 413 trackString.innerHTML = 'Comment on track '+index;
nickjillings@1602 414 // Create the HTML5 comment box 'textarea'
nickjillings@1602 415 var trackCommentBox = document.createElement('textarea');
nickjillings@1602 416 trackCommentBox.rows = '4';
nickjillings@1602 417 trackCommentBox.cols = '100';
nickjillings@1602 418 trackCommentBox.name = 'trackComment'+index;
nickjillings@1602 419 trackCommentBox.className = 'trackComment';
nickjillings@1602 420 var br = document.createElement('br');
nickjillings@1602 421 // Add to the holder.
nickjillings@1602 422 trackComment.appendChild(trackString);
nickjillings@1602 423 trackComment.appendChild(br);
nickjillings@1602 424 trackComment.appendChild(trackCommentBox);
nickjillings@1602 425 feedbackHolder.appendChild(trackComment);
nickjillings@1602 426 }
nickjillings@1602 427
nickjillings@1602 428 // Create a slider per track
nickjillings@1602 429
nickjillings@1602 430 var trackSliderObj = document.createElement('div');
nickjillings@1602 431 trackSliderObj.className = 'track-slider';
nickjillings@1602 432 trackSliderObj.id = 'track-slider-'+index;
nickjillings@1602 433 // Distribute it randomnly
nickjillings@1602 434 var w = window.innerWidth - 100;
nickjillings@1602 435 w = Math.random()*w;
nickjillings@1602 436 trackSliderObj.style.left = Math.floor(w)+50+'px';
nickjillings@1602 437 trackSliderObj.innerHTML = '<span>'+index+'</span>';
nickjillings@1602 438 trackSliderObj.draggable = true;
nickjillings@1602 439 trackSliderObj.ondragend = dragEnd;
nickjillings@1602 440 trackSliderObj.ondragstart = function()
nickjillings@1602 441 {
nickjillings@1602 442 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
nickjillings@1602 443 audioEngineContext.metric.sliderMoveStart(id);
nickjillings@1602 444 };
nickjillings@1602 445
nickjillings@1602 446 // Onclick, switch playback to that track
nickjillings@1602 447 trackSliderObj.onclick = function() {
nickjillings@1602 448 // Start the test on first click, that way timings are more accurate.
nickjillings@1602 449 audioEngineContext.play();
nickjillings@1602 450 if (audioEngineContext.audioObjectsReady) {
nickjillings@1602 451 // Cannot continue to issue play command until audioObjects reported as ready!
nickjillings@1602 452 // Get the track ID from the object ID
nickjillings@1602 453 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
nickjillings@1602 454 //audioEngineContext.metric.sliderPlayed(id);
nickjillings@1602 455 audioEngineContext.selectedTrack(id);
nickjillings@1602 456 // Currently playing track red, rest green
nickjillings@1602 457 document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
nickjillings@1602 458 for (var i = 0; i<$(currentTrackOrder).length; i++)
nickjillings@1602 459 {
nickjillings@1602 460 if (i!=index) // Make all other sliders green
nickjillings@1602 461 {
nickjillings@1602 462 document.getElementById('track-slider-'+i).style.backgroundColor = "rgb(100,200,100)";
nickjillings@1602 463 }
nickjillings@1602 464
nickjillings@1602 465 }
nickjillings@1602 466 }
nickjillings@1602 467 };
nickjillings@1602 468
nickjillings@1602 469 canvas.appendChild(trackSliderObj);
nickjillings@1602 470
nickjillings@1602 471 });
nickjillings@1602 472
nickjillings@1602 473 // Append any commentQuestion boxes
nickjillings@1602 474 var commentQuestions = $(textXML).find('CommentQuestion');
nickjillings@1602 475 $(commentQuestions).each(function(index,element) {
nickjillings@1602 476 // Create document objects to hold the comment boxes
nickjillings@1602 477 var trackComment = document.createElement('div');
nickjillings@1602 478 trackComment.className = 'comment-div commentQuestion';
nickjillings@1602 479 trackComment.id = element.attributes['id'].value;
nickjillings@1602 480 // Create a string next to each comment asking for a comment
nickjillings@1602 481 var trackString = document.createElement('span');
nickjillings@1602 482 trackString.innerHTML = element.textContent;
nickjillings@1602 483 // Create the HTML5 comment box 'textarea'
nickjillings@1602 484 var trackCommentBox = document.createElement('textarea');
nickjillings@1602 485 trackCommentBox.rows = '4';
nickjillings@1602 486 trackCommentBox.cols = '100';
nickjillings@1602 487 trackCommentBox.name = 'commentQuestion'+index;
nickjillings@1602 488 trackCommentBox.className = 'trackComment';
nickjillings@1602 489 var br = document.createElement('br');
nickjillings@1602 490 // Add to the holder.
nickjillings@1602 491 trackComment.appendChild(trackString);
nickjillings@1602 492 trackComment.appendChild(br);
nickjillings@1602 493 trackComment.appendChild(trackCommentBox);
nickjillings@1602 494 feedbackHolder.appendChild(trackComment);
nickjillings@1602 495 });
nickjillings@1602 496 }
nickjillings@1602 497
nickjillings@1602 498
nickjillings@1602 499 function dragEnd(ev) {
nickjillings@1602 500 // Function call when a div has been dropped
nickjillings@1602 501 var slider = document.getElementById('slider');
nickjillings@1602 502 var marginSize = Number(slider.attributes['marginsize'].value);
nickjillings@1602 503 var w = slider.style.width;
nickjillings@1602 504 w = Number(w.substr(0,w.length-2));
nickjillings@1602 505 var x = ev.x;
nickjillings@1602 506 if (x >= marginSize && x < w+marginSize) {
nickjillings@1602 507 this.style.left = (x)+'px';
nickjillings@1602 508 } else {
nickjillings@1602 509 if (x<marginSize) {
nickjillings@1602 510 this.style.left = marginSize+'px';
nickjillings@1602 511 } else {
nickjillings@1602 512 this.style.left = (w+marginSize) + 'px';
nickjillings@1602 513 }
nickjillings@1602 514 }
nickjillings@1602 515 audioEngineContext.metric.sliderMoved();
nickjillings@1602 516 }
nickjillings@1602 517
nickjillings@1602 518 function buttonSubmitClick() // TODO: Only when all songs have been played!
nickjillings@1602 519 {
nickjillings@1602 520 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nickjillings@1602 521 if (hasBeenPlayed.length == 0) {
nickjillings@1602 522 if (audioEngineContext.status == 1) {
nickjillings@1602 523 var playback = document.getElementById('playback-button');
nickjillings@1602 524 playback.click();
nickjillings@1602 525 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
nickjillings@1602 526 } else
nickjillings@1602 527 {
nickjillings@1602 528 if (audioEngineContext.timer.testStarted == false)
nickjillings@1602 529 {
nickjillings@1602 530 alert('You have not started the test! Please press start to begin the test!');
nickjillings@1602 531 return;
nickjillings@1602 532 }
nickjillings@1602 533 }
nickjillings@1602 534 testState.advanceState();
nickjillings@1602 535 } else // if a fragment has not been played yet
nickjillings@1602 536 {
nickjillings@1602 537 str = "";
nickjillings@1602 538 if (hasBeenPlayed.length > 1) {
nickjillings@1602 539 for (var i=0; i<hasBeenPlayed.length; i++) {
nickjillings@1602 540 str = str + hasBeenPlayed[i];
nickjillings@1602 541 if (i < hasBeenPlayed.length-2){
nickjillings@1602 542 str += ", ";
nickjillings@1602 543 } else if (i == hasBeenPlayed.length-2) {
nickjillings@1602 544 str += " or ";
nickjillings@1602 545 }
nickjillings@1602 546 }
nickjillings@1602 547 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nickjillings@1602 548 } else {
nickjillings@1602 549 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nickjillings@1602 550 }
nickjillings@1602 551 return;
nickjillings@1602 552 }
nickjillings@1602 553 }
nickjillings@1602 554
nickjillings@1602 555 function convSliderPosToRate(id)
nickjillings@1602 556 {
nickjillings@1602 557 var w = document.getElementById('slider').style.width;
nickjillings@1602 558 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
nickjillings@1602 559 var maxPix = w.substr(0,w.length-2);
nickjillings@1602 560 var slider = document.getElementsByClassName('track-slider')[id];
nickjillings@1602 561 var pix = slider.style.left;
nickjillings@1602 562 pix = pix.substr(0,pix.length-2);
nickjillings@1602 563 var rate = (pix-marginsize)/maxPix;
nickjillings@1602 564 return rate;
nickjillings@1602 565 }
nickjillings@1602 566
nickjillings@1602 567 function resizeWindow(event){
nickjillings@1602 568 // Function called when the window has been resized.
nickjillings@1602 569 // MANDATORY FUNCTION
nickjillings@1602 570
nickjillings@1602 571 // Store the slider marker values
nickjillings@1602 572 var holdValues = [];
nickjillings@1602 573 $(".track-slider").each(function(index,sliderObj){
nickjillings@1602 574 holdValues.push(convSliderPosToRate(index));
nickjillings@1602 575 });
nickjillings@1602 576
nickjillings@1602 577 var width = event.target.innerWidth;
nickjillings@1602 578 var canvas = document.getElementById('sliderCanvasHolder');
nickjillings@1602 579 var sliderDiv = canvas.children[0];
nickjillings@1602 580 var sliderScaleDiv = canvas.children[1];
nickjillings@1602 581 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
nickjillings@1602 582 var w = (marginsize+8)*2;
nickjillings@1602 583 sliderDiv.style.width = width - w + 'px';
nickjillings@1602 584 var width = width - w;
nickjillings@1602 585 // Move sliders into new position
nickjillings@1602 586 $(".track-slider").each(function(index,sliderObj){
nickjillings@1602 587 var pos = holdValues[index];
nickjillings@1602 588 var pix = pos * width;
nickjillings@1602 589 sliderObj.style.left = pix+marginsize+'px';
nickjillings@1602 590 });
nickjillings@1602 591
nickjillings@1602 592 // Move scale labels
nickjillings@1602 593 $(sliderScaleDiv.children).each(function(index,scaleObj){
nickjillings@1602 594 var position = Number(scaleObj.attributes['value'].value);
nickjillings@1602 595 var pixelPosition = (position*width)+marginsize;
nickjillings@1602 596 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
nickjillings@1602 597 });
nickjillings@1602 598 }
nickjillings@1602 599
nickjillings@1602 600 function pageXMLSave(store, testXML, testId)
nickjillings@1602 601 {
nickjillings@1602 602 // Saves a specific test page
nickjillings@1602 603 var xmlDoc = store;
nickjillings@1602 604 // Check if any session wide metrics are enabled
nickjillings@1602 605
nickjillings@1602 606 var commentShow = testXML.attributes['elementComments'];
nickjillings@1602 607 if (commentShow != undefined) {
nickjillings@1602 608 if (commentShow.value == 'false') {commentShow = false;}
nickjillings@1602 609 else {commentShow = true;}
nickjillings@1602 610 } else {commentShow = true;}
nickjillings@1602 611
nickjillings@1602 612 var metric = document.createElement('metric');
nickjillings@1602 613 if (audioEngineContext.metric.enableTestTimer)
nickjillings@1602 614 {
nickjillings@1602 615 var testTime = document.createElement('metricResult');
nickjillings@1602 616 testTime.id = 'testTime';
nickjillings@1602 617 testTime.textContent = audioEngineContext.timer.testDuration;
nickjillings@1602 618 metric.appendChild(testTime);
nickjillings@1602 619 }
nickjillings@1602 620 xmlDoc.appendChild(metric);
nickjillings@1602 621 var trackSliderObjects = document.getElementsByClassName('track-slider');
nickjillings@1602 622 var commentObjects = document.getElementsByClassName('comment-div');
nickjillings@1602 623 for (var i=0; i<trackSliderObjects.length; i++)
nickjillings@1602 624 {
nickjillings@1602 625 var audioElement = document.createElement('audioElement');
nickjillings@1602 626 audioElement.id = currentTrackOrder[i].attributes['id'].value;
nickjillings@1602 627 audioElement.url = currentTrackOrder[i].attributes['url'].value;
nickjillings@1602 628 var value = document.createElement('value');
nickjillings@1602 629 value.innerHTML = convSliderPosToRate(i);
nickjillings@1602 630 if (commentShow) {
nickjillings@1602 631 var comment = document.createElement("comment");
nickjillings@1602 632 var question = document.createElement("question");
nickjillings@1602 633 var response = document.createElement("response");
nickjillings@1602 634 question.textContent = commentObjects[i].children[0].textContent;
nickjillings@1602 635 response.textContent = commentObjects[i].children[2].value;
nickjillings@1602 636 console.log('Comment ' + i + ': ' + commentObjects[i].children[2].value); // DEBUG/SAFETY
nickjillings@1602 637 comment.appendChild(question);
nickjillings@1602 638 comment.appendChild(response);
nickjillings@1602 639 audioElement.appendChild(comment);
nickjillings@1602 640 }
nickjillings@1602 641 audioElement.appendChild(value);
nickjillings@1602 642 // Check for any per element metrics
nickjillings@1602 643 var metric = document.createElement('metric');
nickjillings@1602 644 var elementMetric = audioEngineContext.audioObjects[i].metric;
nickjillings@1602 645 if (audioEngineContext.metric.enableElementTimer) {
nickjillings@1602 646 var elementTimer = document.createElement('metricResult');
nickjillings@1602 647 elementTimer.id = 'elementTimer';
nickjillings@1602 648 elementTimer.textContent = elementMetric.listenedTimer;
nickjillings@1602 649 metric.appendChild(elementTimer);
nickjillings@1602 650 }
nickjillings@1602 651 if (audioEngineContext.metric.enableElementTracker) {
nickjillings@1602 652 var elementTrackerFull = document.createElement('metricResult');
nickjillings@1602 653 elementTrackerFull.id = 'elementTrackerFull';
nickjillings@1602 654 var data = elementMetric.movementTracker;
nickjillings@1602 655 for (var k=0; k<data.length; k++)
nickjillings@1602 656 {
nickjillings@1602 657 var timePos = document.createElement('timePos');
nickjillings@1602 658 timePos.id = k;
nickjillings@1602 659 var time = document.createElement('time');
nickjillings@1602 660 time.textContent = data[k][0];
nickjillings@1602 661 var position = document.createElement('position');
nickjillings@1602 662 position.textContent = data[k][1];
nickjillings@1602 663 timePos.appendChild(time);
nickjillings@1602 664 timePos.appendChild(position);
nickjillings@1602 665 elementTrackerFull.appendChild(timePos);
nickjillings@1602 666 }
nickjillings@1602 667 metric.appendChild(elementTrackerFull);
nickjillings@1602 668 }
nickjillings@1602 669 if (audioEngineContext.metric.enableElementInitialPosition) {
nickjillings@1602 670 var elementInitial = document.createElement('metricResult');
nickjillings@1602 671 elementInitial.id = 'elementInitialPosition';
nickjillings@1602 672 elementInitial.textContent = elementMetric.initialPosition;
nickjillings@1602 673 metric.appendChild(elementInitial);
nickjillings@1602 674 }
nickjillings@1602 675 if (audioEngineContext.metric.enableFlagListenedTo) {
nickjillings@1602 676 var flagListenedTo = document.createElement('metricResult');
nickjillings@1602 677 flagListenedTo.id = 'elementFlagListenedTo';
nickjillings@1602 678 flagListenedTo.textContent = elementMetric.wasListenedTo;
nickjillings@1602 679 metric.appendChild(flagListenedTo);
nickjillings@1602 680 }
nickjillings@1602 681 if (audioEngineContext.metric.enableFlagMoved) {
nickjillings@1602 682 var flagMoved = document.createElement('metricResult');
nickjillings@1602 683 flagMoved.id = 'elementFlagMoved';
nickjillings@1602 684 flagMoved.textContent = elementMetric.wasMoved;
nickjillings@1602 685 metric.appendChild(flagMoved);
nickjillings@1602 686 }
nickjillings@1602 687 if (audioEngineContext.metric.enableFlagComments) {
nickjillings@1602 688 var flagComments = document.createElement('metricResult');
nickjillings@1602 689 flagComments.id = 'elementFlagComments';
nickjillings@1602 690 if (response.textContent.length == 0) {flag.textContent = 'false';}
nickjillings@1602 691 else {flag.textContet = 'true';}
nickjillings@1602 692 metric.appendChild(flagComments);
nickjillings@1602 693 }
nickjillings@1602 694 audioElement.appendChild(metric);
nickjillings@1602 695 xmlDoc.appendChild(audioElement);
nickjillings@1602 696 }
nickjillings@1602 697 var commentQuestion = document.getElementsByClassName('commentQuestion');
nickjillings@1602 698 for (var i=0; i<commentQuestion.length; i++)
nickjillings@1602 699 {
nickjillings@1602 700 var cqHolder = document.createElement('CommentQuestion');
nickjillings@1602 701 var comment = document.createElement('comment');
nickjillings@1602 702 var question = document.createElement('question');
nickjillings@1602 703 cqHolder.id = commentQuestion[i].id;
nickjillings@1602 704 comment.textContent = commentQuestion[i].children[2].value;
nickjillings@1602 705 question.textContent = commentQuestion[i].children[0].textContent;
nickjillings@1602 706 console.log('Question ' + i + ': ' + commentQuestion[i].children[2].value); // DEBUG/SAFETY
nickjillings@1602 707 cqHolder.appendChild(question);
nickjillings@1602 708 cqHolder.appendChild(comment);
nickjillings@1602 709 xmlDoc.appendChild(cqHolder);
nickjillings@1602 710 }
nickjillings@1602 711 store = xmlDoc;
nickjillings@1602 712 }