annotate mushra.js @ 417:e2543b2ea92c Dev_main

MUSHRA: Set the initial position of sliders using the <audioHolder> attribute initial-position using integer 0 to 100
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Thu, 17 Dec 2015 16:10:58 +0000
parents 7ea59d3594e0
children c22ce754b426
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";
nicholas@415 184 this.slider.className = "track-slider-range";
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@281 196 };
n@281 197
n@281 198 this.play.textContent = "Play";
n@281 199 this.play.value = audioObject.id;
n@281 200 this.play.style.float = "left";
n@281 201 this.play.style.width = "100%";
nicholas@415 202 this.play.disabled = true;
nicholas@415 203 this.play.onclick = function(event)
n@281 204 {
nicholas@415 205 var id = Number(event.srcElement.value);
nicholas@415 206 //audioEngineContext.metric.sliderPlayed(id);
nicholas@415 207 audioEngineContext.play(id);
nicholas@415 208 $(".track-slider").removeClass('track-slider-playing');
nicholas@415 209 $(event.currentTarget.parentElement).addClass('track-slider-playing');
n@281 210 };
n@281 211
n@281 212 this.enable = function() {
nicholas@415 213 this.play.disabled = false;
nicholas@415 214 $(this.slider).removeClass('track-slider-disabled');
n@281 215 };
n@281 216
n@281 217 this.exportXMLDOM = function(audioObject) {
n@281 218 // Called by the audioObject holding this element. Must be present
n@281 219 var node = document.createElement('value');
n@281 220 node.textContent = this.slider.value;
n@281 221 return node;
n@281 222 };
n@281 223 this.getValue = function() {
n@281 224 return this.slider.value;
n@281 225 };
nicholas@415 226
nicholas@416 227 this.resize = function(event)
nicholas@416 228 {
nicholas@416 229 this.holder.style.height = window.innerHeight-200 + 'px';
nicholas@416 230 this.slider.style.height = window.innerHeight-250 + 'px';
nicholas@416 231 }
nicholas@416 232
nicholas@415 233 if (this.parent.state == 1)
nicholas@415 234 {
nicholas@415 235 this.enable();
nicholas@415 236 }
n@281 237 }
n@281 238
nicholas@416 239 function resizeWindow(event)
nicholas@416 240 {
nicholas@416 241 // Function called when the window has been resized.
nicholas@416 242 // MANDATORY FUNCTION
nicholas@416 243
nicholas@416 244 // Auto-align
nicholas@416 245 var numObj = audioEngineContext.audioObjects.length;
nicholas@416 246 var totalWidth = (numObj-1)*150+100;
nicholas@416 247 var diff = (window.innerWidth - totalWidth)/2;
nicholas@416 248 document.getElementById('slider').style.height = window.innerHeight - 180 + 'px';
nicholas@416 249 audioEngineContext.audioObjects[0].interfaceDOM.holder.style.marginLeft = diff + 'px';
nicholas@416 250 for (var i in audioEngineContext.audioObjects)
nicholas@416 251 {
nicholas@416 252 audioEngineContext.audioObjects[i].interfaceDOM.resize(event);
nicholas@416 253 }
nicholas@416 254 }
nicholas@416 255
n@281 256
n@281 257 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@281 258 {
n@281 259 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
n@281 260 var canContinue = true;
n@281 261
n@281 262 // Check that the anchor and reference objects are correctly placed
n@281 263 if (interfaceContext.checkHiddenAnchor() == false) {return;}
n@281 264 if (interfaceContext.checkHiddenReference() == false) {return;}
n@281 265 /*
n@281 266 for (var i=0; i<checks.length; i++) {
n@281 267 if (checks[i].type == 'check')
n@281 268 {
n@281 269 switch(checks[i].check) {
n@281 270 case 'fragmentPlayed':
n@281 271 // Check if all fragments have been played
n@281 272 var checkState = interfaceContext.checkAllPlayed();
n@281 273 if (checkState == false) {canContinue = false;}
n@281 274 break;
n@281 275 case 'fragmentFullPlayback':
n@281 276 // Check all fragments have been played to their full length
n@281 277 var checkState = interfaceContext.checkAllPlayed();
n@281 278 if (checkState == false) {canContinue = false;}
n@281 279 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
n@281 280 break;
n@281 281 case 'fragmentMoved':
n@281 282 // Check all fragment sliders have been moved.
n@281 283 var checkState = interfaceContext.checkAllMoved();
n@281 284 if (checkState == false) {canContinue = false;}
n@281 285 break;
n@281 286 case 'fragmentComments':
n@281 287 // Check all fragment sliders have been moved.
n@281 288 var checkState = interfaceContext.checkAllCommented();
n@281 289 if (checkState == false) {canContinue = false;}
n@281 290 break;
n@281 291 case 'scalerange':
n@281 292 // Check the scale is used to its full width outlined by the node
n@281 293 var checkState = interfaceContext.checkScaleRange();
n@281 294 if (checkState == false) {canContinue = false;}
n@281 295 break;
n@281 296 }
n@281 297
n@281 298 }
n@281 299 if (!canContinue) {break;}
n@281 300 }
n@281 301 */
n@281 302 if (canContinue) {
n@281 303 if (audioEngineContext.status == 1) {
n@281 304 var playback = document.getElementById('playback-button');
n@281 305 playback.click();
n@281 306 // 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 307 } else
n@281 308 {
n@281 309 if (audioEngineContext.timer.testStarted == false)
n@281 310 {
n@281 311 alert('You have not started the test! Please press start to begin the test!');
n@281 312 return;
n@281 313 }
n@281 314 }
n@281 315 testState.advanceState();
n@281 316 }
n@281 317 }
n@281 318
n@281 319 function pageXMLSave(store, testXML)
n@281 320 {
n@381 321 // MANDATORY
n@281 322 // Saves a specific test page
n@381 323 // You can use this space to add any extra nodes to your XML saves
n@281 324 }