annotate interfaces/AB.js @ 1360:a9eb4f6443d8

Interfaces have startPlayback and stopPlayback methods to clean up code management. Looping playbacks now have a 2s cross-fade.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Tue, 26 Jan 2016 13:52:56 +0000
parents 386415d2a5c1
children cb348f6208b2 c0022a09c4f6
rev   line source
nickjillings@1341 1 // Once this is loaded and parsed, begin execution
nickjillings@1341 2 loadInterface();
nickjillings@1341 3
nickjillings@1341 4 function loadInterface() {
nickjillings@1341 5 // Get the dimensions of the screen available to the page
nickjillings@1341 6 var width = window.innerWidth;
nickjillings@1341 7 var height = window.innerHeight;
nickjillings@1341 8 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
nickjillings@1341 9
nickjillings@1341 10 // Custom Comparitor Object
nickjillings@1341 11 Interface.prototype.comparitor = null;
nickjillings@1341 12
nickjillings@1341 13 // The injection point into the HTML page
nickjillings@1341 14 interfaceContext.insertPoint = document.getElementById("topLevelBody");
nickjillings@1341 15 var testContent = document.createElement('div');
nickjillings@1341 16 testContent.id = 'testContent';
nickjillings@1341 17
nickjillings@1341 18 // Create the top div for the Title element
nickjillings@1341 19 var titleAttr = specification.title;
nickjillings@1341 20 var title = document.createElement('div');
nickjillings@1341 21 title.className = "title";
nickjillings@1341 22 title.align = "center";
nickjillings@1341 23 var titleSpan = document.createElement('span');
nickjillings@1341 24
nickjillings@1341 25 // Set title to that defined in XML, else set to default
nickjillings@1341 26 if (titleAttr != undefined) {
nickjillings@1341 27 titleSpan.textContent = titleAttr;
nickjillings@1341 28 } else {
nickjillings@1341 29 titleSpan.textContent = 'Listening test';
nickjillings@1341 30 }
nickjillings@1341 31 // Insert the titleSpan element into the title div element.
nickjillings@1341 32 title.appendChild(titleSpan);
nickjillings@1341 33
nickjillings@1341 34 var pagetitle = document.createElement('div');
nickjillings@1341 35 pagetitle.className = "pageTitle";
nickjillings@1341 36 pagetitle.align = "center";
nickjillings@1341 37 var titleSpan = document.createElement('span');
nickjillings@1341 38 titleSpan.id = "pageTitle";
nickjillings@1341 39 pagetitle.appendChild(titleSpan);
nickjillings@1341 40
nickjillings@1341 41 // Create Interface buttons!
nickjillings@1341 42 var interfaceButtons = document.createElement('div');
nickjillings@1341 43 interfaceButtons.id = 'interface-buttons';
nickjillings@1341 44 interfaceButtons.style.height = '25px';
nickjillings@1341 45
nickjillings@1341 46 // Create playback start/stop points
nickjillings@1341 47 var playback = document.createElement("button");
nickjillings@1341 48 playback.innerHTML = 'Stop';
nickjillings@1341 49 playback.id = 'playback-button';
nickjillings@1341 50 playback.style.float = 'left';
nickjillings@1341 51 // onclick function. Check if it is playing or not, call the correct function in the
nickjillings@1341 52 // audioEngine, change the button text to reflect the next state.
nickjillings@1341 53 playback.onclick = function() {
nickjillings@1341 54 if (audioEngineContext.status == 1) {
nickjillings@1341 55 audioEngineContext.stop();
nickjillings@1341 56 this.innerHTML = 'Stop';
nickjillings@1341 57 var time = audioEngineContext.timer.getTestTime();
nickjillings@1341 58 console.log('Stopped at ' + time); // DEBUG/SAFETY
nickjillings@1341 59 }
nickjillings@1341 60 };
nickjillings@1341 61 // Append the interface buttons into the interfaceButtons object.
nickjillings@1341 62 interfaceButtons.appendChild(playback);
nickjillings@1341 63
nickjillings@1341 64 // Global parent for the comment boxes on the page
nickjillings@1341 65 var feedbackHolder = document.createElement('div');
nickjillings@1341 66 feedbackHolder.id = 'feedbackHolder';
nickjillings@1341 67
nickjillings@1341 68 // Construct the AB Boxes
nickjillings@1341 69 var boxes = document.createElement('div');
nickjillings@1341 70 boxes.align = "center";
nickjillings@1341 71 boxes.id = "box-holders";
nickjillings@1341 72 boxes.style.float = "left";
nickjillings@1341 73
nickjillings@1341 74 var submit = document.createElement('button');
nickjillings@1341 75 submit.id = "submit";
nickjillings@1341 76 submit.onclick = buttonSubmitClick;
nickjillings@1341 77 submit.className = "big-button";
nickjillings@1341 78 submit.textContent = "submit";
nickjillings@1341 79 submit.style.position = "relative";
nickjillings@1341 80 submit.style.left = (window.innerWidth-250)/2 + 'px';
nickjillings@1341 81
nickjillings@1341 82 feedbackHolder.appendChild(boxes);
nickjillings@1341 83
nickjillings@1341 84 // Inject into HTML
nickjillings@1341 85 testContent.appendChild(title); // Insert the title
nickjillings@1341 86 testContent.appendChild(pagetitle);
nickjillings@1341 87 testContent.appendChild(interfaceButtons);
nickjillings@1341 88 testContent.appendChild(feedbackHolder);
nickjillings@1341 89 testContent.appendChild(submit);
nickjillings@1341 90 interfaceContext.insertPoint.appendChild(testContent);
nickjillings@1341 91
nickjillings@1341 92 // Load the full interface
nickjillings@1341 93 testState.initialise();
nickjillings@1341 94 testState.advanceState();
nickjillings@1341 95 }
nickjillings@1341 96
nickjillings@1341 97 function loadTest(audioHolderObject)
nickjillings@1341 98 {
nickjillings@1341 99 var feedbackHolder = document.getElementById('feedbackHolder');
nickjillings@1341 100 var interfaceObj = audioHolderObject.interfaces;
nickjillings@1341 101 if (interfaceObj.length > 1)
nickjillings@1341 102 {
nickjillings@1341 103 console.log("WARNING - This interface only supports one <interface> node per page. Using first interface node");
nickjillings@1341 104 }
nickjillings@1341 105 interfaceObj = interfaceObj[0];
nickjillings@1341 106
nickjillings@1341 107 if(interfaceObj.title != null)
nickjillings@1341 108 {
nickjillings@1341 109 document.getElementById("pageTitle").textContent = interfaceObj.title;
nickjillings@1341 110 }
nickjillings@1356 111
nickjillings@1356 112 var interfaceOptions = specification.interfaces.options.concat(interfaceObj.options);
nickjillings@1356 113 for (var option of interfaceOptions)
nickjillings@1356 114 {
nickjillings@1356 115 if (option.type == "show")
nickjillings@1356 116 {
nickjillings@1356 117 switch(option.name) {
nickjillings@1356 118 case "playhead":
nickjillings@1356 119 var playbackHolder = document.getElementById('playback-holder');
nickjillings@1356 120 if (playbackHolder == null)
nickjillings@1356 121 {
nickjillings@1356 122 playbackHolder = document.createElement('div');
nickjillings@1356 123 playbackHolder.style.width = "100%";
nickjillings@1356 124 playbackHolder.style.float = "left";
nickjillings@1356 125 playbackHolder.align = 'center';
nickjillings@1356 126 playbackHolder.appendChild(interfaceContext.playhead.object);
nickjillings@1356 127 feedbackHolder.appendChild(playbackHolder);
nickjillings@1356 128 }
nickjillings@1356 129 break;
nickjillings@1356 130 case "page-count":
nickjillings@1356 131 var pagecountHolder = document.getElementById('page-count');
nickjillings@1356 132 if (pagecountHolder == null)
nickjillings@1356 133 {
nickjillings@1356 134 pagecountHolder = document.createElement('div');
nickjillings@1356 135 pagecountHolder.id = 'page-count';
nickjillings@1356 136 }
nickjillings@1356 137 pagecountHolder.innerHTML = '<span>Page '+(audioHolderObject.presentedId+1)+' of '+specification.pages.length+'</span>';
nickjillings@1356 138 var inject = document.getElementById('interface-buttons');
nickjillings@1356 139 inject.appendChild(pagecountHolder);
nickjillings@1356 140 break;
nickjillings@1356 141 case "volume":
nickjillings@1356 142 if (document.getElementById('master-volume-holder') == null)
nickjillings@1356 143 {
nickjillings@1356 144 feedbackHolder.appendChild(interfaceContext.volume.object);
nickjillings@1356 145 }
nickjillings@1356 146 break;
nickjillings@1356 147 }
nickjillings@1356 148 }
nickjillings@1356 149 }
nickjillings@1341 150
nickjillings@1341 151 // Populate the comparitor object
nickjillings@1341 152 interfaceContext.comparitor = new Comparitor(audioHolderObject);
nickjillings@1341 153 resizeWindow(null);
nickjillings@1341 154 }
nickjillings@1341 155
nickjillings@1341 156 function Comparitor(audioHolderObject)
nickjillings@1341 157 {
nickjillings@1341 158 this.comparitorBox = function(audioElement,id,text)
nickjillings@1341 159 {
nickjillings@1341 160 this.parent = audioElement;
nickjillings@1341 161 this.id = id;
nickjillings@1341 162 this.value = 0;
nickjillings@1341 163 this.disabled = true;
nickjillings@1341 164 this.box = document.createElement('div');
nickjillings@1341 165 this.box.className = 'comparitor-holder';
nickjillings@1341 166 this.box.setAttribute('track-id',audioElement.id);
nickjillings@1341 167 this.box.id = 'comparitor-'+text;
nickjillings@1341 168 this.selector = document.createElement('div');
nickjillings@1341 169 this.selector.className = 'comparitor-selector disabled';
nickjillings@1341 170 var selectorText = document.createElement('span');
nickjillings@1341 171 selectorText.textContent = text;
nickjillings@1341 172 this.selector.appendChild(selectorText);
nickjillings@1341 173 this.playback = document.createElement('button');
nickjillings@1341 174 this.playback.className = 'comparitor-button';
nickjillings@1341 175 this.playback.disabled = true;
nickjillings@1341 176 this.playback.textContent = "Listen";
nickjillings@1341 177 this.box.appendChild(this.selector);
nickjillings@1341 178 this.box.appendChild(this.playback);
nickjillings@1349 179 this.selector.onclick = function(event)
nickjillings@1341 180 {
nickjillings@1341 181 var time = audioEngineContext.timer.getTestTime();
nickjillings@1341 182 if ($(event.currentTarget).hasClass('disabled'))
nickjillings@1341 183 {
nickjillings@1341 184 console.log("Please wait until sample has loaded");
nickjillings@1341 185 return;
nickjillings@1341 186 }
nickjillings@1341 187 if (audioEngineContext.status == 0)
nickjillings@1341 188 {
nickjillings@1341 189 alert("Please listen to the samples before making a selection");
nickjillings@1341 190 console.log("Please listen to the samples before making a selection");
nickjillings@1341 191 return;
nickjillings@1341 192 }
nickjillings@1341 193 $(".comparitor-selector").removeClass('selected');
nickjillings@1341 194 var id = event.currentTarget.parentElement.getAttribute('track-id');
nickjillings@1341 195 interfaceContext.comparitor.selected = id;
nickjillings@1341 196 $(event.currentTarget).addClass('selected');
nickjillings@1341 197 for (var i=0; i<interfaceContext.comparitor.comparitors.length; i++)
nickjillings@1341 198 {
nickjillings@1341 199 var obj = interfaceContext.comparitor.comparitors[i];
nickjillings@1341 200 if (i == id) {
nickjillings@1341 201 obj.value = 1;
nickjillings@1341 202 } else {
nickjillings@1341 203 obj.value = 0;
nickjillings@1341 204 }
nickjillings@1341 205 obj.parent.metric.moved(time,obj.value);
nickjillings@1341 206 }
nickjillings@1341 207 console.log("Selected "+id+' ('+time+')');
nickjillings@1341 208 };
nickjillings@1349 209 this.playback.onclick = function(event)
nickjillings@1341 210 {
nickjillings@1341 211 var id = event.currentTarget.parentElement.getAttribute('track-id');
nickjillings@1341 212 audioEngineContext.play(id);
nickjillings@1341 213 };
nickjillings@1341 214
nickjillings@1341 215 this.enable = function()
nickjillings@1341 216 {
nickjillings@1341 217 if (this.parent.state == 1)
nickjillings@1341 218 {
nickjillings@1341 219 $(this.selector).removeClass('disabled');
nickjillings@1341 220 this.playback.disabled = false;
nickjillings@1341 221 }
nickjillings@1341 222 };
nickjillings@1341 223 this.updateLoading = function(progress)
nickjillings@1341 224 {
nickjillings@1341 225 if (progress != 100)
nickjillings@1341 226 {
nickjillings@1341 227 progress = String(progress);
nickjillings@1341 228 progress = progress.split('.')[0];
nickjillings@1341 229 this.playback.textContent = progress+'%';
nickjillings@1341 230 } else {
nickjillings@1341 231 this.playback.textContent = "Listen";
nickjillings@1341 232 }
nickjillings@1341 233 };
nickjillings@1360 234 this.startPlayback = function()
nickjillings@1360 235 {
nickjillings@1360 236 $('.comparitor-button').text('Listen');
nickjillings@1360 237 $(this.playback).text('Playing');
nickjillings@1360 238 };
nickjillings@1360 239 this.stopPlayback = function()
nickjillings@1360 240 {
nickjillings@1360 241 $(this.playback).text('Listen');
nickjillings@1360 242 };
nickjillings@1341 243 this.exportXMLDOM = function(audioObject)
nickjillings@1341 244 {
nickjillings@1341 245 var node = storage.document.createElement('value');
nickjillings@1341 246 node.textContent = this.value;
nickjillings@1341 247 return node;
nickjillings@1341 248 };
nickjillings@1341 249 this.getValue = function() {
nickjillings@1341 250 return this.value;
nickjillings@1341 251 };
nickjillings@1341 252 this.getPresentedId = function()
nickjillings@1341 253 {
nickjillings@1341 254 return this.selector.children[0].textContent;
nickjillings@1341 255 };
nickjillings@1341 256 this.canMove = function()
nickjillings@1341 257 {
nickjillings@1341 258 return false;
nickjillings@1341 259 };
nickjillings@1341 260 };
nickjillings@1341 261
nickjillings@1341 262 this.boxHolders = document.getElementById('box-holders');
nickjillings@1341 263 this.boxHolders.innerHTML = null;
nickjillings@1341 264 this.comparitors = [];
nickjillings@1341 265 this.selected = null;
nickjillings@1341 266
nickjillings@1341 267 // First generate the Audio Objects for the Audio Engine
nickjillings@1341 268 for (var index=0; index<audioHolderObject.audioElements.length; index++)
nickjillings@1341 269 {
nickjillings@1341 270 var element = audioHolderObject.audioElements[index];
nickjillings@1341 271 if (index == audioHolderObject.outsideReference || element.type == 'outside-reference')
nickjillings@1341 272 {
nickjillings@1341 273 console.log("WARNING - AB cannot have fixed reference");
nickjillings@1341 274 }
nickjillings@1341 275 var audioObject = audioEngineContext.newTrack(element);
nickjillings@1341 276 var node = new this.comparitorBox(audioObject,index,String.fromCharCode(65 + index));
nickjillings@1341 277 audioObject.bindInterface(node);
nickjillings@1341 278 this.comparitors.push(node);
nickjillings@1341 279 this.boxHolders.appendChild(node.box);
nickjillings@1341 280 }
nickjillings@1341 281 return this;
nickjillings@1341 282 }
nickjillings@1341 283
nickjillings@1341 284 function resizeWindow(event)
nickjillings@1341 285 {
nickjillings@1341 286 document.getElementById('submit').style.left = (window.innerWidth-250)/2 + 'px';
nickjillings@1341 287 var numObj = interfaceContext.comparitor.comparitors.length;
nickjillings@1341 288 var boxW = numObj*312;
nickjillings@1341 289 var diff = window.innerWidth - boxW;
nickjillings@1341 290 while (diff < 0)
nickjillings@1341 291 {
nickjillings@1341 292 numObj = Math.ceil(numObj/2);
nickjillings@1341 293 boxW = numObj*312;
nickjillings@1341 294 diff = window.innerWidth - boxW;
nickjillings@1341 295 }
nickjillings@1341 296 document.getElementById('box-holders').style.marginLeft = diff/2 + 'px';
nickjillings@1341 297 document.getElementById('box-holders').style.marginRight = diff/2 + 'px';
nickjillings@1341 298 document.getElementById('box-holders').style.width = boxW + 'px';
nickjillings@1341 299 }
nickjillings@1341 300
nickjillings@1341 301 function buttonSubmitClick()
nickjillings@1341 302 {
nickjillings@1341 303 var checks = [];
nickjillings@1341 304 checks = checks.concat(testState.currentStateMap.interfaces[0].options);
nickjillings@1341 305 checks = checks.concat(specification.interfaces.options);
nickjillings@1341 306 var canContinue = true;
nickjillings@1341 307
nickjillings@1341 308 for (var i=0; i<checks.length; i++) {
nickjillings@1341 309 if (checks[i].type == 'check')
nickjillings@1341 310 {
nickjillings@1341 311 switch(checks[i].name) {
nickjillings@1341 312 case 'fragmentPlayed':
nickjillings@1341 313 // Check if all fragments have been played
nickjillings@1341 314 var checkState = interfaceContext.checkAllPlayed();
nickjillings@1341 315 if (checkState == false) {canContinue = false;}
nickjillings@1341 316 break;
nickjillings@1341 317 case 'fragmentFullPlayback':
nickjillings@1341 318 // Check all fragments have been played to their full length
nickjillings@1341 319 var checkState = interfaceContext.checkFragmentsFullyPlayed();
nickjillings@1341 320 if (checkState == false) {canContinue = false;}
nickjillings@1341 321 break;
nickjillings@1341 322 case 'fragmentMoved':
nickjillings@1341 323 // Check all fragment sliders have been moved.
nickjillings@1341 324 var checkState = interfaceContext.checkAllMoved();
nickjillings@1341 325 if (checkState == false) {canContinue = false;}
nickjillings@1341 326 break;
nickjillings@1341 327 case 'fragmentComments':
nickjillings@1341 328 // Check all fragment sliders have been moved.
nickjillings@1341 329 var checkState = interfaceContext.checkAllCommented();
nickjillings@1341 330 if (checkState == false) {canContinue = false;}
nickjillings@1341 331 break;
nickjillings@1341 332 default:
nickjillings@1341 333 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
nickjillings@1341 334 break;
nickjillings@1341 335 }
nickjillings@1341 336
nickjillings@1341 337 }
nickjillings@1341 338 if (!canContinue) {break;}
nickjillings@1341 339 }
nickjillings@1341 340 if (canContinue)
nickjillings@1341 341 {
nickjillings@1341 342 if (audioEngineContext.status == 1) {
nickjillings@1341 343 var playback = document.getElementById('playback-button');
nickjillings@1341 344 playback.click();
nickjillings@1341 345 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
nickjillings@1341 346 } else
nickjillings@1341 347 {
nickjillings@1341 348 if (audioEngineContext.timer.testStarted == false)
nickjillings@1341 349 {
nickjillings@1341 350 alert('You have not started the test! Please press start to begin the test!');
nickjillings@1341 351 return;
nickjillings@1341 352 }
nickjillings@1341 353 }
nickjillings@1341 354 testState.advanceState();
nickjillings@1341 355 }
nickjillings@1341 356 }
nickjillings@1341 357
nickjillings@1341 358 function pageXMLSave(store, pageSpecification)
nickjillings@1341 359 {
nickjillings@1341 360 // MANDATORY
nickjillings@1341 361 // Saves a specific test page
nickjillings@1341 362 // You can use this space to add any extra nodes to your XML <audioHolder> saves
nickjillings@1341 363 // Get the current <page> information in store (remember to appendChild your data to it)
nickjillings@1341 364 // pageSpecification is the current page node configuration
nickjillings@1341 365 // To create new XML nodes, use storage.document.createElement();
nickjillings@1341 366 }