window.openTime = Date.now();
-
document.addEventListener("DOMContentLoaded", function () {
- window.player = new Plyr('#player');
+ var media = document.getElementById('player');
+ window.player = new Plyr(media);
+ if (media.tagName.toLowerCase() === 'audio') {
+ window.spectrum = new Spectrum(media);
+ }
+
+ var audiobigplay = document.getElementById('audiobigplay');
+ audiobigplay.addEventListener('click', function () {
+ player.play();
+ audiobigplay.style.display = 'none';
+ return false;
+ });
SCORM.init();
window.addEventListener('unload', function (event) {
finishScorm();
});
+
+function Spectrum(element) {
+ this.started = false;
+ this.element = element;
+ this.init();
+}
+
+Spectrum.prototype = {
+ init: function () {
+ var $this = this;
+
+ this.svg = document.getElementById('spectrum');
+ this.svgNS = this.svg.namespaceURI;
+ this.g = document.createElementNS(this.svgNS, "g");
+
+ window.addEventListener('resize', function () {
+ $this.resize();
+ });
+ this.resize();
+
+
+ this.fftSize = 512;
+ this.tilt = 0;
+ this.choke = 100;
+ this.c = 0;
+
+
+ this.svg.appendChild(this.g);
+
+ this.setup();
+ },
+
+ setup: function () {
+ var $this = this;
+ this.element.addEventListener('loadeddata', function (e) {
+ $this.start();
+ });
+ this.element.addEventListener('play', function (e) {
+ $this.start();
+ });
+ this.element.addEventListener('canplay', function (e) {
+ $this.start();
+ });
+
+ },
+
+ resize: function () {
+ this.width = window.innerWidth;
+ this.height = window.innerHeight - 52;
+ this.maxHeight = Math.max(this.height * 0.3, 300);
+ this.svg.setAttribute("width", this.width + "px");
+ this.svg.setAttribute("height", this.height + "px");
+ this.svg.setAttribute("viewBox", "0 0 " + this.width + " " + this.height);
+ },
+
+ start: function () {
+ if (this.started) {
+ return;
+ }
+ this.resize();
+ this.started = true;
+
+ this.audioContext = new AudioContext();
+ this.analyser = (this.analyser || this.audioContext.createAnalyser());
+ this.analyser.minDecibels = -90;
+ this.analyser.maxDecibels = -10;
+ this.analyser.smoothingTimeConstant = 0.9;
+ this.analyser.fftSize = this.fftSize;
+
+ this.sourceNode = this.audioContext.createMediaElementSource(document.getElementById("player"));
+ this.sourceNode.connect(this.analyser);
+ this.sourceNode.connect(this.audioContext.destination);
+
+ this.update();
+ },
+
+ update: function () {
+ var $this = this;
+ try {
+ this.g.remove();
+ } catch (e) {
+
+ }
+ this.g = document.createElementNS(this.svgNS, "g");
+ var freqArray = new Uint8Array(this.analyser.frequencyBinCount);
+ this.analyser.getByteTimeDomainData(freqArray);
+
+ for (var i = 0; i < freqArray.length; i++) {
+ var v = freqArray[i];
+ this.shape(this.g, v, i + 1, freqArray.length, this.c);
+ }
+ this.svg.appendChild(this.g);
+
+ this.c += 0.5;
+ requestAnimationFrame(function () {
+ $this.update();
+ });
+ },
+
+ shape: function (g, freqValue, freqSequence, freqCount, colorSequence) {
+ var freqRatio = freqSequence / freqCount, x = (this.width - (this.tilt * 2)) * freqRatio + this.tilt,
+ y = this.height / 2;
+
+ var polyline = document.createElementNS(this.svgNS, "polyline"), // using power to increase highs and decrease lows
+ freqRatio = freqValue / 255, throttledRatio = (freqValue - this.choke) / (255 - this.choke),
+ strokeWidth = this.width / freqCount * 0.6 * throttledRatio,
+ throttledY = Math.max(throttledRatio, 0) * this.maxHeight, // color
+ color = "rgba(255,255,255,0.8)";
+
+ var loc_x = x - strokeWidth / 2, loc_y1 = y - throttledY / 2, loc_y2 = y + throttledY / 2,
+ x_offset = this.tilt * throttledRatio;
+
+ if (throttledRatio > 0) {
+ var point_1 = (loc_x - x_offset) + "," + loc_y1, point_2 = (loc_x + x_offset) + "," + loc_y2;
+ var points = [point_1, point_2];
+ } else {
+ var points = [loc_x + "," + (y - 1), loc_x + "," + (y + 1)]
+ }
+
+ polyline.setAttribute("stroke-width", strokeWidth);
+ polyline.setAttribute("stroke", color);
+ polyline.setAttribute("points", points.join(" "));
+ this.g.appendChild(polyline);
+ },
+
+
+}
+
+
+
+