comparison interfaces/ABX.js @ 619:c99d334d8534 Dev_main

WIP. ABX Framework. Minor core.js modifications.
author Nicholas Jillings <n.g.r.jillings@se14.qmul.ac.uk>
date Wed, 16 Mar 2016 13:31:42 +0000
parents
children 85f2fc7a17ca
comparison
equal deleted inserted replaced
618:4a221e761384 619:c99d334d8534
1 /**
2 * WAET Blank Template
3 * Use this to start building your custom interface
4 */
5
6 // Once this is loaded and parsed, begin execution
7 loadInterface();
8
9 function loadInterface() {
10 // Use this to do any one-time page / element construction. For instance, placing any stationary text objects,
11 // holding div's, or setting up any nodes which are present for the entire test sequence
12
13 // Custom comparator Object
14 Interface.prototype.comparator = null;
15
16 // The injection point into the HTML page
17 interfaceContext.insertPoint = document.getElementById("topLevelBody");
18 var testContent = document.createElement('div');
19 testContent.id = 'testContent';
20
21 // Create the top div for the Title element
22 var titleAttr = specification.title;
23 var title = document.createElement('div');
24 title.className = "title";
25 title.align = "center";
26 var titleSpan = document.createElement('span');
27
28 // Set title to that defined in XML, else set to default
29 if (titleAttr != undefined) {
30 titleSpan.textContent = titleAttr;
31 } else {
32 titleSpan.textContent = 'Listening test';
33 }
34 // Insert the titleSpan element into the title div element.
35 title.appendChild(titleSpan);
36
37 var pagetitle = document.createElement('div');
38 pagetitle.className = "pageTitle";
39 pagetitle.align = "center";
40 var titleSpan = document.createElement('span');
41 titleSpan.id = "pageTitle";
42 pagetitle.appendChild(titleSpan);
43
44 // Create Interface buttons!
45 var interfaceButtons = document.createElement('div');
46 interfaceButtons.id = 'interface-buttons';
47 interfaceButtons.style.height = '25px';
48
49 // Create playback start/stop points
50 var playback = document.createElement("button");
51 playback.innerHTML = 'Stop';
52 playback.id = 'playback-button';
53 playback.style.float = 'left';
54 // onclick function. Check if it is playing or not, call the correct function in the
55 // audioEngine, change the button text to reflect the next state.
56 playback.onclick = function() {
57 if (audioEngineContext.status == 1) {
58 audioEngineContext.stop();
59 this.innerHTML = 'Stop';
60 var time = audioEngineContext.timer.getTestTime();
61 console.log('Stopped at ' + time); // DEBUG/SAFETY
62 }
63 };
64 // Append the interface buttons into the interfaceButtons object.
65 interfaceButtons.appendChild(playback);
66
67 // Global parent for the comment boxes on the page
68 var feedbackHolder = document.createElement('div');
69 feedbackHolder.id = 'feedbackHolder';
70
71 // Construct the AB Boxes
72 var boxes = document.createElement('div');
73 boxes.align = "center";
74 boxes.id = "box-holders";
75 boxes.style.float = "left";
76
77 var submit = document.createElement('button');
78 submit.id = "submit";
79 submit.onclick = buttonSubmitClick;
80 submit.className = "big-button";
81 submit.textContent = "submit";
82 submit.style.position = "relative";
83 submit.style.left = (window.innerWidth-250)/2 + 'px';
84
85 feedbackHolder.appendChild(boxes);
86
87 // Inject into HTML
88 testContent.appendChild(title); // Insert the title
89 testContent.appendChild(pagetitle);
90 testContent.appendChild(interfaceButtons);
91 testContent.appendChild(feedbackHolder);
92 testContent.appendChild(submit);
93 interfaceContext.insertPoint.appendChild(testContent);
94
95 // Load the full interface
96 testState.initialise();
97 testState.advanceState();
98 };
99
100 function loadTest(page)
101 {
102 // Called each time a new test page is to be build. The page specification node is the only item passed in
103 interfaceContext.comparator = new comparator(page);
104 }
105
106 function comparator(page)
107 {
108 // Build prototype constructor
109 this.interfaceObject = function(element,label)
110 {
111 // An example node, you can make this however you want for each audioElement.
112 // However, every audioObject (audioEngineContext.audioObject) MUST have an interface object with the following
113 // You attach them by calling audioObject.bindInterface( )
114 this.parent = element;
115 this.id = element.id;
116 this.value = 0;
117 this.disabled = true;
118 this.box = document.createElement('div');
119 this.box.className = 'comparator-holder';
120 this.box.setAttribute('track-id',element.id);
121 this.box.id = 'comparator-'+label;
122 this.selector = document.createElement('div');
123 this.selector.className = 'comparator-selector disabled';
124 var selectorText = document.createElement('span');
125 selectorText.textContent = label;
126 this.selector.appendChild(selectorText);
127 this.playback = document.createElement('button');
128 this.playback.className = 'comparator-button';
129 this.playback.disabled = true;
130 this.playback.textContent = "Listen";
131 this.box.appendChild(this.selector);
132 this.box.appendChild(this.playback);
133 this.selector.onclick = function(event)
134 {
135 var label = event.currentTarget.children[0].textContent;
136 if (label == "X" || label == "x") {return;}
137 var time = audioEngineContext.timer.getTestTime();
138 if ($(event.currentTarget).hasClass('disabled'))
139 {
140 console.log("Please wait until sample has loaded");
141 return;
142 }
143 if (audioEngineContext.status == 0)
144 {
145 alert("Please listen to the samples before making a selection");
146 console.log("Please listen to the samples before making a selection");
147 return;
148 }
149 var id = event.currentTarget.parentElement.getAttribute('track-id');
150 interfaceContext.comparator.selected = id;
151 if ($(event.currentTarget).hasClass("selected")) {
152 $(".comparator-selector").removeClass('selected');
153 for (var i=0; i<interfaceContext.comparator.comparators.length; i++)
154 {
155 var obj = interfaceContext.comparator.comparators[i];
156 obj.parent.metric.moved(time,0);
157 }
158 } else {
159 $(".comparator-selector").removeClass('selected');
160 $(event.currentTarget).addClass('selected');
161 for (var i=0; i<interfaceContext.comparator.comparators.length; i++)
162 {
163 var obj = interfaceContext.comparator.comparators[i];
164 if (i == id) {
165 obj.value = 1;
166 } else {
167 obj.value = 0;
168 }
169 obj.parent.metric.moved(time,obj.value);
170 }
171 console.log("Selected "+id+' ('+time+')');
172 }
173 };
174 this.playback.setAttribute("playstate","ready");
175 this.playback.onclick = function(event)
176 {
177 var id = event.currentTarget.parentElement.getAttribute('track-id');
178 if (event.currentTarget.getAttribute("playstate") == "ready")
179 {
180 audioEngineContext.play(id);
181 } else if (event.currentTarget.getAttribute("playstate") == "playing") {
182 audioEngineContext.stop();
183 }
184
185 };
186 this.enable = function()
187 {
188 // This is used to tell the interface object that playback of this node is ready
189 if (this.parent.state == 1)
190 {
191 $(this.selector).removeClass('disabled');
192 this.playback.disabled = false;
193 }
194 };
195 this.updateLoading = function(progress)
196 {
197 // progress is a value from 0 to 100 indicating the current download state of media files
198 if (progress != 100)
199 {
200 progress = String(progress);
201 progress = progress.split('.')[0];
202 this.playback.textContent = progress+'%';
203 } else {
204 this.playback.textContent = "Play";
205 }
206 };
207 this.error = function() {
208 // audioObject has an error!!
209 this.playback.textContent = "Error";
210 $(this.playback).addClass("error-colour");
211 };
212 this.startPlayback = function()
213 {
214 // Called when playback has begun
215 $('.comparator-button').text('Listen');
216 $(this.playback).text('Stop');
217 this.playback.setAttribute("playstate","playing");
218 };
219 this.stopPlayback = function()
220 {
221 // Called when playback has stopped. This gets called even if playback never started!
222 $(this.playback).text('Listen');
223 this.playback.setAttribute("playstate","ready");
224 };
225 this.getValue = function()
226 {
227 // Return the current value of the object. If there is no value, return 0
228 return this.value;
229 };
230 this.getPresentedId = function()
231 {
232 // Return the presented ID of the object. For instance, the APE has sliders starting from 0. Whilst AB has alphabetical scale
233 return this.selector.children[0].textContent;
234 };
235 this.canMove = function()
236 {
237 // Return either true or false if the interface object can be moved. AB / Reference cannot, whilst sliders can and therefore have a continuous scale.
238 // These are checked primarily if the interface check option 'fragmentMoved' is enabled.
239 return false;
240 };
241 this.exportXMLDOM = function(audioObject) {
242 // Called by the audioObject holding this element to export the interface <value> node.
243 // If there is no value node (such as outside reference), return null
244 // If there are multiple value nodes (such as multiple scale / 2D scales), return an array of nodes with each value node having an 'interfaceName' attribute
245 // Use storage.document.createElement('value'); to generate the XML node.
246 var node = storage.document.createElement('value');
247 node.textContent = this.value;
248 return node;
249
250 };
251 this.error = function() {
252 // If there is an error with the audioObject, this will be called to indicate a failure
253 }
254 };
255 // Ensure there are only two comparisons per page
256 if (page.audioElements.length != 2) {
257 console.error('FATAL - There must be 2 <audioelement> nodes on each <page>: '+page.id);
258 return;
259 }
260 // Build the three audio elements
261 this.pair = [];
262 this.X = null;
263 this.boxHolders = document.getElementById('box-holders');
264 for (var index=0; index<page.audioElements.length; index++) {
265 var element = page.audioElements[index];
266 if (element.type != 'normal')
267 {
268 console.log("WARNING - ABX can only have normal elements. Page "+page.id+", Element "+element.id);
269 element.type = "normal";
270 }
271 var audioObject = audioEngineContext.newTrack(element);
272 var label;
273 switch(audioObject.specification.parent.label) {
274 case "none":
275 label = "";
276 break;
277 case "number":
278 label = ""+index;
279 break;
280 case "letter":
281 label = String.fromCharCode(97 + index);
282 break;
283 default:
284 label = String.fromCharCode(65 + index);
285 break;
286 }
287 var node = new this.interfaceObject(audioObject,label);
288 audioObject.bindInterface(node);
289 this.pair.push(node);
290 this.boxHolders.appendChild(node.box);
291 }
292 var elementId = Math.floor(Math.random() * 2); //Randomly pick A or B to be X
293 var element = new page.audioElementNode();
294 for (var atr in page.audioElements[elementId]) {
295 eval("element."+atr+" = page.audioElements[elementId]."+atr);
296 }
297 element.id += "-X";
298 if (typeof element.name == "string") {element.name+="-X";}
299 page.audioElements.push(element);
300 // Create the save place-holder for the 'X' element
301 var root = testState.currentStore.XMLDOM;
302 var aeNode = storage.document.createElement('audioelement');
303 aeNode.setAttribute('ref',element.id);
304 if (typeof element.name == "string"){aeNode.setAttribute('name',element.name);}
305 aeNode.setAttribute('type','normal');
306 aeNode.setAttribute('url',element.url);
307 aeNode.setAttribute('gain',element.gain);
308 aeNode.appendChild(storage.document.createElement('metric'));
309 root.appendChild(aeNode);
310 // Build the 'X' element
311 var audioObject = audioEngineContext.newTrack(element);
312 var label;
313 switch(audioObject.specification.parent.label) {
314 case "letter":
315 label = "x";
316 break;
317 default:
318 label = "X";
319 break;
320 }
321 var node = new this.interfaceObject(audioObject,label);
322 audioObject.bindInterface(node);
323 this.X = node;
324 this.boxHolders.appendChild(node.box);
325 }
326
327 function resizeWindow(event)
328 {
329 // Called on every window resize event, use this to scale your page properly
330 }
331
332 function buttonSubmitClick()
333 {
334 testState.advanceState();
335 }
336
337 function pageXMLSave(store, pageSpecification)
338 {
339 // MANDATORY
340 // Saves a specific test page
341 // You can use this space to add any extra nodes to your XML <audioHolder> saves
342 // Get the current <page> information in store (remember to appendChild your data to it)
343 // pageSpecification is the current page node configuration
344 // To create new XML nodes, use storage.document.createElement();
345 }