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