annotate mushra.js @ 423:c08af4b5c9f3 Dev_main

MUSHRA: Slider styling across browsers so easy to see. Sliders start grey and turn white when moved. Added mushra example test
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 18 Dec 2015 12:04:04 +0000
parents 928fb9adae81
children ff9ab5f21760
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';
n@281 50 // onclick function. Check if it is playing or not, call the correct function in the
n@281 51 // audioEngine, change the button text to reflect the next state.
n@281 52 playback.onclick = function() {
n@281 53 if (audioEngineContext.status == 1) {
n@281 54 audioEngineContext.stop();
n@281 55 this.innerHTML = 'Stop';
n@281 56 var time = audioEngineContext.timer.getTestTime();
n@281 57 console.log('Stopped at ' + time); // DEBUG/SAFETY
n@281 58 }
n@281 59 };
n@281 60 // Create Submit (save) button
n@281 61 var submit = document.createElement("button");
n@281 62 submit.innerHTML = 'Submit';
n@281 63 submit.onclick = buttonSubmitClick;
n@281 64 submit.id = 'submit-button';
n@281 65 // Append the interface buttons into the interfaceButtons object.
n@281 66 interfaceButtons.appendChild(playback);
n@281 67 interfaceButtons.appendChild(submit);
n@281 68
n@281 69 // Create a slider box
n@281 70 var sliderBox = document.createElement('div');
n@281 71 sliderBox.style.width = "100%";
n@281 72 sliderBox.style.height = window.innerHeight - 180 + 'px';
n@281 73 sliderBox.id = 'slider';
n@281 74 sliderBox.align = "center";
n@281 75
n@281 76 // Global parent for the comment boxes on the page
n@281 77 var feedbackHolder = document.createElement('div');
n@281 78 feedbackHolder.id = 'feedbackHolder';
n@281 79
n@281 80 testContent.style.zIndex = 1;
n@281 81 interfaceContext.insertPoint.innerHTML = null; // Clear the current schema
n@281 82
n@281 83 // Inject into HTML
n@281 84 testContent.appendChild(title); // Insert the title
n@281 85 testContent.appendChild(pagetitle);
n@281 86 testContent.appendChild(interfaceButtons);
n@281 87 testContent.appendChild(sliderBox);
n@281 88 testContent.appendChild(feedbackHolder);
n@281 89 interfaceContext.insertPoint.appendChild(testContent);
n@281 90
n@281 91 // Load the full interface
n@281 92 testState.initialise();
n@281 93 testState.advanceState();
n@281 94 }
n@281 95
n@281 96 function loadTest(audioHolderObject)
n@281 97 {
n@281 98 var id = audioHolderObject.id;
n@281 99
n@281 100 var feedbackHolder = document.getElementById('feedbackHolder');
n@281 101 var interfaceObj = audioHolderObject.interfaces;
nicholas@415 102 if (interfaceObj.length > 1)
nicholas@415 103 {
nicholas@415 104 console.log("WARNING - This interface only supports one <interface> node per page. Using first interface node");
nicholas@415 105 }
n@281 106
n@281 107 var sliderBox = document.getElementById('slider');
n@281 108 feedbackHolder.innerHTML = null;
n@281 109 sliderBox.innerHTML = null;
n@281 110
n@281 111 var commentBoxPrefix = "Comment on track";
n@281 112 if (interfaceObj.commentBoxPrefix != undefined) {
n@281 113 commentBoxPrefix = interfaceObj.commentBoxPrefix;
n@281 114 }
n@281 115 var loopPlayback = audioHolderObject.loop;
n@281 116
n@281 117 currentTestHolder = document.createElement('audioHolder');
n@281 118 currentTestHolder.id = audioHolderObject.id;
n@281 119 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
n@281 120
n@281 121 $(audioHolderObject.commentQuestions).each(function(index,element) {
n@281 122 var node = interfaceContext.createCommentQuestion(element);
n@281 123 feedbackHolder.appendChild(node.holder);
n@281 124 });
n@281 125
n@281 126 // Find all the audioElements from the audioHolder
n@281 127 $(audioHolderObject.audioElements).each(function(index,element){
n@281 128 // Find URL of track
n@281 129 // In this jQuery loop, variable 'this' holds the current audioElement.
n@281 130
n@281 131 // Now load each audio sample. First create the new track by passing the full URL
n@281 132 var trackURL = audioHolderObject.hostURL + element.url;
n@281 133 var audioObject = audioEngineContext.newTrack(element);
n@281 134
n@281 135 var node = interfaceContext.createCommentBox(audioObject);
n@281 136
n@281 137 // Create a slider per track
n@281 138 audioObject.interfaceDOM = new sliderObject(audioObject);
n@281 139
nicholas@417 140 if (typeof audioHolderObject.initialPosition === "number")
nicholas@417 141 {
nicholas@417 142 // Set the values
nicholas@417 143 audioObject.interfaceDOM.slider.value = audioHolderObject.initalPosition;
nicholas@417 144 } else {
nicholas@417 145 // Distribute it randomnly
nicholas@417 146 audioObject.interfaceDOM.slider.value = Math.random();
nicholas@417 147 }
n@281 148
n@281 149 sliderBox.appendChild(audioObject.interfaceDOM.holder);
n@281 150 audioObject.metric.initialised(audioObject.interfaceDOM.slider.value);
n@281 151
n@281 152 });
n@281 153
n@281 154 // Auto-align
n@281 155 var numObj = audioHolderObject.audioElements.length;
n@281 156 var totalWidth = (numObj-1)*150+100;
n@281 157 var diff = (window.innerWidth - totalWidth)/2;
n@281 158 audioEngineContext.audioObjects[0].interfaceDOM.holder.style.marginLeft = diff + 'px';
n@281 159 }
n@281 160
n@281 161 function sliderObject(audioObject)
n@281 162 {
n@281 163 // Constructs the slider object. We use the HTML5 slider object
n@281 164 this.parent = audioObject;
n@281 165 this.holder = document.createElement('div');
n@281 166 this.title = document.createElement('span');
n@281 167 this.slider = document.createElement('input');
n@281 168 this.play = document.createElement('button');
n@281 169
n@281 170 this.holder.className = 'track-slider';
n@281 171 this.holder.style.height = window.innerHeight-200 + 'px';
n@281 172 this.holder.appendChild(this.title);
n@281 173 this.holder.appendChild(this.slider);
n@281 174 this.holder.appendChild(this.play);
n@281 175 this.holder.align = "center";
n@281 176 this.holder.style.marginLeft = "50px";
n@281 177 this.holder.setAttribute('trackIndex',audioObject.id);
n@281 178
n@281 179 this.title.textContent = audioObject.id;
n@281 180 this.title.style.width = "100%";
n@281 181 this.title.style.float = "left";
n@281 182
n@281 183 this.slider.type = "range";
n@423 184 this.slider.className = "track-slider-range track-slider-not-moved";
n@281 185 this.slider.min = "0";
n@281 186 this.slider.max = "1";
n@281 187 this.slider.step = "0.01";
n@281 188 this.slider.setAttribute('orient','vertical');
n@281 189 this.slider.style.height = window.innerHeight-250 + 'px';
n@281 190 this.slider.onchange = function()
n@281 191 {
n@281 192 var time = audioEngineContext.timer.getTestTime();
n@281 193 var id = Number(this.parentNode.getAttribute('trackIndex'));
n@281 194 audioEngineContext.audioObjects[id].metric.moved(time,this.value);
n@281 195 console.log('slider '+id+' moved to '+this.value+' ('+time+')');
n@423 196 $(this).removeClass('track-slider-not-moved');
n@281 197 };
n@281 198
nicholas@418 199 this.play.textContent = "Loading...";
n@281 200 this.play.value = audioObject.id;
n@281 201 this.play.style.float = "left";
n@281 202 this.play.style.width = "100%";
nicholas@415 203 this.play.disabled = true;
nicholas@415 204 this.play.onclick = function(event)
n@281 205 {
n@423 206 var id = Number(event.currentTarget.value);
nicholas@415 207 //audioEngineContext.metric.sliderPlayed(id);
nicholas@415 208 audioEngineContext.play(id);
nicholas@415 209 $(".track-slider").removeClass('track-slider-playing');
nicholas@415 210 $(event.currentTarget.parentElement).addClass('track-slider-playing');
n@281 211 };
n@281 212
n@281 213 this.enable = function() {
nicholas@415 214 this.play.disabled = false;
nicholas@418 215 this.play.textContent = "Play";
nicholas@415 216 $(this.slider).removeClass('track-slider-disabled');
n@281 217 };
n@281 218
n@281 219 this.exportXMLDOM = function(audioObject) {
n@281 220 // Called by the audioObject holding this element. Must be present
n@281 221 var node = document.createElement('value');
n@281 222 node.textContent = this.slider.value;
n@281 223 return node;
n@281 224 };
n@281 225 this.getValue = function() {
n@281 226 return this.slider.value;
n@281 227 };
nicholas@415 228
nicholas@416 229 this.resize = function(event)
nicholas@416 230 {
nicholas@416 231 this.holder.style.height = window.innerHeight-200 + 'px';
nicholas@416 232 this.slider.style.height = window.innerHeight-250 + 'px';
n@423 233 };
nicholas@418 234 this.updateLoading = function(progress)
nicholas@418 235 {
nicholas@418 236 progress = String(progress);
nicholas@418 237 progress = progress.substr(0,5);
nicholas@418 238 this.play.textContent = "Loading: "+progress+"%";
n@423 239 };
nicholas@416 240
nicholas@415 241 if (this.parent.state == 1)
nicholas@415 242 {
nicholas@415 243 this.enable();
nicholas@415 244 }
n@281 245 }
n@281 246
nicholas@416 247 function resizeWindow(event)
nicholas@416 248 {
nicholas@416 249 // Function called when the window has been resized.
nicholas@416 250 // MANDATORY FUNCTION
nicholas@416 251
nicholas@416 252 // Auto-align
nicholas@416 253 var numObj = audioEngineContext.audioObjects.length;
nicholas@416 254 var totalWidth = (numObj-1)*150+100;
nicholas@416 255 var diff = (window.innerWidth - totalWidth)/2;
nicholas@416 256 document.getElementById('slider').style.height = window.innerHeight - 180 + 'px';
nicholas@416 257 audioEngineContext.audioObjects[0].interfaceDOM.holder.style.marginLeft = diff + 'px';
nicholas@416 258 for (var i in audioEngineContext.audioObjects)
nicholas@416 259 {
nicholas@416 260 audioEngineContext.audioObjects[i].interfaceDOM.resize(event);
nicholas@416 261 }
nicholas@416 262 }
nicholas@416 263
n@281 264
n@281 265 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@281 266 {
n@281 267 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
n@281 268 var canContinue = true;
n@281 269
n@281 270 // Check that the anchor and reference objects are correctly placed
n@281 271 if (interfaceContext.checkHiddenAnchor() == false) {return;}
n@281 272 if (interfaceContext.checkHiddenReference() == false) {return;}
nicholas@421 273
n@281 274 for (var i=0; i<checks.length; i++) {
n@281 275 if (checks[i].type == 'check')
n@281 276 {
n@281 277 switch(checks[i].check) {
n@281 278 case 'fragmentPlayed':
n@281 279 // Check if all fragments have been played
n@281 280 var checkState = interfaceContext.checkAllPlayed();
n@281 281 if (checkState == false) {canContinue = false;}
n@281 282 break;
n@281 283 case 'fragmentFullPlayback':
n@281 284 // Check all fragments have been played to their full length
n@281 285 var checkState = interfaceContext.checkAllPlayed();
n@281 286 if (checkState == false) {canContinue = false;}
n@281 287 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
n@281 288 break;
n@281 289 case 'fragmentMoved':
n@281 290 // Check all fragment sliders have been moved.
n@281 291 var checkState = interfaceContext.checkAllMoved();
n@281 292 if (checkState == false) {canContinue = false;}
n@281 293 break;
n@281 294 case 'fragmentComments':
n@281 295 // Check all fragment sliders have been moved.
n@281 296 var checkState = interfaceContext.checkAllCommented();
n@281 297 if (checkState == false) {canContinue = false;}
n@281 298 break;
nicholas@421 299 //case 'scalerange':
n@281 300 // Check the scale is used to its full width outlined by the node
nicholas@421 301 //var checkState = interfaceContext.checkScaleRange();
nicholas@421 302 //if (checkState == false) {canContinue = false;}
nicholas@421 303 // break;
nicholas@421 304 default:
nicholas@421 305 console.log("WARNING - Check option "+checks[i].check+" is not supported on this interface");
n@281 306 break;
n@281 307 }
n@281 308
n@281 309 }
n@281 310 if (!canContinue) {break;}
n@281 311 }
nicholas@421 312
n@281 313 if (canContinue) {
n@281 314 if (audioEngineContext.status == 1) {
n@281 315 var playback = document.getElementById('playback-button');
n@281 316 playback.click();
n@281 317 // 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 318 } else
n@281 319 {
n@281 320 if (audioEngineContext.timer.testStarted == false)
n@281 321 {
n@281 322 alert('You have not started the test! Please press start to begin the test!');
n@281 323 return;
n@281 324 }
n@281 325 }
n@281 326 testState.advanceState();
n@281 327 }
n@281 328 }
n@281 329
n@281 330 function pageXMLSave(store, testXML)
n@281 331 {
n@381 332 // MANDATORY
n@281 333 // Saves a specific test page
n@381 334 // You can use this space to add any extra nodes to your XML saves
n@281 335 }