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