annotate interfaces/AB.js @ 1160:f0fa49f5bbb0

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