annotate mushra.js @ 425:1ba2d8685c47 Dev_main

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