annotate ape.js @ 210:7658d51a9ccb Dev_main

Fix for Bug #1273
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Tue, 16 Jun 2015 14:34:50 +0100
parents 538322113524
children 5d251b4aabd6
rev   line source
nicholas@2 1 /**
nicholas@2 2 * ape.js
nicholas@2 3 * Create the APE interface
nicholas@2 4 */
nicholas@2 5
n@38 6 // preTest - In preTest state
n@38 7 // testRun-ID - In test running, test Id number at the end 'testRun-2'
n@38 8 // testRunPost-ID - Post test of test ID
n@38 9 // testRunPre-ID - Pre-test of test ID
n@38 10 // postTest - End of test, final submission!
n@38 11
n@32 12
nicholas@2 13 // Once this is loaded and parsed, begin execution
n@181 14 loadInterface();
nicholas@2 15
n@181 16 function loadInterface() {
nicholas@2 17
n@16 18 // Get the dimensions of the screen available to the page
nicholas@2 19 var width = window.innerWidth;
nicholas@2 20 var height = window.innerHeight;
nicholas@2 21
nicholas@2 22 // The injection point into the HTML page
n@182 23 interfaceContext.insertPoint = document.getElementById("topLevelBody");
n@22 24 var testContent = document.createElement('div');
nicholas@135 25
n@22 26 testContent.id = 'testContent';
n@176 27
n@34 28
n@52 29 // Create APE specific metric functions
n@52 30 audioEngineContext.metric.initialiseTest = function()
n@52 31 {
n@52 32 };
n@52 33
n@52 34 audioEngineContext.metric.sliderMoved = function()
n@52 35 {
n@183 36
n@52 37 var id = this.data;
n@52 38 this.data = -1;
n@52 39 var position = convSliderPosToRate(id);
b@115 40 console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id
n@52 41 if (audioEngineContext.timer.testStarted)
n@52 42 {
n@52 43 audioEngineContext.audioObjects[id].metric.moved(time,position);
n@52 44 }
n@52 45 };
n@52 46
n@52 47 audioEngineContext.metric.sliderPlayed = function(id)
n@52 48 {
n@52 49 var time = audioEngineContext.timer.getTestTime();
n@52 50 if (audioEngineContext.timer.testStarted)
n@52 51 {
n@52 52 if (this.lastClicked >= 0)
n@52 53 {
n@52 54 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
n@52 55 }
n@52 56 this.lastClicked = id;
n@52 57 audioEngineContext.audioObjects[id].metric.listening(time);
n@52 58 }
b@115 59 console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
n@52 60 };
n@52 61
n@177 62 // Bindings for audioObjects
n@177 63
nicholas@2 64 // Create the top div for the Title element
n@181 65 var titleAttr = specification.title;
nicholas@2 66 var title = document.createElement('div');
nicholas@2 67 title.className = "title";
nicholas@2 68 title.align = "center";
nicholas@2 69 var titleSpan = document.createElement('span');
nicholas@2 70
nicholas@2 71 // Set title to that defined in XML, else set to default
nicholas@2 72 if (titleAttr != undefined) {
n@181 73 titleSpan.textContent = titleAttr;
nicholas@2 74 } else {
n@181 75 titleSpan.textContent = 'Listening test';
nicholas@2 76 }
nicholas@2 77 // Insert the titleSpan element into the title div element.
nicholas@2 78 title.appendChild(titleSpan);
nicholas@2 79
n@47 80 var pagetitle = document.createElement('div');
n@47 81 pagetitle.className = "pageTitle";
n@47 82 pagetitle.align = "center";
n@47 83 var titleSpan = document.createElement('span');
n@47 84 titleSpan.id = "pageTitle";
n@47 85 pagetitle.appendChild(titleSpan);
n@47 86
nicholas@7 87 // Create Interface buttons!
nicholas@7 88 var interfaceButtons = document.createElement('div');
nicholas@7 89 interfaceButtons.id = 'interface-buttons';
nicholas@7 90
nicholas@7 91 // Create playback start/stop points
nicholas@7 92 var playback = document.createElement("button");
b@101 93 playback.innerHTML = 'Stop';
n@49 94 playback.id = 'playback-button';
n@16 95 // onclick function. Check if it is playing or not, call the correct function in the
n@16 96 // audioEngine, change the button text to reflect the next state.
nicholas@7 97 playback.onclick = function() {
b@101 98 if (audioEngineContext.status == 1) {
b@101 99 audioEngineContext.stop();
n@25 100 this.innerHTML = 'Stop';
b@115 101 var time = audioEngineContext.timer.getTestTime();
b@115 102 console.log('Stopped at ' + time); // DEBUG/SAFETY
nicholas@7 103 }
n@16 104 };
nicholas@7 105 // Create Submit (save) button
nicholas@7 106 var submit = document.createElement("button");
n@25 107 submit.innerHTML = 'Submit';
n@43 108 submit.onclick = buttonSubmitClick;
n@49 109 submit.id = 'submit-button';
n@16 110 // Append the interface buttons into the interfaceButtons object.
nicholas@7 111 interfaceButtons.appendChild(playback);
nicholas@7 112 interfaceButtons.appendChild(submit);
nicholas@7 113
nicholas@2 114 // Now create the slider and HTML5 canvas boxes
nicholas@2 115
n@16 116 // Create the div box to center align
nicholas@2 117 var sliderBox = document.createElement('div');
nicholas@2 118 sliderBox.className = 'sliderCanvasDiv';
n@16 119 sliderBox.id = 'sliderCanvasHolder';
nicholas@2 120
n@16 121 // Create the slider box to hold the slider elements
nicholas@5 122 var canvas = document.createElement('div');
nicholas@2 123 canvas.id = 'slider';
n@127 124 canvas.align = "left";
n@127 125 canvas.addEventListener('dragover',function(event){
n@127 126 event.preventDefault();
n@127 127 return false;
n@127 128 },false);
n@127 129 var sliderMargin = document.createAttribute('marginsize');
n@127 130 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
n@16 131 // Must have a known EXACT width, as this is used later to determine the ratings
n@127 132 var w = (Number(sliderMargin.nodeValue)+8)*2;
n@127 133 canvas.style.width = width - w +"px";
n@127 134 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
n@127 135 canvas.setAttributeNode(sliderMargin);
nicholas@2 136 sliderBox.appendChild(canvas);
n@16 137
n@47 138 // Create the div to hold any scale objects
n@47 139 var scale = document.createElement('div');
n@47 140 scale.className = 'sliderScale';
n@47 141 scale.id = 'sliderScaleHolder';
n@47 142 scale.align = 'left';
n@47 143 sliderBox.appendChild(scale);
n@47 144
n@16 145 // Global parent for the comment boxes on the page
nicholas@3 146 var feedbackHolder = document.createElement('div');
n@34 147 feedbackHolder.id = 'feedbackHolder';
nicholas@2 148
n@38 149 testContent.style.zIndex = 1;
n@182 150 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
n@38 151
n@38 152 // Inject into HTML
n@38 153 testContent.appendChild(title); // Insert the title
n@47 154 testContent.appendChild(pagetitle);
n@38 155 testContent.appendChild(interfaceButtons);
n@38 156 testContent.appendChild(sliderBox);
n@38 157 testContent.appendChild(feedbackHolder);
n@182 158 interfaceContext.insertPoint.appendChild(testContent);
n@22 159
n@36 160 // Load the full interface
nicholas@129 161 testState.initialise();
nicholas@129 162 testState.advanceState();
nicholas@135 163
nicholas@2 164 }
nicholas@5 165
n@181 166 function loadTest(audioHolderObject)
n@34 167 {
nicholas@110 168
nicholas@110 169 // Reset audioEngineContext.Metric globals for new test
n@113 170 audioEngineContext.newTestPage();
nicholas@110 171
n@181 172 var id = audioHolderObject.id;
n@34 173
n@34 174 var feedbackHolder = document.getElementById('feedbackHolder');
n@34 175 var canvas = document.getElementById('slider');
n@34 176 feedbackHolder.innerHTML = null;
n@34 177 canvas.innerHTML = null;
n@47 178
n@201 179 var playbackHolder = document.createElement('div');
n@201 180 playbackHolder.style.width = "100%";
n@201 181 playbackHolder.align = 'center';
n@201 182 playbackHolder.appendChild(interfaceContext.playhead.object);
n@201 183 feedbackHolder.appendChild(playbackHolder);
n@47 184 // Setup question title
n@184 185 var interfaceObj = audioHolderObject.interfaces;
n@184 186 var commentBoxPrefix = "Comment on track";
n@184 187 if (interfaceObj.length != 0) {
n@184 188 interfaceObj = interfaceObj[0];
n@184 189 var titleNode = interfaceObj.title;
n@184 190 if (titleNode != undefined)
n@184 191 {
n@184 192 document.getElementById('pageTitle').textContent = titleNode;
n@184 193 }
n@184 194 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
n@184 195 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
n@184 196 var scale = document.getElementById('sliderScaleHolder');
n@184 197 scale.innerHTML = null;
n@184 198 $(interfaceObj.scale).each(function(index,scaleObj){
n@184 199 var value = document.createAttribute('value');
n@184 200 var position = Number(scaleObj[0])*0.01;
n@184 201 value.nodeValue = position;
n@184 202 var pixelPosition = (position*positionScale)+offset;
n@184 203 var scaleDOM = document.createElement('span');
n@184 204 scaleDOM.textContent = scaleObj[1];
n@184 205 scale.appendChild(scaleDOM);
n@184 206 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
n@184 207 scaleDOM.setAttributeNode(value);
n@184 208 });
n@184 209
n@184 210 if (interfaceObj.commentBoxPrefix != undefined) {
n@184 211 commentBoxPrefix = interfaceObj.commentBoxPrefix;
n@184 212 }
n@174 213 }
n@34 214
n@34 215 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@181 216 if (audioHolderObject.sampleRate != undefined) {
n@181 217 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
n@184 218 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.';
n@34 219 alert(errStr);
n@34 220 return;
n@34 221 }
n@34 222 }
n@45 223
n@181 224 var commentShow = audioHolderObject.elementComments;
nicholas@66 225
n@181 226 var loopPlayback = audioHolderObject.loop;
n@181 227
n@57 228 audioEngineContext.loopPlayback = loopPlayback;
n@57 229 // Create AudioEngine bindings for playback
n@202 230 audioEngineContext.selectedTrack = function(id) {
n@202 231 console.log('Deprecated');
n@202 232 };
n@57 233
n@46 234 currentTestHolder = document.createElement('audioHolder');
n@181 235 currentTestHolder.id = audioHolderObject.id;
n@181 236 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
n@46 237
n@181 238 var randomise = audioHolderObject.randomiseOrder;
n@45 239
n@181 240 var audioElements = audioHolderObject.audioElements;
nicholas@58 241 currentTrackOrder = [];
n@45 242 if (randomise) {
n@181 243 audioHolderObject.audioElements = randomiseOrder(audioHolderObject.audioElements);
n@45 244 }
n@45 245
nicholas@58 246 // Delete any previous audioObjects associated with the audioEngine
nicholas@58 247 audioEngineContext.audioObjects = [];
nicholas@58 248
n@45 249 // Find all the audioElements from the audioHolder
n@181 250 $(audioHolderObject.audioElements).each(function(index,element){
n@34 251 // Find URL of track
n@34 252 // In this jQuery loop, variable 'this' holds the current audioElement.
n@34 253
n@34 254 // Now load each audio sample. First create the new track by passing the full URL
n@181 255 var trackURL = audioHolderObject.hostURL + element.url;
n@182 256 var audioObject = audioEngineContext.newTrack(element);
nicholas@66 257
n@205 258 var node = interfaceContext.createCommentBox(audioObject);
n@34 259
n@34 260 // Create a slider per track
n@183 261 audioObject.interfaceDOM = new sliderObject(audioObject);
n@154 262
n@34 263 // Distribute it randomnly
n@138 264 var w = window.innerWidth - (offset+8)*2;
n@34 265 w = Math.random()*w;
n@138 266 w = Math.floor(w+(offset+8));
n@183 267 audioObject.interfaceDOM.trackSliderObj.style.left = w+'px';
n@34 268
n@183 269 canvas.appendChild(audioObject.interfaceDOM.trackSliderObj);
n@183 270 audioObject.metric.initialised(convSliderPosToRate(audioObject.interfaceDOM.trackSliderObj));
b@102 271
n@34 272 });
n@182 273 if (commentShow) {
n@182 274 interfaceContext.showCommentBoxes(feedbackHolder,true);
n@182 275 }
n@39 276
n@181 277 $(audioHolderObject.commentQuestions).each(function(index,element) {
n@193 278 var node = interfaceContext.createCommentQuestion(element);
n@193 279 feedbackHolder.appendChild(node.holder);
nicholas@65 280 });
n@153 281
n@153 282
n@153 283 testWaitIndicator();
n@39 284 }
n@39 285
n@183 286 function sliderObject(audioObject) {
n@183 287 // Create a new slider object;
n@183 288 this.parent = audioObject;
n@183 289 this.trackSliderObj = document.createElement('div');
n@183 290 this.trackSliderObj.className = 'track-slider';
n@183 291 this.trackSliderObj.id = 'track-slider-'+audioObject.id;
n@183 292
n@183 293 this.trackSliderObj.setAttribute('trackIndex',audioObject.id);
n@183 294 this.trackSliderObj.innerHTML = '<span>'+audioObject.id+'</span>';
n@183 295 this.trackSliderObj.draggable = true;
n@183 296 this.trackSliderObj.ondragend = dragEnd;
n@183 297
n@183 298 // Onclick, switch playback to that track
n@183 299 this.trackSliderObj.onclick = function() {
n@183 300 // Start the test on first click, that way timings are more accurate.
n@183 301 audioEngineContext.play();
n@183 302 if (audioEngineContext.audioObjectsReady) {
n@183 303 // Cannot continue to issue play command until audioObjects reported as ready!
n@183 304 // Get the track ID from the object ID
nicholas@210 305 var element;
nicholas@210 306 if (event.srcElement.nodeName == "SPAN") {
nicholas@210 307 element = event.srcElement.parentNode;
nicholas@210 308 } else {
nicholas@210 309 element = event.srcElement;
nicholas@210 310 }
nicholas@210 311 var id = Number(element.attributes['trackIndex'].value);
n@183 312 //audioEngineContext.metric.sliderPlayed(id);
n@202 313 audioEngineContext.play(id);
n@183 314 // Currently playing track red, rest green
n@183 315
n@183 316 //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
n@183 317 $('.track-slider').removeClass('track-slider-playing');
nicholas@210 318 $(element).addClass('track-slider-playing');
n@183 319 $('.comment-div').removeClass('comment-box-playing');
n@183 320 $('#comment-div-'+id).addClass('comment-box-playing');
n@183 321 }
n@183 322 };
n@183 323
n@183 324 this.exportXMLDOM = function() {
n@183 325 // Called by the audioObject holding this element. Must be present
n@183 326 var node = document.createElement('value');
n@183 327 node.textContent = convSliderPosToRate(this.trackSliderObj);
n@183 328 return node;
n@183 329 };
n@183 330 }
nicholas@119 331
nicholas@5 332 function dragEnd(ev) {
nicholas@5 333 // Function call when a div has been dropped
n@33 334 var slider = document.getElementById('slider');
n@127 335 var marginSize = Number(slider.attributes['marginsize'].value);
n@51 336 var w = slider.style.width;
n@51 337 w = Number(w.substr(0,w.length-2));
nicholas@133 338 var x = ev.x;
n@127 339 if (x >= marginSize && x < w+marginSize) {
n@51 340 this.style.left = (x)+'px';
nicholas@5 341 } else {
n@127 342 if (x<marginSize) {
n@127 343 this.style.left = marginSize+'px';
nicholas@5 344 } else {
n@127 345 this.style.left = (w+marginSize) + 'px';
nicholas@5 346 }
nicholas@5 347 }
n@183 348 var time = audioEngineContext.timer.getTestTime();
n@183 349 var id = Number(ev.srcElement.getAttribute('trackindex'));
n@183 350 audioEngineContext.audioObjects[id].metric.moved(time,convSliderPosToRate(ev.srcElement));
nicholas@5 351 }
nicholas@7 352
b@101 353 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@40 354 {
nicholas@107 355 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nicholas@107 356 if (hasBeenPlayed.length == 0) {
nicholas@107 357 if (audioEngineContext.status == 1) {
nicholas@107 358 var playback = document.getElementById('playback-button');
nicholas@107 359 playback.click();
nicholas@107 360 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
nicholas@107 361 } else
nicholas@107 362 {
nicholas@107 363 if (audioEngineContext.timer.testStarted == false)
nicholas@107 364 {
nicholas@107 365 alert('You have not started the test! Please press start to begin the test!');
nicholas@107 366 return;
nicholas@107 367 }
nicholas@107 368 }
nicholas@129 369 testState.advanceState();
b@102 370 } else // if a fragment has not been played yet
b@102 371 {
nicholas@107 372 str = "";
nicholas@107 373 if (hasBeenPlayed.length > 1) {
nicholas@107 374 for (var i=0; i<hasBeenPlayed.length; i++) {
nicholas@107 375 str = str + hasBeenPlayed[i];
nicholas@107 376 if (i < hasBeenPlayed.length-2){
nicholas@107 377 str += ", ";
nicholas@107 378 } else if (i == hasBeenPlayed.length-2) {
nicholas@107 379 str += " or ";
nicholas@107 380 }
nicholas@107 381 }
nicholas@107 382 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@107 383 } else {
nicholas@107 384 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@107 385 }
b@102 386 return;
b@102 387 }
n@40 388 }
n@40 389
n@183 390 function convSliderPosToRate(slider)
n@51 391 {
n@51 392 var w = document.getElementById('slider').style.width;
n@127 393 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
n@51 394 var maxPix = w.substr(0,w.length-2);
n@51 395 var pix = slider.style.left;
n@51 396 pix = pix.substr(0,pix.length-2);
n@127 397 var rate = (pix-marginsize)/maxPix;
n@51 398 return rate;
n@51 399 }
n@51 400
n@127 401 function resizeWindow(event){
n@127 402 // Function called when the window has been resized.
n@127 403 // MANDATORY FUNCTION
n@127 404
n@127 405 // Store the slider marker values
n@127 406 var holdValues = [];
n@127 407 $(".track-slider").each(function(index,sliderObj){
n@127 408 holdValues.push(convSliderPosToRate(index));
n@127 409 });
n@127 410
n@127 411 var width = event.target.innerWidth;
n@127 412 var canvas = document.getElementById('sliderCanvasHolder');
n@127 413 var sliderDiv = canvas.children[0];
n@127 414 var sliderScaleDiv = canvas.children[1];
n@127 415 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
n@127 416 var w = (marginsize+8)*2;
n@127 417 sliderDiv.style.width = width - w + 'px';
n@127 418 var width = width - w;
n@127 419 // Move sliders into new position
n@127 420 $(".track-slider").each(function(index,sliderObj){
n@127 421 var pos = holdValues[index];
n@127 422 var pix = pos * width;
n@127 423 sliderObj.style.left = pix+marginsize+'px';
n@127 424 });
n@127 425
n@127 426 // Move scale labels
n@127 427 $(sliderScaleDiv.children).each(function(index,scaleObj){
n@127 428 var position = Number(scaleObj.attributes['value'].value);
n@127 429 var pixelPosition = (position*width)+marginsize;
n@127 430 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
n@127 431 });
n@127 432 }
n@127 433
n@181 434 function pageXMLSave(store, testXML)
n@44 435 {
n@44 436 // Saves a specific test page
nicholas@129 437 var xmlDoc = store;
n@50 438 // Check if any session wide metrics are enabled
nicholas@67 439
n@181 440 var commentShow = testXML.elementComments;
nicholas@67 441
n@50 442 var metric = document.createElement('metric');
n@50 443 if (audioEngineContext.metric.enableTestTimer)
n@50 444 {
n@50 445 var testTime = document.createElement('metricResult');
n@50 446 testTime.id = 'testTime';
n@50 447 testTime.textContent = audioEngineContext.timer.testDuration;
n@50 448 metric.appendChild(testTime);
n@50 449 }
n@50 450 xmlDoc.appendChild(metric);
n@178 451 var audioObjects = audioEngineContext.audioObjects;
n@178 452 for (var i=0; i<audioObjects.length; i++)
n@45 453 {
n@183 454 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM();
n@45 455 xmlDoc.appendChild(audioElement);
n@45 456 }
n@193 457
n@193 458 $(interfaceContext.commentQuestions).each(function(index,element){
n@193 459 var node = element.exportXMLDOM();
n@193 460 xmlDoc.appendChild(node);
n@193 461 });
nicholas@129 462 store = xmlDoc;
nicholas@67 463 }