annotate mushra.js @ 1186:fd5390509d52

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