nicholas@2
|
1 /**
|
nicholas@2
|
2 * ape.js
|
nicholas@2
|
3 * Create the APE interface
|
nicholas@2
|
4 */
|
nicholas@2
|
5
|
n@38
|
6 // preTest - In preTest state
|
n@38
|
7 // testRun-ID - In test running, test Id number at the end 'testRun-2'
|
n@38
|
8 // testRunPost-ID - Post test of test ID
|
n@38
|
9 // testRunPre-ID - Pre-test of test ID
|
n@38
|
10 // postTest - End of test, final submission!
|
n@38
|
11
|
n@32
|
12
|
nicholas@2
|
13 // Once this is loaded and parsed, begin execution
|
nicholas@2
|
14 loadInterface(projectXML);
|
nicholas@2
|
15
|
nicholas@2
|
16 function loadInterface(xmlDoc) {
|
nicholas@2
|
17
|
n@16
|
18 // Get the dimensions of the screen available to the page
|
nicholas@2
|
19 var width = window.innerWidth;
|
nicholas@2
|
20 var height = window.innerHeight;
|
nicholas@2
|
21
|
nicholas@2
|
22 // The injection point into the HTML page
|
nicholas@2
|
23 var insertPoint = document.getElementById("topLevelBody");
|
n@22
|
24 var testContent = document.createElement('div');
|
nicholas@135
|
25
|
n@22
|
26 testContent.id = 'testContent';
|
nicholas@2
|
27
|
nicholas@7
|
28
|
nicholas@2
|
29 // Decode parts of the xmlDoc that are needed
|
nicholas@2
|
30 // xmlDoc MUST already be parsed by jQuery!
|
nicholas@2
|
31 var xmlSetup = xmlDoc.find('setup');
|
nicholas@2
|
32 // Should put in an error function here incase of malprocessed or malformed XML
|
nicholas@2
|
33
|
nicholas@129
|
34 // Create pre and post test questions
|
nicholas@129
|
35
|
nicholas@129
|
36 var preTest = xmlSetup.find('PreTest');
|
nicholas@129
|
37 var postTest = xmlSetup.find('PostTest');
|
nicholas@129
|
38 preTest = preTest[0];
|
nicholas@129
|
39 postTest = postTest[0];
|
nicholas@129
|
40
|
nicholas@129
|
41 if (preTest == undefined) {preTest = document.createElement("preTest");}
|
nicholas@129
|
42 if (postTest == undefined){postTest= document.createElement("postTest");}
|
nicholas@129
|
43
|
nicholas@129
|
44 testState.stateMap.push(preTest);
|
nicholas@129
|
45
|
n@34
|
46 // Extract the different test XML DOM trees
|
n@50
|
47 var audioHolders = xmlDoc.find('audioHolder');
|
nicholas@129
|
48 var testXMLSetups = [];
|
n@50
|
49 audioHolders.each(function(index,element) {
|
n@50
|
50 var repeatN = element.attributes['repeatCount'].value;
|
n@50
|
51 for (var r=0; r<=repeatN; r++) {
|
nicholas@129
|
52 testXMLSetups.push(element);
|
n@50
|
53 }
|
n@50
|
54 });
|
n@44
|
55
|
n@50
|
56 // New check if we need to randomise the test order
|
n@50
|
57 var randomise = xmlSetup[0].attributes['randomiseOrder'];
|
n@50
|
58 if (randomise != undefined) {
|
nicholas@109
|
59 if (randomise.value === 'true'){
|
nicholas@109
|
60 randomise = true;
|
nicholas@109
|
61 } else {
|
nicholas@109
|
62 randomise = false;
|
nicholas@109
|
63 }
|
n@50
|
64 } else {
|
n@50
|
65 randomise = false;
|
n@50
|
66 }
|
nicholas@109
|
67
|
n@50
|
68 if (randomise)
|
n@50
|
69 {
|
n@54
|
70 testXMLSetups = randomiseOrder(testXMLSetups);
|
n@50
|
71 }
|
nicholas@129
|
72
|
nicholas@129
|
73 $(testXMLSetups).each(function(index,elem){
|
nicholas@129
|
74 testState.stateMap.push(elem);
|
nicholas@129
|
75 })
|
nicholas@129
|
76
|
nicholas@129
|
77 testState.stateMap.push(postTest);
|
n@44
|
78
|
n@50
|
79 // Obtain the metrics enabled
|
n@50
|
80 var metricNode = xmlSetup.find('Metric');
|
n@50
|
81 var metricNode = metricNode.find('metricEnable');
|
n@50
|
82 metricNode.each(function(index,node){
|
n@50
|
83 var enabled = node.textContent;
|
n@50
|
84 switch(enabled)
|
n@50
|
85 {
|
n@50
|
86 case 'testTimer':
|
n@50
|
87 sessionMetrics.prototype.enableTestTimer = true;
|
n@50
|
88 break;
|
n@50
|
89 case 'elementTimer':
|
n@50
|
90 sessionMetrics.prototype.enableElementTimer = true;
|
n@50
|
91 break;
|
n@50
|
92 case 'elementTracker':
|
n@50
|
93 sessionMetrics.prototype.enableElementTracker = true;
|
n@50
|
94 break;
|
n@164
|
95 case 'elementListenTracker':
|
n@164
|
96 sessionMetrics.prototype.enableElementListenTracker = true;
|
n@164
|
97 break;
|
n@170
|
98 case 'elementInitialPosition':
|
n@50
|
99 sessionMetrics.prototype.enableElementInitialPosition = true;
|
n@50
|
100 break;
|
n@50
|
101 case 'elementFlagListenedTo':
|
n@50
|
102 sessionMetrics.prototype.enableFlagListenedTo = true;
|
n@50
|
103 break;
|
n@50
|
104 case 'elementFlagMoved':
|
n@50
|
105 sessionMetrics.prototype.enableFlagMoved = true;
|
n@50
|
106 break;
|
n@50
|
107 case 'elementFlagComments':
|
n@50
|
108 sessionMetrics.prototype.enableFlagComments = true;
|
n@50
|
109 break;
|
n@50
|
110 }
|
n@50
|
111 });
|
n@34
|
112
|
n@52
|
113 // Create APE specific metric functions
|
n@52
|
114 audioEngineContext.metric.initialiseTest = function()
|
n@52
|
115 {
|
n@52
|
116 };
|
n@52
|
117
|
n@52
|
118 audioEngineContext.metric.sliderMoveStart = function(id)
|
n@52
|
119 {
|
n@52
|
120 if (this.data == -1)
|
n@52
|
121 {
|
n@52
|
122 this.data = id;
|
n@52
|
123 } else {
|
n@52
|
124 console.log('ERROR: Metric tracker detecting two moves!');
|
n@52
|
125 this.data = -1;
|
n@52
|
126 }
|
n@52
|
127 };
|
n@52
|
128 audioEngineContext.metric.sliderMoved = function()
|
n@52
|
129 {
|
n@52
|
130 var time = audioEngineContext.timer.getTestTime();
|
n@52
|
131 var id = this.data;
|
n@52
|
132 this.data = -1;
|
n@52
|
133 var position = convSliderPosToRate(id);
|
b@115
|
134 console.log('slider ' + id + ': '+ position + ' (' + time + ')'); // DEBUG/SAFETY: show position and slider id
|
n@52
|
135 if (audioEngineContext.timer.testStarted)
|
n@52
|
136 {
|
n@52
|
137 audioEngineContext.audioObjects[id].metric.moved(time,position);
|
n@52
|
138 }
|
n@52
|
139 };
|
n@52
|
140
|
n@52
|
141 audioEngineContext.metric.sliderPlayed = function(id)
|
n@52
|
142 {
|
n@52
|
143 var time = audioEngineContext.timer.getTestTime();
|
n@52
|
144 if (audioEngineContext.timer.testStarted)
|
n@52
|
145 {
|
n@52
|
146 if (this.lastClicked >= 0)
|
n@52
|
147 {
|
n@52
|
148 audioEngineContext.audioObjects[this.lastClicked].metric.listening(time);
|
n@52
|
149 }
|
n@52
|
150 this.lastClicked = id;
|
n@52
|
151 audioEngineContext.audioObjects[id].metric.listening(time);
|
n@52
|
152 }
|
b@115
|
153 console.log('slider ' + id + ' played (' + time + ')'); // DEBUG/SAFETY: show played slider id
|
n@52
|
154 };
|
n@52
|
155
|
nicholas@2
|
156 // Create the top div for the Title element
|
nicholas@2
|
157 var titleAttr = xmlSetup[0].attributes['title'];
|
nicholas@2
|
158 var title = document.createElement('div');
|
nicholas@2
|
159 title.className = "title";
|
nicholas@2
|
160 title.align = "center";
|
nicholas@2
|
161 var titleSpan = document.createElement('span');
|
nicholas@2
|
162
|
nicholas@2
|
163 // Set title to that defined in XML, else set to default
|
nicholas@2
|
164 if (titleAttr != undefined) {
|
n@25
|
165 titleSpan.innerHTML = titleAttr.value;
|
nicholas@2
|
166 } else {
|
b@75
|
167 titleSpan.innerHTML = 'Listening test';
|
nicholas@2
|
168 }
|
nicholas@2
|
169 // Insert the titleSpan element into the title div element.
|
nicholas@2
|
170 title.appendChild(titleSpan);
|
nicholas@2
|
171
|
n@47
|
172 var pagetitle = document.createElement('div');
|
n@47
|
173 pagetitle.className = "pageTitle";
|
n@47
|
174 pagetitle.align = "center";
|
n@47
|
175 var titleSpan = document.createElement('span');
|
n@47
|
176 titleSpan.id = "pageTitle";
|
n@47
|
177 pagetitle.appendChild(titleSpan);
|
n@47
|
178
|
nicholas@7
|
179 // Store the return URL path in global projectReturn
|
n@140
|
180 projectReturn = xmlSetup[0].attributes['projectReturn'];
|
n@140
|
181 if (projectReturn == undefined) {
|
n@140
|
182 console.log("WARNING - projectReturn not specified! Will assume null.");
|
n@140
|
183 projectReturn = "null";
|
n@140
|
184 } else {
|
n@140
|
185 projectReturn = projectReturn.value;
|
n@140
|
186 }
|
nicholas@7
|
187
|
nicholas@7
|
188 // Create Interface buttons!
|
nicholas@7
|
189 var interfaceButtons = document.createElement('div');
|
nicholas@7
|
190 interfaceButtons.id = 'interface-buttons';
|
nicholas@7
|
191
|
nicholas@7
|
192 // MANUAL DOWNLOAD POINT
|
nicholas@7
|
193 // If project return is null, this MUST be specified as the location to create the download link
|
nicholas@7
|
194 var downloadPoint = document.createElement('div');
|
nicholas@7
|
195 downloadPoint.id = 'download-point';
|
nicholas@7
|
196
|
nicholas@7
|
197 // Create playback start/stop points
|
nicholas@7
|
198 var playback = document.createElement("button");
|
b@101
|
199 playback.innerHTML = 'Stop';
|
n@49
|
200 playback.id = 'playback-button';
|
n@16
|
201 // onclick function. Check if it is playing or not, call the correct function in the
|
n@16
|
202 // audioEngine, change the button text to reflect the next state.
|
nicholas@7
|
203 playback.onclick = function() {
|
b@101
|
204 if (audioEngineContext.status == 1) {
|
b@101
|
205 audioEngineContext.stop();
|
n@25
|
206 this.innerHTML = 'Stop';
|
b@115
|
207 var time = audioEngineContext.timer.getTestTime();
|
b@115
|
208 console.log('Stopped at ' + time); // DEBUG/SAFETY
|
nicholas@7
|
209 }
|
n@16
|
210 };
|
nicholas@7
|
211 // Create Submit (save) button
|
nicholas@7
|
212 var submit = document.createElement("button");
|
n@25
|
213 submit.innerHTML = 'Submit';
|
n@43
|
214 submit.onclick = buttonSubmitClick;
|
n@49
|
215 submit.id = 'submit-button';
|
n@16
|
216 // Append the interface buttons into the interfaceButtons object.
|
nicholas@7
|
217 interfaceButtons.appendChild(playback);
|
nicholas@7
|
218 interfaceButtons.appendChild(submit);
|
nicholas@7
|
219 interfaceButtons.appendChild(downloadPoint);
|
nicholas@7
|
220
|
nicholas@2
|
221 // Now create the slider and HTML5 canvas boxes
|
nicholas@2
|
222
|
n@16
|
223 // Create the div box to center align
|
nicholas@2
|
224 var sliderBox = document.createElement('div');
|
nicholas@2
|
225 sliderBox.className = 'sliderCanvasDiv';
|
n@16
|
226 sliderBox.id = 'sliderCanvasHolder';
|
nicholas@2
|
227
|
n@16
|
228 // Create the slider box to hold the slider elements
|
nicholas@5
|
229 var canvas = document.createElement('div');
|
nicholas@2
|
230 canvas.id = 'slider';
|
n@127
|
231 canvas.align = "left";
|
n@127
|
232 canvas.addEventListener('dragover',function(event){
|
n@127
|
233 event.preventDefault();
|
n@127
|
234 return false;
|
n@127
|
235 },false);
|
n@127
|
236 var sliderMargin = document.createAttribute('marginsize');
|
n@127
|
237 sliderMargin.nodeValue = 42; // Set default margins to 42px either side
|
n@16
|
238 // Must have a known EXACT width, as this is used later to determine the ratings
|
n@127
|
239 var w = (Number(sliderMargin.nodeValue)+8)*2;
|
n@127
|
240 canvas.style.width = width - w +"px";
|
n@127
|
241 canvas.style.marginLeft = sliderMargin.nodeValue +'px';
|
n@127
|
242 canvas.setAttributeNode(sliderMargin);
|
nicholas@2
|
243 sliderBox.appendChild(canvas);
|
n@16
|
244
|
n@47
|
245 // Create the div to hold any scale objects
|
n@47
|
246 var scale = document.createElement('div');
|
n@47
|
247 scale.className = 'sliderScale';
|
n@47
|
248 scale.id = 'sliderScaleHolder';
|
n@47
|
249 scale.align = 'left';
|
n@47
|
250 sliderBox.appendChild(scale);
|
n@47
|
251
|
n@16
|
252 // Global parent for the comment boxes on the page
|
nicholas@3
|
253 var feedbackHolder = document.createElement('div');
|
n@34
|
254 feedbackHolder.id = 'feedbackHolder';
|
nicholas@2
|
255
|
n@38
|
256 testContent.style.zIndex = 1;
|
n@38
|
257 insertPoint.innerHTML = null; // Clear the current schema
|
n@38
|
258
|
n@38
|
259 currentState = 'preTest';
|
n@22
|
260
|
n@38
|
261 // Inject into HTML
|
n@38
|
262 testContent.appendChild(title); // Insert the title
|
n@47
|
263 testContent.appendChild(pagetitle);
|
n@38
|
264 testContent.appendChild(interfaceButtons);
|
n@38
|
265 testContent.appendChild(sliderBox);
|
n@38
|
266 testContent.appendChild(feedbackHolder);
|
n@38
|
267 insertPoint.appendChild(testContent);
|
n@22
|
268
|
n@36
|
269 // Load the full interface
|
nicholas@129
|
270 testState.initialise();
|
nicholas@129
|
271 testState.advanceState();
|
nicholas@135
|
272
|
nicholas@2
|
273 }
|
nicholas@5
|
274
|
nicholas@129
|
275 function loadTest(textXML)
|
n@34
|
276 {
|
nicholas@110
|
277
|
nicholas@110
|
278 // Reset audioEngineContext.Metric globals for new test
|
n@113
|
279 audioEngineContext.newTestPage();
|
nicholas@110
|
280
|
nicholas@129
|
281 var id = textXML.id;
|
n@34
|
282
|
n@34
|
283 var feedbackHolder = document.getElementById('feedbackHolder');
|
n@34
|
284 var canvas = document.getElementById('slider');
|
n@34
|
285 feedbackHolder.innerHTML = null;
|
n@34
|
286 canvas.innerHTML = null;
|
n@47
|
287
|
n@47
|
288 // Setup question title
|
n@47
|
289 var interfaceObj = $(textXML).find('interface');
|
n@47
|
290 var titleNode = interfaceObj.find('title');
|
n@47
|
291 if (titleNode[0] != undefined)
|
n@47
|
292 {
|
n@47
|
293 document.getElementById('pageTitle').textContent = titleNode[0].textContent;
|
n@47
|
294 }
|
n@47
|
295 var positionScale = canvas.style.width.substr(0,canvas.style.width.length-2);
|
n@127
|
296 var offset = Number(document.getElementById('slider').attributes['marginsize'].value);
|
n@47
|
297 var scale = document.getElementById('sliderScaleHolder');
|
n@47
|
298 scale.innerHTML = null;
|
n@47
|
299 interfaceObj.find('scale').each(function(index,scaleObj){
|
n@127
|
300 var value = document.createAttribute('value');
|
n@47
|
301 var position = Number(scaleObj.attributes['position'].value)*0.01;
|
n@127
|
302 value.nodeValue = position;
|
n@47
|
303 var pixelPosition = (position*positionScale)+offset;
|
n@47
|
304 var scaleDOM = document.createElement('span');
|
n@47
|
305 scaleDOM.textContent = scaleObj.textContent;
|
n@47
|
306 scale.appendChild(scaleDOM);
|
n@47
|
307 scaleDOM.style.left = Math.floor((pixelPosition-($(scaleDOM).width()/2)))+'px';
|
n@127
|
308 scaleDOM.setAttributeNode(value);
|
n@47
|
309 });
|
n@174
|
310
|
n@174
|
311 var commentBoxPrefix = interfaceObj.find('commentBoxPrefix');
|
n@174
|
312 if (commentBoxPrefix.length != 0) {
|
n@174
|
313 commentBoxPrefix = commentBoxPrefix[0].textContent;
|
n@174
|
314 } else {
|
n@174
|
315 commentBoxPrefix = "Comment on track";
|
n@174
|
316 }
|
n@34
|
317
|
n@34
|
318 // Extract the hostURL attribute. If not set, create an empty string.
|
n@34
|
319 var hostURL = textXML.attributes['hostURL'];
|
n@34
|
320 if (hostURL == undefined) {
|
n@34
|
321 hostURL = "";
|
n@34
|
322 } else {
|
n@34
|
323 hostURL = hostURL.value;
|
n@34
|
324 }
|
n@34
|
325 // Extract the sampleRate. If set, convert the string to a Number.
|
n@34
|
326 var hostFs = textXML.attributes['sampleRate'];
|
n@34
|
327 if (hostFs != undefined) {
|
n@34
|
328 hostFs = Number(hostFs.value);
|
n@34
|
329 }
|
n@34
|
330
|
n@34
|
331 /// CHECK FOR SAMPLE RATE COMPATIBILITY
|
n@34
|
332 if (hostFs != undefined) {
|
n@34
|
333 if (Number(hostFs) != audioContext.sampleRate) {
|
n@34
|
334 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
|
n@34
|
335 alert(errStr);
|
n@34
|
336 return;
|
n@34
|
337 }
|
n@34
|
338 }
|
n@45
|
339
|
nicholas@66
|
340 var commentShow = textXML.attributes['elementComments'];
|
nicholas@66
|
341 if (commentShow != undefined) {
|
nicholas@66
|
342 if (commentShow.value == 'false') {commentShow = false;}
|
nicholas@66
|
343 else {commentShow = true;}
|
nicholas@66
|
344 } else {commentShow = true;}
|
nicholas@66
|
345
|
n@57
|
346 var loopPlayback = textXML.attributes['loop'];
|
n@57
|
347 if (loopPlayback != undefined)
|
n@57
|
348 {
|
n@57
|
349 loopPlayback = loopPlayback.value;
|
n@57
|
350 if (loopPlayback == 'true') {
|
n@57
|
351 loopPlayback = true;
|
n@57
|
352 } else {
|
n@57
|
353 loopPlayback = false;
|
n@57
|
354 }
|
n@57
|
355 } else {
|
n@57
|
356 loopPlayback = false;
|
n@57
|
357 }
|
n@57
|
358 audioEngineContext.loopPlayback = loopPlayback;
|
n@57
|
359 // Create AudioEngine bindings for playback
|
n@57
|
360 if (loopPlayback) {
|
n@57
|
361 audioEngineContext.selectedTrack = function(id) {
|
n@57
|
362 for (var i=0; i<this.audioObjects.length; i++)
|
n@57
|
363 {
|
n@57
|
364 if (id == i) {
|
nicholas@132
|
365 this.audioObjects[i].loopStart();
|
n@57
|
366 } else {
|
nicholas@132
|
367 this.audioObjects[i].loopStop();
|
n@57
|
368 }
|
n@57
|
369 }
|
n@57
|
370 };
|
n@57
|
371 } else {
|
n@57
|
372 audioEngineContext.selectedTrack = function(id) {
|
n@57
|
373 for (var i=0; i<this.audioObjects.length; i++)
|
n@57
|
374 {
|
n@97
|
375 this.audioObjects[i].outputGain.gain.value = 0.0;
|
n@97
|
376 this.audioObjects[i].stop();
|
n@57
|
377 }
|
n@99
|
378 if (this.status == 1) {
|
n@99
|
379 this.audioObjects[id].outputGain.gain.value = 1.0;
|
n@99
|
380 this.audioObjects[id].play(audioContext.currentTime+0.01);
|
n@99
|
381 }
|
n@57
|
382 };
|
n@57
|
383 }
|
n@57
|
384
|
n@46
|
385 currentTestHolder = document.createElement('audioHolder');
|
n@46
|
386 currentTestHolder.id = textXML.id;
|
n@46
|
387 currentTestHolder.repeatCount = textXML.attributes['repeatCount'].value;
|
n@46
|
388
|
n@45
|
389 var randomise = textXML.attributes['randomiseOrder'];
|
n@45
|
390 if (randomise != undefined) {randomise = randomise.value;}
|
n@45
|
391 else {randomise = false;}
|
n@45
|
392
|
n@34
|
393 var audioElements = $(textXML).find('audioElements');
|
nicholas@58
|
394 currentTrackOrder = [];
|
n@34
|
395 audioElements.each(function(index,element){
|
n@45
|
396 // Find any blind-repeats
|
b@100
|
397 // Not implemented yet, but just in case
|
n@45
|
398 currentTrackOrder[index] = element;
|
n@45
|
399 });
|
n@45
|
400 if (randomise) {
|
n@54
|
401 currentTrackOrder = randomiseOrder(currentTrackOrder);
|
n@45
|
402 }
|
n@45
|
403
|
nicholas@58
|
404 // Delete any previous audioObjects associated with the audioEngine
|
nicholas@58
|
405 audioEngineContext.audioObjects = [];
|
nicholas@58
|
406
|
n@45
|
407 // Find all the audioElements from the audioHolder
|
n@45
|
408 $(currentTrackOrder).each(function(index,element){
|
n@34
|
409 // Find URL of track
|
n@34
|
410 // In this jQuery loop, variable 'this' holds the current audioElement.
|
n@34
|
411
|
n@34
|
412 // Now load each audio sample. First create the new track by passing the full URL
|
n@34
|
413 var trackURL = hostURL + this.attributes['url'].value;
|
n@34
|
414 audioEngineContext.newTrack(trackURL);
|
nicholas@66
|
415
|
nicholas@66
|
416 if (commentShow) {
|
nicholas@66
|
417 // Create document objects to hold the comment boxes
|
nicholas@66
|
418 var trackComment = document.createElement('div');
|
nicholas@66
|
419 trackComment.className = 'comment-div';
|
n@154
|
420 trackComment.id = 'comment-div-'+index;
|
nicholas@66
|
421 // Create a string next to each comment asking for a comment
|
nicholas@66
|
422 var trackString = document.createElement('span');
|
n@174
|
423 trackString.innerHTML = commentBoxPrefix+' '+index;
|
nicholas@66
|
424 // Create the HTML5 comment box 'textarea'
|
nicholas@66
|
425 var trackCommentBox = document.createElement('textarea');
|
nicholas@66
|
426 trackCommentBox.rows = '4';
|
nicholas@66
|
427 trackCommentBox.cols = '100';
|
nicholas@66
|
428 trackCommentBox.name = 'trackComment'+index;
|
nicholas@66
|
429 trackCommentBox.className = 'trackComment';
|
nicholas@66
|
430 var br = document.createElement('br');
|
nicholas@66
|
431 // Add to the holder.
|
nicholas@66
|
432 trackComment.appendChild(trackString);
|
nicholas@66
|
433 trackComment.appendChild(br);
|
nicholas@66
|
434 trackComment.appendChild(trackCommentBox);
|
nicholas@66
|
435 feedbackHolder.appendChild(trackComment);
|
nicholas@66
|
436 }
|
n@34
|
437
|
n@34
|
438 // Create a slider per track
|
n@34
|
439
|
n@34
|
440 var trackSliderObj = document.createElement('div');
|
n@34
|
441 trackSliderObj.className = 'track-slider';
|
n@34
|
442 trackSliderObj.id = 'track-slider-'+index;
|
n@154
|
443
|
n@154
|
444 var trackSliderAOIndex = document.createAttribute('trackIndex');
|
n@154
|
445 trackSliderAOIndex.nodeValue = index;
|
n@154
|
446 trackSliderObj.setAttributeNode(trackSliderAOIndex);
|
n@154
|
447
|
n@34
|
448 // Distribute it randomnly
|
n@138
|
449 var w = window.innerWidth - (offset+8)*2;
|
n@34
|
450 w = Math.random()*w;
|
n@138
|
451 w = Math.floor(w+(offset+8));
|
n@138
|
452 trackSliderObj.style.left = w+'px';
|
n@34
|
453 trackSliderObj.innerHTML = '<span>'+index+'</span>';
|
n@34
|
454 trackSliderObj.draggable = true;
|
n@34
|
455 trackSliderObj.ondragend = dragEnd;
|
n@49
|
456 trackSliderObj.ondragstart = function()
|
n@49
|
457 {
|
n@154
|
458 var id = Number(event.srcElement.attributes['trackIndex'].value);
|
n@49
|
459 audioEngineContext.metric.sliderMoveStart(id);
|
n@49
|
460 };
|
n@34
|
461
|
n@34
|
462 // Onclick, switch playback to that track
|
n@34
|
463 trackSliderObj.onclick = function() {
|
nicholas@108
|
464 // Start the test on first click, that way timings are more accurate.
|
nicholas@108
|
465 audioEngineContext.play();
|
nicholas@135
|
466 if (audioEngineContext.audioObjectsReady) {
|
nicholas@135
|
467 // Cannot continue to issue play command until audioObjects reported as ready!
|
nicholas@135
|
468 // Get the track ID from the object ID
|
n@154
|
469 var id = Number(event.srcElement.attributes['trackIndex'].value);
|
nicholas@135
|
470 //audioEngineContext.metric.sliderPlayed(id);
|
nicholas@135
|
471 audioEngineContext.selectedTrack(id);
|
nicholas@135
|
472 // Currently playing track red, rest green
|
n@154
|
473
|
n@154
|
474 //document.getElementById('track-slider-'+index).style.backgroundColor = "#FF0000";
|
n@154
|
475 $('.track-slider').removeClass('track-slider-playing');
|
n@154
|
476 $(event.srcElement).addClass('track-slider-playing');
|
n@154
|
477 $('.comment-div').removeClass('comment-box-playing');
|
n@154
|
478 $('#comment-div-'+id).addClass('comment-box-playing');
|
nicholas@135
|
479 }
|
n@34
|
480 };
|
n@34
|
481
|
n@34
|
482 canvas.appendChild(trackSliderObj);
|
n@138
|
483 audioEngineContext.audioObjects[index].metric.initialised(convSliderPosToRate(index));
|
b@102
|
484
|
n@34
|
485 });
|
n@39
|
486
|
nicholas@65
|
487 // Append any commentQuestion boxes
|
nicholas@65
|
488 var commentQuestions = $(textXML).find('CommentQuestion');
|
nicholas@65
|
489 $(commentQuestions).each(function(index,element) {
|
nicholas@65
|
490 // Create document objects to hold the comment boxes
|
nicholas@65
|
491 var trackComment = document.createElement('div');
|
nicholas@65
|
492 trackComment.className = 'comment-div commentQuestion';
|
nicholas@65
|
493 trackComment.id = element.attributes['id'].value;
|
nicholas@65
|
494 // Create a string next to each comment asking for a comment
|
nicholas@65
|
495 var trackString = document.createElement('span');
|
nicholas@65
|
496 trackString.innerHTML = element.textContent;
|
nicholas@65
|
497 // Create the HTML5 comment box 'textarea'
|
nicholas@65
|
498 var trackCommentBox = document.createElement('textarea');
|
nicholas@65
|
499 trackCommentBox.rows = '4';
|
nicholas@65
|
500 trackCommentBox.cols = '100';
|
nicholas@65
|
501 trackCommentBox.name = 'commentQuestion'+index;
|
nicholas@65
|
502 trackCommentBox.className = 'trackComment';
|
nicholas@65
|
503 var br = document.createElement('br');
|
nicholas@65
|
504 // Add to the holder.
|
nicholas@65
|
505 trackComment.appendChild(trackString);
|
nicholas@65
|
506 trackComment.appendChild(br);
|
nicholas@65
|
507 trackComment.appendChild(trackCommentBox);
|
nicholas@65
|
508 feedbackHolder.appendChild(trackComment);
|
nicholas@65
|
509 });
|
n@153
|
510
|
n@153
|
511
|
n@153
|
512 testWaitIndicator();
|
n@39
|
513 }
|
n@39
|
514
|
nicholas@119
|
515
|
nicholas@5
|
516 function dragEnd(ev) {
|
nicholas@5
|
517 // Function call when a div has been dropped
|
n@33
|
518 var slider = document.getElementById('slider');
|
n@127
|
519 var marginSize = Number(slider.attributes['marginsize'].value);
|
n@51
|
520 var w = slider.style.width;
|
n@51
|
521 w = Number(w.substr(0,w.length-2));
|
nicholas@133
|
522 var x = ev.x;
|
n@127
|
523 if (x >= marginSize && x < w+marginSize) {
|
n@51
|
524 this.style.left = (x)+'px';
|
nicholas@5
|
525 } else {
|
n@127
|
526 if (x<marginSize) {
|
n@127
|
527 this.style.left = marginSize+'px';
|
nicholas@5
|
528 } else {
|
n@127
|
529 this.style.left = (w+marginSize) + 'px';
|
nicholas@5
|
530 }
|
nicholas@5
|
531 }
|
n@49
|
532 audioEngineContext.metric.sliderMoved();
|
nicholas@5
|
533 }
|
nicholas@7
|
534
|
b@101
|
535 function buttonSubmitClick() // TODO: Only when all songs have been played!
|
n@40
|
536 {
|
nicholas@107
|
537 hasBeenPlayed = audioEngineContext.checkAllPlayed();
|
nicholas@107
|
538 if (hasBeenPlayed.length == 0) {
|
nicholas@107
|
539 if (audioEngineContext.status == 1) {
|
nicholas@107
|
540 var playback = document.getElementById('playback-button');
|
nicholas@107
|
541 playback.click();
|
nicholas@107
|
542 // This function is called when the submit button is clicked. Will check for any further tests to perform, or any post-test options
|
nicholas@107
|
543 } else
|
nicholas@107
|
544 {
|
nicholas@107
|
545 if (audioEngineContext.timer.testStarted == false)
|
nicholas@107
|
546 {
|
nicholas@107
|
547 alert('You have not started the test! Please press start to begin the test!');
|
nicholas@107
|
548 return;
|
nicholas@107
|
549 }
|
nicholas@107
|
550 }
|
nicholas@129
|
551 testState.advanceState();
|
b@102
|
552 } else // if a fragment has not been played yet
|
b@102
|
553 {
|
nicholas@107
|
554 str = "";
|
nicholas@107
|
555 if (hasBeenPlayed.length > 1) {
|
nicholas@107
|
556 for (var i=0; i<hasBeenPlayed.length; i++) {
|
nicholas@107
|
557 str = str + hasBeenPlayed[i];
|
nicholas@107
|
558 if (i < hasBeenPlayed.length-2){
|
nicholas@107
|
559 str += ", ";
|
nicholas@107
|
560 } else if (i == hasBeenPlayed.length-2) {
|
nicholas@107
|
561 str += " or ";
|
nicholas@107
|
562 }
|
nicholas@107
|
563 }
|
nicholas@107
|
564 alert('You have not played fragments ' + str + ' yet. Please listen, rate and comment all samples before submitting.');
|
nicholas@107
|
565 } else {
|
nicholas@107
|
566 alert('You have not played fragment ' + hasBeenPlayed[0] + ' yet. Please listen, rate and comment all samples before submitting.');
|
nicholas@107
|
567 }
|
b@102
|
568 return;
|
b@102
|
569 }
|
n@40
|
570 }
|
n@40
|
571
|
n@51
|
572 function convSliderPosToRate(id)
|
n@51
|
573 {
|
n@51
|
574 var w = document.getElementById('slider').style.width;
|
n@127
|
575 var marginsize = Number(document.getElementById('slider').attributes['marginsize'].value);
|
n@51
|
576 var maxPix = w.substr(0,w.length-2);
|
n@51
|
577 var slider = document.getElementsByClassName('track-slider')[id];
|
n@51
|
578 var pix = slider.style.left;
|
n@51
|
579 pix = pix.substr(0,pix.length-2);
|
n@127
|
580 var rate = (pix-marginsize)/maxPix;
|
n@51
|
581 return rate;
|
n@51
|
582 }
|
n@51
|
583
|
n@127
|
584 function resizeWindow(event){
|
n@127
|
585 // Function called when the window has been resized.
|
n@127
|
586 // MANDATORY FUNCTION
|
n@127
|
587
|
n@127
|
588 // Store the slider marker values
|
n@127
|
589 var holdValues = [];
|
n@127
|
590 $(".track-slider").each(function(index,sliderObj){
|
n@127
|
591 holdValues.push(convSliderPosToRate(index));
|
n@127
|
592 });
|
n@127
|
593
|
n@127
|
594 var width = event.target.innerWidth;
|
n@127
|
595 var canvas = document.getElementById('sliderCanvasHolder');
|
n@127
|
596 var sliderDiv = canvas.children[0];
|
n@127
|
597 var sliderScaleDiv = canvas.children[1];
|
n@127
|
598 var marginsize = Number(sliderDiv.attributes['marginsize'].value);
|
n@127
|
599 var w = (marginsize+8)*2;
|
n@127
|
600 sliderDiv.style.width = width - w + 'px';
|
n@127
|
601 var width = width - w;
|
n@127
|
602 // Move sliders into new position
|
n@127
|
603 $(".track-slider").each(function(index,sliderObj){
|
n@127
|
604 var pos = holdValues[index];
|
n@127
|
605 var pix = pos * width;
|
n@127
|
606 sliderObj.style.left = pix+marginsize+'px';
|
n@127
|
607 });
|
n@127
|
608
|
n@127
|
609 // Move scale labels
|
n@127
|
610 $(sliderScaleDiv.children).each(function(index,scaleObj){
|
n@127
|
611 var position = Number(scaleObj.attributes['value'].value);
|
n@127
|
612 var pixelPosition = (position*width)+marginsize;
|
n@127
|
613 scaleObj.style.left = Math.floor((pixelPosition-($(scaleObj).width()/2)))+'px';
|
n@127
|
614 });
|
n@127
|
615 }
|
n@127
|
616
|
nicholas@129
|
617 function pageXMLSave(store, testXML, testId)
|
n@44
|
618 {
|
n@44
|
619 // Saves a specific test page
|
nicholas@129
|
620 var xmlDoc = store;
|
n@50
|
621 // Check if any session wide metrics are enabled
|
nicholas@67
|
622
|
nicholas@129
|
623 var commentShow = testXML.attributes['elementComments'];
|
nicholas@67
|
624 if (commentShow != undefined) {
|
nicholas@67
|
625 if (commentShow.value == 'false') {commentShow = false;}
|
nicholas@67
|
626 else {commentShow = true;}
|
nicholas@67
|
627 } else {commentShow = true;}
|
nicholas@67
|
628
|
n@50
|
629 var metric = document.createElement('metric');
|
n@50
|
630 if (audioEngineContext.metric.enableTestTimer)
|
n@50
|
631 {
|
n@50
|
632 var testTime = document.createElement('metricResult');
|
n@50
|
633 testTime.id = 'testTime';
|
n@50
|
634 testTime.textContent = audioEngineContext.timer.testDuration;
|
n@50
|
635 metric.appendChild(testTime);
|
n@50
|
636 }
|
n@50
|
637 xmlDoc.appendChild(metric);
|
n@45
|
638 var trackSliderObjects = document.getElementsByClassName('track-slider');
|
n@45
|
639 var commentObjects = document.getElementsByClassName('comment-div');
|
n@45
|
640 for (var i=0; i<trackSliderObjects.length; i++)
|
n@45
|
641 {
|
n@45
|
642 var audioElement = document.createElement('audioElement');
|
n@45
|
643 audioElement.id = currentTrackOrder[i].attributes['id'].value;
|
n@45
|
644 audioElement.url = currentTrackOrder[i].attributes['url'].value;
|
n@51
|
645 var value = document.createElement('value');
|
n@51
|
646 value.innerHTML = convSliderPosToRate(i);
|
nicholas@67
|
647 if (commentShow) {
|
nicholas@67
|
648 var comment = document.createElement("comment");
|
nicholas@67
|
649 var question = document.createElement("question");
|
nicholas@67
|
650 var response = document.createElement("response");
|
nicholas@67
|
651 question.textContent = commentObjects[i].children[0].textContent;
|
nicholas@67
|
652 response.textContent = commentObjects[i].children[2].value;
|
b@115
|
653 console.log('Comment ' + i + ': ' + commentObjects[i].children[2].value); // DEBUG/SAFETY
|
nicholas@67
|
654 comment.appendChild(question);
|
nicholas@67
|
655 comment.appendChild(response);
|
nicholas@67
|
656 audioElement.appendChild(comment);
|
nicholas@67
|
657 }
|
n@45
|
658 audioElement.appendChild(value);
|
n@50
|
659 // Check for any per element metrics
|
n@50
|
660 var metric = document.createElement('metric');
|
n@50
|
661 var elementMetric = audioEngineContext.audioObjects[i].metric;
|
n@50
|
662 if (audioEngineContext.metric.enableElementTimer) {
|
n@50
|
663 var elementTimer = document.createElement('metricResult');
|
n@50
|
664 elementTimer.id = 'elementTimer';
|
n@50
|
665 elementTimer.textContent = elementMetric.listenedTimer;
|
n@50
|
666 metric.appendChild(elementTimer);
|
n@50
|
667 }
|
n@50
|
668 if (audioEngineContext.metric.enableElementTracker) {
|
n@50
|
669 var elementTrackerFull = document.createElement('metricResult');
|
n@50
|
670 elementTrackerFull.id = 'elementTrackerFull';
|
n@50
|
671 var data = elementMetric.movementTracker;
|
n@50
|
672 for (var k=0; k<data.length; k++)
|
n@50
|
673 {
|
n@50
|
674 var timePos = document.createElement('timePos');
|
n@50
|
675 timePos.id = k;
|
n@50
|
676 var time = document.createElement('time');
|
n@50
|
677 time.textContent = data[k][0];
|
n@50
|
678 var position = document.createElement('position');
|
n@50
|
679 position.textContent = data[k][1];
|
n@50
|
680 timePos.appendChild(time);
|
n@50
|
681 timePos.appendChild(position);
|
n@50
|
682 elementTrackerFull.appendChild(timePos);
|
n@50
|
683 }
|
n@50
|
684 metric.appendChild(elementTrackerFull);
|
n@50
|
685 }
|
n@164
|
686 if (audioEngineContext.metric.enableElementListenTracker) {
|
n@164
|
687 var elementListenTracker = document.createElement('metricResult');
|
n@164
|
688 elementListenTracker.id = 'elementListenTracker';
|
n@164
|
689 var obj = elementMetric.listenTracker;
|
n@164
|
690 for (var k=0; k<obj.length; k++) {
|
n@164
|
691 elementListenTracker.appendChild(obj[k]);
|
n@164
|
692 }
|
n@164
|
693 metric.appendChild(elementListenTracker);
|
n@164
|
694 }
|
n@50
|
695 if (audioEngineContext.metric.enableElementInitialPosition) {
|
n@50
|
696 var elementInitial = document.createElement('metricResult');
|
n@50
|
697 elementInitial.id = 'elementInitialPosition';
|
n@50
|
698 elementInitial.textContent = elementMetric.initialPosition;
|
n@50
|
699 metric.appendChild(elementInitial);
|
n@50
|
700 }
|
n@50
|
701 if (audioEngineContext.metric.enableFlagListenedTo) {
|
n@50
|
702 var flagListenedTo = document.createElement('metricResult');
|
n@50
|
703 flagListenedTo.id = 'elementFlagListenedTo';
|
n@50
|
704 flagListenedTo.textContent = elementMetric.wasListenedTo;
|
n@50
|
705 metric.appendChild(flagListenedTo);
|
n@50
|
706 }
|
n@50
|
707 if (audioEngineContext.metric.enableFlagMoved) {
|
n@50
|
708 var flagMoved = document.createElement('metricResult');
|
n@50
|
709 flagMoved.id = 'elementFlagMoved';
|
n@50
|
710 flagMoved.textContent = elementMetric.wasMoved;
|
n@50
|
711 metric.appendChild(flagMoved);
|
n@50
|
712 }
|
n@50
|
713 if (audioEngineContext.metric.enableFlagComments) {
|
n@50
|
714 var flagComments = document.createElement('metricResult');
|
n@50
|
715 flagComments.id = 'elementFlagComments';
|
n@50
|
716 if (response.textContent.length == 0) {flag.textContent = 'false';}
|
n@50
|
717 else {flag.textContet = 'true';}
|
n@50
|
718 metric.appendChild(flagComments);
|
n@50
|
719 }
|
n@50
|
720 audioElement.appendChild(metric);
|
n@45
|
721 xmlDoc.appendChild(audioElement);
|
n@45
|
722 }
|
nicholas@65
|
723 var commentQuestion = document.getElementsByClassName('commentQuestion');
|
nicholas@65
|
724 for (var i=0; i<commentQuestion.length; i++)
|
nicholas@65
|
725 {
|
nicholas@65
|
726 var cqHolder = document.createElement('CommentQuestion');
|
nicholas@65
|
727 var comment = document.createElement('comment');
|
nicholas@65
|
728 var question = document.createElement('question');
|
nicholas@65
|
729 cqHolder.id = commentQuestion[i].id;
|
nicholas@65
|
730 comment.textContent = commentQuestion[i].children[2].value;
|
nicholas@65
|
731 question.textContent = commentQuestion[i].children[0].textContent;
|
n@124
|
732 console.log('Question ' + i + ': ' + commentQuestion[i].children[2].value); // DEBUG/SAFETY
|
nicholas@65
|
733 cqHolder.appendChild(question);
|
nicholas@65
|
734 cqHolder.appendChild(comment);
|
nicholas@65
|
735 xmlDoc.appendChild(cqHolder);
|
nicholas@65
|
736 }
|
nicholas@129
|
737 store = xmlDoc;
|
nicholas@67
|
738 } |