annotate ape.js @ 842:1e8bdac90b78

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