annotate ape.js @ 1581:284251e3a6a3

Everything tied into Specification object which needs information from specification document.
author Nicholas Jillings <nickjillings@users.noreply.github.com>
date Thu, 04 Jun 2015 15:54:56 +0100
parents 4ffbccf448c2
children b84004661558
rev   line source
nickjillings@1575 1 /**
nickjillings@1575 2 * ape.js
nickjillings@1575 3 * Create the APE interface
nickjillings@1575 4 */
nickjillings@1575 5
nickjillings@1575 6 // preTest - In preTest state
nickjillings@1575 7 // testRun-ID - In test running, test Id number at the end 'testRun-2'
nickjillings@1575 8 // testRunPost-ID - Post test of test ID
nickjillings@1575 9 // testRunPre-ID - Pre-test of test ID
nickjillings@1575 10 // postTest - End of test, final submission!
nickjillings@1575 11
nickjillings@1575 12
nickjillings@1575 13 // Once this is loaded and parsed, begin execution
nickjillings@1581 14 loadInterface();
nickjillings@1575 15
nickjillings@1581 16 function loadInterface() {
nickjillings@1575 17
nickjillings@1575 18 // Get the dimensions of the screen available to the page
nickjillings@1575 19 var width = window.innerWidth;
nickjillings@1575 20 var height = window.innerHeight;
nickjillings@1575 21
nickjillings@1575 22 // The injection point into the HTML page
nickjillings@1575 23 var insertPoint = document.getElementById("topLevelBody");
nickjillings@1575 24 var testContent = document.createElement('div');
nickjillings@1575 25
nickjillings@1575 26 testContent.id = 'testContent';
nickjillings@1576 27
nickjillings@1575 28
nickjillings@1575 29 // Create APE specific metric functions
nickjillings@1575 30 audioEngineContext.metric.initialiseTest = function()
nickjillings@1575 31 {
nickjillings@1575 32 };
nickjillings@1575 33
nickjillings@1575 34 audioEngineContext.metric.sliderMoveStart = function(id)
nickjillings@1575 35 {
nickjillings@1575 36 if (this.data == -1)
nickjillings@1575 37 {
nickjillings@1575 38 this.data = id;
nickjillings@1575 39 } else {
nickjillings@1575 40 console.log('ERROR: Metric tracker detecting two moves!');
nickjillings@1575 41 this.data = -1;
nickjillings@1575 42 }
nickjillings@1575 43 };
nickjillings@1575 44 audioEngineContext.metric.sliderMoved = function()
nickjillings@1575 45 {
nickjillings@1575 46 var time = audioEngineContext.timer.getTestTime();
nickjillings@1575 47 var id = this.data;
nickjillings@1575 48 this.data = -1;
nickjillings@1575 49 var position = convSliderPosToRate(id);
nickjillings@1575 50 console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id
nickjillings@1575 51 if (audioEngineContext.timer.testStarted)
nickjillings@1575 52 {
nickjillings@1575 53 audioEngineContext.audioObjects[id].metric.moved(time,position);
nickjillings@1575 54 }
nickjillings@1575 55 };
nickjillings@1575 56
nickjillings@1575 57 audioEngineContext.metric.sliderPlayed = function(id)
nickjillings@1575 58 {
nickjillings@1575 59 var time = audioEngineContext.timer.getTestTime();
nickjillings@1575 60 if (audioEngineContext.timer.testStarted)
nickjillings@1575 61 {
nickjillings@1575 62 if (this.lastClicked >= 0)
nickjillings@1575 63 {
nickjillings@1575 64 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
nickjillings@1575 65 }
nickjillings@1575 66 this.lastClicked = id;
nickjillings@1575 67 audioEngineContext.audioObjects[id].metric.listening(time);
nickjillings@1575 68 }
nickjillings@1575 69 console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
nickjillings@1575 70 };
nickjillings@1575 71
nickjillings@1577 72 // Bindings for audioObjects
nickjillings@1577 73
nickjillings@1575 74 // Create the top div for the Title element
nickjillings@1581 75 var titleAttr = specification.title;
nickjillings@1575 76 var title = document.createElement('div');
nickjillings@1575 77 title.className = "title";
nickjillings@1575 78 title.align = "center";
nickjillings@1575 79 var titleSpan = document.createElement('span');
nickjillings@1575 80
nickjillings@1575 81 // Set title to that defined in XML, else set to default
nickjillings@1575 82 if (titleAttr != undefined) {
nickjillings@1581 83 titleSpan.textContent = titleAttr;
nickjillings@1575 84 } else {
nickjillings@1581 85 titleSpan.textContent = 'Listening test';
nickjillings@1575 86 }
nickjillings@1575 87 // Insert the titleSpan element into the title div element.
nickjillings@1575 88 title.appendChild(titleSpan);
nickjillings@1575 89
nickjillings@1575 90 var pagetitle = document.createElement('div');
nickjillings@1575 91 pagetitle.className = "pageTitle";
nickjillings@1575 92 pagetitle.align = "center";
nickjillings@1575 93 var titleSpan = document.createElement('span');
nickjillings@1575 94 titleSpan.id = "pageTitle";
nickjillings@1575 95 pagetitle.appendChild(titleSpan);
nickjillings@1575 96
nickjillings@1575 97 // Create Interface buttons!
nickjillings@1575 98 var interfaceButtons = document.createElement('div');
nickjillings@1575 99 interfaceButtons.id = 'interface-buttons';
nickjillings@1575 100
nickjillings@1575 101 // Create playback start/stop points
nickjillings@1575 102 var playback = document.createElement("button");
nickjillings@1575 103 playback.innerHTML = 'Stop';
nickjillings@1575 104 playback.id = 'playback-button';
nickjillings@1575 105 // onclick function. Check if it is playing or not, call the correct function in the
nickjillings@1575 106 // audioEngine, change the button text to reflect the next state.
nickjillings@1575 107 playback.onclick = function() {
nickjillings@1575 108 if (audioEngineContext.status == 1) {
nickjillings@1575 109 audioEngineContext.stop();
nickjillings@1575 110 this.innerHTML = 'Stop';
nickjillings@1575 111 var time = audioEngineContext.timer.getTestTime();
nickjillings@1575 112 console.log('Stopped at ' + time); // DEBUG/SAFETY
nickjillings@1575 113 }
nickjillings@1575 114 };
nickjillings@1575 115 // Create Submit (save) button
nickjillings@1575 116 var submit = document.createElement("button");
nickjillings@1575 117 submit.innerHTML = 'Submit';
nickjillings@1575 118 submit.onclick = buttonSubmitClick;
nickjillings@1575 119 submit.id = 'submit-button';
nickjillings@1575 120 // Append the interface buttons into the interfaceButtons object.
nickjillings@1575 121 interfaceButtons.appendChild(playback);
nickjillings@1575 122 interfaceButtons.appendChild(submit);
nickjillings@1575 123
nickjillings@1575 124 // Now create the slider and HTML5 canvas boxes
nickjillings@1575 125
nickjillings@1575 126 // Create the div box to center align
nickjillings@1575 127 var sliderBox = document.createElement('div');
nickjillings@1575 128 sliderBox.className = 'sliderCanvasDiv';
nickjillings@1575 129 sliderBox.id = 'sliderCanvasHolder';
nickjillings@1575 130
nickjillings@1575 131 // Create the slider box to hold the slider elements
nickjillings@1575 132 var canvas = document.createElement('div');
nickjillings@1575 133 canvas.id = 'slider';
nickjillings@1575 134 canvas.align = "left";
nickjillings@1575 135 canvas.addEventListener('dragover',function(event){
nickjillings@1575 136 event.preventDefault();
nickjillings@1575 137 return false;
nickjillings@1575 138 },false);
nickjillings@1575 139 var sliderMargin = document.createAttribute('marginsize');
nickjillings@1575 140 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
nickjillings@1575 141 // Must have a known EXACT width, as this is used later to determine the ratings
nickjillings@1575 142 var w = (Number(sliderMargin.nodeValue)+8)*2;
nickjillings@1575 143 canvas.style.width = width - w +"px";
nickjillings@1575 144 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
nickjillings@1575 145 canvas.setAttributeNode(sliderMargin);
nickjillings@1575 146 sliderBox.appendChild(canvas);
nickjillings@1575 147
nickjillings@1575 148 // Create the div to hold any scale objects
nickjillings@1575 149 var scale = document.createElement('div');
nickjillings@1575 150 scale.className = 'sliderScale';
nickjillings@1575 151 scale.id = 'sliderScaleHolder';
nickjillings@1575 152 scale.align = 'left';
nickjillings@1575 153 sliderBox.appendChild(scale);
nickjillings@1575 154
nickjillings@1575 155 // Global parent for the comment boxes on the page
nickjillings@1575 156 var feedbackHolder = document.createElement('div');
nickjillings@1575 157 feedbackHolder.id = 'feedbackHolder';
nickjillings@1575 158
nickjillings@1575 159 testContent.style.zIndex = 1;
nickjillings@1575 160 insertPoint.innerHTML = null; // Clear the current schema
nickjillings@1575 161
nickjillings@1575 162 // Inject into HTML
nickjillings@1575 163 testContent.appendChild(title); // Insert the title
nickjillings@1575 164 testContent.appendChild(pagetitle);
nickjillings@1575 165 testContent.appendChild(interfaceButtons);
nickjillings@1575 166 testContent.appendChild(sliderBox);
nickjillings@1575 167 testContent.appendChild(feedbackHolder);
nickjillings@1575 168 insertPoint.appendChild(testContent);
nickjillings@1575 169
nickjillings@1575 170 // Load the full interface
nickjillings@1575 171 testState.initialise();
nickjillings@1575 172 testState.advanceState();
nickjillings@1575 173
nickjillings@1575 174 }
nickjillings@1575 175
nickjillings@1581 176 function loadTest(audioHolderObject)
nickjillings@1575 177 {
nickjillings@1575 178
nickjillings@1575 179 // Reset audioEngineContext.Metric globals for new test
nickjillings@1575 180 audioEngineContext.newTestPage();
nickjillings@1575 181
nickjillings@1581 182 var id = audioHolderObject.id;
nickjillings@1575 183
nickjillings@1575 184 var feedbackHolder = document.getElementById('feedbackHolder');
nickjillings@1575 185 var canvas = document.getElementById('slider');
nickjillings@1575 186 feedbackHolder.innerHTML = null;
nickjillings@1575 187 canvas.innerHTML = null;
nickjillings@1575 188
nickjillings@1575 189 // Setup question title
nickjillings@1581 190 var interfaceObj = $(audioHolderObject).find('interface');
nickjillings@1575 191 var titleNode = interfaceObj.find('title');
nickjillings@1575 192 if (titleNode[0] != undefined)
nickjillings@1575 193 {
nickjillings@1575 194 document.getElementById('pageTitle').textContent = titleNode[0].textContent;
nickjillings@1575 195 }
nickjillings@1575 196 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
nickjillings@1575 197 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
nickjillings@1575 198 var scale = document.getElementById('sliderScaleHolder');
nickjillings@1575 199 scale.innerHTML = null;
nickjillings@1575 200 interfaceObj.find('scale').each(function(index,scaleObj){
nickjillings@1575 201 var value = document.createAttribute('value');
nickjillings@1575 202 var position = Number(scaleObj.attributes['position'].value)*0.01;
nickjillings@1575 203 value.nodeValue = position;
nickjillings@1575 204 var pixelPosition = (position*positionScale)+offset;
nickjillings@1575 205 var scaleDOM = document.createElement('span');
nickjillings@1575 206 scaleDOM.textContent = scaleObj.textContent;
nickjillings@1575 207 scale.appendChild(scaleDOM);
nickjillings@1575 208 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
nickjillings@1575 209 scaleDOM.setAttributeNode(value);
nickjillings@1575 210 });
nickjillings@1575 211
nickjillings@1575 212 var commentBoxPrefix = interfaceObj.find('commentBoxPrefix');
nickjillings@1575 213 if (commentBoxPrefix.length != 0) {
nickjillings@1575 214 commentBoxPrefix = commentBoxPrefix[0].textContent;
nickjillings@1575 215 } else {
nickjillings@1575 216 commentBoxPrefix = "Comment on track";
nickjillings@1575 217 }
nickjillings@1575 218
nickjillings@1575 219 /// CHECK FOR SAMPLE RATE COMPATIBILITY
nickjillings@1581 220 if (audioHolderObject.sampleRate != undefined) {
nickjillings@1581 221 if (Number(audioHolderObject.sampleRate) != audioContext.sampleRate) {
nickjillings@1575 222 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
nickjillings@1575 223 alert(errStr);
nickjillings@1575 224 return;
nickjillings@1575 225 }
nickjillings@1575 226 }
nickjillings@1575 227
nickjillings@1581 228 var commentShow = audioHolderObject.elementComments;
nickjillings@1575 229
nickjillings@1581 230 var loopPlayback = audioHolderObject.loop;
nickjillings@1581 231
nickjillings@1575 232 audioEngineContext.loopPlayback = loopPlayback;
nickjillings@1575 233 // Create AudioEngine bindings for playback
nickjillings@1575 234 if (loopPlayback) {
nickjillings@1575 235 audioEngineContext.selectedTrack = function(id) {
nickjillings@1575 236 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1575 237 {
nickjillings@1575 238 if (id == i) {
nickjillings@1575 239 this.audioObjects[i].loopStart();
nickjillings@1575 240 } else {
nickjillings@1575 241 this.audioObjects[i].loopStop();
nickjillings@1575 242 }
nickjillings@1575 243 }
nickjillings@1575 244 };
nickjillings@1575 245 } else {
nickjillings@1575 246 audioEngineContext.selectedTrack = function(id) {
nickjillings@1575 247 for (var i=0; i<this.audioObjects.length; i++)
nickjillings@1575 248 {
nickjillings@1575 249 this.audioObjects[i].outputGain.gain.value = 0.0;
nickjillings@1575 250 this.audioObjects[i].stop();
nickjillings@1575 251 }
nickjillings@1575 252 if (this.status == 1) {
nickjillings@1575 253 this.audioObjects[id].outputGain.gain.value = 1.0;
nickjillings@1575 254 this.audioObjects[id].play(audioContext.currentTime+0.01);
nickjillings@1575 255 }
nickjillings@1575 256 };
nickjillings@1575 257 }
nickjillings@1575 258
nickjillings@1575 259 currentTestHolder = document.createElement('audioHolder');
nickjillings@1581 260 currentTestHolder.id = audioHolderObject.id;
nickjillings@1581 261 currentTestHolder.repeatCount = audioHolderObject.repeatCount;
nickjillings@1575 262
nickjillings@1581 263 var randomise = audioHolderObject.randomiseOrder;
nickjillings@1575 264
nickjillings@1581 265 var audioElements = audioHolderObject.audioElements;
nickjillings@1575 266 currentTrackOrder = [];
nickjillings@1575 267 if (randomise) {
nickjillings@1581 268 audioHolderObject.audioElements = randomiseOrder(audioHolderObject.audioElements);
nickjillings@1575 269 }
nickjillings@1575 270
nickjillings@1575 271 // Delete any previous audioObjects associated with the audioEngine
nickjillings@1575 272 audioEngineContext.audioObjects = [];
nickjillings@1575 273
nickjillings@1575 274 // Find all the audioElements from the audioHolder
nickjillings@1581 275 $(audioHolderObject.audioElements).each(function(index,element){
nickjillings@1575 276 // Find URL of track
nickjillings@1575 277 // In this jQuery loop, variable 'this' holds the current audioElement.
nickjillings@1575 278
nickjillings@1575 279 // Now load each audio sample. First create the new track by passing the full URL
nickjillings@1581 280 var trackURL = audioHolderObject.hostURL + element.url;
nickjillings@1579 281 var audioObject = audioEngineContext.newTrack(trackURL);
nickjillings@1575 282
nickjillings@1575 283 if (commentShow) {
nickjillings@1575 284 // Create document objects to hold the comment boxes
nickjillings@1575 285 var trackComment = document.createElement('div');
nickjillings@1575 286 trackComment.className = 'comment-div';
nickjillings@1575 287 trackComment.id = 'comment-div-'+index;
nickjillings@1575 288 // Create a string next to each comment asking for a comment
nickjillings@1575 289 var trackString = document.createElement('span');
nickjillings@1575 290 trackString.innerHTML = commentBoxPrefix+' '+index;
nickjillings@1575 291 // Create the HTML5 comment box 'textarea'
nickjillings@1575 292 var trackCommentBox = document.createElement('textarea');
nickjillings@1575 293 trackCommentBox.rows = '4';
nickjillings@1575 294 trackCommentBox.cols = '100';
nickjillings@1575 295 trackCommentBox.name = 'trackComment'+index;
nickjillings@1575 296 trackCommentBox.className = 'trackComment';
nickjillings@1575 297 var br = document.createElement('br');
nickjillings@1575 298 // Add to the holder.
nickjillings@1575 299 trackComment.appendChild(trackString);
nickjillings@1575 300 trackComment.appendChild(br);
nickjillings@1575 301 trackComment.appendChild(trackCommentBox);
nickjillings@1575 302 feedbackHolder.appendChild(trackComment);
nickjillings@1579 303 audioObject.commentDOM = trackComment;
nickjillings@1575 304 }
nickjillings@1575 305
nickjillings@1575 306 // Create a slider per track
nickjillings@1575 307
nickjillings@1575 308 var trackSliderObj = document.createElement('div');
nickjillings@1575 309 trackSliderObj.className = 'track-slider';
nickjillings@1575 310 trackSliderObj.id = 'track-slider-'+index;
nickjillings@1575 311
nickjillings@1575 312 var trackSliderAOIndex = document.createAttribute('trackIndex');
nickjillings@1575 313 trackSliderAOIndex.nodeValue = index;
nickjillings@1575 314 trackSliderObj.setAttributeNode(trackSliderAOIndex);
nickjillings@1575 315
nickjillings@1575 316 // Distribute it randomnly
nickjillings@1575 317 var w = window.innerWidth - (offset+8)*2;
nickjillings@1575 318 w = Math.random()*w;
nickjillings@1575 319 w = Math.floor(w+(offset+8));
nickjillings@1575 320 trackSliderObj.style.left = w+'px';
nickjillings@1575 321 trackSliderObj.innerHTML = '<span>'+index+'</span>';
nickjillings@1575 322 trackSliderObj.draggable = true;
nickjillings@1575 323 trackSliderObj.ondragend = dragEnd;
nickjillings@1575 324 trackSliderObj.ondragstart = function()
nickjillings@1575 325 {
nickjillings@1575 326 var id = Number(event.srcElement.attributes['trackIndex'].value);
nickjillings@1575 327 audioEngineContext.metric.sliderMoveStart(id);
nickjillings@1575 328 };
nickjillings@1575 329
nickjillings@1575 330 // Onclick, switch playback to that track
nickjillings@1575 331 trackSliderObj.onclick = function() {
nickjillings@1575 332 // Start the test on first click, that way timings are more accurate.
nickjillings@1575 333 audioEngineContext.play();
nickjillings@1575 334 if (audioEngineContext.audioObjectsReady) {
nickjillings@1575 335 // Cannot continue to issue play command until audioObjects reported as ready!
nickjillings@1575 336 // Get the track ID from the object ID
nickjillings@1575 337 var id = Number(event.srcElement.attributes['trackIndex'].value);
nickjillings@1575 338 //audioEngineContext.metric.sliderPlayed(id);
nickjillings@1575 339 audioEngineContext.selectedTrack(id);
nickjillings@1575 340 // Currently playing track red, rest green
nickjillings@1575 341
nickjillings@1575 342 //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
nickjillings@1575 343 $('.track-slider').removeClass('track-slider-playing');
nickjillings@1575 344 $(event.srcElement).addClass('track-slider-playing');
nickjillings@1575 345 $('.comment-div').removeClass('comment-box-playing');
nickjillings@1575 346 $('#comment-div-'+id).addClass('comment-box-playing');
nickjillings@1575 347 }
nickjillings@1575 348 };
nickjillings@1575 349
nickjillings@1577 350 // Attach binding
nickjillings@1579 351 audioObject.sliderDOM = trackSliderObj;
nickjillings@1577 352
nickjillings@1575 353 canvas.appendChild(trackSliderObj);
nickjillings@1579 354 audioObject.metric.initialised(convSliderPosToRate(index));
nickjillings@1575 355
nickjillings@1575 356 });
nickjillings@1575 357
nickjillings@1575 358 // Append any commentQuestion boxes
nickjillings@1581 359 $(audioHolderObject.commentQuestions).each(function(index,element) {
nickjillings@1575 360 // Create document objects to hold the comment boxes
nickjillings@1575 361 var trackComment = document.createElement('div');
nickjillings@1575 362 trackComment.className = 'comment-div commentQuestion';
nickjillings@1581 363 trackComment.id = element.id;
nickjillings@1575 364 // Create a string next to each comment asking for a comment
nickjillings@1575 365 var trackString = document.createElement('span');
nickjillings@1581 366 trackString.innerHTML = element.question;
nickjillings@1575 367 // Create the HTML5 comment box 'textarea'
nickjillings@1575 368 var trackCommentBox = document.createElement('textarea');
nickjillings@1575 369 trackCommentBox.rows = '4';
nickjillings@1575 370 trackCommentBox.cols = '100';
nickjillings@1575 371 trackCommentBox.name = 'commentQuestion'+index;
nickjillings@1575 372 trackCommentBox.className = 'trackComment';
nickjillings@1575 373 var br = document.createElement('br');
nickjillings@1575 374 // Add to the holder.
nickjillings@1575 375 trackComment.appendChild(trackString);
nickjillings@1575 376 trackComment.appendChild(br);
nickjillings@1575 377 trackComment.appendChild(trackCommentBox);
nickjillings@1575 378 feedbackHolder.appendChild(trackComment);
nickjillings@1575 379 });
nickjillings@1575 380
nickjillings@1575 381
nickjillings@1575 382 testWaitIndicator();
nickjillings@1575 383 }
nickjillings@1575 384
nickjillings@1575 385
nickjillings@1575 386 function dragEnd(ev) {
nickjillings@1575 387 // Function call when a div has been dropped
nickjillings@1575 388 var slider = document.getElementById('slider');
nickjillings@1575 389 var marginSize = Number(slider.attributes['marginsize'].value);
nickjillings@1575 390 var w = slider.style.width;
nickjillings@1575 391 w = Number(w.substr(0,w.length-2));
nickjillings@1575 392 var x = ev.x;
nickjillings@1575 393 if (x >= marginSize && x < w+marginSize) {
nickjillings@1575 394 this.style.left = (x)+'px';
nickjillings@1575 395 } else {
nickjillings@1575 396 if (x<marginSize) {
nickjillings@1575 397 this.style.left = marginSize+'px';
nickjillings@1575 398 } else {
nickjillings@1575 399 this.style.left = (w+marginSize) + 'px';
nickjillings@1575 400 }
nickjillings@1575 401 }
nickjillings@1575 402 audioEngineContext.metric.sliderMoved();
nickjillings@1575 403 }
nickjillings@1575 404
nickjillings@1575 405 function buttonSubmitClick() // TODO: Only when all songs have been played!
nickjillings@1575 406 {
nickjillings@1575 407 hasBeenPlayed = audioEngineContext.checkAllPlayed();
nickjillings@1575 408 if (hasBeenPlayed.length == 0) {
nickjillings@1575 409 if (audioEngineContext.status == 1) {
nickjillings@1575 410 var playback = document.getElementById('playback-button');
nickjillings@1575 411 playback.click();
nickjillings@1575 412 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
nickjillings@1575 413 } else
nickjillings@1575 414 {
nickjillings@1575 415 if (audioEngineContext.timer.testStarted == false)
nickjillings@1575 416 {
nickjillings@1575 417 alert('You have not started the test! Please press start to begin the test!');
nickjillings@1575 418 return;
nickjillings@1575 419 }
nickjillings@1575 420 }
nickjillings@1575 421 testState.advanceState();
nickjillings@1575 422 } else // if a fragment has not been played yet
nickjillings@1575 423 {
nickjillings@1575 424 str = "";
nickjillings@1575 425 if (hasBeenPlayed.length > 1) {
nickjillings@1575 426 for (var i=0; i<hasBeenPlayed.length; i++) {
nickjillings@1575 427 str = str + hasBeenPlayed[i];
nickjillings@1575 428 if (i < hasBeenPlayed.length-2){
nickjillings@1575 429 str += ", ";
nickjillings@1575 430 } else if (i == hasBeenPlayed.length-2) {
nickjillings@1575 431 str += " or ";
nickjillings@1575 432 }
nickjillings@1575 433 }
nickjillings@1575 434 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
nickjillings@1575 435 } else {
nickjillings@1575 436 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
nickjillings@1575 437 }
nickjillings@1575 438 return;
nickjillings@1575 439 }
nickjillings@1575 440 }
nickjillings@1575 441
nickjillings@1575 442 function convSliderPosToRate(id)
nickjillings@1575 443 {
nickjillings@1575 444 var w = document.getElementById('slider').style.width;
nickjillings@1575 445 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
nickjillings@1575 446 var maxPix = w.substr(0,w.length-2);
nickjillings@1575 447 var slider = document.getElementsByClassName('track-slider')[id];
nickjillings@1575 448 var pix = slider.style.left;
nickjillings@1575 449 pix = pix.substr(0,pix.length-2);
nickjillings@1575 450 var rate = (pix-marginsize)/maxPix;
nickjillings@1575 451 return rate;
nickjillings@1575 452 }
nickjillings@1575 453
nickjillings@1575 454 function resizeWindow(event){
nickjillings@1575 455 // Function called when the window has been resized.
nickjillings@1575 456 // MANDATORY FUNCTION
nickjillings@1575 457
nickjillings@1575 458 // Store the slider marker values
nickjillings@1575 459 var holdValues = [];
nickjillings@1575 460 $(".track-slider").each(function(index,sliderObj){
nickjillings@1575 461 holdValues.push(convSliderPosToRate(index));
nickjillings@1575 462 });
nickjillings@1575 463
nickjillings@1575 464 var width = event.target.innerWidth;
nickjillings@1575 465 var canvas = document.getElementById('sliderCanvasHolder');
nickjillings@1575 466 var sliderDiv = canvas.children[0];
nickjillings@1575 467 var sliderScaleDiv = canvas.children[1];
nickjillings@1575 468 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
nickjillings@1575 469 var w = (marginsize+8)*2;
nickjillings@1575 470 sliderDiv.style.width = width - w + 'px';
nickjillings@1575 471 var width = width - w;
nickjillings@1575 472 // Move sliders into new position
nickjillings@1575 473 $(".track-slider").each(function(index,sliderObj){
nickjillings@1575 474 var pos = holdValues[index];
nickjillings@1575 475 var pix = pos * width;
nickjillings@1575 476 sliderObj.style.left = pix+marginsize+'px';
nickjillings@1575 477 });
nickjillings@1575 478
nickjillings@1575 479 // Move scale labels
nickjillings@1575 480 $(sliderScaleDiv.children).each(function(index,scaleObj){
nickjillings@1575 481 var position = Number(scaleObj.attributes['value'].value);
nickjillings@1575 482 var pixelPosition = (position*width)+marginsize;
nickjillings@1575 483 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
nickjillings@1575 484 });
nickjillings@1575 485 }
nickjillings@1575 486
nickjillings@1581 487 function pageXMLSave(store, testXML)
nickjillings@1575 488 {
nickjillings@1575 489 // Saves a specific test page
nickjillings@1575 490 var xmlDoc = store;
nickjillings@1575 491 // Check if any session wide metrics are enabled
nickjillings@1575 492
nickjillings@1581 493 var commentShow = testXML.elementComments;
nickjillings@1575 494
nickjillings@1575 495 var metric = document.createElement('metric');
nickjillings@1575 496 if (audioEngineContext.metric.enableTestTimer)
nickjillings@1575 497 {
nickjillings@1575 498 var testTime = document.createElement('metricResult');
nickjillings@1575 499 testTime.id = 'testTime';
nickjillings@1575 500 testTime.textContent = audioEngineContext.timer.testDuration;
nickjillings@1575 501 metric.appendChild(testTime);
nickjillings@1575 502 }
nickjillings@1575 503 xmlDoc.appendChild(metric);
nickjillings@1578 504 var audioObjects = audioEngineContext.audioObjects;
nickjillings@1578 505 for (var i=0; i<audioObjects.length; i++)
nickjillings@1575 506 {
nickjillings@1575 507 var audioElement = document.createElement('audioElement');
nickjillings@1578 508 audioElement.id = audioObjects[i].id;
nickjillings@1578 509 audioElement.setAttribute('url',audioObjects[i].url);
nickjillings@1575 510 var value = document.createElement('value');
nickjillings@1575 511 value.innerHTML = convSliderPosToRate(i);
nickjillings@1575 512 if (commentShow) {
nickjillings@1575 513 var comment = document.createElement("comment");
nickjillings@1575 514 var question = document.createElement("question");
nickjillings@1575 515 var response = document.createElement("response");
nickjillings@1578 516 question.textContent = audioObjects[i].commentDOM.children[0].textContent;
nickjillings@1578 517 response.textContent = audioObjects[i].commentDOM.children[2].value;
nickjillings@1578 518 console.log('Comment ' + i + ': ' + response.textContent); // DEBUG/SAFETY
nickjillings@1575 519 comment.appendChild(question);
nickjillings@1575 520 comment.appendChild(response);
nickjillings@1575 521 audioElement.appendChild(comment);
nickjillings@1575 522 }
nickjillings@1575 523 audioElement.appendChild(value);
nickjillings@1575 524 // Check for any per element metrics
nickjillings@1577 525
nickjillings@1578 526 audioElement.appendChild(audioObjects[i].metric.exportXMLDOM());
nickjillings@1575 527 xmlDoc.appendChild(audioElement);
nickjillings@1575 528 }
nickjillings@1575 529 var commentQuestion = document.getElementsByClassName('commentQuestion');
nickjillings@1575 530 for (var i=0; i<commentQuestion.length; i++)
nickjillings@1575 531 {
nickjillings@1575 532 var cqHolder = document.createElement('CommentQuestion');
nickjillings@1575 533 var comment = document.createElement('comment');
nickjillings@1575 534 var question = document.createElement('question');
nickjillings@1575 535 cqHolder.id = commentQuestion[i].id;
nickjillings@1575 536 comment.textContent = commentQuestion[i].children[2].value;
nickjillings@1575 537 question.textContent = commentQuestion[i].children[0].textContent;
nickjillings@1575 538 console.log('Question ' + i + ': ' + commentQuestion[i].children[2].value); // DEBUG/SAFETY
nickjillings@1575 539 cqHolder.appendChild(question);
nickjillings@1575 540 cqHolder.appendChild(comment);
nickjillings@1575 541 xmlDoc.appendChild(cqHolder);
nickjillings@1575 542 }
nickjillings@1575 543 store = xmlDoc;
nickjillings@1575 544 }