annotate ape.js @ 2085:11328fe5d16d

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