annotate ape.js @ 858:30d5aa52b034

Update dev_main
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Wed, 22 Jul 2015 12:41:22 +0100
parents
children 54c1fb5ca218
rev   line source
nicholas@858 1 /**
nicholas@858 2 * ape.js
nicholas@858 3 * Create the APE interface
nicholas@858 4 */
nicholas@858 5
nicholas@858 6 // preTest - In preTest state
nicholas@858 7 // testRun-ID - In test running, test Id number at the end 'testRun-2'
nicholas@858 8 // testRunPost-ID - Post test of test ID
nicholas@858 9 // testRunPre-ID - Pre-test of test ID
nicholas@858 10 // postTest - End of test, final submission!
nicholas@858 11
nicholas@858 12
nicholas@858 13 // Once this is loaded and parsed, begin execution
nicholas@858 14 loadInterface();
nicholas@858 15
nicholas@858 16 function loadInterface() {
nicholas@858 17
nicholas@858 18 // Get the dimensions of the screen available to the page
nicholas@858 19 var width = window.innerWidth;
nicholas@858 20 var height = window.innerHeight;
nicholas@858 21
nicholas@858 22 // The injection point into the HTML page
nicholas@858 23 interfaceContext.insertPoint = document.getElementById("topLevelBody");
nicholas@858 24 var testContent = document.createElement('div');
nicholas@858 25
nicholas@858 26 testContent.id = 'testContent';
nicholas@858 27
nicholas@858 28
nicholas@858 29 // Create APE specific metric functions
nicholas@858 30 audioEngineContext.metric.initialiseTest = function()
nicholas@858 31 {
nicholas@858 32 };
nicholas@858 33
nicholas@858 34 audioEngineContext.metric.sliderMoved = function()
nicholas@858 35 {
nicholas@858 36 var id = this.data;
nicholas@858 37 this.data = -1;
nicholas@858 38 var position = convSliderPosToRate(id);
nicholas@858 39 console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id
nicholas@858 40 if (audioEngineContext.timer.testStarted)
nicholas@858 41 {
nicholas@858 42 audioEngineContext.audioObjects[id].metric.moved(time,position);
nicholas@858 43 }
nicholas@858 44 };
nicholas@858 45
nicholas@858 46 audioEngineContext.metric.sliderPlayed = function(id)
nicholas@858 47 {
nicholas@858 48 var time = audioEngineContext.timer.getTestTime();
nicholas@858 49 if (audioEngineContext.timer.testStarted)
nicholas@858 50 {
nicholas@858 51 if (this.lastClicked >= 0)
nicholas@858 52 {
nicholas@858 53 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
nicholas@858 54 }
nicholas@858 55 this.lastClicked = id;
nicholas@858 56 audioEngineContext.audioObjects[id].metric.listening(time);
nicholas@858 57 }
nicholas@858 58 console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nicholas@858 59 };
nicholas@858 60
nicholas@858 61 // Bindings for interfaceContext
nicholas@858 62 Interface.prototype.checkAllPlayed = function()
nicholas@858 63 {
nicholas@858 64 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nicholas@858 65 if (hasBeenPlayed.length > 0) // if a fragment has not been played yet
nicholas@858 66 {
nicholas@858 67 str = "";
nicholas@858 68 if (hasBeenPlayed.length > 1) {
nicholas@858 69 for (var i=0; i<hasBeenPlayed.length; i++) {
nicholas@858 70 str = str + hasBeenPlayed[i];
nicholas@858 71 if (i < hasBeenPlayed.length-2){
nicholas@858 72 str += ", ";
nicholas@858 73 } else if (i == hasBeenPlayed.length-2) {
nicholas@858 74 str += " or ";
nicholas@858 75 }
nicholas@858 76 }
nicholas@858 77 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@858 78 } else {
nicholas@858 79 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@858 80 }
nicholas@858 81 return false;
nicholas@858 82 }
nicholas@858 83 return true;
nicholas@858 84 };
nicholas@858 85
nicholas@858 86 Interface.prototype.checkAllMoved = function() {
nicholas@858 87 var audioObjs = audioEngineContext.audioObjects;
nicholas@858 88 var state = true;
nicholas@858 89 var strNums = [];
nicholas@858 90 for (var i=0; i<audioObjs.length; i++)
nicholas@858 91 {
nicholas@858 92 if (audioObjs[i].metric.wasMoved == false) {
nicholas@858 93 state = false;
nicholas@858 94 strNums.push(i);
nicholas@858 95 }
nicholas@858 96 }
nicholas@858 97 if (state == false) {
nicholas@858 98 if (strNums.length > 1) {
nicholas@858 99 var str = "";
nicholas@858 100 for (var i=0; i<strNums.length; i++) {
nicholas@858 101 str = str + strNums[i];
nicholas@858 102 if (i < strNums.length-2){
nicholas@858 103 str += ", ";
nicholas@858 104 } else if (i == strNums.length-2) {
nicholas@858 105 str += " or ";
nicholas@858 106 }
nicholas@858 107 }
nicholas@858 108 alert('You have not moved fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@858 109 } else {
nicholas@858 110 alert('You have not moved fragment ' + strNums[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@858 111 }
nicholas@858 112 }
nicholas@858 113 return state;
nicholas@858 114 };
nicholas@858 115
nicholas@858 116 Interface.prototype.checkAllCommented = function() {
nicholas@858 117 var audioObjs = audioEngineContext.audioObjects;
nicholas@858 118 var audioHolder = testState.stateMap[testState.stateIndex];
nicholas@858 119 var state = true;
nicholas@858 120 if (audioHolder.elementComments) {
nicholas@858 121 var strNums = [];
nicholas@858 122 for (var i=0; i<audioObjs.length; i++)
nicholas@858 123 {
nicholas@858 124 if (audioObjs[i].commentDOM.trackCommentBox.value.length == 0) {
nicholas@858 125 state = false;
nicholas@858 126 strNums.push(i);
nicholas@858 127 }
nicholas@858 128 }
nicholas@858 129 if (state == false) {
nicholas@858 130 if (strNums.length > 1) {
nicholas@858 131 var str = "";
nicholas@858 132 for (var i=0; i<strNums.length; i++) {
nicholas@858 133 str = str + strNums[i];
nicholas@858 134 if (i < strNums.length-2){
nicholas@858 135 str += ", ";
nicholas@858 136 } else if (i == strNums.length-2) {
nicholas@858 137 str += " or ";
nicholas@858 138 }
nicholas@858 139 }
nicholas@858 140 alert('You have not commented on fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@858 141 } else {
nicholas@858 142 alert('You have not commented on fragment ' + strNums[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@858 143 }
nicholas@858 144 }
nicholas@858 145 }
nicholas@858 146 return state;
nicholas@858 147 };
nicholas@858 148
nicholas@858 149 Interface.prototype.checkScaleRange = function()
nicholas@858 150 {
nicholas@858 151 var audioObjs = audioEngineContext.audioObjects;
nicholas@858 152 var audioHolder = testState.stateMap[testState.stateIndex];
nicholas@858 153 var interfaces = audioHolder.interfaces;
nicholas@858 154
nicholas@858 155 var minRanking = audioObjs[0].interfaceDOM.getValue();
nicholas@858 156 var maxRanking = minRanking;
nicholas@858 157
nicholas@858 158 var minScale;
nicholas@858 159 var maxScale;
nicholas@858 160 for (var i=0; i<interfaces[0].options.length; i++)
nicholas@858 161 {
nicholas@858 162 if (interfaces[0].options[i].check == "scalerange") {
nicholas@858 163 minScale = interfaces[0].options[i].min;
nicholas@858 164 maxScale = interfaces[0].options[i].max;
nicholas@858 165 }
nicholas@858 166 }
nicholas@858 167
nicholas@858 168 for (var i=1; i<audioObjs.length; i++){
nicholas@858 169 if (audioObjs[i].specification.type != 'outsidereference') {
nicholas@858 170 var ranking = audioObjs[i].interfaceDOM.getValue();
nicholas@858 171 if (ranking < minRanking) { minRanking = ranking;}
nicholas@858 172 if (ranking > maxRanking) { maxRanking = ranking;}
nicholas@858 173 }
nicholas@858 174 }
nicholas@858 175 if (minRanking > minScale || maxRanking < maxScale) {
nicholas@858 176 alert('Please use the full width of the scale');
nicholas@858 177 return false;
nicholas@858 178 } else {
nicholas@858 179 return true;
nicholas@858 180 }
nicholas@858 181 };
nicholas@858 182
nicholas@858 183 // Bindings for audioObjects
nicholas@858 184
nicholas@858 185 // Create the top div for the Title element
nicholas@858 186 var titleAttr = specification.title;
nicholas@858 187 var title = document.createElement('div');
nicholas@858 188 title.className = "title";
nicholas@858 189 title.align = "center";
nicholas@858 190 var titleSpan = document.createElement('span');
nicholas@858 191
nicholas@858 192 // Set title to that defined in XML, else set to default
nicholas@858 193 if (titleAttr != undefined) {
nicholas@858 194 titleSpan.textContent = titleAttr;
nicholas@858 195 } else {
nicholas@858 196 titleSpan.textContent = 'Listening test';
nicholas@858 197 }
nicholas@858 198 // Insert the titleSpan element into the title div element.
nicholas@858 199 title.appendChild(titleSpan);
nicholas@858 200
nicholas@858 201 var pagetitle = document.createElement('div');
nicholas@858 202 pagetitle.className = "pageTitle";
nicholas@858 203 pagetitle.align = "center";
nicholas@858 204 var titleSpan = document.createElement('span');
nicholas@858 205 titleSpan.id = "pageTitle";
nicholas@858 206 pagetitle.appendChild(titleSpan);
nicholas@858 207
nicholas@858 208 // Create Interface buttons!
nicholas@858 209 var interfaceButtons = document.createElement('div');
nicholas@858 210 interfaceButtons.id = 'interface-buttons';
nicholas@858 211
nicholas@858 212 // Create playback start/stop points
nicholas@858 213 var playback = document.createElement("button");
nicholas@858 214 playback.innerHTML = 'Stop';
nicholas@858 215 playback.id = 'playback-button';
nicholas@858 216 // onclick function. Check if it is playing or not, call the correct function in the
nicholas@858 217 // audioEngine, change the button text to reflect the next state.
nicholas@858 218 playback.onclick = function() {
nicholas@858 219 if (audioEngineContext.status == 1) {
nicholas@858 220 audioEngineContext.stop();
nicholas@858 221 this.innerHTML = 'Stop';
nicholas@858 222 var time = audioEngineContext.timer.getTestTime();
nicholas@858 223 console.log('Stopped at ' + time); // DEBUG/SAFETY
nicholas@858 224 }
nicholas@858 225 };
nicholas@858 226 // Create Submit (save) button
nicholas@858 227 var submit = document.createElement("button");
nicholas@858 228 submit.innerHTML = 'Submit';
nicholas@858 229 submit.onclick = buttonSubmitClick;
nicholas@858 230 submit.id = 'submit-button';
nicholas@858 231 // Append the interface buttons into the interfaceButtons object.
nicholas@858 232 interfaceButtons.appendChild(playback);
nicholas@858 233 interfaceButtons.appendChild(submit);
nicholas@858 234
nicholas@858 235 // Now create the slider and HTML5 canvas boxes
nicholas@858 236
nicholas@858 237 // Create the div box to center align
nicholas@858 238 var sliderBox = document.createElement('div');
nicholas@858 239 sliderBox.className = 'sliderCanvasDiv';
nicholas@858 240 sliderBox.id = 'sliderCanvasHolder';
nicholas@858 241
nicholas@858 242 // Create the slider box to hold the slider elements
nicholas@858 243 var canvas = document.createElement('div');
nicholas@858 244 canvas.id = 'slider';
nicholas@858 245 canvas.align = "left";
nicholas@858 246 canvas.addEventListener('dragover',function(event){
nicholas@858 247 event.preventDefault();
nicholas@858 248 return false;
nicholas@858 249 },false);
nicholas@858 250 var sliderMargin = document.createAttribute('marginsize');
nicholas@858 251 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
nicholas@858 252 // Must have a known EXACT width, as this is used later to determine the ratings
nicholas@858 253 var w = (Number(sliderMargin.nodeValue)+8)*2;
nicholas@858 254 canvas.style.width = width - w +"px";
nicholas@858 255 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
nicholas@858 256 canvas.setAttributeNode(sliderMargin);
nicholas@858 257 sliderBox.appendChild(canvas);
nicholas@858 258
nicholas@858 259 // Create the div to hold any scale objects
nicholas@858 260 var scale = document.createElement('div');
nicholas@858 261 scale.className = 'sliderScale';
nicholas@858 262 scale.id = 'sliderScaleHolder';
nicholas@858 263 scale.align = 'left';
nicholas@858 264 sliderBox.appendChild(scale);
nicholas@858 265
nicholas@858 266 // Global parent for the comment boxes on the page
nicholas@858 267 var feedbackHolder = document.createElement('div');
nicholas@858 268 feedbackHolder.id = 'feedbackHolder';
nicholas@858 269
nicholas@858 270 testContent.style.zIndex = 1;
nicholas@858 271 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
nicholas@858 272
nicholas@858 273 // Inject into HTML
nicholas@858 274 testContent.appendChild(title); // Insert the title
nicholas@858 275 testContent.appendChild(pagetitle);
nicholas@858 276 testContent.appendChild(interfaceButtons);
nicholas@858 277 testContent.appendChild(sliderBox);
nicholas@858 278 testContent.appendChild(feedbackHolder);
nicholas@858 279 interfaceContext.insertPoint.appendChild(testContent);
nicholas@858 280
nicholas@858 281 // Load the full interface
nicholas@858 282 testState.initialise();
nicholas@858 283 testState.advanceState();
nicholas@858 284
nicholas@858 285 }
nicholas@858 286
nicholas@858 287 function loadTest(audioHolderObject)
nicholas@858 288 {
nicholas@858 289
nicholas@858 290 // Reset audioEngineContext.Metric globals for new test
nicholas@858 291 audioEngineContext.newTestPage();
nicholas@858 292
nicholas@858 293 var id = audioHolderObject.id;
nicholas@858 294
nicholas@858 295 var feedbackHolder = document.getElementById('feedbackHolder');
nicholas@858 296 var canvas = document.getElementById('slider');
nicholas@858 297 feedbackHolder.innerHTML = null;
nicholas@858 298 canvas.innerHTML = null;
nicholas@858 299
nicholas@858 300 var interfaceObj = audioHolderObject.interfaces;
nicholas@858 301 for (var k=0; k<interfaceObj.length; k++) {
nicholas@858 302 for (var i=0; i<interfaceObj[k].options.length; i++)
nicholas@858 303 {
nicholas@858 304 if (interfaceObj[k].options[i].type == 'option' && interfaceObj[k].options[i].name == 'playhead')
nicholas@858 305 {
nicholas@858 306 var playbackHolder = document.getElementById('playback-holder');
nicholas@858 307 if (playbackHolder == null)
nicholas@858 308 {
nicholas@858 309 playbackHolder = document.createElement('div');
nicholas@858 310 playbackHolder.style.width = "100%";
nicholas@858 311 playbackHolder.align = 'center';
nicholas@858 312 playbackHolder.appendChild(interfaceContext.playhead.object);
nicholas@858 313 feedbackHolder.appendChild(playbackHolder);
nicholas@858 314 }
nicholas@858 315 break;
nicholas@858 316 }
nicholas@858 317 }
nicholas@858 318 }
nicholas@858 319 // Setup question title
nicholas@858 320
nicholas@858 321 var commentBoxPrefix = "Comment on track";
nicholas@858 322 if (interfaceObj.length != 0) {
nicholas@858 323 interfaceObj = interfaceObj[0];
nicholas@858 324 var titleNode = interfaceObj.title;
nicholas@858 325 if (titleNode != undefined)
nicholas@858 326 {
nicholas@858 327 document.getElementById('pageTitle').textContent = titleNode;
nicholas@858 328 }
nicholas@858 329 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
nicholas@858 330 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
nicholas@858 331 var scale = document.getElementById('sliderScaleHolder');
nicholas@858 332 scale.innerHTML = null;
nicholas@858 333 $(interfaceObj.scale).each(function(index,scaleObj){
nicholas@858 334 var value = document.createAttribute('value');
nicholas@858 335 var position = Number(scaleObj[0])*0.01;
nicholas@858 336 value.nodeValue = position;
nicholas@858 337 var pixelPosition = (position*positionScale)+offset;
nicholas@858 338 var scaleDOM = document.createElement('span');
nicholas@858 339 scaleDOM.textContent = scaleObj[1];
nicholas@858 340 scale.appendChild(scaleDOM);
nicholas@858 341 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
nicholas@858 342 scaleDOM.setAttributeNode(value);
nicholas@858 343 });
nicholas@858 344
nicholas@858 345 if (interfaceObj.commentBoxPrefix != undefined) {
nicholas@858 346 commentBoxPrefix = interfaceObj.commentBoxPrefix;
nicholas@858 347 }
nicholas@858 348 }
nicholas@858 349
nicholas@858 350 /// CHECK FOR SAMPLE RATE COMPATIBILITY
nicholas@858 351 if (audioHolderObject.sampleRate != undefined) {
nicholas@858 352 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
nicholas@858 353 var errStr = 'Sample rates do not match! Requested '+Number(audioHolderObject.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nicholas@858 354 alert(errStr);
nicholas@858 355 return;
nicholas@858 356 }
nicholas@858 357 }
nicholas@858 358
nicholas@858 359 var commentShow = audioHolderObject.elementComments;
nicholas@858 360
nicholas@858 361 var loopPlayback = audioHolderObject.loop;
nicholas@858 362
nicholas@858 363 audioEngineContext.loopPlayback = loopPlayback;
nicholas@858 364 // Create AudioEngine bindings for playback
nicholas@858 365 audioEngineContext.selectedTrack = function(id) {
nicholas@858 366 console.log('Deprecated');
nicholas@858 367 };
nicholas@858 368
nicholas@858 369 currentTestHolder = document.createElement('audioHolder');
nicholas@858 370 currentTestHolder.id = audioHolderObject.id;
nicholas@858 371 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
nicholas@858 372
nicholas@858 373 // Delete any previous audioObjects associated with the audioEngine
nicholas@858 374 audioEngineContext.audioObjects = [];
nicholas@858 375 interfaceContext.deleteCommentBoxes();
nicholas@858 376 interfaceContext.deleteCommentQuestions();
nicholas@858 377
nicholas@858 378 // Find all the audioElements from the audioHolder
nicholas@858 379 $(audioHolderObject.audioElements).each(function(index,element){
nicholas@858 380 // Find URL of track
nicholas@858 381 // In this jQuery loop, variable 'this' holds the current audioElement.
nicholas@858 382
nicholas@858 383 // Now load each audio sample. First create the new track by passing the full URL
nicholas@858 384 var trackURL = audioHolderObject.hostURL + element.url;
nicholas@858 385 var audioObject = audioEngineContext.newTrack(element);
nicholas@858 386
nicholas@858 387 var node = interfaceContext.createCommentBox(audioObject);
nicholas@858 388
nicholas@858 389 // Create a slider per track
nicholas@858 390 audioObject.interfaceDOM = new sliderObject(audioObject);
nicholas@858 391
nicholas@858 392 // Distribute it randomnly
nicholas@858 393 var w = window.innerWidth - (offset+8)*2;
nicholas@858 394 w = Math.random()*w;
nicholas@858 395 w = Math.floor(w+(offset+8));
nicholas@858 396 audioObject.interfaceDOM.trackSliderObj.style.left = w+'px';
nicholas@858 397
nicholas@858 398 canvas.appendChild(audioObject.interfaceDOM.trackSliderObj);
nicholas@858 399 audioObject.metric.initialised(convSliderPosToRate(audioObject.interfaceDOM.trackSliderObj));
nicholas@858 400
nicholas@858 401 });
nicholas@858 402 if (commentShow) {
nicholas@858 403 interfaceContext.showCommentBoxes(feedbackHolder,true);
nicholas@858 404 }
nicholas@858 405
nicholas@858 406 $(audioHolderObject.commentQuestions).each(function(index,element) {
nicholas@858 407 var node = interfaceContext.createCommentQuestion(element);
nicholas@858 408 feedbackHolder.appendChild(node.holder);
nicholas@858 409 });
nicholas@858 410
nicholas@858 411 // Construct outside reference;
nicholas@858 412 if (audioHolderObject.outsideReference != null) {
nicholas@858 413 var outsideReferenceHolder = document.createElement('div');
nicholas@858 414 outsideReferenceHolder.id = 'outside-reference';
nicholas@858 415 outsideReferenceHolderspan = document.createElement('span');
nicholas@858 416 outsideReferenceHolderspan.textContent = 'Reference';
nicholas@858 417 outsideReferenceHolder.appendChild(outsideReferenceHolderspan);
nicholas@858 418
nicholas@858 419 var audioObject = audioEngineContext.newTrack(audioHolderObject.outsideReference);
nicholas@858 420
nicholas@858 421 outsideReferenceHolder.onclick = function()
nicholas@858 422 {
nicholas@858 423 audioEngineContext.play(audioEngineContext.audioObjects.length-1);
nicholas@858 424 $('.track-slider').removeClass('track-slider-playing');
nicholas@858 425 $('.comment-div').removeClass('comment-box-playing');
nicholas@858 426 if (event.srcElement.nodeName == 'DIV') {
nicholas@858 427 $(event.srcElement).addClass('track-slider-playing');
nicholas@858 428 } else {
nicholas@858 429 $(event.srcElement.parentElement).addClass('track-slider-playing');
nicholas@858 430 }
nicholas@858 431 };
nicholas@858 432
nicholas@858 433 document.getElementById('interface-buttons').appendChild(outsideReferenceHolder);
nicholas@858 434 }
nicholas@858 435
nicholas@858 436
nicholas@858 437 testWaitIndicator();
nicholas@858 438 }
nicholas@858 439
nicholas@858 440 function sliderObject(audioObject) {
nicholas@858 441 // Create a new slider object;
nicholas@858 442 this.parent = audioObject;
nicholas@858 443 this.trackSliderObj = document.createElement('div');
nicholas@858 444 this.trackSliderObj.className = 'track-slider';
nicholas@858 445 this.trackSliderObj.id = 'track-slider-'+audioObject.id;
nicholas@858 446
nicholas@858 447 this.trackSliderObj.setAttribute('trackIndex',audioObject.id);
nicholas@858 448 this.trackSliderObj.innerHTML = '<span>'+audioObject.id+'</span>';
nicholas@858 449 this.trackSliderObj.draggable = true;
nicholas@858 450 this.trackSliderObj.ondragend = dragEnd;
nicholas@858 451
nicholas@858 452 // Onclick, switch playback to that track
nicholas@858 453 this.trackSliderObj.onclick = function() {
nicholas@858 454 // Start the test on first click, that way timings are more accurate.
nicholas@858 455 audioEngineContext.play();
nicholas@858 456 if (audioEngineContext.audioObjectsReady) {
nicholas@858 457 // Cannot continue to issue play command until audioObjects reported as ready!
nicholas@858 458 // Get the track ID from the object ID
nicholas@858 459 var element;
nicholas@858 460 if (event.srcElement.nodeName == "SPAN") {
nicholas@858 461 element = event.srcElement.parentNode;
nicholas@858 462 } else {
nicholas@858 463 element = event.srcElement;
nicholas@858 464 }
nicholas@858 465 var id = Number(element.attributes['trackIndex'].value);
nicholas@858 466 //audioEngineContext.metric.sliderPlayed(id);
nicholas@858 467 audioEngineContext.play(id);
nicholas@858 468 // Currently playing track red, rest green
nicholas@858 469
nicholas@858 470 //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
nicholas@858 471 $('.track-slider').removeClass('track-slider-playing');
nicholas@858 472 $(element).addClass('track-slider-playing');
nicholas@858 473 $('.comment-div').removeClass('comment-box-playing');
nicholas@858 474 $('#comment-div-'+id).addClass('comment-box-playing');
nicholas@858 475 var outsideReference = document.getElementById('outside-reference');
nicholas@858 476 if (outsideReference != undefined)
nicholas@858 477 $(outsideReference).removeClass('track-slider-playing');
nicholas@858 478 }
nicholas@858 479 };
nicholas@858 480
nicholas@858 481 this.exportXMLDOM = function(audioObject) {
nicholas@858 482 // Called by the audioObject holding this element. Must be present
nicholas@858 483 var node = document.createElement('value');
nicholas@858 484 node.textContent = convSliderPosToRate(this.trackSliderObj);
nicholas@858 485 return node;
nicholas@858 486 };
nicholas@858 487 this.getValue = function() {
nicholas@858 488 return convSliderPosToRate(this.trackSliderObj);
nicholas@858 489 };
nicholas@858 490 }
nicholas@858 491
nicholas@858 492 function dragEnd(ev) {
nicholas@858 493 // Function call when a div has been dropped
nicholas@858 494 var slider = document.getElementById('slider');
nicholas@858 495 var marginSize = Number(slider.attributes['marginsize'].value);
nicholas@858 496 var w = slider.style.width;
nicholas@858 497 w = Number(w.substr(0,w.length-2));
nicholas@858 498 var x = ev.x;
nicholas@858 499 if (x >= marginSize && x < w+marginSize) {
nicholas@858 500 this.style.left = (x)+'px';
nicholas@858 501 } else {
nicholas@858 502 if (x<marginSize) {
nicholas@858 503 this.style.left = marginSize+'px';
nicholas@858 504 } else {
nicholas@858 505 this.style.left = (w+marginSize) + 'px';
nicholas@858 506 }
nicholas@858 507 }
nicholas@858 508 var time = audioEngineContext.timer.getTestTime();
nicholas@858 509 var id = Number(ev.srcElement.getAttribute('trackindex'));
nicholas@858 510 audioEngineContext.audioObjects[id].metric.moved(time,convSliderPosToRate(ev.srcElement));
nicholas@858 511 console.log('slider '+id+' moved to '+convSliderPosToRate(ev.srcElement)+' ('+time+')');
nicholas@858 512 }
nicholas@858 513
nicholas@858 514 function buttonSubmitClick() // TODO: Only when all songs have been played!
nicholas@858 515 {
nicholas@858 516 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
nicholas@858 517 var canContinue = true;
nicholas@858 518
nicholas@858 519 // Check that the anchor and reference objects are correctly placed
nicholas@858 520 if (interfaceContext.checkHiddenAnchor() == false) {return;}
nicholas@858 521 if (interfaceContext.checkHiddenReference() == false) {return;}
nicholas@858 522
nicholas@858 523 for (var i=0; i<checks.length; i++) {
nicholas@858 524 if (checks[i].type == 'check')
nicholas@858 525 {
nicholas@858 526 switch(checks[i].check) {
nicholas@858 527 case 'fragmentPlayed':
nicholas@858 528 // Check if all fragments have been played
nicholas@858 529 var checkState = interfaceContext.checkAllPlayed();
nicholas@858 530 if (checkState == false) {canContinue = false;}
nicholas@858 531 break;
nicholas@858 532 case 'fragmentFullPlayback':
nicholas@858 533 // Check all fragments have been played to their full length
nicholas@858 534 var checkState = interfaceContext.checkAllPlayed();
nicholas@858 535 if (checkState == false) {canContinue = false;}
nicholas@858 536 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
nicholas@858 537 break;
nicholas@858 538 case 'fragmentMoved':
nicholas@858 539 // Check all fragment sliders have been moved.
nicholas@858 540 var checkState = interfaceContext.checkAllMoved();
nicholas@858 541 if (checkState == false) {canContinue = false;}
nicholas@858 542 break;
nicholas@858 543 case 'fragmentComments':
nicholas@858 544 // Check all fragment sliders have been moved.
nicholas@858 545 var checkState = interfaceContext.checkAllCommented();
nicholas@858 546 if (checkState == false) {canContinue = false;}
nicholas@858 547 break;
nicholas@858 548 case 'scalerange':
nicholas@858 549 // Check the scale is used to its full width outlined by the node
nicholas@858 550 var checkState = interfaceContext.checkScaleRange();
nicholas@858 551 if (checkState == false) {canContinue = false;}
nicholas@858 552 break;
nicholas@858 553 }
nicholas@858 554
nicholas@858 555 }
nicholas@858 556 if (!canContinue) {break;}
nicholas@858 557 }
nicholas@858 558
nicholas@858 559 if (canContinue) {
nicholas@858 560 if (audioEngineContext.status == 1) {
nicholas@858 561 var playback = document.getElementById('playback-button');
nicholas@858 562 playback.click();
nicholas@858 563 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
nicholas@858 564 } else
nicholas@858 565 {
nicholas@858 566 if (audioEngineContext.timer.testStarted == false)
nicholas@858 567 {
nicholas@858 568 alert('You have not started the test! Please press start to begin the test!');
nicholas@858 569 return;
nicholas@858 570 }
nicholas@858 571 }
nicholas@858 572 testState.advanceState();
nicholas@858 573 }
nicholas@858 574 }
nicholas@858 575
nicholas@858 576 function convSliderPosToRate(slider)
nicholas@858 577 {
nicholas@858 578 var w = document.getElementById('slider').style.width;
nicholas@858 579 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
nicholas@858 580 var maxPix = w.substr(0,w.length-2);
nicholas@858 581 var pix = slider.style.left;
nicholas@858 582 pix = pix.substr(0,pix.length-2);
nicholas@858 583 var rate = (pix-marginsize)/maxPix;
nicholas@858 584 return rate;
nicholas@858 585 }
nicholas@858 586
nicholas@858 587 function resizeWindow(event){
nicholas@858 588 // Function called when the window has been resized.
nicholas@858 589 // MANDATORY FUNCTION
nicholas@858 590
nicholas@858 591 // Store the slider marker values
nicholas@858 592 var holdValues = [];
nicholas@858 593 $(".track-slider").each(function(index,sliderObj){
nicholas@858 594 holdValues.push(convSliderPosToRate(index));
nicholas@858 595 });
nicholas@858 596
nicholas@858 597 var width = event.target.innerWidth;
nicholas@858 598 var canvas = document.getElementById('sliderCanvasHolder');
nicholas@858 599 var sliderDiv = canvas.children[0];
nicholas@858 600 var sliderScaleDiv = canvas.children[1];
nicholas@858 601 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
nicholas@858 602 var w = (marginsize+8)*2;
nicholas@858 603 sliderDiv.style.width = width - w + 'px';
nicholas@858 604 var width = width - w;
nicholas@858 605 // Move sliders into new position
nicholas@858 606 $(".track-slider").each(function(index,sliderObj){
nicholas@858 607 var pos = holdValues[index];
nicholas@858 608 var pix = pos * width;
nicholas@858 609 sliderObj.style.left = pix+marginsize+'px';
nicholas@858 610 });
nicholas@858 611
nicholas@858 612 // Move scale labels
nicholas@858 613 $(sliderScaleDiv.children).each(function(index,scaleObj){
nicholas@858 614 var position = Number(scaleObj.attributes['value'].value);
nicholas@858 615 var pixelPosition = (position*width)+marginsize;
nicholas@858 616 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
nicholas@858 617 });
nicholas@858 618 }
nicholas@858 619
nicholas@858 620 function pageXMLSave(store, testXML)
nicholas@858 621 {
nicholas@858 622 // Saves a specific test page
nicholas@858 623 var xmlDoc = store;
nicholas@858 624 // Check if any session wide metrics are enabled
nicholas@858 625
nicholas@858 626 var commentShow = testXML.elementComments;
nicholas@858 627
nicholas@858 628 var metric = document.createElement('metric');
nicholas@858 629 if (audioEngineContext.metric.enableTestTimer)
nicholas@858 630 {
nicholas@858 631 var testTime = document.createElement('metricResult');
nicholas@858 632 testTime.id = 'testTime';
nicholas@858 633 testTime.textContent = audioEngineContext.timer.testDuration;
nicholas@858 634 metric.appendChild(testTime);
nicholas@858 635 }
nicholas@858 636 xmlDoc.appendChild(metric);
nicholas@858 637 var audioObjects = audioEngineContext.audioObjects;
nicholas@858 638 for (var i=0; i<audioObjects.length; i++)
nicholas@858 639 {
nicholas@858 640 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM();
nicholas@858 641 audioElement.setAttribute('presentedId',i);
nicholas@858 642 xmlDoc.appendChild(audioElement);
nicholas@858 643 }
nicholas@858 644
nicholas@858 645 $(interfaceContext.commentQuestions).each(function(index,element){
nicholas@858 646 var node = element.exportXMLDOM();
nicholas@858 647 xmlDoc.appendChild(node);
nicholas@858 648 });
nicholas@858 649 store = xmlDoc;
nicholas@858 650 }