annotate ape.js @ 1457:c8a9825aaa05

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