annotate ape.js @ 2019:fea5c4f14e16

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