comparison ape.js @ 693:a74cb47e779c

Creation of experimental variables and control methods document
author Dave Moffat <djmoffat@users.noreply.github.com>
date Thu, 09 Apr 2015 12:59:46 +0100
parents
children 869ed636f7c0 375410a5571d
comparison
equal deleted inserted replaced
-1:000000000000 693:a74cb47e779c
1 /**
2 * ape.js
3 * Create the APE interface
4 */
5
6 // Once this is loaded and parsed, begin execution
7 loadInterface(projectXML);
8
9 function loadInterface(xmlDoc) {
10
11 // Get the dimensions of the screen available to the page
12 var width = window.innerWidth;
13 var height = window.innerHeight;
14
15 // Set background to grey #ddd
16 document.getElementsByTagName('body')[0].style.backgroundColor = '#ddd';
17
18 // The injection point into the HTML page
19 var insertPoint = document.getElementById("topLevelBody");
20
21
22 // Decode parts of the xmlDoc that are needed
23 // xmlDoc MUST already be parsed by jQuery!
24 var xmlSetup = xmlDoc.find('setup');
25 // Should put in an error function here incase of malprocessed or malformed XML
26
27 // Create the top div for the Title element
28 var titleAttr = xmlSetup[0].attributes['title'];
29 var title = document.createElement('div');
30 title.className = "title";
31 title.align = "center";
32 var titleSpan = document.createElement('span');
33
34 // Set title to that defined in XML, else set to default
35 if (titleAttr != undefined) {
36 titleSpan.innerText = titleAttr.value;
37 } else {
38 titleSpan.innerText = 'APE Tool';
39 }
40 // Insert the titleSpan element into the title div element.
41 title.appendChild(titleSpan);
42
43 // Store the return URL path in global projectReturn
44 projectReturn = xmlSetup[0].attributes['projectReturn'].value;
45
46 // Create Interface buttons!
47 var interfaceButtons = document.createElement('div');
48 interfaceButtons.id = 'interface-buttons';
49
50 // MANUAL DOWNLOAD POINT
51 // If project return is null, this MUST be specified as the location to create the download link
52 var downloadPoint = document.createElement('div');
53 downloadPoint.id = 'download-point';
54
55 // Create playback start/stop points
56 var playback = document.createElement("button");
57 playback.innerText = 'Start';
58 // onclick function. Check if it is playing or not, call the correct function in the
59 // audioEngine, change the button text to reflect the next state.
60 playback.onclick = function() {
61 if (audioEngineContext.status == 0) {
62 audioEngineContext.play();
63 this.innerText = 'Stop';
64 } else {
65 audioEngineContext.stop();
66 this.innerText = 'Start';
67 }
68 };
69 // Create Submit (save) button
70 var submit = document.createElement("button");
71 submit.innerText = 'Submit';
72 submit.onclick = function() {
73 // TODO: Update this for postTest tags
74 createProjectSave(projectReturn)
75 };
76 // Append the interface buttons into the interfaceButtons object.
77 interfaceButtons.appendChild(playback);
78 interfaceButtons.appendChild(submit);
79 interfaceButtons.appendChild(downloadPoint);
80
81 // Now create the slider and HTML5 canvas boxes
82
83 // Create the div box to center align
84 var sliderBox = document.createElement('div');
85 sliderBox.className = 'sliderCanvasDiv';
86 sliderBox.id = 'sliderCanvasHolder';
87 sliderBox.align = 'center';
88
89 // Create the slider box to hold the slider elements
90 var canvas = document.createElement('div');
91 canvas.id = 'slider';
92 // Must have a known EXACT width, as this is used later to determine the ratings
93 canvas.style.width = width - 100 +"px";
94 canvas.style.height = 150 + "px";
95 canvas.style.marginBottom = "25px";
96 canvas.style.backgroundColor = '#eee';
97 canvas.align = "left";
98 sliderBox.appendChild(canvas);
99
100 // Global parent for the comment boxes on the page
101 var feedbackHolder = document.createElement('div');
102 // Find the parent audioHolder object.
103 var audioHolder = xmlDoc.find('audioHolder');
104 audioHolder = audioHolder[0]; // Remove from one field array
105 // Extract the hostURL attribute. If not set, create an empty string.
106 var hostURL = audioHolder.attributes['hostURL'];
107 if (hostURL == undefined) {
108 hostURL = "";
109 } else {
110 hostURL = hostURL.value;
111 }
112 // Extract the sampleRate. If set, convert the string to a Number.
113 var hostFs = audioHolder.attributes['sampleRate'];
114 if (hostFs != undefined) {
115 hostFs = Number(hostFs.value);
116 }
117
118 /// CHECK FOR SAMPLE RATE COMPATIBILITY
119 if (hostFs != undefined) {
120 if (Number(hostFs) != audioContext.sampleRate) {
121 var errStr = 'Sample rates do not match! Requested '+Number(hostFs)+', got '+audioContext.sampleRate+'. Please set the sample rate to match before completing this test.';
122 alert(errStr);
123 return;
124 }
125 }
126 // Find all the audioElements from the audioHolder
127 var audioElements = $(audioHolder).find('audioElements');
128 audioElements.each(function(index,element){
129 // Find URL of track
130 // In this jQuery loop, variable 'this' holds the current audioElement.
131
132 // Now load each audio sample. First create the new track by passing the full URL
133 var trackURL = hostURL + this.attributes['url'].value;
134 audioEngineContext.newTrack(trackURL);
135 // Create document objects to hold the comment boxes
136 var trackComment = document.createElement('div');
137 // Create a string next to each comment asking for a comment
138 var trackString = document.createElement('span');
139 trackString.innerText = 'Comment on track '+index;
140 // Create the HTML5 comment box 'textarea'
141 var trackCommentBox = document.createElement('textarea');
142 trackCommentBox.rows = '4';
143 trackCommentBox.cols = '100';
144 trackCommentBox.name = 'trackComment'+index;
145 trackCommentBox.className = 'trackComment';
146 // Add to the holder.
147 trackComment.appendChild(trackString);
148 trackComment.appendChild(trackCommentBox);
149 feedbackHolder.appendChild(trackComment);
150
151 // Create a slider per track
152
153 var trackSliderObj = document.createElement('div');
154 trackSliderObj.className = 'track-slider';
155 trackSliderObj.id = 'track-slider-'+index;
156 trackSliderObj.style.position = 'absolute';
157 // Distribute it randomnly
158 var w = window.innerWidth - 100;
159 w = Math.random()*w;
160 trackSliderObj.style.left = Math.floor(w)+50+'px';
161 trackSliderObj.style.height = "150px";
162 trackSliderObj.style.width = "10px";
163 trackSliderObj.style.backgroundColor = 'rgb(100,200,100)';
164 trackSliderObj.innerHTML = '<span>'+index+'</span>';
165 trackSliderObj.style.float = "left";
166 trackSliderObj.draggable = true;
167 trackSliderObj.ondragend = dragEnd;
168
169 // Onclick, switch playback to that track
170 trackSliderObj.onclick = function() {
171 // Get the track ID from the object ID
172 var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99!
173 audioEngineContext.selectedTrack(id);
174 };
175
176 canvas.appendChild(trackSliderObj);
177 });
178
179
180 // Inject into HTML
181 insertPoint.innerHTML = null; // Clear the current schema
182 insertPoint.appendChild(title); // Insert the title
183 insertPoint.appendChild(interfaceButtons);
184 insertPoint.appendChild(sliderBox);
185 insertPoint.appendChild(feedbackHolder);
186 }
187
188 function dragEnd(ev) {
189 // Function call when a div has been dropped
190 if (ev.x >= 50 && ev.x < window.innerWidth-50) {
191 this.style.left = (ev.x)+'px';
192 } else {
193 if (ev.x<50) {
194 this.style.left = '50px';
195 } else {
196 this.style.left = window.innerWidth-50 + 'px';
197 }
198 }
199 }
200
201 // Only other global function which must be defined in the interface class. Determines how to create the XML document.
202 function interfaceXMLSave(){
203 // Create the XML string to be exported with results
204 var xmlDoc = document.createElement("BrowserEvaluationResult");
205 var trackSliderObjects = document.getElementsByClassName('track-slider');
206 var commentObjects = document.getElementsByClassName('trackComment');
207 var rateMin = 50;
208 var rateMax = window.innerWidth-50;
209 for (var i=0; i<trackSliderObjects.length; i++)
210 {
211 var trackObj = document.createElement("Track");
212 trackObj.id = i;
213 var slider = document.createElement("Rating");
214 var rate = Number(trackSliderObjects[i].style.left.substr(0,trackSliderObjects[i].style.left.length-2));
215 rate = (rate-rateMin)/rateMax;
216 slider.innerText = Math.floor(rate*100);
217 var comment = document.createElement("Comment");
218 comment.innerText = commentObjects[i].value;
219 trackObj.appendChild(slider);
220 trackObj.appendChild(comment);
221 xmlDoc.appendChild(trackObj);
222 }
223
224 return xmlDoc;
225 }
226