annotate ape.js @ 202:c311017f4f97 Dev_main

Removed selected track. audioEngineContext.play(id) now controls audioObject playbacks
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Thu, 11 Jun 2015 09:38:09 +0100
parents 13946f91f0a3
children 538322113524
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
nicholas@66 258 if (commentShow) {
n@182 259 var node = interfaceContext.createCommentBox(audioObject);
nicholas@66 260 }
n@34 261
n@34 262 // Create a slider per track
n@183 263 audioObject.interfaceDOM = new sliderObject(audioObject);
n@154 264
n@34 265 // Distribute it randomnly
n@138 266 var w = window.innerWidth - (offset+8)*2;
n@34 267 w = Math.random()*w;
n@138 268 w = Math.floor(w+(offset+8));
n@183 269 audioObject.interfaceDOM.trackSliderObj.style.left = w+'px';
n@34 270
n@183 271 canvas.appendChild(audioObject.interfaceDOM.trackSliderObj);
n@183 272 audioObject.metric.initialised(convSliderPosToRate(audioObject.interfaceDOM.trackSliderObj));
b@102 273
n@34 274 });
n@182 275 if (commentShow) {
n@182 276 interfaceContext.showCommentBoxes(feedbackHolder,true);
n@182 277 }
n@39 278
n@181 279 $(audioHolderObject.commentQuestions).each(function(index,element) {
n@193 280 var node = interfaceContext.createCommentQuestion(element);
n@193 281 feedbackHolder.appendChild(node.holder);
nicholas@65 282 });
n@153 283
n@153 284
n@153 285 testWaitIndicator();
n@39 286 }
n@39 287
n@183 288 function sliderObject(audioObject) {
n@183 289 // Create a new slider object;
n@183 290 this.parent = audioObject;
n@183 291 this.trackSliderObj = document.createElement('div');
n@183 292 this.trackSliderObj.className = 'track-slider';
n@183 293 this.trackSliderObj.id = 'track-slider-'+audioObject.id;
n@183 294
n@183 295 this.trackSliderObj.setAttribute('trackIndex',audioObject.id);
n@183 296 this.trackSliderObj.innerHTML = '<span>'+audioObject.id+'</span>';
n@183 297 this.trackSliderObj.draggable = true;
n@183 298 this.trackSliderObj.ondragend = dragEnd;
n@183 299
n@183 300 // Onclick, switch playback to that track
n@183 301 this.trackSliderObj.onclick = function() {
n@183 302 // Start the test on first click, that way timings are more accurate.
n@183 303 audioEngineContext.play();
n@183 304 if (audioEngineContext.audioObjectsReady) {
n@183 305 // Cannot continue to issue play command until audioObjects reported as ready!
n@183 306 // Get the track ID from the object ID
n@183 307 var id = Number(event.srcElement.attributes['trackIndex'].value);
n@183 308 //audioEngineContext.metric.sliderPlayed(id);
n@202 309 audioEngineContext.play(id);
n@183 310 // Currently playing track red, rest green
n@183 311
n@183 312 //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
n@183 313 $('.track-slider').removeClass('track-slider-playing');
n@183 314 $(event.srcElement).addClass('track-slider-playing');
n@183 315 $('.comment-div').removeClass('comment-box-playing');
n@183 316 $('#comment-div-'+id).addClass('comment-box-playing');
n@183 317 }
n@183 318 };
n@183 319
n@183 320 this.exportXMLDOM = function() {
n@183 321 // Called by the audioObject holding this element. Must be present
n@183 322 var node = document.createElement('value');
n@183 323 node.textContent = convSliderPosToRate(this.trackSliderObj);
n@183 324 return node;
n@183 325 };
n@183 326 }
nicholas@119 327
nicholas@5 328 function dragEnd(ev) {
nicholas@5 329 // Function call when a div has been dropped
n@33 330 var slider = document.getElementById('slider');
n@127 331 var marginSize = Number(slider.attributes['marginsize'].value);
n@51 332 var w = slider.style.width;
n@51 333 w = Number(w.substr(0,w.length-2));
nicholas@133 334 var x = ev.x;
n@127 335 if (x >= marginSize && x < w+marginSize) {
n@51 336 this.style.left = (x)+'px';
nicholas@5 337 } else {
n@127 338 if (x<marginSize) {
n@127 339 this.style.left = marginSize+'px';
nicholas@5 340 } else {
n@127 341 this.style.left = (w+marginSize) + 'px';
nicholas@5 342 }
nicholas@5 343 }
n@183 344 var time = audioEngineContext.timer.getTestTime();
n@183 345 var id = Number(ev.srcElement.getAttribute('trackindex'));
n@183 346 audioEngineContext.audioObjects[id].metric.moved(time,convSliderPosToRate(ev.srcElement));
nicholas@5 347 }
nicholas@7 348
b@101 349 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@40 350 {
nicholas@107 351 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nicholas@107 352 if (hasBeenPlayed.length == 0) {
nicholas@107 353 if (audioEngineContext.status == 1) {
nicholas@107 354 var playback = document.getElementById('playback-button');
nicholas@107 355 playback.click();
nicholas@107 356 // 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 357 } else
nicholas@107 358 {
nicholas@107 359 if (audioEngineContext.timer.testStarted == false)
nicholas@107 360 {
nicholas@107 361 alert('You have not started the test! Please press start to begin the test!');
nicholas@107 362 return;
nicholas@107 363 }
nicholas@107 364 }
nicholas@129 365 testState.advanceState();
b@102 366 } else // if a fragment has not been played yet
b@102 367 {
nicholas@107 368 str = "";
nicholas@107 369 if (hasBeenPlayed.length > 1) {
nicholas@107 370 for (var i=0; i<hasBeenPlayed.length; i++) {
nicholas@107 371 str = str + hasBeenPlayed[i];
nicholas@107 372 if (i < hasBeenPlayed.length-2){
nicholas@107 373 str += ", ";
nicholas@107 374 } else if (i == hasBeenPlayed.length-2) {
nicholas@107 375 str += " or ";
nicholas@107 376 }
nicholas@107 377 }
nicholas@107 378 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@107 379 } else {
nicholas@107 380 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@107 381 }
b@102 382 return;
b@102 383 }
n@40 384 }
n@40 385
n@183 386 function convSliderPosToRate(slider)
n@51 387 {
n@51 388 var w = document.getElementById('slider').style.width;
n@127 389 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
n@51 390 var maxPix = w.substr(0,w.length-2);
n@51 391 var pix = slider.style.left;
n@51 392 pix = pix.substr(0,pix.length-2);
n@127 393 var rate = (pix-marginsize)/maxPix;
n@51 394 return rate;
n@51 395 }
n@51 396
n@127 397 function resizeWindow(event){
n@127 398 // Function called when the window has been resized.
n@127 399 // MANDATORY FUNCTION
n@127 400
n@127 401 // Store the slider marker values
n@127 402 var holdValues = [];
n@127 403 $(".track-slider").each(function(index,sliderObj){
n@127 404 holdValues.push(convSliderPosToRate(index));
n@127 405 });
n@127 406
n@127 407 var width = event.target.innerWidth;
n@127 408 var canvas = document.getElementById('sliderCanvasHolder');
n@127 409 var sliderDiv = canvas.children[0];
n@127 410 var sliderScaleDiv = canvas.children[1];
n@127 411 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
n@127 412 var w = (marginsize+8)*2;
n@127 413 sliderDiv.style.width = width - w + 'px';
n@127 414 var width = width - w;
n@127 415 // Move sliders into new position
n@127 416 $(".track-slider").each(function(index,sliderObj){
n@127 417 var pos = holdValues[index];
n@127 418 var pix = pos * width;
n@127 419 sliderObj.style.left = pix+marginsize+'px';
n@127 420 });
n@127 421
n@127 422 // Move scale labels
n@127 423 $(sliderScaleDiv.children).each(function(index,scaleObj){
n@127 424 var position = Number(scaleObj.attributes['value'].value);
n@127 425 var pixelPosition = (position*width)+marginsize;
n@127 426 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
n@127 427 });
n@127 428 }
n@127 429
n@181 430 function pageXMLSave(store, testXML)
n@44 431 {
n@44 432 // Saves a specific test page
nicholas@129 433 var xmlDoc = store;
n@50 434 // Check if any session wide metrics are enabled
nicholas@67 435
n@181 436 var commentShow = testXML.elementComments;
nicholas@67 437
n@50 438 var metric = document.createElement('metric');
n@50 439 if (audioEngineContext.metric.enableTestTimer)
n@50 440 {
n@50 441 var testTime = document.createElement('metricResult');
n@50 442 testTime.id = 'testTime';
n@50 443 testTime.textContent = audioEngineContext.timer.testDuration;
n@50 444 metric.appendChild(testTime);
n@50 445 }
n@50 446 xmlDoc.appendChild(metric);
n@178 447 var audioObjects = audioEngineContext.audioObjects;
n@178 448 for (var i=0; i<audioObjects.length; i++)
n@45 449 {
n@183 450 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM();
n@45 451 xmlDoc.appendChild(audioElement);
n@45 452 }
n@193 453
n@193 454 $(interfaceContext.commentQuestions).each(function(index,element){
n@193 455 var node = element.exportXMLDOM();
n@193 456 xmlDoc.appendChild(node);
n@193 457 });
nicholas@129 458 store = xmlDoc;
nicholas@67 459 }