This repository has been archived by the owner on Jun 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
172 lines (148 loc) · 5.52 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>StereoPannerNode</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href=//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css>
<style>
#code { padding: 0; margin: 0; background: white; border: none }
#app { margin: 10px 0 }
#app .btn { width: 100px }
</style>
</head>
<body>
<div class="container">
<div class="page-header">
<h1>stereo-panner-node</h1>
<div>
<a href="https://travis-ci.org/mohayonao/stereo-panner-node"><img src="http://img.shields.io/travis/mohayonao/stereo-panner-node.svg?style=flat-square"/></a>
<a href="https://www.npmjs.org/package/stereo-panner-node"><img src="http://img.shields.io/npm/v/stereo-panner-node.svg?style=flat-square"/></a>
<a href="http://mohayonao.mit-license.org/"><img src="http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square"/></a>
</div>
</div>
<div>StereoPannerNode for legacy Web Audio API | <a href="https://github.com/mohayonao/stereo-panner-node" target="github">GitHub</a></div>
<div id="app">
<button id="button" class="btn btn-default">Start</button>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">example</h3>
</div>
<div class="panel-body">
<pre class="prettyprint" id="code"></pre>
</div>
</div>
</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.js"></script>
<script src="//mohayonao.github.io/web-audio-scheduler/build/web-audio-scheduler.min.js"></script>
<script src="build/stereo-panner-node.js"></script>
<script>
StereoPannerNode.polyfill();
window.AudioContext = window.AudioContext || window.webkitAudioContext;
window.addEventListener("DOMContentLoaded", function() {
"use strict";
var isPlaying = false;
function startWebAudioAPI() {
if (startWebAudioAPI.done) {
return;
}
var bufSrc = audioContext.createBufferSource();
bufSrc.buffer = audioContext.createBuffer(1, 128, audioContext.sampleRate);
bufSrc.start(audioContext.currentTime);
bufSrc.stop(audioContext.currentTime + bufSrc.buffer.duration);
bufSrc.onended = function() {
bufSrc.disconnect();
};
bufSrc.connect(audioContext.destination);
startWebAudioAPI.done = true;
}
document.getElementById("button").addEventListener("click", function(e) {
isPlaying = !isPlaying;
startWebAudioAPI();
if (isPlaying) {
sched.start(compose, { counter: 0 });
e.target.textContent = "Stop";
} else {
sched.stop(true);
e.target.textContent = "Start";
}
});
// code
document.getElementById("code").textContent = document.getElementById("example").textContent;
prettyPrint();
});
</script>
<script id="example">
var audioContext = new AudioContext();
var sched = new WebAudioScheduler({ context: audioContext });
var chords = [
[ 62, 65, 69, 72 ], [ 62, 65, 69, 71 ], [ 60, 64, 67, 71 ], [ 60, 64, 67, 69 ],
[ 62, 65, 69, 72 ], [ 62, 65, 69, 71 ], [ 60, 64, 67, 71 ], [ 60, 64, 67, 72 ],
[ 62, 65, 69, 72 ], [ 62, 65, 69, 71 ], [ 60, 64, 67, 71 ], [ 60, 64, 67, 69 ],
[ 59, 62, 66, 69 ], [ 59, 62, 66, 68 ], [ 57, 62, 64, 69 ], [ 57, 61, 64, 69 ],
];
function sine(e) {
var t0 = e.playbackTime;
var dur = e.args.dur;
var chord = e.args.chord;
var destination = e.args.destination;
var t1 = t0 + dur * 0.01;
var t2 = t1 + dur * 0.99;
var volume = 0.8 / chord.length;
var gain = audioContext.createGain();
chord.forEach(function(midi) {
var oscillator = audioContext.createOscillator();
oscillator.frequency.value = mtof(midi);
oscillator.start(t0);
oscillator.stop(t2);
oscillator.connect(gain);
});
gain.gain.setValueAtTime(0, t0);
gain.gain.linearRampToValueAtTime(volume, t1);
gain.gain.linearRampToValueAtTime(0, t2);
gain.connect(destination);
}
function autopan(t0, t3) {
var panner = audioContext.createStereoPanner();
var panLFO = audioContext.createOscillator();
var gain = audioContext.createGain();
var t1 = t0 + (t3 - t0) * 0.05;
var t2 = t0 + (t3 - t0) * 0.50;
panner.pan.value = 0;
panner.connect(gain);
gain.gain.setValueAtTime(0, t0);
gain.gain.linearRampToValueAtTime(1, t1);
gain.gain.setValueAtTime(1, t2);
gain.gain.linearRampToValueAtTime(0, t3);
gain.connect(audioContext.destination);
panLFO.frequency.setValueAtTime(0.1, t0);
panLFO.frequency.linearRampToValueAtTime(8, t3);
panLFO.start(t0);
panLFO.stop(t2);
panLFO.connect(panner.pan);
return panner;
}
function compose(e) {
var t0 = e.playbackTime;
var counter = e.args.counter;
var chord = chords[counter % chords.length];
var t1 = t0 + 4;
var t2 = t1 + 4;
var n = 64;
var panner = autopan(t0, t2);
var interval = (t2 - t0) / n;
var dur = interval * 2;
var opts = { chord: chord, dur: dur, destination: panner };
for (var i = 0; i < n; i++) {
sched.insert(t0 + interval * i, sine, opts);
}
sched.insert(t1, compose, { counter: counter + 1 });
}
function mtof(midi) {
return 440 * Math.pow(2, (midi - 69) / 12);
}
</script>
</body>
</html>