-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·115 lines (91 loc) · 4.46 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Cellular acoustics</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<link href="assets/vendor/normalize.css" rel="stylesheet">
<link href="assets/main.css" rel="stylesheet">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
<header>
<h1>Cyto-Acoustics</h1>
</header>
<a href="https://github.com/lourds-n-cie/cyto-acoustics"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/652c5b9acfaddf3a9c326fa6bde407b87f7be0f4/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67"
alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png"></a>
<div id="root" >Loading ...
</div>
<script type="text/javascript" src="target/cyto-acoustics.js"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
var gridSize = Number.parseInt(getParameterByName('gridSize'), 10) || 24;
var rootNode = document.getElementById('root');
var app = Elm.Cytoacoustics.embed(rootNode, gridSize);
// remove loading message
rootNode.innerHTML = '';
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
app.ports.newCells.subscribe(function(newCells) {
var osc = createOscillatorsForCoords(app, audioCtx, gridSize, newCells);
startOscillators(audioCtx, osc, 0.2);
});
});
var currentGain = 1;
var currentBaseFreqMod = 1;
function createOscillatorsForCoords(app, audioCtx, gridSize, coordsArray){
var result = [];
coordsArray.forEach(function(element, index, array){
var stereoPanning = element[1];
var baseFrequency = 55; // A 55 Hz
var y = gridSize - element[0];
var frequency = baseFrequency * y;
//console.log({x : x, y:y, frequency : frequency, pan: stereoPanning})
// create Oscillator node
var oscillator = audioCtx.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.value = currentBaseFreqMod * frequency; // value in hertz
var gain = audioCtx.createGain();
gain.gain.value = currentGain;
oscillator.connect(gain);
// add a stereo panner to move sound left or right
if(audioCtx.createStereoPanner){ // not supported in Safari
var panNode = audioCtx.createStereoPanner();
panNode.pan.value = stereoPanning;
gain.connect(panNode);
panNode.connect(audioCtx.destination);
}else{
gain.connect(audioCtx.destination);
}
var subscriber = function(scalings) {
currentBaseFreqMod = Math.pow(2, scalings.x * 3);
oscillator.frequency.value = currentBaseFreqMod * frequency;
currentGain = scalings.y * scalings.y;
gain.gain.value = currentGain;
};
app.ports.modulation.subscribe(subscriber);
oscillator.onended = function() {
app.ports.modulation.unsubscribe(subscriber);
};
result.push(oscillator);
});
return result;
}
function startOscillators(audioCtx, oscillators, noteDuration){
oscillators.forEach(function(oscillator) {
oscillator.start();
oscillator.stop(audioCtx.currentTime + noteDuration);
});
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
</script>
</body>
</html>