Skip to content

Commit

Permalink
try audio
Browse files Browse the repository at this point in the history
  • Loading branch information
bewest committed Aug 14, 2016
1 parent de0ff7f commit 5045df9
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"jquery-flot": "0.8.3",
"jquery-ui": "~1.11.3",
"swagger-ui": "~2.1.2",
"tipsy-jmalonzo": "~1.0.1"
"tipsy-jmalonzo": "~1.0.1",
"tone": "*"
},
"resolutions": {
"jquery": "2.1.0"
Expand Down
61 changes: 61 additions & 0 deletions static/radio.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, maximum-scale=1, initial-scale=1, user-scalable=0" />
<meta name="apple-mobile-web-app-capable" content="yes">
<title>Nightscout radio</title>

<link rel="apple-touch-icon" sizes="57x57" href="/images/apple-touch-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/images/apple-touch-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/images/apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/images/apple-touch-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/images/apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="/images/apple-touch-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="/images/apple-touch-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-180x180.png">
<link rel="icon" type="image/png" href="/images/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/images/android-chrome-192x192.png" sizes="192x192">
<link rel="icon" type="image/png" href="/images/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="/images/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/manifest.json">
<link rel="shortcut icon" href="/images/favicon.ico">
<meta name="msapplication-TileColor" content="#00a300">
<meta name="msapplication-TileImage" content="/images/mstile-144x144.png">
<meta name="msapplication-config" content="/browserconfig.xml">
<meta name="theme-color" content="#333333">

<link rel="stylesheet" type="text/css" href="/css/main.css" />
<link rel="stylesheet" type="text/css" href="/css/drawer.css" />
<link rel="stylesheet" type="text/css" href="/bower_components/jquery-ui/themes/ui-darkness/jquery-ui.min.css">
</head>
<body>
<div id="toolbar">
<h1 class="customTitle">Nightscout</h1>
</div>

<div id="radio-tools">
<h1 class="translate">Audio</h1>
<p>
Convert glucose to frequency.
<button id="again" class="btn" tabindex=1>Again</button>
</p>
</div>

<div id="admin_placeholder"></div>

<hr>
<b><span class="translate">Authentication status</span>: </b>
<span id="authentication_placeholder"></span>

<script src="/js/init.js"></script>
<script src="/public/js/bundle.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script src="/bower_components/jQuery-Storage-API/jquery.storageapi.min.js"></script>
<script src="/bower_components/jquery-ui/jquery-ui.min.js"></script>
<script src="/bower_components/tipsy-jmalonzo/src/javascripts/jquery.tipsy.js"></script>
<script src="/bower_components/tone/build/Tone.min.js"></script>
<script src="/js/client.js"></script>
<script src="/radio/js/radio.js"></script>
</body>
</html>
92 changes: 92 additions & 0 deletions static/radio/js/radio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Many thanks to @ps2, Pete Schwamb
* https://gist.github.com/ps2/314145bb91fa720bba59cf58f7e9cad2
2**((numOctaves/(maxBG-minBG))*(bg-minBG) + Math.log2(minFreq))
*/
function convert (opts) {
opts = opts || { };
var octaves = opts.octaves || 9;
var maxBG = opts.maxBG || 400;
var minBG = opts.minBG || 40;
var minFreq = opts.minFreq || 55;
var x = minBG
, y = minFreq
, z = octaves/(maxBG - minBG)
;

function freq (bg) {
return Math.pow(2, (z* (bg - x ) ) + Math.log2(y)) ;
// return Math.pow(2, (z* (bg + x ) ) + Math.log2(y)) ;
}

function invert (freq) {

return ((Math.log2(freq) - Math.log2(y)) / z ) + x;
}

function api (glucose) {
return freq(glucose);
}

api.invert = invert;
api.freq = freq;
return api;
}



function createLoop (synth, sgvs) {
function callback (time, note) {
console.log(time, note);
synth.triggerAttackRelease(note, "8n", time);
}
var seq = new Tone.Sequence(callback, sgvs, "8n");
return seq;
}

function glucose (sgv) {
if (sgv)
return parseInt(sgv.mgdl || sgv.sgv || sgv.glucose || 35)
return 20
}

$(document).ready(function ( ) {
console.log("OK");
var converter = convert( );
var synth = new Tone.PolySynth(16, Tone.MonoSynth);
// default volume always makes my ears bleed
synth.connect(new Tone.Volume(-26), Tone.Master);
// synth.toMaster();
Tone.Transport.timeSignature = [ 12, 8 ];
Tone.Transport.bpm = 180;

function play_next (time) {
var sgv = sgvs.shift( );
console.log(sgv);
if (!sgv) {
loop.stop( );
}
if (sgv) {
var freq = converter.freq(sgv.mgdl || 30);
synth.triggerAttackRelease(parseInt(sgv.mgdl || sgv.sgv || sgv.glucose || 39) * 4, "8n", time);
}
}

// var loop = new Tone.Loop(play_next, "4n");
function play_data ( ) {
var sgvs = Nightscout.client.sbx.data.sgvs.slice( ).map(glucose).map(converter.freq);
console.log('last two hours', sgvs.length);
var loop = createLoop(synth, sgvs);
loop.start( );
}


Nightscout.client.socket.on('dataUpdate', function (update) {
play_data( );
});
$('#again').on('click', function (ev) {
play_data( );
});
Tone.Transport.start( );

});

0 comments on commit 5045df9

Please sign in to comment.