annotate mushra.js @ 767:d7f2912bf487

test_create: Specification Node handles complete XML to DOM and DOM to XML conversions
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Fri, 04 Dec 2015 18:34:04 +0000
parents
children 81246d594793
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 // Reset audioEngineContext.Metric globals for new test
n@767 99 audioEngineContext.newTestPage();
n@767 100
n@767 101 // Delete any previous audioObjects associated with the audioEngine
n@767 102 audioEngineContext.audioObjects = [];
n@767 103 interfaceContext.deleteCommentBoxes();
n@767 104 interfaceContext.deleteCommentQuestions();
n@767 105
n@767 106 var id = audioHolderObject.id;
n@767 107
n@767 108 var feedbackHolder = document.getElementById('feedbackHolder');
n@767 109 var interfaceObj = audioHolderObject.interfaces;
n@767 110
n@767 111 var sliderBox = document.getElementById('slider');
n@767 112 feedbackHolder.innerHTML = null;
n@767 113 sliderBox.innerHTML = null;
n@767 114
n@767 115 var commentBoxPrefix = "Comment on track";
n@767 116 if (interfaceObj.commentBoxPrefix != undefined) {
n@767 117 commentBoxPrefix = interfaceObj.commentBoxPrefix;
n@767 118 }
n@767 119
n@767 120 /// CHECK FOR SAMPLE RATE COMPATIBILITY
n@767 121 if (audioHolderObject.sampleRate != undefined) {
n@767 122 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
n@767 123 var errStr = 'Sample rates do not match! Requested '+Number(audioHolderObject.sampleRate)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
n@767 124 alert(errStr);
n@767 125 return;
n@767 126 }
n@767 127 }
n@767 128
n@767 129 var loopPlayback = audioHolderObject.loop;
n@767 130
n@767 131 audioEngineContext.loopPlayback = loopPlayback;
n@767 132
n@767 133 currentTestHolder = document.createElement('audioHolder');
n@767 134 currentTestHolder.id = audioHolderObject.id;
n@767 135 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
n@767 136
n@767 137 $(audioHolderObject.commentQuestions).each(function(index,element) {
n@767 138 var node = interfaceContext.createCommentQuestion(element);
n@767 139 feedbackHolder.appendChild(node.holder);
n@767 140 });
n@767 141
n@767 142 // Find all the audioElements from the audioHolder
n@767 143 $(audioHolderObject.audioElements).each(function(index,element){
n@767 144 // Find URL of track
n@767 145 // In this jQuery loop, variable 'this' holds the current audioElement.
n@767 146
n@767 147 // Now load each audio sample. First create the new track by passing the full URL
n@767 148 var trackURL = audioHolderObject.hostURL + element.url;
n@767 149 var audioObject = audioEngineContext.newTrack(element);
n@767 150
n@767 151 var node = interfaceContext.createCommentBox(audioObject);
n@767 152
n@767 153 // Create a slider per track
n@767 154 audioObject.interfaceDOM = new sliderObject(audioObject);
n@767 155
n@767 156 // Distribute it randomnly
n@767 157 audioObject.interfaceDOM.slider.value = Math.random();
n@767 158
n@767 159 sliderBox.appendChild(audioObject.interfaceDOM.holder);
n@767 160 audioObject.metric.initialised(audioObject.interfaceDOM.slider.value);
n@767 161
n@767 162 });
n@767 163
n@767 164 // Auto-align
n@767 165 var numObj = audioHolderObject.audioElements.length;
n@767 166 var totalWidth = (numObj-1)*150+100;
n@767 167 var diff = (window.innerWidth - totalWidth)/2;
n@767 168 audioEngineContext.audioObjects[0].interfaceDOM.holder.style.marginLeft = diff + 'px';
n@767 169 }
n@767 170
n@767 171 function sliderObject(audioObject)
n@767 172 {
n@767 173 // Constructs the slider object. We use the HTML5 slider object
n@767 174 this.parent = audioObject;
n@767 175 this.holder = document.createElement('div');
n@767 176 this.title = document.createElement('span');
n@767 177 this.slider = document.createElement('input');
n@767 178 this.play = document.createElement('button');
n@767 179
n@767 180 this.holder.className = 'track-slider';
n@767 181 this.holder.style.height = window.innerHeight-200 + 'px';
n@767 182 this.holder.appendChild(this.title);
n@767 183 this.holder.appendChild(this.slider);
n@767 184 this.holder.appendChild(this.play);
n@767 185 this.holder.align = "center";
n@767 186 this.holder.style.marginLeft = "50px";
n@767 187 this.holder.setAttribute('trackIndex',audioObject.id);
n@767 188
n@767 189 this.title.textContent = audioObject.id;
n@767 190 this.title.style.width = "100%";
n@767 191 this.title.style.float = "left";
n@767 192
n@767 193 this.slider.type = "range";
n@767 194 this.slider.min = "0";
n@767 195 this.slider.max = "1";
n@767 196 this.slider.step = "0.01";
n@767 197 this.slider.setAttribute('orient','vertical');
n@767 198 this.slider.style.float = "left";
n@767 199 this.slider.style.width = "100%";
n@767 200 this.slider.style.height = window.innerHeight-250 + 'px';
n@767 201 this.slider.onchange = function()
n@767 202 {
n@767 203 var time = audioEngineContext.timer.getTestTime();
n@767 204 var id = Number(this.parentNode.getAttribute('trackIndex'));
n@767 205 audioEngineContext.audioObjects[id].metric.moved(time,this.value);
n@767 206 console.log('slider '+id+' moved to '+this.value+' ('+time+')');
n@767 207 };
n@767 208
n@767 209 this.play.textContent = "Play";
n@767 210 this.play.value = audioObject.id;
n@767 211 this.play.style.float = "left";
n@767 212 this.play.style.width = "100%";
n@767 213 this.play.onclick = function()
n@767 214 {
n@767 215 audioEngineContext.play();
n@767 216 if (audioEngineContext.audioObjectsReady) {
n@767 217 var id = Number(event.srcElement.value);
n@767 218 //audioEngineContext.metric.sliderPlayed(id);
n@767 219 audioEngineContext.play(id);
n@767 220 }
n@767 221 };
n@767 222
n@767 223 this.enable = function() {
n@767 224 if (this.parent.state == 1)
n@767 225 {
n@767 226 $(this.slider).removeClass('track-slider-disabled');
n@767 227 }
n@767 228 };
n@767 229
n@767 230 this.exportXMLDOM = function(audioObject) {
n@767 231 // Called by the audioObject holding this element. Must be present
n@767 232 var node = document.createElement('value');
n@767 233 node.textContent = this.slider.value;
n@767 234 return node;
n@767 235 };
n@767 236 this.getValue = function() {
n@767 237 return this.slider.value;
n@767 238 };
n@767 239 }
n@767 240
n@767 241
n@767 242 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@767 243 {
n@767 244 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
n@767 245 var canContinue = true;
n@767 246
n@767 247 // Check that the anchor and reference objects are correctly placed
n@767 248 if (interfaceContext.checkHiddenAnchor() == false) {return;}
n@767 249 if (interfaceContext.checkHiddenReference() == false) {return;}
n@767 250 /*
n@767 251 for (var i=0; i<checks.length; i++) {
n@767 252 if (checks[i].type == 'check')
n@767 253 {
n@767 254 switch(checks[i].check) {
n@767 255 case 'fragmentPlayed':
n@767 256 // Check if all fragments have been played
n@767 257 var checkState = interfaceContext.checkAllPlayed();
n@767 258 if (checkState == false) {canContinue = false;}
n@767 259 break;
n@767 260 case 'fragmentFullPlayback':
n@767 261 // Check all fragments have been played to their full length
n@767 262 var checkState = interfaceContext.checkAllPlayed();
n@767 263 if (checkState == false) {canContinue = false;}
n@767 264 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
n@767 265 break;
n@767 266 case 'fragmentMoved':
n@767 267 // Check all fragment sliders have been moved.
n@767 268 var checkState = interfaceContext.checkAllMoved();
n@767 269 if (checkState == false) {canContinue = false;}
n@767 270 break;
n@767 271 case 'fragmentComments':
n@767 272 // Check all fragment sliders have been moved.
n@767 273 var checkState = interfaceContext.checkAllCommented();
n@767 274 if (checkState == false) {canContinue = false;}
n@767 275 break;
n@767 276 case 'scalerange':
n@767 277 // Check the scale is used to its full width outlined by the node
n@767 278 var checkState = interfaceContext.checkScaleRange();
n@767 279 if (checkState == false) {canContinue = false;}
n@767 280 break;
n@767 281 }
n@767 282
n@767 283 }
n@767 284 if (!canContinue) {break;}
n@767 285 }
n@767 286 */
n@767 287 if (canContinue) {
n@767 288 if (audioEngineContext.status == 1) {
n@767 289 var playback = document.getElementById('playback-button');
n@767 290 playback.click();
n@767 291 // 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 292 } else
n@767 293 {
n@767 294 if (audioEngineContext.timer.testStarted == false)
n@767 295 {
n@767 296 alert('You have not started the test! Please press start to begin the test!');
n@767 297 return;
n@767 298 }
n@767 299 }
n@767 300 testState.advanceState();
n@767 301 }
n@767 302 }
n@767 303
n@767 304 function pageXMLSave(store, testXML)
n@767 305 {
n@767 306 // Saves a specific test page
n@767 307 var xmlDoc = store;
n@767 308 // Check if any session wide metrics are enabled
n@767 309
n@767 310 var commentShow = testXML.elementComments;
n@767 311
n@767 312 var metric = document.createElement('metric');
n@767 313 if (audioEngineContext.metric.enableTestTimer)
n@767 314 {
n@767 315 var testTime = document.createElement('metricResult');
n@767 316 testTime.id = 'testTime';
n@767 317 testTime.textContent = audioEngineContext.timer.testDuration;
n@767 318 metric.appendChild(testTime);
n@767 319 }
n@767 320 xmlDoc.appendChild(metric);
n@767 321 var audioObjects = audioEngineContext.audioObjects;
n@767 322 for (var i=0; i<audioObjects.length; i++)
n@767 323 {
n@767 324 var audioElement = audioEngineContext.audioObjects[i].exportXMLDOM();
n@767 325 audioElement.setAttribute('presentedId',i);
n@767 326 xmlDoc.appendChild(audioElement);
n@767 327 }
n@767 328
n@767 329 $(interfaceContext.commentQuestions).each(function(index,element){
n@767 330 var node = element.exportXMLDOM();
n@767 331 xmlDoc.appendChild(node);
n@767 332 });
n@767 333 store = xmlDoc;
n@767 334 }