-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscurve.html
More file actions
75 lines (63 loc) · 1.8 KB
/
Copy pathscurve.html
File metadata and controls
75 lines (63 loc) · 1.8 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>S-Curve Fade Demo</title>
<style>
.hidden {
display: none;
}
</style>
</head>
<body>
<div id='play' class='hidden'>Play</div>
<div id='stop'>Stop</div>
<script type="text/javascript">
(function(){
//creating a curve to simulate an S-curve with setValueCurveAtTime.
var createSCurveBuffer = function createSCurveBuffer(length, phase) {
var curve = new Float32Array(length),
i;
for (i = 0; i < length; ++i) {
//scale the curve to be between 0-1
curve[i] = (Math.sin((Math.PI * i / length) - phase)) /2 + 0.5;
}
return curve;
};
/*
Test script to demonstrate an exponential fade.
*/
var ctx = new AudioContext(),
url = 'Legend.mp3',
audio = new Audio(url),
gainNode = ctx.createGain(),
source,
start,
end,
play,
stop;
audio.addEventListener('canplaythrough', function(){
source = ctx.createMediaElementSource(audio);
source.connect(gainNode);
gainNode.connect(ctx.destination);
curve = createSCurveBuffer(ctx.samplerate, (Math.PI/2));
start = ctx.currentTime;
gainNode.gain.setValueCurveAtTime(curve, start, 5);
audio.play();
});
play = document.getElementById("play");
play.addEventListener('click', function(e){
play.className='hidden';
stop.className='';
audio.play();
})
stop = document.getElementById("stop");
stop.addEventListener('click', function(e){
play.className='';
stop.className='hidden';
audio.pause();
})
})();
</script>
</body>
</html>