Tone.js/examples/rampTo.html

72 lines
1.9 KiB
HTML
Raw Normal View History

2015-06-27 21:25:01 +00:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
2019-01-09 01:21:29 +00:00
<title>Signal Ramping</title>
2015-06-27 21:25:01 +00:00
2019-01-09 01:21:29 +00:00
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="icon" type="image/png" sizes="174x174" href="./favicon.png">
2015-06-27 21:25:01 +00:00
2019-01-09 01:21:29 +00:00
<script src="https://unpkg.com/@webcomponents/webcomponentsjs@^2/webcomponents-bundle.js"></script>
<script src="../build/Tone.js"></script>
2019-01-09 01:21:29 +00:00
<script src="./js/tonejs-ui.js"></script>
2015-06-27 21:25:01 +00:00
</head>
<body>
2019-01-09 01:21:29 +00:00
<style>
tone-slider {
width: 100%;
margin-top: 10px;
2015-06-27 21:25:01 +00:00
}
</style>
2019-01-09 01:21:29 +00:00
<tone-example>
<tone-explanation label="rampTo">
2017-08-31 16:41:54 +00:00
Working with signals is different than working with numbers or strings:
2015-06-27 22:02:55 +00:00
Signals are values which are updated at audio rate,
2015-06-27 21:25:01 +00:00
which allows for sample-accurate scheduling and ramping. <code>.rampTo(value, rampTime)</code>
2017-08-31 16:41:54 +00:00
smoothly changes the signal from the current value to the target value over the duration of the rampTime.
This example uses <code>.rampTo</code> in to smooth out changes in volume and frequency.
2019-01-09 01:21:29 +00:00
</tone-explanation>
2015-06-27 21:25:01 +00:00
2019-01-09 01:21:29 +00:00
<tone-content>
<tone-play-toggle></tone-play-toggle>
<tone-slider bare min="0.5" max="2" value="1"></tone-slider>
</tone-content>
</tone-example>
2015-06-27 21:25:01 +00:00
2019-01-09 01:21:29 +00:00
<script type="text/javascript">
2017-08-31 16:41:54 +00:00
var oscillators = [];
2015-06-27 21:25:01 +00:00
var bassFreq = 32;
for (var i = 0; i < 8; i++){
2017-08-31 16:41:54 +00:00
oscillators.push(new Tone.Oscillator({
2015-06-27 21:25:01 +00:00
"frequency" : bassFreq * i,
2019-01-09 01:21:29 +00:00
"type" : "sawtooth10",
2017-08-31 16:41:54 +00:00
"volume" : -Infinity,
2015-06-27 21:25:01 +00:00
"detune" : Math.random() * 30 - 15,
2019-01-09 01:21:29 +00:00
}).toMaster());
2015-06-27 21:25:01 +00:00
}
2019-01-09 01:21:29 +00:00
//bind the interface
document.querySelector("tone-play-toggle").addEventListener("play", e => {
oscillators.forEach(o => {
if (e.detail){
o.start();
o.volume.rampTo(0, 1);
} else {
o.stop("+1.2");
o.volume.rampTo(-Infinity, 1);
}
});
2017-08-31 16:41:54 +00:00
});
2019-01-09 01:21:29 +00:00
document.querySelector("tone-slider").addEventListener("change", e => {
oscillators.forEach((osc, i) => {
osc.frequency.rampTo(bassFreq * i * e.detail, 0.4);
});
2017-08-31 16:41:54 +00:00
});
2015-06-27 21:25:01 +00:00
</script>
</body>
</html>