Tone.js/examples/rampTo.html

76 lines
2.2 KiB
HTML
Raw Permalink 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
2020-07-18 00:58:09 +00:00
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/2.4.3/webcomponents-bundle.js"></script>
<link href="https://fonts.googleapis.com/css?family=Material+Icons&display=block" rel="stylesheet"/>
<script src="../build/Tone.js"></script>
2020-07-18 00:58:09 +00:00
<script src="./js/tone-ui.js"></script>
<script src="./js/components.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>
<tone-example label="rampTo">
<div slot="explanation">
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.
</div>
2015-06-27 21:25:01 +00:00
<div id="content">
2019-01-09 01:21:29 +00:00
<tone-play-toggle></tone-play-toggle>
<tone-slider label="harmonicity" min="0.5" max="2" value="1"></tone-slider>
</div>
2019-01-09 01:21:29 +00:00
</tone-example>
2015-06-27 21:25:01 +00:00
2019-01-09 01:21:29 +00:00
<script type="text/javascript">
2020-07-19 00:20:19 +00:00
const oscillators = [];
2015-06-27 21:25:01 +00:00
2020-07-19 00:20:19 +00:00
const bassFreq = 32;
2015-06-27 21:25:01 +00:00
2020-07-19 00:20:19 +00:00
for (let i = 0; i < 8; i++) {
2017-08-31 16:41:54 +00:00
oscillators.push(new Tone.Oscillator({
2020-07-19 00:20:19 +00:00
frequency: bassFreq * i,
type: "sawtooth4",
volume: -Infinity,
detune: Math.random() * 30 - 15,
}).toDestination());
2015-06-27 21:25:01 +00:00
}
2020-07-19 00:20:19 +00:00
// bind the interface
document.querySelector("tone-play-toggle").addEventListener("start", e => {
2019-01-09 01:21:29 +00:00
oscillators.forEach(o => {
o.start();
o.volume.rampTo(-20, 1);
2019-01-09 01:21:29 +00:00
});
2017-08-31 16:41:54 +00:00
});
document.querySelector("tone-play-toggle").addEventListener("stop", e => {
oscillators.forEach(o => {
o.stop("+1.2");
o.volume.rampTo(-Infinity, 1);
});
2020-07-19 00:20:19 +00:00
});
2017-08-31 16:41:54 +00:00
document.querySelector("tone-slider").addEventListener("input", e => {
2019-01-09 01:21:29 +00:00
oscillators.forEach((osc, i) => {
osc.frequency.rampTo(bassFreq * i * parseFloat(e.target.value), 0.4);
2019-01-09 01:21:29 +00:00
});
2017-08-31 16:41:54 +00:00
});
2015-06-27 21:25:01 +00:00
</script>
</body>
</html>