Tone.js/examples/events.html

144 lines
3.7 KiB
HTML
Raw Normal View History

2015-12-05 18:11:20 +00:00
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
2019-01-09 01:21:29 +00:00
<title>Events</title>
2015-12-05 18:11:20 +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-12-08 05:06:58 +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-12-05 18:11:20 +00:00
</head>
2020-10-15 22:06:51 +00:00
<body>
<tone-example label="Events">
<div slot="explanation">
2020-10-15 22:06:51 +00:00
Tone's Event classes (<a href="https://tonejs.github.io/docs/ToneEvent">Tone.ToneEvent</a>,
<a href="https://tonejs.github.io/docs/Loop">Tone.Loop</a>,
<a href="https://tonejs.github.io/docs/Part">Tone.Part</a> and
2019-01-09 01:21:29 +00:00
<a href="https://tonejs.github.io/docs/Sequence">Tone.Sequence</a>)
2020-10-15 22:06:51 +00:00
simplify scheduling events along the Transport. Each class abstracts away calls to
<a href="https://tonejs.github.io/docs/Transport.schedule">Transport.schedule</a> or
2019-01-09 01:21:29 +00:00
<a href="https://tonejs.github.io/docs/Transport.scheduleRepeat">scheduleRepeat</a>
2020-10-15 22:06:51 +00:00
and lets you create precise, rhythmic events which are startable, stoppable and loopable.
(note that ToneEvent was called <a href="https://tonejs.github.io/docs/Event">Event</a> before Tone.js 14.x)
</div>
2019-01-09 01:21:29 +00:00
<div id="content">
2019-01-09 01:21:29 +00:00
<tone-play-toggle></tone-play-toggle>
</div>
2019-01-09 01:21:29 +00:00
</tone-example>
2015-12-05 18:11:20 +00:00
2019-01-09 01:21:29 +00:00
<script type="text/javascript">
/**
* KICK
2015-12-05 18:11:20 +00:00
*/
const kick = new Tone.MembraneSynth({
envelope: {
sustain: 0,
attack: 0.02,
decay: 0.8
2015-12-05 18:11:20 +00:00
},
octaves: 10,
pitchDecay: 0.01,
}).toDestination();
2015-12-05 18:11:20 +00:00
const kickPart = new Tone.Loop(((time) => {
2015-12-05 18:11:20 +00:00
kick.triggerAttackRelease("C2", "8n", time);
}), "2n").start(0);
/**
* SNARE
2015-12-05 18:11:20 +00:00
*/
const snare = new Tone.NoiseSynth({
volume: -10,
envelope: {
attack: 0.001,
decay: 0.2,
sustain: 0
2015-12-05 18:11:20 +00:00
}
}).toDestination();
2015-12-05 18:11:20 +00:00
const snarePart = new Tone.Loop(((time) => {
2015-12-05 18:11:20 +00:00
snare.triggerAttack(time);
}), "2n").start("4n");
2015-12-05 18:11:20 +00:00
/**
* PIANO
2015-12-05 18:11:20 +00:00
*/
2020-07-19 17:18:02 +00:00
const keys = new Tone.PolySynth(Tone.Synth, {
volume: -8,
oscillator: {
partials: [1, 2, 1],
2015-12-05 18:11:20 +00:00
},
}).toDestination();
2015-12-05 18:11:20 +00:00
const cChord = ["C4", "E4", "G4", "B4"];
const dChord = ["D4", "F4", "A4", "C5"];
const gChord = ["B3", "D4", "E4", "A4"];
2015-12-05 18:11:20 +00:00
const pianoPart = new Tone.Part(((time, chord) => {
2020-07-19 17:18:02 +00:00
keys.triggerAttackRelease(chord, "8n", time);
}), [["0:0:2", cChord], ["0:1", cChord], ["0:1:3", dChord], ["0:2:2", cChord], ["0:3", cChord], ["0:3:2", gChord]]).start("2m");
2015-12-05 18:11:20 +00:00
pianoPart.loop = true;
pianoPart.loopEnd = "1m";
pianoPart.humanize = true;
/**
* BASS
2015-12-05 18:11:20 +00:00
*/
const bass = new Tone.MonoSynth({
volume: -10,
envelope: {
attack: 0.1,
decay: 0.3,
release: 2,
2015-12-05 18:11:20 +00:00
},
filterEnvelope: {
attack: 0.001,
decay: 0.01,
sustain: 0.5,
baseFrequency: 200,
octaves: 2.6
2015-12-05 18:11:20 +00:00
}
}).toDestination();
2015-12-05 18:11:20 +00:00
const bassPart = new Tone.Sequence(((time, note) => {
2015-12-05 18:11:20 +00:00
bass.triggerAttackRelease(note, "16n", time);
}), ["C2", ["C3", ["C3", "D2"]], "E2", ["D2", "A1"]], "4n").start(0);
2015-12-05 18:11:20 +00:00
bassPart.probability = 0.9;
// set the transport
2015-12-05 18:11:20 +00:00
Tone.Transport.bpm.value = 90;
drawer().add({
name: "Kick",
tone: kick,
open: false,
}).add({
name: "Snare",
tone: snare,
open: false,
}).add({
name: "Bass",
tone: bass,
open: false,
}).add({
name: "Keys",
2020-07-19 17:18:02 +00:00
tone: keys,
open: false,
});
2020-07-19 00:20:19 +00:00
// bind the interface
document.querySelector("tone-play-toggle").addEventListener("start", e => Tone.Transport.start());
document.querySelector("tone-play-toggle").addEventListener("stop", e => Tone.Transport.stop());
2020-07-19 00:20:19 +00:00
2015-12-05 18:11:20 +00:00
</script>
</body>
</html>