annotate ape.js @ 2017:4a14c8a65af2

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