annotate mushra.js @ 800:62cd4657fc38

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