annotate mushra.js @ 775:bb02c801c8c9

Bugs #1454 #1391 buffers are pooled and loaded at the begining. Page Save not interface specific.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Mon, 07 Dec 2015 18:42:36 +0000
parents d2a83474a967
children 6e01da7f9f92 3123565e0c49
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;
n@767 102
n@767 103 var sliderBox = document.getElementById('slider');
n@767 104 feedbackHolder.innerHTML = null;
n@767 105 sliderBox.innerHTML = null;
n@767 106
n@767 107 var commentBoxPrefix = "Comment on track";
n@767 108 if (interfaceObj.commentBoxPrefix != undefined) {
n@767 109 commentBoxPrefix = interfaceObj.commentBoxPrefix;
n@767 110 }
n@767 111 var loopPlayback = audioHolderObject.loop;
n@767 112
n@767 113 currentTestHolder = document.createElement('audioHolder');
n@767 114 currentTestHolder.id = audioHolderObject.id;
n@767 115 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
n@767 116
n@767 117 $(audioHolderObject.commentQuestions).each(function(index,element) {
n@767 118 var node = interfaceContext.createCommentQuestion(element);
n@767 119 feedbackHolder.appendChild(node.holder);
n@767 120 });
n@767 121
n@767 122 // Find all the audioElements from the audioHolder
n@767 123 $(audioHolderObject.audioElements).each(function(index,element){
n@767 124 // Find URL of track
n@767 125 // In this jQuery loop, variable 'this' holds the current audioElement.
n@767 126
n@767 127 // Now load each audio sample. First create the new track by passing the full URL
n@767 128 var trackURL = audioHolderObject.hostURL + element.url;
n@767 129 var audioObject = audioEngineContext.newTrack(element);
n@767 130
n@767 131 var node = interfaceContext.createCommentBox(audioObject);
n@767 132
n@767 133 // Create a slider per track
n@767 134 audioObject.interfaceDOM = new sliderObject(audioObject);
n@767 135
n@767 136 // Distribute it randomnly
n@767 137 audioObject.interfaceDOM.slider.value = Math.random();
n@767 138
n@767 139 sliderBox.appendChild(audioObject.interfaceDOM.holder);
n@767 140 audioObject.metric.initialised(audioObject.interfaceDOM.slider.value);
n@767 141
n@767 142 });
n@767 143
n@767 144 // Auto-align
n@767 145 var numObj = audioHolderObject.audioElements.length;
n@767 146 var totalWidth = (numObj-1)*150+100;
n@767 147 var diff = (window.innerWidth - totalWidth)/2;
n@767 148 audioEngineContext.audioObjects[0].interfaceDOM.holder.style.marginLeft = diff + 'px';
n@767 149 }
n@767 150
n@767 151 function sliderObject(audioObject)
n@767 152 {
n@767 153 // Constructs the slider object. We use the HTML5 slider object
n@767 154 this.parent = audioObject;
n@767 155 this.holder = document.createElement('div');
n@767 156 this.title = document.createElement('span');
n@767 157 this.slider = document.createElement('input');
n@767 158 this.play = document.createElement('button');
n@767 159
n@767 160 this.holder.className = 'track-slider';
n@767 161 this.holder.style.height = window.innerHeight-200 + 'px';
n@767 162 this.holder.appendChild(this.title);
n@767 163 this.holder.appendChild(this.slider);
n@767 164 this.holder.appendChild(this.play);
n@767 165 this.holder.align = "center";
n@767 166 this.holder.style.marginLeft = "50px";
n@767 167 this.holder.setAttribute('trackIndex',audioObject.id);
n@767 168
n@767 169 this.title.textContent = audioObject.id;
n@767 170 this.title.style.width = "100%";
n@767 171 this.title.style.float = "left";
n@767 172
n@767 173 this.slider.type = "range";
n@767 174 this.slider.min = "0";
n@767 175 this.slider.max = "1";
n@767 176 this.slider.step = "0.01";
n@767 177 this.slider.setAttribute('orient','vertical');
n@767 178 this.slider.style.float = "left";
n@767 179 this.slider.style.width = "100%";
n@767 180 this.slider.style.height = window.innerHeight-250 + 'px';
n@767 181 this.slider.onchange = function()
n@767 182 {
n@767 183 var time = audioEngineContext.timer.getTestTime();
n@767 184 var id = Number(this.parentNode.getAttribute('trackIndex'));
n@767 185 audioEngineContext.audioObjects[id].metric.moved(time,this.value);
n@767 186 console.log('slider '+id+' moved to '+this.value+' ('+time+')');
n@767 187 };
n@767 188
n@767 189 this.play.textContent = "Play";
n@767 190 this.play.value = audioObject.id;
n@767 191 this.play.style.float = "left";
n@767 192 this.play.style.width = "100%";
n@767 193 this.play.onclick = function()
n@767 194 {
n@767 195 audioEngineContext.play();
n@767 196 if (audioEngineContext.audioObjectsReady) {
n@767 197 var id = Number(event.srcElement.value);
n@767 198 //audioEngineContext.metric.sliderPlayed(id);
n@767 199 audioEngineContext.play(id);
n@767 200 }
n@767 201 };
n@767 202
n@767 203 this.enable = function() {
n@767 204 if (this.parent.state == 1)
n@767 205 {
n@767 206 $(this.slider).removeClass('track-slider-disabled');
n@767 207 }
n@767 208 };
n@767 209
n@767 210 this.exportXMLDOM = function(audioObject) {
n@767 211 // Called by the audioObject holding this element. Must be present
n@767 212 var node = document.createElement('value');
n@767 213 node.textContent = this.slider.value;
n@767 214 return node;
n@767 215 };
n@767 216 this.getValue = function() {
n@767 217 return this.slider.value;
n@767 218 };
n@767 219 }
n@767 220
n@767 221
n@767 222 function buttonSubmitClick() // TODO: Only when all songs have been played!
n@767 223 {
n@767 224 var checks = testState.currentStateMap[testState.currentIndex].interfaces[0].options;
n@767 225 var canContinue = true;
n@767 226
n@767 227 // Check that the anchor and reference objects are correctly placed
n@767 228 if (interfaceContext.checkHiddenAnchor() == false) {return;}
n@767 229 if (interfaceContext.checkHiddenReference() == false) {return;}
n@767 230 /*
n@767 231 for (var i=0; i<checks.length; i++) {
n@767 232 if (checks[i].type == 'check')
n@767 233 {
n@767 234 switch(checks[i].check) {
n@767 235 case 'fragmentPlayed':
n@767 236 // Check if all fragments have been played
n@767 237 var checkState = interfaceContext.checkAllPlayed();
n@767 238 if (checkState == false) {canContinue = false;}
n@767 239 break;
n@767 240 case 'fragmentFullPlayback':
n@767 241 // Check all fragments have been played to their full length
n@767 242 var checkState = interfaceContext.checkAllPlayed();
n@767 243 if (checkState == false) {canContinue = false;}
n@767 244 console.log('NOTE: fragmentFullPlayback not currently implemented, performing check fragmentPlayed instead');
n@767 245 break;
n@767 246 case 'fragmentMoved':
n@767 247 // Check all fragment sliders have been moved.
n@767 248 var checkState = interfaceContext.checkAllMoved();
n@767 249 if (checkState == false) {canContinue = false;}
n@767 250 break;
n@767 251 case 'fragmentComments':
n@767 252 // Check all fragment sliders have been moved.
n@767 253 var checkState = interfaceContext.checkAllCommented();
n@767 254 if (checkState == false) {canContinue = false;}
n@767 255 break;
n@767 256 case 'scalerange':
n@767 257 // Check the scale is used to its full width outlined by the node
n@767 258 var checkState = interfaceContext.checkScaleRange();
n@767 259 if (checkState == false) {canContinue = false;}
n@767 260 break;
n@767 261 }
n@767 262
n@767 263 }
n@767 264 if (!canContinue) {break;}
n@767 265 }
n@767 266 */
n@767 267 if (canContinue) {
n@767 268 if (audioEngineContext.status == 1) {
n@767 269 var playback = document.getElementById('playback-button');
n@767 270 playback.click();
n@767 271 // 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 272 } else
n@767 273 {
n@767 274 if (audioEngineContext.timer.testStarted == false)
n@767 275 {
n@767 276 alert('You have not started the test! Please press start to begin the test!');
n@767 277 return;
n@767 278 }
n@767 279 }
n@767 280 testState.advanceState();
n@767 281 }
n@767 282 }
n@767 283
n@767 284 function pageXMLSave(store, testXML)
n@767 285 {
n@775 286 // MANDATORY
n@767 287 // Saves a specific test page
n@775 288 // You can use this space to add any extra nodes to your XML saves
n@767 289 }