Skip to content

Instantly share code, notes, and snippets.

@darkwave
Created January 28, 2018 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkwave/0f9e322f597ecd3dbaae8c89b0548133 to your computer and use it in GitHub Desktop.
Save darkwave/0f9e322f597ecd3dbaae8c89b0548133 to your computer and use it in GitHub Desktop.
GGJ2018
<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
<!--
Customize this policy to fit your own app's needs. For more guidance, see:
https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
Some notes:
* gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
* Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<audio id="maria" src="radio_maria.mp3"></audio>
<audio id="track1" src="lala.mp3"></audio>
<audio id="track2" src="bob.mp3"></audio>
<audio id="track3" src="marley.mp3"></audio>
<div class="app">
<h1>Apache Cordova</h1>
<div id="deviceready" class="blink">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
<div>
<ul>
<li id="indizio1"></li>
<li id="indizio2"></li>
<li id="indizio3"></li>
<li id="indizio4"></li>
<li id="indizio5"></li>
<li id="indizio6"></li>
<li id="indizio7"></li>
</ul>
</div>
</div>
<audio id="maria" src="radio_maria.mp3"></audio>
<audio id="lala" src="lala.mp3"></audio>
<script type="text/javascript" src="cordova.js"></script>
<!-- <script src="js/radio.js"/> -->
<script type="text/javascript" src="js/index.js"></script>
</body>
</html>
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent('deviceready');
//console.log("ciao");
console.log("new mixer");
//Create mixer
console.log(m);
//Set master gain
m.setGain(0.5);
//Set distance min/max values
m.maxDistance = -90;
m.minDistance = -60;
// var maria_src = document.querySelector('#maria');
// var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
// var maria = audioCtx.createMediaElementSource(maria_src);
// console.log(maria);
// maria.connect(audioCtx.destination);
//
// maria_src.play();
// maria_src.loop = true;
this.startScan();
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
//console.log('Received Event: ' + id);
},
startScan: function() {
//console.log("ARIciao");
// Scan for specific service (Eddystone Service UUID).
startScan()
//,
//{ serviceUUIDs: ['0000feaa-0000-1000-8000-00805f9b34fb'] }
}
};
function getJamID(device)
{
var idBeacon = JSON.stringify(device.advertisementData.kCBAdvDataServiceData);
if(idBeacon.includes("5fkY0crCtuX5GNE="))
return 1;
if(idBeacon.includes("PEVVCJP8tjxFVQg="))
return 2;
if(idBeacon.includes("c5WK4wXitnOViuM="))
return 3;
return -1;
}
function setJamID(device)
{
var idBeacon = JSON.stringify(device.advertisementData.kCBAdvDataServiceData);
//console.log(idBeacon);
if(idBeacon.includes("5fkY0crCtuX5GNE="))
{
device.JamID = 1;
return;
}
if(idBeacon.includes("PEVVCJP8tjxFVQg="))
{
device.JamID = 2;
return;
}
if(idBeacon.includes("c5WK4wXitnOViuM="))
{
device.JamID = 3;
return;
}
if (idBeacon.includes("J3JDbqX3tidyQ24="))
{
device.JamID = 4;
return;
}
if (idBeacon.includes("2txGgfL1ttrcRoE="))
{
device.JamID = 5;
return;
}
if (idBeacon.includes("cV5+/mHJtnFefv4="))
{
device.JamID = 6;
return;
}
if (idBeacon.includes("hNDJ40zMtoTQyeM="))
{
device.JamID = 7;
return;
}
return;
}
function updateDeviceList()
{
var timeNow = Date.now();
for (var key in devices) {
if(devices[key].timeStamp + 5000 > timeNow)
{
//console.log(devices[key].JamID);
}
else {
if (devices[key].JamID == 1) {
m.distance(-100, "lala");
}
delete devices[key];
}
}
for (var i = 1; i < 8; i++) {
document.querySelector("#indizio"+i).innerHTML = "X";
}
for (var key in devices) {
if (devices[key].JamID == 1) {
m.distance(devices[key].rssi, "lala");
}
var foundBeacon = document.querySelector("#indizio"+devices[key].JamID);
if (foundBeacon) {
foundBeacon.innerHTML = devices[key].rssi;
}
//console.log(key + ": " + devices[key].rssi);
}
}
function stopScan()
{
// Stop scan.
evothings.ble.stopScan()
// Clear devices.
devices = {}
// Stop update timer.
if (updateTimer)
{
clearInterval(updateTimer)
updateTimer = null
}
}
function startScan()
{
// Make sure scan is stopped.
stopScan()
// Start scan.
evothings.ble.startScan(
function(device)
{
// Device found. Sometimes an RSSI of +127 is reported.
// We filter out these values here.
if (device.rssi <= 0 && device.name =='EST')
{
// Set timeStamp.
device.timeStamp = Date.now()
setJamID(device);
// Store device in table of found devices.
devices[device.address] = device
}
},
function(error)
{
stopScan()
}
)
// Start update timer.
updateTimer = setInterval(updateDeviceList, 500)
}
var devices = {}
// Timer that updates the displayed list of devices.
var updateTimer = null
var Perlin = function () {
var mask = 0xff;
var size = mask + 1;
var values = new Uint8Array(size * 2);
for (var i = 0; i < size; i++) {
values[i] = values[size + i] = 0|(Math.random() * 0xff);
}
var lerp = function (t, a, b) {
return a + t * (b - a);
};
var fade = function (t) {
return t * t * t * (t * (t * 6 - 15) + 10);
};
var grad1d = function (hash, x) {
return (hash & 1) === 0 ? x : -x;
};
var noise1d = function (x) {
var intX = (0|x) & mask;
var fracX = x - (0|x);
var t = fade(fracX);
var a = grad1d(values[intX], fracX);
var b = grad1d(values[intX + 1], fracX - 1);
return lerp(t, a, b);
};
return noise1d;
};
function Mixer() {
//////////////////////////////////
/// SETTINGS
//////////////////////////////////
this.maxDistance = -90;
this.minDistance = -60;
//Attention, the white noise can get pretty loud!! keep it low
this.maxNoiseGain = 0.05;
this.maxMariaGain = 1;
//////////////////////////////////
/// INTERNAL VARS
//////////////////////////////////
var self = this;
this.t = 0.0;
this.perlin = Perlin();
self.noise = 0;
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
this.interference = 0;
this.sources = {};
this.gains = {};
this.gains['master'] = this.audioCtx.createGain();
//White noise
var whiteNoise = createWhiteNoise(this.audioCtx);
this.whiteGain = this.audioCtx.createGain();
this.whiteGain.gain.value = this.maxNoiseGain = 0.05;
whiteNoise.connect(this.whiteGain);
//Radio maria interference
var maria_src = document.querySelector('#maria');
console.log(maria_src);
maria_src.play();
maria_src.loop = true;
var maria = self.audioCtx.createMediaElementSource(maria_src);
this.mariaGain = this.audioCtx.createGain();
this.mariaGain.gain.value = this.maxMariaGain;
maria.connect(this.mariaGain);
//General noise combines white and maria
this.noiseGain = this.audioCtx.createGain();
this.whiteGain.connect(this.noiseGain);
this.mariaGain.connect(this.noiseGain);
this.noiseGain.connect(this.gains['master']);
document.querySelectorAll('audio').forEach(function(a){
if (a.id == "maria") return;
self.sources[a.id] = self.audioCtx.createMediaElementSource(a);
self.gains[a.id] = self.audioCtx.createGain();
a.play();
a.loop = true;
self.sources[a.id].connect(self.gains[a.id]);
self.gains[a.id].connect(self.gains['master']);
//Init all gain values to 0;
self.gains[a.id].gain.value = 0;
});
this.gains['master'].connect(this.audioCtx.destination);
this.setGain = function(g, key = "master"){
if (!this.gains.hasOwnProperty(key)){
console.log("WARNING [" + key + "] track doesn't exist");
return;
}
this.gains[key].gain.value = g;
console.log(key +" SETTTING GAIN VALUE : " + this.gains[key].gain.value)
var maxgain = 0
for (var g in this.gains) {
if (this.gains.hasOwnProperty(g) && g != "master") {
console.log(g +" VALUE : " + this.gains[g].gain.value)
maxgain = Math.max(this.gains[g].gain.value, maxgain);
this.interference = 1 - maxgain;
}
}
this.noiseGain.gain.value = this.interference ;
// console.log(maxgain);
}
this.distance = function(x, key){
if (!this.gains.hasOwnProperty(key)){
console.log("WARNING [" + key + "] track doesn't exist");
this.setGain(1, key);
return 1;
}
if (x < this.maxDistance){
console.log("MINOR: ", x)
this.setGain(0, key);
return 0;
} else if (x > this.minDistance){
console.log("MAYOR: ", x)
this.setGain(1);
return 1;
}
//Si, pippo
console.log("DISTANCE: ", x)
var pippo = (x - self.maxDistance) / (self.minDistance - self.maxDistance);
this.setGain(pippo, key);
return (pippo);
}
function animation(){
self.t+= 0.0015;
self.noise = self.perlin( (self.t+2) );
self.noise = Math.min(1, Math.max(self.noise, 0));
self.noise *= 5;
// console.log( self.noise );
self.mariaGain.gain.value = self.noise * self.maxMariaGain ;
window.requestAnimationFrame(animation);
}
console.log("ANIMATION");
window.requestAnimationFrame(animation);
};
function createWhiteNoise(audioContext){
var bufferSize = 4096;
var whiteNoise = audioContext.createScriptProcessor(bufferSize, 1, 1);
whiteNoise.onaudioprocess = function(e) {
var output = e.outputBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
output[i] = Math.random() * 2 - 1;
}
}
return whiteNoise;
}
var m = new Mixer();
app.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment