Tone.js/examples/rampTo.html
2020-07-18 17:20:19 -07:00

75 lines
2.2 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Signal Ramping</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="icon" type="image/png" sizes="174x174" href="./favicon.png">
<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>
<script src="./js/tone-ui.js"></script>
<script src="./js/components.js"></script>
</head>
<body>
<style>
tone-slider {
width: 100%;
margin-top: 10px;
}
</style>
<tone-example label="rampTo">
<div slot="explanation">
Working with signals is different than working with numbers or strings:
Signals are values which are updated at audio rate,
which allows for sample-accurate scheduling and ramping. <code>.rampTo(value, rampTime)</code>
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>
<div id="content">
<tone-play-toggle></tone-play-toggle>
<tone-slider label="harmonicity" min="0.5" max="2" value="1"></tone-slider>
</div>
</tone-example>
<script type="text/javascript">
const oscillators = [];
const bassFreq = 32;
for (let i = 0; i < 8; i++) {
oscillators.push(new Tone.Oscillator({
frequency: bassFreq * i,
type: "sawtooth4",
volume: -Infinity,
detune: Math.random() * 30 - 15,
}).toDestination());
}
// bind the interface
document.querySelector("tone-play-toggle").addEventListener("start", e => {
oscillators.forEach(o => {
o.start();
o.volume.rampTo(-20, 1);
});
});
document.querySelector("tone-play-toggle").addEventListener("stop", e => {
oscillators.forEach(o => {
o.stop("+1.2");
o.volume.rampTo(-Infinity, 1);
});
});
document.querySelector("tone-slider").addEventListener("input", e => {
oscillators.forEach((osc, i) => {
osc.frequency.rampTo(bassFreq * i * parseFloat(e.target.value), 0.4);
});
});
</script>
</body>
</html>