annotate ape.js @ 183:aaeebbacc159 Dev_main

XML-DOM created by their own objects
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Thu, 04 Jun 2015 17:23:32 +0100
parents 105b6bd810e9
children b33a22cb6e98
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@47 179 // Setup question title
n@181 180 var interfaceObj = $(audioHolderObject).find('interface');
n@47 181 var titleNode = interfaceObj.find('title');
n@47 182 if (titleNode[0] != undefined)
n@47 183 {
n@47 184 document.getElementById('pageTitle').textContent = titleNode[0].textContent;
n@47 185 }
n@47 186 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
n@127 187 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
n@47 188 var scale = document.getElementById('sliderScaleHolder');
n@47 189 scale.innerHTML = null;
n@47 190 interfaceObj.find('scale').each(function(index,scaleObj){
n@127 191 var value = document.createAttribute('value');
n@47 192 var position = Number(scaleObj.attributes['position'].value)*0.01;
n@127 193 value.nodeValue = position;
n@47 194 var pixelPosition = (position*positionScale)+offset;
n@47 195 var scaleDOM = document.createElement('span');
n@47 196 scaleDOM.textContent = scaleObj.textContent;
n@47 197 scale.appendChild(scaleDOM);
n@47 198 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
n@127 199 scaleDOM.setAttributeNode(value);
n@47 200 });
n@174 201
n@174 202 var commentBoxPrefix = interfaceObj.find('commentBoxPrefix');
n@174 203 if (commentBoxPrefix.length != 0) {
n@174 204 commentBoxPrefix = commentBoxPrefix[0].textContent;
n@174 205 } else {
n@174 206 commentBoxPrefix = "Comment on track";
n@174 207 }
n@34 208
n@34 209 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@181 210 if (audioHolderObject.sampleRate != undefined) {
n@181 211 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
n@34 212 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
n@34 213 alert(errStr);
n@34 214 return;
n@34 215 }
n@34 216 }
n@45 217
n@181 218 var commentShow = audioHolderObject.elementComments;
nicholas@66 219
n@181 220 var loopPlayback = audioHolderObject.loop;
n@181 221
n@57 222 audioEngineContext.loopPlayback = loopPlayback;
n@57 223 // Create AudioEngine bindings for playback
n@57 224 if (loopPlayback) {
n@57 225 audioEngineContext.selectedTrack = function(id) {
n@57 226 for (var i=0; i<this.audioObjects.length; i++)
n@57 227 {
n@57 228 if (id == i) {
nicholas@132 229 this.audioObjects[i].loopStart();
n@57 230 } else {
nicholas@132 231 this.audioObjects[i].loopStop();
n@57 232 }
n@57 233 }
n@57 234 };
n@57 235 } else {
n@57 236 audioEngineContext.selectedTrack = function(id) {
n@57 237 for (var i=0; i<this.audioObjects.length; i++)
n@57 238 {
n@97 239 this.audioObjects[i].outputGain.gain.value = 0.0;
n@97 240 this.audioObjects[i].stop();
n@57 241 }
n@99 242 if (this.status == 1) {
n@99 243 this.audioObjects[id].outputGain.gain.value = 1.0;
n@99 244 this.audioObjects[id].play(audioContext.currentTime+0.01);
n@99 245 }
n@57 246 };
n@57 247 }
n@57 248
n@46 249 currentTestHolder = document.createElement('audioHolder');
n@181 250 currentTestHolder.id = audioHolderObject.id;
n@181 251 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
n@46 252
n@181 253 var randomise = audioHolderObject.randomiseOrder;
n@45 254
n@181 255 var audioElements = audioHolderObject.audioElements;
nicholas@58 256 currentTrackOrder = [];
n@45 257 if (randomise) {
n@181 258 audioHolderObject.audioElements = randomiseOrder(audioHolderObject.audioElements);
n@45 259 }
n@45 260
nicholas@58 261 // Delete any previous audioObjects associated with the audioEngine
nicholas@58 262 audioEngineContext.audioObjects = [];
nicholas@58 263
n@45 264 // Find all the audioElements from the audioHolder
n@181 265 $(audioHolderObject.audioElements).each(function(index,element){
n@34 266 // Find URL of track
n@34 267 // In this jQuery loop, variable 'this' holds the current audioElement.
n@34 268
n@34 269 // Now load each audio sample. First create the new track by passing the full URL
n@181 270 var trackURL = audioHolderObject.hostURL + element.url;
n@182 271 var audioObject = audioEngineContext.newTrack(element);
nicholas@66 272
nicholas@66 273 if (commentShow) {
n@182 274 var node = interfaceContext.createCommentBox(audioObject);
nicholas@66 275 }
n@34 276
n@34 277 // Create a slider per track
n@183 278 audioObject.interfaceDOM = new sliderObject(audioObject);
n@154 279
n@34 280 // Distribute it randomnly
n@138 281 var w = window.innerWidth - (offset+8)*2;
n@34 282 w = Math.random()*w;
n@138 283 w = Math.floor(w+(offset+8));
n@183 284 audioObject.interfaceDOM.trackSliderObj.style.left = w+'px';
n@34 285
n@183 286 canvas.appendChild(audioObject.interfaceDOM.trackSliderObj);
n@183 287 audioObject.metric.initialised(convSliderPosToRate(audioObject.interfaceDOM.trackSliderObj));
b@102 288
n@34 289 });
n@182 290 if (commentShow) {
n@182 291 interfaceContext.showCommentBoxes(feedbackHolder,true);
n@182 292 }
n@39 293
nicholas@65 294 // Append any commentQuestion boxes
n@181 295 $(audioHolderObject.commentQuestions).each(function(index,element) {
nicholas@65 296 // Create document objects to hold the comment boxes
nicholas@65 297 var trackComment = document.createElement('div');
nicholas@65 298 trackComment.className = 'comment-div commentQuestion';
n@181 299 trackComment.id = element.id;
nicholas@65 300 // Create a string next to each comment asking for a comment
nicholas@65 301 var trackString = document.createElement('span');
n@181 302 trackString.innerHTML = element.question;
nicholas@65 303 // Create the HTML5 comment box 'textarea'
nicholas@65 304 var trackCommentBox = document.createElement('textarea');
nicholas@65 305 trackCommentBox.rows = '4';
nicholas@65 306 trackCommentBox.cols = '100';
nicholas@65 307 trackCommentBox.name = 'commentQuestion'+index;
nicholas@65 308 trackCommentBox.className = 'trackComment';
nicholas@65 309 var br = document.createElement('br');
nicholas@65 310 // Add to the holder.
nicholas@65 311 trackComment.appendChild(trackString);
nicholas@65 312 trackComment.appendChild(br);
nicholas@65 313 trackComment.appendChild(trackCommentBox);
nicholas@65 314 feedbackHolder.appendChild(trackComment);
nicholas@65 315 });
n@153 316
n@153 317
n@153 318 testWaitIndicator();
n@39 319 }
n@39 320
n@183 321 function sliderObject(audioObject) {
n@183 322 // Create a new slider object;
n@183 323 this.parent = audioObject;
n@183 324 this.trackSliderObj = document.createElement('div');
n@183 325 this.trackSliderObj.className = 'track-slider';
n@183 326 this.trackSliderObj.id = 'track-slider-'+audioObject.id;
n@183 327
n@183 328 this.trackSliderObj.setAttribute('trackIndex',audioObject.id);
n@183 329 this.trackSliderObj.innerHTML = '<span>'+audioObject.id+'</span>';
n@183 330 this.trackSliderObj.draggable = true;
n@183 331 this.trackSliderObj.ondragend = dragEnd;
n@183 332
n@183 333 // Onclick, switch playback to that track
n@183 334 this.trackSliderObj.onclick = function() {
n@183 335 // Start the test on first click, that way timings are more accurate.
n@183 336 audioEngineContext.play();
n@183 337 if (audioEngineContext.audioObjectsReady) {
n@183 338 // Cannot continue to issue play command until audioObjects reported as ready!
n@183 339 // Get the track ID from the object ID
n@183 340 var id = Number(event.srcElement.attributes['trackIndex'].value);
n@183 341 //audioEngineContext.metric.sliderPlayed(id);
n@183 342 audioEngineContext.selectedTrack(id);
n@183 343 // Currently playing track red, rest green
n@183 344
n@183 345 //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
n@183 346 $('.track-slider').removeClass('track-slider-playing');
n@183 347 $(event.srcElement).addClass('track-slider-playing');
n@183 348 $('.comment-div').removeClass('comment-box-playing');
n@183 349 $('#comment-div-'+id).addClass('comment-box-playing');
n@183 350 }
n@183 351 };
n@183 352
n@183 353 this.exportXMLDOM = function() {
n@183 354 // Called by the audioObject holding this element. Must be present
n@183 355 var node = document.createElement('value');
n@183 356 node.textContent = convSliderPosToRate(this.trackSliderObj);
n@183 357 return node;
n@183 358 };
n@183 359 }
nicholas@119 360
nicholas@5 361 function dragEnd(ev) {
nicholas@5 362 // Function call when a div has been dropped
n@33 363 var slider = document.getElementById('slider');
n@127 364 var marginSize = Number(slider.attributes['marginsize'].value);
n@51 365 var w = slider.style.width;
n@51 366 w = Number(w.substr(0,w.length-2));
nicholas@133 367 var x = ev.x;
n@127 368 if (x >= marginSize && x < w+marginSize) {
n@51 369 this.style.left = (x)+'px';
nicholas@5 370 } else {
n@127 371 if (x<marginSize) {
n@127 372 this.style.left = marginSize+'px';
nicholas@5 373 } else {
n@127 374 this.style.left = (w+marginSize) + 'px';
nicholas@5 375 }
nicholas@5 376 }
n@183 377 var time = audioEngineContext.timer.getTestTime();
n@183 378 var id = Number(ev.srcElement.getAttribute('trackindex'));
n@183 379 audioEngineContext.audioObjects[id].metric.moved(time,convSliderPosToRate(ev.srcElement));
nicholas@5 380 }
nicholas@7 381
b@101 382 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@40 383 {
nicholas@107 384 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nicholas@107 385 if (hasBeenPlayed.length == 0) {
nicholas@107 386 if (audioEngineContext.status == 1) {
nicholas@107 387 var playback = document.getElementById('playback-button');
nicholas@107 388 playback.click();
nicholas@107 389 // 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 390 } else
nicholas@107 391 {
nicholas@107 392 if (audioEngineContext.timer.testStarted == false)
nicholas@107 393 {
nicholas@107 394 alert('You have not started the test! Please press start to begin the test!');
nicholas@107 395 return;
nicholas@107 396 }
nicholas@107 397 }
nicholas@129 398 testState.advanceState();
b@102 399 } else // if a fragment has not been played yet
b@102 400 {
nicholas@107 401 str = "";
nicholas@107 402 if (hasBeenPlayed.length > 1) {
nicholas@107 403 for (var i=0; i<hasBeenPlayed.length; i++) {
nicholas@107 404 str = str + hasBeenPlayed[i];
nicholas@107 405 if (i < hasBeenPlayed.length-2){
nicholas@107 406 str += ", ";
nicholas@107 407 } else if (i == hasBeenPlayed.length-2) {
nicholas@107 408 str += " or ";
nicholas@107 409 }
nicholas@107 410 }
nicholas@107 411 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@107 412 } else {
nicholas@107 413 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nicholas@107 414 }
b@102 415 return;
b@102 416 }
n@40 417 }
n@40 418
n@183 419 function convSliderPosToRate(slider)
n@51 420 {
n@51 421 var w = document.getElementById('slider').style.width;
n@127 422 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
n@51 423 var maxPix = w.substr(0,w.length-2);
n@51 424 var pix = slider.style.left;
n@51 425 pix = pix.substr(0,pix.length-2);
n@127 426 var rate = (pix-marginsize)/maxPix;
n@51 427 return rate;
n@51 428 }
n@51 429
n@127 430 function resizeWindow(event){
n@127 431 // Function called when the window has been resized.
n@127 432 // MANDATORY FUNCTION
n@127 433
n@127 434 // Store the slider marker values
n@127 435 var holdValues = [];
n@127 436 $(".track-slider").each(function(index,sliderObj){
n@127 437 holdValues.push(convSliderPosToRate(index));
n@127 438 });
n@127 439
n@127 440 var width = event.target.innerWidth;
n@127 441 var canvas = document.getElementById('sliderCanvasHolder');
n@127 442 var sliderDiv = canvas.children[0];
n@127 443 var sliderScaleDiv = canvas.children[1];
n@127 444 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
n@127 445 var w = (marginsize+8)*2;
n@127 446 sliderDiv.style.width = width - w + 'px';
n@127 447 var width = width - w;
n@127 448 // Move sliders into new position
n@127 449 $(".track-slider").each(function(index,sliderObj){
n@127 450 var pos = holdValues[index];
n@127 451 var pix = pos * width;
n@127 452 sliderObj.style.left = pix+marginsize+'px';
n@127 453 });
n@127 454
n@127 455 // Move scale labels
n@127 456 $(sliderScaleDiv.children).each(function(index,scaleObj){
n@127 457 var position = Number(scaleObj.attributes['value'].value);
n@127 458 var pixelPosition = (position*width)+marginsize;
n@127 459 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
n@127 460 });
n@127 461 }
n@127 462
n@181 463 function pageXMLSave(store, testXML)
n@44 464 {
n@44 465 // Saves a specific test page
nicholas@129 466 var xmlDoc = store;
n@50 467 // Check if any session wide metrics are enabled
nicholas@67 468
n@181 469 var commentShow = testXML.elementComments;
nicholas@67 470
n@50 471 var metric = document.createElement('metric');
n@50 472 if (audioEngineContext.metric.enableTestTimer)
n@50 473 {
n@50 474 var testTime = document.createElement('metricResult');
n@50 475 testTime.id = 'testTime';
n@50 476 testTime.textContent = audioEngineContext.timer.testDuration;
n@50 477 metric.appendChild(testTime);
n@50 478 }
n@50 479 xmlDoc.appendChild(metric);
n@178 480 var audioObjects = audioEngineContext.audioObjects;
n@178 481 for (var i=0; i<audioObjects.length; i++)
n@45 482 {
n@183 483 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM();
n@45 484 xmlDoc.appendChild(audioElement);
n@45 485 }
nicholas@65 486 var commentQuestion = document.getElementsByClassName('commentQuestion');
nicholas@65 487 for (var i=0; i<commentQuestion.length; i++)
nicholas@65 488 {
nicholas@65 489 var cqHolder = document.createElement('CommentQuestion');
nicholas@65 490 var comment = document.createElement('comment');
nicholas@65 491 var question = document.createElement('question');
nicholas@65 492 cqHolder.id = commentQuestion[i].id;
nicholas@65 493 comment.textContent = commentQuestion[i].children[2].value;
nicholas@65 494 question.textContent = commentQuestion[i].children[0].textContent;
n@124 495 console.log('Question ' + i + ': ' + commentQuestion[i].children[2].value); // DEBUG/SAFETY
nicholas@65 496 cqHolder.appendChild(question);
nicholas@65 497 cqHolder.appendChild(comment);
nicholas@65 498 xmlDoc.appendChild(cqHolder);
nicholas@65 499 }
nicholas@129 500 store = xmlDoc;
nicholas@67 501 }