UNPKG

11.5 kBMarkdownView Raw
1# Tone.js
2
3[![codecov](https://codecov.io/gh/Tonejs/Tone.js/branch/dev/graph/badge.svg)](https://codecov.io/gh/Tonejs/Tone.js)
4
5Tone.js is a Web Audio framework for creating interactive music in the browser. The architecture of Tone.js aims to be familiar to both musicians and audio programmers creating web-based audio applications. On the high-level, Tone offers common DAW (digital audio workstation) features like a global transport for synchronizing and scheduling events as well as prebuilt synths and effects. Additionally, Tone provides high-performance building blocks to create your own synthesizers, effects, and complex control signals.
6
7- [API](https://tonejs.github.io/docs/)
8- [Examples](https://tonejs.github.io/examples/)
9
10# Installation
11
12There are two ways to incorporate Tone.js into a project. First, it can be installed locally into a project using `npm`:
13
14```bash
15npm install tone // Install the latest stable version
16npm install tone@next // Or, alternatively, use the 'next' version
17```
18
19Add Tone.js to a project using the JavaScript `import` syntax:
20
21```js
22import * as Tone from "tone";
23```
24
25Tone.js is also hosted at unpkg.com. It can be added directly within an HTML document, as long as it precedes any project scripts. [See the example here](https://github.com/Tonejs/Tone.js/blob/master/examples/simpleHtml.html) for more details.
26
27```html
28<script src="http://unpkg.com/tone"></script>
29```
30
31# Hello Tone
32
33```javascript
34//create a synth and connect it to the main output (your speakers)
35const synth = new Tone.Synth().toDestination();
36
37//play a middle 'C' for the duration of an 8th note
38synth.triggerAttackRelease("C4", "8n");
39```
40
41## Tone.Synth
42
43`Tone.Synth` is a basic synthesizer with a single oscillator and an ADSR envelope.
44
45### triggerAttack / triggerRelease
46
47`triggerAttack` starts the note (the amplitude is rising), and `triggerRelease` is when the amplitude is going back to 0 (i.e. **note off**).
48
49```javascript
50const synth = new Tone.Synth().toDestination();
51const now = Tone.now();
52// trigger the attack immediately
53synth.triggerAttack("C4", now);
54// wait one second before triggering the release
55synth.triggerRelease(now + 1);
56```
57
58### triggerAttackRelease
59
60`triggerAttackRelease` is a combination of `triggerAttack` and `triggerRelease`
61
62The first argument to the note which can either be a frequency in hertz (like `440`) or as "pitch-octave" notation (like `"D#2"`).
63
64The second argument is the duration that the note is held. This value can either be in seconds, or as a [tempo-relative value](https://github.com/Tonejs/Tone.js/wiki/Time).
65
66The third (optional) argument of `triggerAttackRelease` is _when_ along the AudioContext time the note should play. It can be used to schedule events in the future.
67
68```javascript
69const synth = new Tone.Synth().toDestination();
70const now = Tone.now();
71synth.triggerAttackRelease("C4", "8n", now);
72synth.triggerAttackRelease("E4", "8n", now + 0.5);
73synth.triggerAttackRelease("G4", "8n", now + 1);
74```
75
76## Time
77
78Web Audio has advanced, sample accurate scheduling capabilities. The AudioContext time is what the Web Audio API uses to schedule events, starts at 0 when the page loads and counts up in **seconds**.
79
80`Tone.now()` gets the current time of the AudioContext.
81
82```javascript
83setInterval(() => console.log(Tone.now()), 100);
84```
85
86Tone.js abstracts away the AudioContext time. Instead of defining all values in seconds, any method which takes time as an argument can accept a number or a string. For example `"4n"` is a quarter-note, `"8t"` is an eighth-note triplet, and `"1m"` is one measure.
87
88[Read about Time encodings](https://github.com/Tonejs/Tone.js/wiki/Time).
89
90# Starting Audio
91
92**IMPORTANT**: Browsers will not play _any_ audio until a user clicks something (like a play button). Run your Tone.js code only after calling `Tone.start()` from a event listener which is triggered by a user action such as "click" or "keydown".
93
94`Tone.start()` returns a promise, the audio will be ready only after that promise is resolved. Scheduling or playing audio before the AudioContext is running will result in silence or incorrect scheduling.
95
96```javascript
97//attach a click listener to a play button
98document.querySelector("button")?.addEventListener("click", async () => {
99 await Tone.start();
100 console.log("audio is ready");
101});
102```
103
104# Scheduling
105
106## Transport
107
108`Tone.getTransport()` returns the main timekeeper. Unlike the AudioContext clock, it can be started, stopped, looped and adjusted on the fly. You can think of it like the arrangement view in a Digital Audio Workstation.
109
110Multiple events and parts can be arranged and synchronized along the Transport. `Tone.Loop` is a simple way to create a looped callback that can be scheduled to start and stop.
111
112```javascript
113// create two monophonic synths
114const synthA = new Tone.FMSynth().toDestination();
115const synthB = new Tone.AMSynth().toDestination();
116//play a note every quarter-note
117const loopA = new Tone.Loop((time) => {
118 synthA.triggerAttackRelease("C2", "8n", time);
119}, "4n").start(0);
120//play another note every off quarter-note, by starting it "8n"
121const loopB = new Tone.Loop((time) => {
122 synthB.triggerAttackRelease("C4", "8n", time);
123}, "4n").start("8n");
124// all loops start when the Transport is started
125Tone.getTransport().start();
126// ramp up to 800 bpm over 10 seconds
127Tone.getTransport().bpm.rampTo(800, 10);
128```
129
130Since Javascript callbacks are **not precisely timed**, the sample-accurate time of the event is passed into the callback function. **Use this time value to schedule the events**.
131
132# Instruments
133
134There are numerous synths to choose from including `Tone.FMSynth`, `Tone.AMSynth` and `Tone.NoiseSynth`.
135
136All of these instruments are **monophonic** (single voice) which means that they can only play one note at a time.
137
138To create a **polyphonic** synthesizer, use `Tone.PolySynth`, which accepts a monophonic synth as its first parameter and automatically handles the note allocation so you can pass in multiple notes. The API is similar to the monophonic synths, except `triggerRelease` must be given a note or array of notes.
139
140```javascript
141const synth = new Tone.PolySynth(Tone.Synth).toDestination();
142const now = Tone.now();
143synth.triggerAttack("D4", now);
144synth.triggerAttack("F4", now + 0.5);
145synth.triggerAttack("A4", now + 1);
146synth.triggerAttack("C5", now + 1.5);
147synth.triggerAttack("E5", now + 2);
148synth.triggerRelease(["D4", "F4", "A4", "C5", "E5"], now + 4);
149```
150
151# Samples
152
153Sound generation is not limited to synthesized sounds. You can also load a sample and play that back in a number of ways. `Tone.Player` is one way to load and play back an audio file.
154
155```javascript
156const player = new Tone.Player(
157 "https://tonejs.github.io/audio/berklee/gong_1.mp3"
158).toDestination();
159Tone.loaded().then(() => {
160 player.start();
161});
162```
163
164`Tone.loaded()` returns a promise which resolves when _all_ audio files are loaded. It's a helpful shorthand instead of waiting on each individual audio buffer's `onload` event to resolve.
165
166## Tone.Sampler
167
168Multiple samples can also be combined into an instrument. If you have audio files organized by note, `Tone.Sampler` will pitch shift the samples to fill in gaps between notes. So for example, if you only have every 3rd note on a piano sampled, you could turn that into a full piano sample.
169
170Unlike the other synths, Tone.Sampler is polyphonic so doesn't need to be passed into Tone.PolySynth
171
172```javascript
173const sampler = new Tone.Sampler({
174 urls: {
175 C4: "C4.mp3",
176 "D#4": "Ds4.mp3",
177 "F#4": "Fs4.mp3",
178 A4: "A4.mp3",
179 },
180 release: 1,
181 baseUrl: "https://tonejs.github.io/audio/salamander/",
182}).toDestination();
183
184Tone.loaded().then(() => {
185 sampler.triggerAttackRelease(["Eb4", "G4", "Bb4"], 4);
186});
187```
188
189# Effects
190
191In the above examples, the sources were always connected directly to the `Destination`, but the output of the synth could also be routed through one (or more) effects before going to the speakers.
192
193```javascript
194const player = new Tone.Player({
195 url: "https://tonejs.github.io/audio/berklee/gurgling_theremin_1.mp3",
196 loop: true,
197 autostart: true,
198});
199//create a distortion effect
200const distortion = new Tone.Distortion(0.4).toDestination();
201//connect a player to the distortion
202player.connect(distortion);
203```
204
205The connection routing is flexible, connections can run serially or in parallel.
206
207```javascript
208const player = new Tone.Player({
209 url: "https://tonejs.github.io/audio/drum-samples/loops/ominous.mp3",
210 autostart: true,
211});
212const filter = new Tone.Filter(400, "lowpass").toDestination();
213const feedbackDelay = new Tone.FeedbackDelay(0.125, 0.5).toDestination();
214
215// connect the player to the feedback delay and filter in parallel
216player.connect(filter);
217player.connect(feedbackDelay);
218```
219
220Multiple nodes can be connected to the same input enabling sources to share effects. `Tone.Gain` is useful utility node for creating complex routing.
221
222# Signals
223
224Like the underlying Web Audio API, Tone.js is built with audio-rate signal control over nearly everything. This is a powerful feature which allows for sample-accurate synchronization and scheduling of parameters.
225
226`Signal` properties have a few built in methods for creating automation curves.
227
228For example, the `frequency` parameter on `Oscillator` is a Signal so you can create a smooth ramp from one frequency to another.
229
230```javascript
231const osc = new Tone.Oscillator().toDestination();
232// start at "C4"
233osc.frequency.value = "C4";
234// ramp to "C2" over 2 seconds
235osc.frequency.rampTo("C2", 2);
236// start the oscillator for 2 seconds
237osc.start().stop("+3");
238```
239
240# AudioContext
241
242Tone.js creates an AudioContext when it loads and shims it for maximum browser compatibility using [standardized-audio-context](https://github.com/chrisguttandin/standardized-audio-context). The AudioContext can be accessed at `Tone.getContext`. Or set your own AudioContext using `Tone.setContext(audioContext)`.
243
244# MIDI
245
246To use MIDI files, you'll first need to convert them into a JSON format which Tone.js can understand using [Midi](https://tonejs.github.io/Midi/).
247
248# Performance
249
250Tone.js makes extensive use of the native Web Audio Nodes such as the GainNode and WaveShaperNode for all signal processing, which enables Tone.js to work well on both desktop and mobile browsers.
251
252[This wiki](https://github.com/Tonejs/Tone.js/wiki/Performance) article has some suggestions related to performance for best practices.
253
254# Testing
255
256Tone.js runs an extensive test suite using [mocha](https://mochajs.org/) and [chai](http://chaijs.com/) with nearly 100% coverage. Passing builds on the 'dev' branch are published on npm as `tone@next`.
257
258# Contributing
259
260There are many ways to contribute to Tone.js. Check out [this wiki](https://github.com/Tonejs/Tone.js/wiki/Contributing) if you're interested.
261
262If you have questions (or answers) that are not necessarily bugs/issues, please post them to the [forum](https://groups.google.com/forum/#!forum/tonejs).
263
264# References and Inspiration
265
266- [Many of Chris Wilson's Repositories](https://github.com/cwilso)
267- [Many of Mohayonao's Repositories](https://github.com/mohayonao)
268- [The Spec](http://webaudio.github.io/web-audio-api/)
269- [Sound on Sound - Synth Secrets](http://www.soundonsound.com/sos/may99/articles/synthsec.htm)
270- [Miller Puckette - Theory and Techniques of Electronic Music](http://msp.ucsd.edu/techniques.htm)
271- [standardized-audio-context](https://github.com/chrisguttandin/standardized-audio-context)