annotate mushra.js @ 802:a21c0da62b04

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