nicholas@2: /** nicholas@2: * ape.js nicholas@2: * Create the APE interface nicholas@2: */ nicholas@2: nicholas@2: // Once this is loaded and parsed, begin execution nicholas@2: loadInterface(projectXML); nicholas@2: nicholas@2: function loadInterface(xmlDoc) { nicholas@2: nicholas@2: var width = window.innerWidth; nicholas@2: var height = window.innerHeight; nicholas@2: nicholas@2: // Set background to grey #ddd nicholas@2: document.getElementsByTagName('body')[0].style.backgroundColor = '#ddd'; nicholas@2: nicholas@2: // The injection point into the HTML page nicholas@2: var insertPoint = document.getElementById("topLevelBody"); nicholas@2: nicholas@7: nicholas@2: // Decode parts of the xmlDoc that are needed nicholas@2: // xmlDoc MUST already be parsed by jQuery! nicholas@2: var xmlSetup = xmlDoc.find('setup'); nicholas@2: // Should put in an error function here incase of malprocessed or malformed XML nicholas@2: nicholas@2: // Create the top div for the Title element nicholas@2: var titleAttr = xmlSetup[0].attributes['title']; nicholas@2: var title = document.createElement('div'); nicholas@2: title.className = "title"; nicholas@2: title.align = "center"; nicholas@2: var titleSpan = document.createElement('span'); nicholas@2: nicholas@2: // Set title to that defined in XML, else set to default nicholas@2: if (titleAttr != undefined) { nicholas@2: titleSpan.innerText = titleAttr.value; nicholas@2: } else { nicholas@2: titleSpan.innerText = 'APE Tool'; nicholas@2: } nicholas@2: // Insert the titleSpan element into the title div element. nicholas@2: title.appendChild(titleSpan); nicholas@2: nicholas@7: // Store the return URL path in global projectReturn nicholas@7: projectReturn = xmlSetup[0].attributes['projectReturn'].value; nicholas@7: nicholas@7: // Create Interface buttons! nicholas@7: var interfaceButtons = document.createElement('div'); nicholas@7: interfaceButtons.id = 'interface-buttons'; nicholas@7: nicholas@7: // MANUAL DOWNLOAD POINT nicholas@7: // If project return is null, this MUST be specified as the location to create the download link nicholas@7: var downloadPoint = document.createElement('div'); nicholas@7: downloadPoint.id = 'download-point'; nicholas@7: nicholas@7: // Create playback start/stop points nicholas@7: var playback = document.createElement("button"); nicholas@7: playback.innerText = 'Start'; nicholas@7: playback.onclick = function() { nicholas@7: if (audioEngineContext.status == 0) { nicholas@7: audioEngineContext.play(); nicholas@7: this.innerText = 'Stop'; nicholas@7: } else { nicholas@7: audioEngineContext.stop(); nicholas@7: this.innerText = 'Start'; nicholas@7: } nicholas@7: } nicholas@7: // Create Submit (save) button nicholas@7: var submit = document.createElement("button"); nicholas@7: submit.innerText = 'Submit'; nicholas@7: submit.onclick = function() { nicholas@7: createProjectSave(projectReturn) nicholas@7: } nicholas@7: nicholas@7: interfaceButtons.appendChild(playback); nicholas@7: interfaceButtons.appendChild(submit); nicholas@7: interfaceButtons.appendChild(downloadPoint); nicholas@7: nicholas@2: // Now create the slider and HTML5 canvas boxes nicholas@2: nicholas@2: var sliderBox = document.createElement('div'); nicholas@2: sliderBox.className = 'sliderCanvasDiv'; nicholas@2: sliderBox.id = 'sliderCanvasHolder'; // create an id so we can easily link to it later nicholas@2: sliderBox.align = 'center'; nicholas@2: nicholas@5: var canvas = document.createElement('div'); nicholas@2: canvas.id = 'slider'; nicholas@5: canvas.style.width = width - 100 +"px"; nicholas@5: canvas.style.height = 150 + "px"; nicholas@5: canvas.style.marginBottom = "25px" nicholas@2: canvas.style.backgroundColor = '#eee'; nicholas@5: canvas.align = "left"; nicholas@2: sliderBox.appendChild(canvas); nicholas@5: nicholas@3: var feedbackHolder = document.createElement('div'); nicholas@3: nicholas@7: var tracks = xmlDoc.find('tracks'); nicholas@7: tracks = tracks[0]; nicholas@7: var hostURL = tracks.attributes['hostURL']; nicholas@7: if (hostURL == undefined) { nicholas@7: hostURL = ""; nicholas@7: } else { nicholas@7: hostURL = hostURL.value; nicholas@7: } nicholas@7: nicholas@7: var hostFs = tracks.attributes['sampleRate']; nicholas@7: var hostFsExplicit = tracks.attributes['sampleRateExplicit']; nicholas@7: if (hostFs == undefined) { nicholas@7: hostFsExplicit = false; nicholas@7: } else { nicholas@7: hostFs = hostFs.value; nicholas@7: if (hostFsExplicit != undefined) { nicholas@7: hostFsExplicit = hostFsExplicit.value; nicholas@7: } nicholas@7: } nicholas@7: nicholas@7: /// CHECK FOR SAMPLE RATE COMPATIBILITY nicholas@7: if (hostFsExplicit == true) { nicholas@7: if (Number(hostFs) != audioContext.sampleRate) { nicholas@7: 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@7: alert(errStr); nicholas@7: return; nicholas@7: } nicholas@7: } nicholas@7: nicholas@3: var tracksXML = xmlDoc.find('track'); nicholas@3: tracksXML.each(function(index,element){ nicholas@7: // Find URL of track nicholas@7: var trackURL = hostURL + this.attributes['url'].value; nicholas@7: // Now load each track in nicholas@7: audioEngineContext.newTrack(trackURL); nicholas@3: var trackObj = document.createElement('div'); nicholas@3: var trackTitle = document.createElement('span'); nicholas@3: trackTitle.innerText = 'Comment on track '+index; nicholas@3: var trackComment = document.createElement('textarea'); nicholas@3: trackComment.rows = '4'; nicholas@3: trackComment.cols = '100'; nicholas@3: trackComment.name = 'trackComment'+index; nicholas@3: trackComment.className = 'trackComment'; nicholas@3: feedbackHolder.appendChild(trackTitle); nicholas@3: feedbackHolder.appendChild(trackComment); nicholas@3: feedbackHolder.appendChild(trackObj); nicholas@5: // Create a slider per track nicholas@5: nicholas@5: var trackSliderObj = document.createElement('div'); nicholas@5: trackSliderObj.className = 'track-slider'; nicholas@5: trackSliderObj.id = 'track-slider-'+index; nicholas@5: trackSliderObj.style.position = 'absolute'; nicholas@5: // Distribute it randomnly nicholas@5: var w = window.innerWidth - 100; nicholas@5: w = Math.random()*w; nicholas@5: trackSliderObj.style.left = Math.floor(w)+50+'px'; nicholas@5: trackSliderObj.style.height = "150px"; nicholas@5: trackSliderObj.style.width = "10px"; nicholas@5: trackSliderObj.style.backgroundColor = 'rgb(100,200,100)'; nicholas@5: trackSliderObj.innerHTML = ''+index+''; nicholas@5: trackSliderObj.style.float = "left"; nicholas@5: trackSliderObj.draggable = true; nicholas@5: trackSliderObj.ondragend = dragEnd; nicholas@8: nicholas@8: // Onclick, switch playback to that track nicholas@8: trackSliderObj.onclick = function() { nicholas@8: // Get the track ID from the object ID nicholas@8: var id = Number(this.id.substr(13,2)); // Maximum theoretical tracks is 99! nicholas@8: audioEngineContext.selectedTrack(id); nicholas@8: } nicholas@8: nicholas@5: canvas.appendChild(trackSliderObj); nicholas@3: }) nicholas@3: nicholas@2: nicholas@2: // Inject into HTML nicholas@2: insertPoint.innerHTML = null; // Clear the current schema nicholas@2: insertPoint.appendChild(title); // Insert the title nicholas@7: insertPoint.appendChild(interfaceButtons); nicholas@2: insertPoint.appendChild(sliderBox); nicholas@3: insertPoint.appendChild(feedbackHolder); nicholas@2: } nicholas@5: nicholas@5: function dragEnd(ev) { nicholas@5: // Function call when a div has been dropped nicholas@5: if (ev.x >= 50 && ev.x < window.innerWidth-50) { nicholas@5: this.style.left = (ev.x)+'px'; nicholas@5: } else { nicholas@5: if (ev.x<50) { nicholas@5: this.style.left = '50px'; nicholas@5: } else { nicholas@5: this.style.left = window.innerWidth-50 + 'px'; nicholas@5: } nicholas@5: } nicholas@5: } nicholas@7: nicholas@7: // Only other global function which must be defined in the interface class. Determines how to create the XML document. nicholas@7: function interfaceXMLSave(){ nicholas@7: // Create the XML string to be exported with results nicholas@7: var xmlDoc = document.createElement("BrowserEvaluationResult"); nicholas@7: var trackSliderObjects = document.getElementsByClassName('track-slider'); nicholas@7: var commentObjects = document.getElementsByClassName('trackComment'); nicholas@7: var rateMin = 50; nicholas@7: var rateMax = window.innerWidth-50; nicholas@7: for (var i=0; i