annotate mushra.js @ 446:bb650ec76d81

Added tag 1.1.0 for changeset 63c4163fc705
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 23 Dec 2015 14:48:48 +0000
parents da368f23bcd3
children 4772247426f8
rev   line source
n@281 1 /**
n@281 2 * mushra.js
n@281 3 * Create the MUSHRA interface
n@281 4 */
n@281 5
n@281 6 // Once this is loaded and parsed, begin execution
n@281 7 loadInterface();
n@281 8
n@281 9 function loadInterface() {
n@281 10 // Get the dimensions of the screen available to the page
n@281 11 var width = window.innerWidth;
n@281 12 var height = window.innerHeight;
n@281 13
n@281 14 // The injection point into the HTML page
n@281 15 interfaceContext.insertPoint = document.getElementById("topLevelBody");
n@281 16 var testContent = document.createElement('div');
n@281 17 testContent.id = 'testContent';
n@281 18
n@281 19 // Create the top div for the Title element
n@281 20 var titleAttr = specification.title;
n@281 21 var title = document.createElement('div');
n@281 22 title.className = "title";
n@281 23 title.align = "center";
n@281 24 var titleSpan = document.createElement('span');
n@281 25
n@281 26 // Set title to that defined in XML, else set to default
n@281 27 if (titleAttr != undefined) {
n@281 28 titleSpan.textContent = titleAttr;
n@281 29 } else {
n@281 30 titleSpan.textContent = 'Listening test';
n@281 31 }
n@281 32 // Insert the titleSpan element into the title div element.
n@281 33 title.appendChild(titleSpan);
n@281 34
n@281 35 var pagetitle = document.createElement('div');
n@281 36 pagetitle.className = "pageTitle";
n@281 37 pagetitle.align = "center";
n@281 38 var titleSpan = document.createElement('span');
n@281 39 titleSpan.id = "pageTitle";
n@281 40 pagetitle.appendChild(titleSpan);
n@281 41
n@281 42 // Create Interface buttons!
n@281 43 var interfaceButtons = document.createElement('div');
n@281 44 interfaceButtons.id = 'interface-buttons';
n@281 45
n@281 46 // Create playback start/stop points
n@281 47 var playback = document.createElement("button");
n@281 48 playback.innerHTML = 'Stop';
n@281 49 playback.id = 'playback-button';
nicholas@437 50 playback.style.float = 'left';
n@281 51 // onclick function. Check if it is playing or not, call the correct function in the
n@281 52 // audioEngine, change the button text to reflect the next state.
n@281 53 playback.onclick = function() {
n@281 54 if (audioEngineContext.status == 1) {
n@281 55 audioEngineContext.stop();
n@281 56 this.innerHTML = 'Stop';
n@281 57 var time = audioEngineContext.timer.getTestTime();
n@281 58 console.log('Stopped at ' + time); // DEBUG/SAFETY
n@281 59 }
n@281 60 };
n@281 61 // Create Submit (save) button
n@281 62 var submit = document.createElement("button");
n@281 63 submit.innerHTML = 'Submit';
n@281 64 submit.onclick = buttonSubmitClick;
n@281 65 submit.id = 'submit-button';
nicholas@437 66 submit.style.float = 'left';
n@281 67 // Append the interface buttons into the interfaceButtons object.
n@281 68 interfaceButtons.appendChild(playback);
n@281 69 interfaceButtons.appendChild(submit);
n@281 70
n@281 71 // Create a slider box
n@281 72 var sliderBox = document.createElement('div');
n@281 73 sliderBox.style.width = "100%";
n@425 74 sliderBox.style.height = window.innerHeight - 200+12 + 'px';
n@425 75 sliderBox.style.marginBottom = '10px';
n@281 76 sliderBox.id = 'slider';
n@425 77 var scaleHolder = document.createElement('div');
n@425 78 scaleHolder.id = "scale-holder";
n@425 79 sliderBox.appendChild(scaleHolder);
n@425 80 var sliderObjectHolder = document.createElement('div');
n@425 81 sliderObjectHolder.id = 'slider-holder';
n@425 82 sliderObjectHolder.align = "center";
n@425 83 sliderBox.appendChild(sliderObjectHolder);
n@281 84
n@281 85 // Global parent for the comment boxes on the page
n@281 86 var feedbackHolder = document.createElement('div');
n@281 87 feedbackHolder.id = 'feedbackHolder';
n@281 88
n@281 89 testContent.style.zIndex = 1;
n@281 90 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
n@281 91
n@281 92 // Inject into HTML
n@281 93 testContent.appendChild(title); // Insert the title
n@281 94 testContent.appendChild(pagetitle);
n@281 95 testContent.appendChild(interfaceButtons);
n@281 96 testContent.appendChild(sliderBox);
n@281 97 testContent.appendChild(feedbackHolder);
n@281 98 interfaceContext.insertPoint.appendChild(testContent);
n@281 99
n@281 100 // Load the full interface
n@281 101 testState.initialise();
n@281 102 testState.advanceState();
n@281 103 }
n@281 104
n@281 105 function loadTest(audioHolderObject)
n@281 106 {
n@281 107 var id = audioHolderObject.id;
n@281 108
n@281 109 var feedbackHolder = document.getElementById('feedbackHolder');
n@281 110 var interfaceObj = audioHolderObject.interfaces;
nicholas@415 111 if (interfaceObj.length > 1)
nicholas@415 112 {
nicholas@415 113 console.log("WARNING - This interface only supports one <interface> node per page. Using first interface node");
nicholas@415 114 }
n@424 115 interfaceObj = interfaceObj[0];
n@424 116 if(interfaceObj.title != null)
n@424 117 {
n@424 118 document.getElementById("pageTitle").textContent = interfaceObj.title;
n@424 119 }
n@281 120
nicholas@437 121 // Delete outside reference
nicholas@437 122 var outsideReferenceHolder = document.getElementById('outside-reference');
nicholas@437 123 if (outsideReferenceHolder != null) {
nicholas@437 124 document.getElementById('interface-buttons').removeChild(outsideReferenceHolder);
nicholas@437 125 }
nicholas@437 126
n@425 127 var sliderBox = document.getElementById('slider-holder');
n@281 128 feedbackHolder.innerHTML = null;
n@281 129 sliderBox.innerHTML = null;
n@281 130
n@281 131 var commentBoxPrefix = "Comment on track";
n@281 132 if (interfaceObj.commentBoxPrefix != undefined) {
n@281 133 commentBoxPrefix = interfaceObj.commentBoxPrefix;
n@281 134 }
n@281 135 var loopPlayback = audioHolderObject.loop;
n@281 136
n@281 137 currentTestHolder = document.createElement('audioHolder');
n@281 138 currentTestHolder.id = audioHolderObject.id;
n@281 139 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
n@281 140
n@281 141 $(audioHolderObject.commentQuestions).each(function(index,element) {
n@281 142 var node = interfaceContext.createCommentQuestion(element);
n@281 143 feedbackHolder.appendChild(node.holder);
n@281 144 });
n@281 145
n@281 146 // Find all the audioElements from the audioHolder
n@281 147 $(audioHolderObject.audioElements).each(function(index,element){
n@281 148 // Find URL of track
n@281 149 // In this jQuery loop, variable 'this' holds the current audioElement.
n@281 150
nicholas@437 151 // Check if an outside reference
nicholas@437 152 if (index == audioHolderObject.outsideReference)
nicholas@437 153 {
nicholas@437 154 return;
nicholas@437 155 }
nicholas@437 156
n@281 157 var audioObject = audioEngineContext.newTrack(element);
n@281 158
n@281 159 var node = interfaceContext.createCommentBox(audioObject);
n@281 160
n@281 161 // Create a slider per track
n@281 162 audioObject.interfaceDOM = new sliderObject(audioObject);
n@281 163
nicholas@417 164 if (typeof audioHolderObject.initialPosition === "number")
nicholas@417 165 {
nicholas@417 166 // Set the values
nicholas@417 167 audioObject.interfaceDOM.slider.value = audioHolderObject.initalPosition;
nicholas@417 168 } else {
nicholas@417 169 // Distribute it randomnly
nicholas@417 170 audioObject.interfaceDOM.slider.value = Math.random();
nicholas@417 171 }
n@281 172
n@281 173 sliderBox.appendChild(audioObject.interfaceDOM.holder);
n@281 174 audioObject.metric.initialised(audioObject.interfaceDOM.slider.value);
n@281 175
n@281 176 });
n@281 177
n@281 178 // Auto-align
n@281 179 var numObj = audioHolderObject.audioElements.length;
n@281 180 var totalWidth = (numObj-1)*150+100;
n@281 181 var diff = (window.innerWidth - totalWidth)/2;
n@425 182 sliderBox.style.marginLeft = diff + 'px';
nicholas@437 183
nicholas@437 184 // Construct outside reference;
nicholas@437 185 if (audioHolderObject.outsideReference != null) {
nicholas@437 186 var outsideReferenceHolder = document.createElement('div');
nicholas@437 187 outsideReferenceHolder.id = 'outside-reference';
nicholas@437 188 outsideReferenceHolder.className = 'outside-reference';
nicholas@437 189 outsideReferenceHolderspan = document.createElement('span');
nicholas@437 190 outsideReferenceHolderspan.textContent = 'Reference';
nicholas@437 191 outsideReferenceHolder.appendChild(outsideReferenceHolderspan);
nicholas@437 192
nicholas@437 193 var audioObject = audioEngineContext.newTrack(audioHolderObject.audioElements[audioHolderObject.outsideReference]);
nicholas@437 194
nicholas@437 195 outsideReferenceHolder.onclick = function(event)
nicholas@437 196 {
nicholas@437 197 audioEngineContext.play(audioEngineContext.audioObjects.length-1);
nicholas@437 198 $('.track-slider').removeClass('track-slider-playing');
nicholas@437 199 $('.comment-div').removeClass('comment-box-playing');
nicholas@437 200 if (event.currentTarget.nodeName == 'DIV') {
nicholas@437 201 $(event.currentTarget).addClass('track-slider-playing');
nicholas@437 202 } else {
nicholas@437 203 $(event.currentTarget.parentElement).addClass('track-slider-playing');
nicholas@437 204 }
nicholas@437 205 };
nicholas@437 206
nicholas@437 207 document.getElementById('interface-buttons').appendChild(outsideReferenceHolder);
nicholas@437 208 }
n@281 209 }
n@281 210
n@281 211 function sliderObject(audioObject)
n@281 212 {
n@281 213 // Constructs the slider object. We use the HTML5 slider object
n@281 214 this.parent = audioObject;
n@281 215 this.holder = document.createElement('div');
n@281 216 this.title = document.createElement('span');
n@281 217 this.slider = document.createElement('input');
n@281 218 this.play = document.createElement('button');
n@281 219
n@281 220 this.holder.className = 'track-slider';
n@281 221 this.holder.style.height = window.innerHeight-200 + 'px';
n@281 222 this.holder.appendChild(this.title);
n@281 223 this.holder.appendChild(this.slider);
n@281 224 this.holder.appendChild(this.play);
n@281 225 this.holder.align = "center";
n@425 226 if (audioObject.id == 0)
n@425 227 {
n@425 228 this.holder.style.marginLeft = '0px';
n@425 229 }
n@281 230 this.holder.setAttribute('trackIndex',audioObject.id);
n@281 231
n@281 232 this.title.textContent = audioObject.id;
n@281 233 this.title.style.width = "100%";
n@281 234 this.title.style.float = "left";
n@281 235
n@281 236 this.slider.type = "range";
n@423 237 this.slider.className = "track-slider-range track-slider-not-moved";
n@281 238 this.slider.min = "0";
n@281 239 this.slider.max = "1";
n@281 240 this.slider.step = "0.01";
n@281 241 this.slider.setAttribute('orient','vertical');
n@281 242 this.slider.style.height = window.innerHeight-250 + 'px';
n@281 243 this.slider.onchange = function()
n@281 244 {
n@281 245 var time = audioEngineContext.timer.getTestTime();
n@281 246 var id = Number(this.parentNode.getAttribute('trackIndex'));
n@281 247 audioEngineContext.audioObjects[id].metric.moved(time,this.value);
n@281 248 console.log('slider '+id+' moved to '+this.value+' ('+time+')');
n@423 249 $(this).removeClass('track-slider-not-moved');
n@281 250 };
n@281 251
nicholas@418 252 this.play.textContent = "Loading...";
n@281 253 this.play.value = audioObject.id;
n@281 254 this.play.style.float = "left";
n@281 255 this.play.style.width = "100%";
nicholas@415 256 this.play.disabled = true;
nicholas@415 257 this.play.onclick = function(event)
n@281 258 {
n@423 259 var id = Number(event.currentTarget.value);
nicholas@415 260 //audioEngineContext.metric.sliderPlayed(id);
nicholas@415 261 audioEngineContext.play(id);
nicholas@415 262 $(".track-slider").removeClass('track-slider-playing');
nicholas@415 263 $(event.currentTarget.parentElement).addClass('track-slider-playing');
nicholas@437 264 var outsideReference = document.getElementById('outside-reference');
nicholas@437 265 if (outsideReference != null) {
nicholas@437 266 $(outsideReference).removeClass('track-slider-playing');
nicholas@437 267 }
n@281 268 };
n@281 269
n@281 270 this.enable = function() {
nicholas@415 271 this.play.disabled = false;
nicholas@418 272 this.play.textContent = "Play";
nicholas@415 273 $(this.slider).removeClass('track-slider-disabled');
n@281 274 };
n@281 275
n@281 276 this.exportXMLDOM = function(audioObject) {
n@281 277 // Called by the audioObject holding this element. Must be present
n@281 278 var node = document.createElement('value');
n@281 279 node.textContent = this.slider.value;
n@281 280 return node;
n@281 281 };
n@281 282 this.getValue = function() {
n@281 283 return this.slider.value;
n@281 284 };
nicholas@415 285
nicholas@416 286 this.resize = function(event)
nicholas@416 287 {
nicholas@416 288 this.holder.style.height = window.innerHeight-200 + 'px';
nicholas@416 289 this.slider.style.height = window.innerHeight-250 + 'px';
n@423 290 };
nicholas@418 291 this.updateLoading = function(progress)
nicholas@418 292 {
nicholas@418 293 progress = String(progress);
nicholas@418 294 progress = progress.substr(0,5);
nicholas@418 295 this.play.textContent = "Loading: "+progress+"%";
n@423 296 };
nicholas@416 297
nicholas@415 298 if (this.parent.state == 1)
nicholas@415 299 {
nicholas@415 300 this.enable();
nicholas@415 301 }
n@281 302 }
n@281 303
nicholas@416 304 function resizeWindow(event)
nicholas@416 305 {
nicholas@416 306 // Function called when the window has been resized.
nicholas@416 307 // MANDATORY FUNCTION
nicholas@416 308
nicholas@416 309 // Auto-align
nicholas@416 310 var numObj = audioEngineContext.audioObjects.length;
nicholas@416 311 var totalWidth = (numObj-1)*150+100;
nicholas@416 312 var diff = (window.innerWidth - totalWidth)/2;
nicholas@416 313 document.getElementById('slider').style.height = window.innerHeight - 180 + 'px';
n@425 314 if (diff <= 0){diff = 0;}
n@425 315 document.getElementById('slider-holder').style.marginLeft = diff + 'px';
nicholas@416 316 for (var i in audioEngineContext.audioObjects)
nicholas@416 317 {
nicholas@416 318 audioEngineContext.audioObjects[i].interfaceDOM.resize(event);
nicholas@416 319 }
nicholas@416 320 }
nicholas@416 321
n@281 322
n@281 323 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@281 324 {
n@281 325 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
n@281 326 var canContinue = true;
n@281 327
n@281 328 // Check that the anchor and reference objects are correctly placed
n@281 329 if (interfaceContext.checkHiddenAnchor() == false) {return;}
n@281 330 if (interfaceContext.checkHiddenReference() == false) {return;}
nicholas@421 331
n@281 332 for (var i=0; i<checks.length; i++) {
n@281 333 if (checks[i].type == 'check')
n@281 334 {
n@281 335 switch(checks[i].check) {
n@281 336 case 'fragmentPlayed':
n@281 337 // Check if all fragments have been played
n@281 338 var checkState = interfaceContext.checkAllPlayed();
n@281 339 if (checkState == false) {canContinue = false;}
n@281 340 break;
n@281 341 case 'fragmentFullPlayback':
n@281 342 // Check all fragments have been played to their full length
n@281 343 var checkState = interfaceContext.checkAllPlayed();
n@281 344 if (checkState == false) {canContinue = false;}
n@281 345 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
n@281 346 break;
n@281 347 case 'fragmentMoved':
n@281 348 // Check all fragment sliders have been moved.
n@281 349 var checkState = interfaceContext.checkAllMoved();
n@281 350 if (checkState == false) {canContinue = false;}
n@281 351 break;
n@281 352 case 'fragmentComments':
n@281 353 // Check all fragment sliders have been moved.
n@281 354 var checkState = interfaceContext.checkAllCommented();
n@281 355 if (checkState == false) {canContinue = false;}
n@281 356 break;
nicholas@421 357 //case 'scalerange':
n@281 358 // Check the scale is used to its full width outlined by the node
nicholas@421 359 //var checkState = interfaceContext.checkScaleRange();
nicholas@421 360 //if (checkState == false) {canContinue = false;}
nicholas@421 361 // break;
nicholas@421 362 default:
nicholas@421 363 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
n@281 364 break;
n@281 365 }
n@281 366
n@281 367 }
n@281 368 if (!canContinue) {break;}
n@281 369 }
nicholas@421 370
n@281 371 if (canContinue) {
n@281 372 if (audioEngineContext.status == 1) {
n@281 373 var playback = document.getElementById('playback-button');
n@281 374 playback.click();
n@281 375 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
n@281 376 } else
n@281 377 {
n@281 378 if (audioEngineContext.timer.testStarted == false)
n@281 379 {
n@281 380 alert('You have not started the test! Please press start to begin the test!');
n@281 381 return;
n@281 382 }
n@281 383 }
n@281 384 testState.advanceState();
n@281 385 }
n@281 386 }
n@281 387
n@281 388 function pageXMLSave(store, testXML)
n@281 389 {
n@381 390 // MANDATORY
n@281 391 // Saves a specific test page
n@381 392 // You can use this space to add any extra nodes to your XML saves
n@281 393 }