パッケージの詳細

tone

Tonejs189.9kMIT15.0.4

A Web Audio framework for making interactive music in the browser.

Web Audio, Web Audio API, Synthesis, Playback

readme

Tone.js

codecov

Tone.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.

Installation

There are two ways to incorporate Tone.js into a project. First, it can be installed locally into a project using npm:

npm install tone      // Install the latest stable version
npm install tone@next // Or, alternatively, use the 'next' version

Add Tone.js to a project using the JavaScript import syntax:

import * as Tone from "tone";

Tone.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 for more details.

<script src="http://unpkg.com/tone"></script>

Hello Tone

//create a synth and connect it to the main output (your speakers)
const synth = new Tone.Synth().toDestination();

//play a middle 'C' for the duration of an 8th note
synth.triggerAttackRelease("C4", "8n");

Tone.Synth

Tone.Synth is a basic synthesizer with a single oscillator and an ADSR envelope.

triggerAttack / triggerRelease

triggerAttack starts the note (the amplitude is rising), and triggerRelease is when the amplitude is going back to 0 (i.e. note off).

const synth = new Tone.Synth().toDestination();
const now = Tone.now();
// trigger the attack immediately
synth.triggerAttack("C4", now);
// wait one second before triggering the release
synth.triggerRelease(now + 1);

triggerAttackRelease

triggerAttackRelease is a combination of triggerAttack and triggerRelease

The first argument to the note which can either be a frequency in hertz (like 440) or as "pitch-octave" notation (like "D#2").

The second argument is the duration that the note is held. This value can either be in seconds, or as a tempo-relative value.

The 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.

const synth = new Tone.Synth().toDestination();
const now = Tone.now();
synth.triggerAttackRelease("C4", "8n", now);
synth.triggerAttackRelease("E4", "8n", now + 0.5);
synth.triggerAttackRelease("G4", "8n", now + 1);

Time

Web 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.

Tone.now() gets the current time of the AudioContext.

setInterval(() => console.log(Tone.now()), 100);

Tone.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.

Read about Time encodings.

Starting Audio

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".

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.

//attach a click listener to a play button
document.querySelector("button")?.addEventListener("click", async () => {
    await Tone.start();
    console.log("audio is ready");
});

Scheduling

Transport

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.

Multiple 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.

// create two monophonic synths
const synthA = new Tone.FMSynth().toDestination();
const synthB = new Tone.AMSynth().toDestination();
//play a note every quarter-note
const loopA = new Tone.Loop((time) => {
    synthA.triggerAttackRelease("C2", "8n", time);
}, "4n").start(0);
//play another note every off quarter-note, by starting it "8n"
const loopB = new Tone.Loop((time) => {
    synthB.triggerAttackRelease("C4", "8n", time);
}, "4n").start("8n");
// all loops start when the Transport is started
Tone.getTransport().start();
// ramp up to 800 bpm over 10 seconds
Tone.getTransport().bpm.rampTo(800, 10);

Since 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.

Instruments

There are numerous synths to choose from including Tone.FMSynth, Tone.AMSynth and Tone.NoiseSynth.

All of these instruments are monophonic (single voice) which means that they can only play one note at a time.

To 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.

const synth = new Tone.PolySynth(Tone.Synth).toDestination();
const now = Tone.now();
synth.triggerAttack("D4", now);
synth.triggerAttack("F4", now + 0.5);
synth.triggerAttack("A4", now + 1);
synth.triggerAttack("C5", now + 1.5);
synth.triggerAttack("E5", now + 2);
synth.triggerRelease(["D4", "F4", "A4", "C5", "E5"], now + 4);

Samples

Sound 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.

const player = new Tone.Player(
    "https://tonejs.github.io/audio/berklee/gong_1.mp3"
).toDestination();
Tone.loaded().then(() => {
    player.start();
});

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.

Tone.Sampler

Multiple 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.

Unlike the other synths, Tone.Sampler is polyphonic so doesn't need to be passed into Tone.PolySynth

const sampler = new Tone.Sampler({
    urls: {
        C4: "C4.mp3",
        "D#4": "Ds4.mp3",
        "F#4": "Fs4.mp3",
        A4: "A4.mp3",
    },
    release: 1,
    baseUrl: "https://tonejs.github.io/audio/salamander/",
}).toDestination();

Tone.loaded().then(() => {
    sampler.triggerAttackRelease(["Eb4", "G4", "Bb4"], 4);
});

Effects

In 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.

const player = new Tone.Player({
    url: "https://tonejs.github.io/audio/berklee/gurgling_theremin_1.mp3",
    loop: true,
    autostart: true,
});
//create a distortion effect
const distortion = new Tone.Distortion(0.4).toDestination();
//connect a player to the distortion
player.connect(distortion);

The connection routing is flexible, connections can run serially or in parallel.

const player = new Tone.Player({
    url: "https://tonejs.github.io/audio/drum-samples/loops/ominous.mp3",
    autostart: true,
});
const filter = new Tone.Filter(400, "lowpass").toDestination();
const feedbackDelay = new Tone.FeedbackDelay(0.125, 0.5).toDestination();

// connect the player to the feedback delay and filter in parallel
player.connect(filter);
player.connect(feedbackDelay);

Multiple nodes can be connected to the same input enabling sources to share effects. Tone.Gain is useful utility node for creating complex routing.

Signals

Like 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.

Signal properties have a few built in methods for creating automation curves.

For example, the frequency parameter on Oscillator is a Signal so you can create a smooth ramp from one frequency to another.

const osc = new Tone.Oscillator().toDestination();
// start at "C4"
osc.frequency.value = "C4";
// ramp to "C2" over 2 seconds
osc.frequency.rampTo("C2", 2);
// start the oscillator for 2 seconds
osc.start().stop("+3");

AudioContext

Tone.js creates an AudioContext when it loads and shims it for maximum browser compatibility using standardized-audio-context. The AudioContext can be accessed at Tone.getContext. Or set your own AudioContext using Tone.setContext(audioContext).

MIDI

To use MIDI files, you'll first need to convert them into a JSON format which Tone.js can understand using Midi.

Performance

Tone.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.

This wiki article has some suggestions related to performance for best practices.

Testing

Tone.js runs an extensive test suite using mocha and chai with nearly 100% coverage. Passing builds on the 'dev' branch are published on npm as tone@next.

Contributing

There are many ways to contribute to Tone.js. Check out this wiki if you're interested.

If you have questions (or answers) that are not necessarily bugs/issues, please post them to the forum.

References and Inspiration

更新履歴

14.7.0

Features

  • Converted to typescript!!!
  • adding AudioWorkletNode constructors to Context (f7bdd75)
  • adding ability to get the frequency at the FFT index (22cecdc)
  • adding AudioWorkletNode constructors to Context (f7bdd75)
  • adding BiquadFilter (75617d3), closes #686
  • adding linting to jsdocs (10ef513)
  • adding send/receive to Channel (703f27a)
  • Adding triggerRelease to PluckSynth (04405af)
  • Can set the parameter after constructing Param (23ca0f9)
  • adding onerror to Sampler (7236600), closes #605
  • Chorus extends StereoFeedbackEffect (a28f1af), closes #575
  • Convolver is just a wrapper around the ConvolverNode, no longer an effect (1668dec)
  • Get an oscillator wave as an array (9ad519e)
  • OfflineContext returns a ToneAudioBuffer (889dafa)
  • OfflineContext yields thread every second of audio rendered (1154470), closes #436
  • Renaming TransportTimelineSignal to SyncedSignal (86853fb)
  • es6 output (e5d28ba)
  • Render a segment of the envelope as an array (fc5b6f7)
  • testing examples in jsdocs (e306319)
  • Wrapper around the AudioWorkletNode (2ee8cb1)
  • Input/Outputs are no longer arrays.
    • simplifies connect/disconnect logic greatly. Simplifies API to just have clearly named inputs/outputs instead of overloading input/output connect numbers
  • Using "Destination" instead of "Master" for output
    • More consistent with Web Audio API
  • FrequencyShifter - thanks @Foaly
  • PolySynth does not require a polyphony value.
    • Voice allocation and disposing is done automatically based on demand.
  • MetalSynth and MembraneSynth extends Monophonic enabling them to be used in PolySynth
  • OnePoleFilter is a 6b-per-octave lowpass or highpass filter
    • Using OnePoleFilter in PluckSynth and LowpassCombFilter
  • latencyHint is now set in constructor (ba8e82b), closes #658
  • meter output can be normalRange in addition to decibels (2625a13)
  • option to pass in the number of input channels to Panner (d966735), closes #609

BREAKING CHANGES

  • TransportTimelineSignal renamed SyncedSignal
  • Master renamed Destination
  • Buffer renamed ToneAudioBuffer
  • Buffer.on("loaded") is should now use: Tone.loaded(): Promise<void>
  • Removing bower (71c8b3b), closes #197
  • Removing Ctrl classes (51d06bd)
  • Players.get(name: string) is renamed to Players.player(name: string)

13.8.25

  • Moving to common.js-style code

BREAKING CHANGES

  • AudioNode.prototype.connect is no longer overwritten. This means that you can no longer connect native nodes to Tone.js Nodes.
  • Tone.connect(srcNode, destNode, [ouputNum], [inputNum]) is the way to connect native Web Audio nodes with Tone.js nodes.

13.4.9

  • Updating semantic versioning to be more in line with other semvers. Now version is 13.x.x
  • logging full version
  • Added Object notation for Tone.TimeBase and classes that extend it.
    • i.e. Tone.Time({'4n' : 1, '8t' : 2})
    • Replacement for deprecated expression strings.
  • Tone.Meter uses RMS instead of peak (thanks @Idicious)
  • Tone.Sampler supports polyphonic syntax (thanks @zfan40)
  • Building files with webpack
  • Follower/Gate uses a single "smoothing" value instead of separate attacks and releases
  • Changing references to window allowing it to not throw error in node context
  • Testing examples
  • Tone.Channel combines Tone.PanVol with Tone.Solo.
  • Removing require.html example.
  • adding partialCount and baseType to Oscillator classes, helps with getting/setting complex types.

r12

  • Consolidating all shims into shim folder
  • Using ConstantSourceNode in Signal when available
  • switching to eslint from jshint
  • Running CI tests on Firefox, Chrome (latest and canary) and Safari (latest and version 9).
  • Tone.Reverb is a convolution-based stereo reverb. Example.
  • Optimizing basic Oscillator types and many Signal use-case
  • Optimizing basic connection use-case of Tone.Signal where one signal is controlling another signal
  • Testing rendered output against an existing audio file for continuity and consistency
  • Optimizing triggerAttack/Release by starting/stopping oscillators when not playing
  • TickSource (used in Clock and Player) tracks the elapsed ticks
    • Improved precision of tracking ticks in Transport and Clock
  • Player.position returns the playback position of the AudioBuffer accounting for any playbackRate changes
  • Removing retrigger option with Tone.Player. Tone.BufferSource should be used if retriggering is desired.

BREAKING CHANGES:

  • Tone.TimeBase and all classes that extend it not longer support string expressions. RATIONALE :
      * Since all classes implement `valueOf`, expressions can be composed in JS instead of as strings
          * e.g. `Time('4n') * 2 + Time('3t')` instead of `Time('4n * 2 + 3t')`
      * this change greatly simplifies the code and is more performant

r11

r10

  • Tone.Context wraps AudioContext
  • Tone.OfflineContext wraps OfflineAudioContext
  • Tone.Offline: method for rendering audio offline
  • Rewriting tests with Tone.Offline
  • Optimizing Tone.Draw to only loop when events are scheduled: #194
  • Time.eval->valueOf which takes advantage of build-in primitive evaluation #205
  • Offline example

r9

  • Tone.Clock performance and lookAhead updates.
  • Tone.Transport.lookAhead = seconds|'playback'|'interactive'|'balanced'
  • Convolver.load and Player.load returns Promise
  • Tone.ExternalInput -> Tone.UserMedia, simplified API, open() returns Promise.
  • Tone.Draw for animation-frame synced drawing
  • Compressor Parameters are now Tone.Params
  • Bug fixes

r8

  • Transport.seconds returns the progress in seconds.
  • Buffer.from/toArray, Float32Array <-> Buffer conversions
  • Buffer.slice(start, end) slices and returns a subsection of the Buffer
  • Source.sync now syncs all subsequent calls to start and stop to the TransportTime instead of the AudioContext time.
    • e.g. source.sync().start(0).stop(0.8); //plays source between 0 and 0.8 of the Transport
  • Transport.on("start" / "stop") callbacks are invoked just before the event.
  • Param can accept an LFO description in the constructor or .value
    • e.g. param.value = {min : 10, max : 20, frequency : 0.4}
  • Time.TimeBase has clone/copy methods.
  • Tone.Buffer.prototype.load returns Promise
  • Using Tone.Delay and Tone.Gain everywhere
  • Patch for Chrome 53+ issue of not correctly scheduling AudioParams with setValueAtTime
  • Panner3D and Tone.Listener wrap native PannerNode and AudioListener to give 3D panning ability.

r7

  • MetalSynth creates metalic, cymbal sounds
  • DrumSynth -> MembraneSynth
  • FMOscillator, AMOscillator types
  • FatOscillator creates multiple oscillators and detunes them slightly
  • FM, AM, Fat Oscillators incorporated into OmniOscillator
  • Simplified FM and AM Synths and APIs
  • Panner.pan is between -1,1 like the StereoPannerNode
  • Pruned away unused (or little used) Signal classes.
    • All this functionality will be available when the AudioWorkerNode is introduced.
  • Clock uses Web Workers instead of requestAnimationFrame which allows it to run in the background.
  • Removed startMobile. Using StartAudioContext in examples.
  • Automated test runner using Travis CI
  • Simplified NoiseSynth by removing filter and filter envelope.
  • Added new timing primitive types: Time, Frequency, TransportTime.
  • Switching parameter position of type and size in Tone.Analyser
  • Tone.Meter uses Tone.Analyser instead of ScriptProcessorNode.
  • Tone.Envelope has 5 new attack/release curves: "sine", "cosine", "bounce", "ripple", "step"
  • Renamed Tone.SimpleSynth -> Tone.Synth
  • Tone.Buffers combines multiple buffers
  • Tone.BufferSource a low-level wrapper, and Tone.MultiPlayer which is good for multisampled instruments.
  • Tone.GrainPlayer: granular synthesis buffer player.
  • Simplified Sampler

DEPRECATED:

  • Removed SimpleFM and SimpleAM

r6

  • Added PitchShift and Vibrato Effect.
  • Added Timeline/TimelineState/TimelineSignal which keeps track of all scheduled state changes.
  • Clock uses requestAnimationFrame instead of ScriptProcessorNode
  • Removed onended event from Tone.Source
  • Refactored tests into individual files.
  • Renamed some Signal methods: exponentialRampToValueNow->exponentialRampToValue, setCurrentValueNow->setRampPoint
  • LFO no longer starts at bottom of cycle. Starts at whatever phase it's set at.
  • Transport is an event emitter. triggers events on "start", "stop", "pause", and "loop".
  • Oscillator accepts a "partials" array.
  • Microphone inherits from ExternalInput which is generalized for different inputs.
  • New scheduling methods on Transport - schedule, scheduleOnce, and scheduleRepeat.
  • Tone.Gain and Tone.Delay classes wrap the native Web Audio nodes.
  • Moved MidiToScore and TypeScript definitions to separate repos.
  • Tone.Param wraps the native AudioParam and allows for unit conversion.
  • Quantization with Transport.quantize and using "@" in any Time. Read more.
  • Control-rate generators for value interpolation, patterns, random numbers, and markov chains.
  • Scheduable musical events: Tone.Event, Tone.Loop, Tone.Part, Tone.Pattern, Tone.Sequence.
  • Player's playbackRate is now a signal and Noise includes a playbackRate signal.
  • All filterEnvelopes use new Tone.FrequencyEnvelope with frequency units and baseFrequency and octaves instead of min and max.
  • Phaser uses "octaves" instead of "depth" to be more consistent across the whole Tone.js API.
  • Presets now have their own repo

DEPRECATED:

  • setTimeout, setInterval, setTimeline in favor of new schedule, scheduleOnce, and scheduleRepeat.
  • Tone.Signal no longer takes an AudioParam in the first argument. Use Tone.Param instead.
  • Tone.Buffer.onload/onprogress/onerror is deprecated. Use Tone.Buffer.on("load", callback) instead.

r5

  • reverse buffer for Player and Sampler.
  • Tone.Volume for simple volume control in Decibels.
  • Panner uses StereoPannerNode when available.
  • AutoFilter and Tremolo effects.
  • Made many attributes read-only. preventing this common type of error: oscillator.frequency = 200 when it should be oscillator.frequency.value = 200.
  • Envelope supports "linear" and "exponential" attack curves.
  • Renamed Tone.EQ -> Tone.EQ3.
  • Tone.DrumSynth makes kick and tom sounds.
  • Tone.MidSideCompressor and Tone.MidSideSplit/Tone.MidSideMerge
  • Tone.Oscillator - can specify the number of partials in the type: i.e. "sine10", "triangle3", "square4", etc.
  • mute/unmute the master output: Tone.Master.mute = true.
  • 3 new simplified synths: SimpleSynth, SimpleAM and SimpleFM
  • harmonicity is a signal-rate value for all instruments.
  • expose Q in Phaser.
  • unit conversions using Tone.Type for signals and LFO.
  • new docs
  • updated examples

r4

  • toFrequency accepts notes by name (i.e. "C4")
  • Envelope no longer accepts exponential scaling, only Tone.ScaledEnvelope
  • Buffer progress and load events which tracks the progress of all downloads
  • Buffer only accepts a single url
  • Sampler accepts multiple samples as an object.
  • setPitch in sampler -> setNote
  • Deprecated MultiSampler - use Sampler with PolySynth instead
  • Added cdn - please don't use for production code
  • Renamed DryWet to CrossFade
  • Functions return this to allow for chaining. i.e. player.toMaster().start(2).
  • Added units to Signal class which allows signals to be set in terms of Tone.Time, Tone.Frequency, Numbers, or Decibels.
  • Replaced set/get method with ES5 dot notation. i.e. player.setVolume(-10) is now player.volume.value = -10. To ramp the volume use either player.volume.linearRampToValueNow(-10, "4n"), or the new rampTo method which automaically selects the ramp (linear|exponential) based on the type of data.
  • set/get methods for all components
  • syncSignal and unsyncSignal moved from Signal to Transport
  • Add/Multiply/Subtract/Min/Max/GreaterThan/LessThan all extend Tone.Signal which allows them to be scheduled and automated just like Tone.Signal.
  • Deprecated Tone.Divide and Tone.Inverse. They were more complicated than they were useful.

BREAKING CHANGES:

The API has been changed consistently to use .attribute for getting and setting instead of getAttribute and setAttribute methods. The reasoning for this is twofold: firstly, Tone.Signal attributes were previously limited in their scheduling capabilities when set through a setter function. For exactly, it was not possible to do a setValueAtTime on the bpm of the Transport. Secondly, the new EcmaScript 5 getter/setter approach resembles the Web Audio API much more closely, which will make intermixing the two APIs even easier.

If you're using Sublime Text, one way to transition from the old API to the new one is with a regex find/replace: find Tone.Transport.setBpm\((\d+)\) and replace it with Tone.Transport.bpm.value = $1.

Or if setBpm was being invoked with a rampTime: find Tone.Transport.setBpm\((\d+)\, (\d+)\) and replace it with Tone.Transport.bpm.rampTo($1, $2).

r3

Core Change:

  • Swing parameter on Transport
  • Player loop positions stay in tempo-relative terms even with tempo changes
  • Envelope ASDR stay in tempo-relative terms even with tempo changes
  • Modified build script to accommodate using requirejs with build and minified version

Signal Processing:

  • Tone.Expr: signal processing expression parser for Tone.Signal math
  • All signal binary operators accept two signals as inputs
  • Deprecated Tone.Threshold - new class Tone.GreaterThanZero
  • NOT, OR, AND, and IfThenElse signal logic operators
  • Additional signal classes: Inverse, Divide, Pow, AudioToGain, Subtract
  • Scale no longer accepts input min/max. Assumes [0,1] range.
  • Normalize class if scaling needs to happen from other input ranges
  • WaveShaper function wraps the WaveShaperNode

Effects:

  • Distortion and Chebyshev distortion effects
  • Compressor and MultibandCompressor
  • MidSide effect type and StereoWidener
  • Convolver effect and example

Synths:

  • Setters on PluckSynth and PulseOscillator
  • new PWMOscillator
  • OmniOscillator which combines PWMOscillator, Oscillator, and PulseOscillator into one
  • NoiseSynth

r2

  • PluckSynth - Karplus-Strong Plucked String modeling synth
  • Freeverb
  • John Chowning Reverb (JCReverb)
  • LowpassCombFilter and FeedbackCombFilter
  • Sampler with pitch control
  • Clock tick callback is out of the audio thread using setTimeout
  • Optimized Tone.Modulo
  • Tests run using OfflineRenderingContext
  • Fixed Transport bug where timeouts/intervals and timelines were on a different tick counter
  • AmplitudeEnvelope + triggerAttackDecay on Envelope
  • Instruments inherit from Tone.Instrument base-class
  • midi<-->note conversions

r1 - First!