docs: working on code examples

This commit is contained in:
Yotam Mann 2019-10-25 16:54:33 -04:00
parent 4266d2b93c
commit d2b064f41e
39 changed files with 354 additions and 459 deletions

View file

@ -15,6 +15,7 @@ export interface PanVolOptions extends ToneAudioNodeOptions {
/**
* PanVol is a Tone.Panner and Tone.Volume in one.
* @example
* import { Oscillator, PanVol } from "tone";
* // pan the incoming signal left and drop the volume
* const panVol = new PanVol(-0.25, -12).toDestination();
* const osc = new Oscillator().connect(panVol).start();

View file

@ -274,7 +274,7 @@ export class Clock<Type extends BPM | Hertz = Hertz>
* @return The name of the state input in setStateAtTime.
* @example
* import { Clock } from "tone";
* const clock = new Tone.Clock();
* const clock = new Clock();
* clock.start("+0.1");
* clock.getStateAtTime("+0.1"); // returns "started"
*/

View file

@ -172,7 +172,7 @@ export abstract class AbstractParam<Type extends Unit> {
* import { Oscillator } from "tone";
* const osc = new Oscillator().toDestination().start();
* // schedule it to ramp starting at a specific time
* signal.rampTo("A2", 10, "+2");
* osc.frequency.rampTo("A2", 10, "+2");
*/
abstract rampTo(value: Type, rampTime: Time, startTime?: Time): this;

View file

@ -22,8 +22,8 @@ interface ToneAudioBuffersOptions {
* @example
* import { Player, ToneAudioBuffers } from "tone";
* const pianoSamples = new ToneAudioBuffers({
* C1: "https://tonejs.github.io/examples/audio/casio/C1.mp3",
* C2: "https://tonejs.github.io/examples/audio/casio/C2.mp3",
* C3: "https://tonejs.github.io/examples/audio/casio/C3.mp3",
* }, () => {
* const player = new Player().toDestination();
* // play one of the samples when they all load
@ -35,8 +35,8 @@ interface ToneAudioBuffersOptions {
* // To pass in additional parameters in the second parameter
* const buffers = new ToneAudioBuffers({
* urls: {
* C1: "C1.mp3",
* C2: "C2.mp3",
* C3: "C3.mp3",
* },
* onload: () => console.log("loaded"),
* baseUrl: "https://tonejs.github.io/examples/audio/casio/"

View file

@ -8,8 +8,6 @@ import { Hertz, Interval, MidiNote, Seconds, Ticks } from "./Units";
* Midi is a primitive type for encoding Time values.
* Midi can be constructed with or without the `new` keyword. Midi can be passed
* into the parameter of any method which takes time as an argument.
* @example
* var t = Midi("4n");//a quarter note
* @category Unit
*/
export class MidiClass extends FrequencyClass<MidiNote> {
@ -48,8 +46,8 @@ export class MidiClass extends FrequencyClass<MidiNote> {
/**
* Return the value of the frequency as a MIDI note
* @return {MIDI}
* @example
* import { Midi } from "tone";
* Midi(60).toMidi(); // 60
*/
toMidi(): MidiNote {
@ -58,8 +56,8 @@ export class MidiClass extends FrequencyClass<MidiNote> {
/**
* Return the value of the frequency as a MIDI note
* @return {MIDI}
* @example
* import { Midi } from "tone";
* Midi(60).toFrequency(); // 261.6255653005986
*/
toFrequency(): Hertz {
@ -70,6 +68,7 @@ export class MidiClass extends FrequencyClass<MidiNote> {
* Transposes the frequency by the given number of semitones.
* @return A new transposed MidiClass
* @example
* import { Midi } from "tone";
* Midi("A4").transpose(3); // "C5"
*/
transpose(interval: Interval): MidiClass {

View file

@ -1,3 +1,4 @@
export { FeedbackDelay } from "./FeedbackDelay";
export { Reverb } from "./Reverb";
export { Distortion } from "./Distortion";
export * from "./Distortion";
export * from "./FeedbackDelay";
export * from "./FrequencyShifter";
export * from "./Reverb";

View file

@ -212,11 +212,6 @@ export class Part<ValueType = any> extends ToneEvent<ValueType> {
* import { Part } from "tone";
* const part = new Part();
* part.add("1m", "C#+11");
* @example
* part.add({
* time: "1m",
* note: "C#11"
* });
*/
add(obj: {
time: Time;

View file

@ -212,6 +212,8 @@ export class ToneEvent<ValueType = any> extends ToneWithContext<ToneEventOptions
* to the callback time. If the value is given as a time, it will randomize
* by that amount.
* @example
* import { ToneEvent } from "tone";
* const event = new ToneEvent();
* event.humanize = true;
*/
get humanize(): Time | boolean {

View file

@ -83,7 +83,7 @@ export class FMSynth extends Monophonic<FMSynthOptions> {
* import { FMSynth } from "tone";
* const fmSynth = new FMSynth().toDestination();
* // pitch the modulator an octave below oscillator
* synth.harmonicity.value = 0.5;
* fmSynth.harmonicity.value = 0.5;
* fmSynth.triggerAttackRelease("C5", "4n");
*/
readonly harmonicity: Multiply;

View file

@ -23,7 +23,7 @@ export interface NoiseSynthOptions extends InstrumentOptions {
* @example
* import { NoiseSynth } from "tone";
* const noiseSynth = new NoiseSynth().toDestination();
* noiseSynth.triggerAttackRelease("8n");
* noiseSynth.triggerAttackRelease("8n", 0.05);
* @category Instrument
*/
export class NoiseSynth extends Instrument<NoiseSynthOptions> {

View file

@ -16,7 +16,8 @@ export interface PluckSynthOptions extends InstrumentOptions {
/**
* Karplus-String string synthesis.
* @example
* var plucky = new Tone.PluckSynth().toDestination();
* import { PluckSynth } from "tone";
* const plucky = new PluckSynth().toDestination();
* plucky.triggerAttack("C4");
* @category Instrument
*/

View file

@ -43,15 +43,12 @@ export interface PolySynthOptions<Voice> extends InstrumentOptions {
* monophonic synthesizers to be polyphonic.
*
* @example
* var synth = new PolySynth(Tone.Synth, {
* oscillator : {
* type : "square"
* }
* }).toMaster();
* //set the attributes using the set interface
* synth.set("detune", -1200);
* import { PolySynth } from "tone";
* const synth = new PolySynth().toDestination();
* // set the attributes across all the voices using 'set'
* synth.set({ detune: -1200 });
* // play a chord
* synth.triggerAttackRelease(["C4", "E4", "A4"], "4n");
* synth.triggerAttackRelease(["C4", "E4", "A4"], 1);
* @category Instrument
*/
export class PolySynth<Voice extends Monophonic<any> = Synth> extends Instrument<VoiceOptions<Voice>> {
@ -271,6 +268,8 @@ export class PolySynth<Voice extends Monophonic<any> = Synth> extends Instrument
* @param time The start time of the note.
* @param velocity The velocity of the note.
* @example
* import { FMSynth, PolySynth } from "tone";
* const synth = new PolySynth(FMSynth).toDestination();
* // trigger a chord immediately with a velocity of 0.2
* poly.triggerAttack(["Ab3", "C4", "F5"], undefined, 0.2);
*/
@ -290,7 +289,13 @@ export class PolySynth<Voice extends Monophonic<any> = Synth> extends Instrument
* @param notes The notes to play. Accepts a single Frequency or an array of frequencies.
* @param time When the release will be triggered.
* @example
* poly.triggerRelease(["Ab3", "C4", "F5"], "+2n");
* @example
* import { AMSynth, PolySynth } from "tone";
* const poly = new PolySynth(AMSynth).toDestination();
* poly.triggerAttack(["Ab3", "C4", "F5"]);
* // trigger the release of the given notes.
* poly.triggerRelease(["Ab3", "C4"], "+1");
* poly.triggerRelease("F5", "+3");
*/
triggerRelease(notes: Frequency | Frequency[], time?: Time): this {
if (!Array.isArray(notes)) {
@ -308,11 +313,10 @@ export class PolySynth<Voice extends Monophonic<any> = Synth> extends Instrument
* @param time if no time is given, defaults to now
* @param velocity the velocity of the attack (0-1)
* @example
* //trigger a chord for a duration of a half note
* poly.triggerAttackRelease(["Eb3", "G4", "C5"], "2n");
* @example
* import { AMSynth, PolySynth } from "tone";
* const poly = new PolySynth(AMSynth).toDestination();
* // can pass in an array of durations as well
* poly.triggerAttackRelease(["Eb3", "G4", "C5"], ["2n", "4n", "4n"]);
* poly.triggerAttackRelease(["Eb3", "G4", "Bb4", "D5"], [4, 3, 2, 1]);
*/
triggerAttackRelease(
notes: Frequency | Frequency[],
@ -339,18 +343,6 @@ export class PolySynth<Voice extends Monophonic<any> = Synth> extends Instrument
return this;
}
/**
* Sync the instrument to the Transport. All subsequent calls of
* {@link triggerAttack} and {@link triggerRelease} will be scheduled along the transport.
* @example
* synth.sync()
* //schedule 3 notes when the transport first starts
* synth.triggerAttackRelease('8n', 0)
* synth.triggerAttackRelease('8n', '8n')
* synth.triggerAttackRelease('8n', '4n')
* //start the transport to hear the notes
* Transport.start()
*/
sync(): this {
this._syncMethod("triggerAttack", 1);
this._syncMethod("triggerRelease", 1);
@ -360,14 +352,15 @@ export class PolySynth<Voice extends Monophonic<any> = Synth> extends Instrument
/**
* Set a member/attribute of the voices
* @example
* import { PolySynth } from "tone";
* const poly = new PolySynth().toDestination();
* // set all of the voices using an options object for the synth type
* poly.set({
* "filter" : {
* "type" : "highpass"
* },
* "envelope" : {
* "attack" : 0.25
* envelope: {
* attack: 0.25
* }
* });
* poly.triggerAttackRelease("Bb3", 0.2);
*/
set(options: RecursivePartial<VoiceOptions<Voice>>): this {
// remove options which are controlled by the PolySynth

View file

@ -32,15 +32,17 @@ export interface SamplerOptions extends InstrumentOptions {
* For sample or buffer playback where repitching is not necessary,
* use [[Player]].
* @example
* var sampler = new Sampler({
* "C3" : "path/to/C3.mp3",
* "D#3" : "path/to/Dsharp3.mp3",
* "F#3" : "path/to/Fsharp3.mp3",
* "A3" : "path/to/A3.mp3",
* }, function(){
* //sampler will repitch the closest sample
* sampler.triggerAttack("D3")
* })
* import { Sampler } from "tone";
* const sampler = new Sampler({
* urls: {
* C1: "C1.mp3",
* C2: "C2.mp3",
* },
* baseUrl: "https://tonejs.github.io/examples/audio/casio/",
* onload: () => {
* sampler.triggerAttackRelease(["C1", "E1", "G1", "B1"], 0.5);
* },
* });
* @category Instrument
*/
export class Sampler extends Instrument<SamplerOptions> {
@ -239,20 +241,6 @@ export class Sampler extends Instrument<SamplerOptions> {
return this;
}
/**
* Sync the instrument to the Transport. All subsequent calls of
* [[triggerAttack]] and [[triggerRelease]]
* will be scheduled along the transport.
* @example
* synth.sync()
* //schedule 3 notes when the transport first starts
* synth.triggerAttackRelease('8n', 0)
* synth.triggerAttackRelease('8n', '8n')
* synth.triggerAttackRelease('8n', '4n')
* //start the transport to hear the notes
* Transport.start()
* @returns {Tone.Instrument} this
*/
sync(): this {
this._syncMethod("triggerAttack", 1);
this._syncMethod("triggerRelease", 1);

View file

@ -24,7 +24,8 @@ export interface SynthOptions extends MonophonicOptions {
* +----------------+ +-------------------+
* ```
* @example
* var synth = new Synth().toDestination();
* import { Synth } from "tone";
* const synth = new Synth().toDestination();
* synth.triggerAttackRelease("C4", "8n");
* @category Instrument
*/

View file

@ -6,8 +6,9 @@ import { WaveShaper } from "./WaveShaper";
* Return the absolute value of an incoming signal.
*
* @example
* var signal = new Tone.Signal(-1);
* var abs = new Tone.Abs();
* import { Abs, Signal } from "tone";
* const signal = new Signal(-1);
* const abs = new Abs();
* signal.connect(abs);
* // the output of abs is 1.
* @category Signal

View file

@ -10,16 +10,18 @@ import { Signal, SignalOptions } from "./Signal";
* If a value is passed into the constructor, the it will be added to the input.
*
* @example
* var signal = new Signal(2);
* var add = new Add(2);
* import { Add, Signal } from "tone";
* const signal = new Signal(2);
* // add a signal and a scalar
* const add = new Add(2);
* signal.connect(add);
* // the output of add equals 4
* @example
* //if constructed with no arguments
* //it will add the first and second inputs
* var add = new Add();
* var sig0 = new Signal(3).connect(add);
* var sig1 = new Signal(4).connect(add.addend);
* import { Add, Signal } from "tone";
* // Add two signal inputs
* const add = new Add();
* const sig0 = new Signal(3).connect(add);
* const sig1 = new Signal(4).connect(add.addend);
* // the output of add equals 7.
* @category Signal
*/

View file

@ -5,9 +5,6 @@ import { WaveShaper } from "./WaveShaper";
/**
* AudioToGain converts an input in AudioRange [-1,1] to NormalRange [0,1].
* See {@link GainToAudio}.
*
* @example
* var a2g = new AudioToGain();
* @category Signal
*/
export class AudioToGain extends SignalOperator<ToneAudioNodeOptions> {

View file

@ -5,9 +5,6 @@ import { WaveShaper } from "./WaveShaper";
/**
* GainToAudio converts an input in NormalRange [0,1] to AudioRange [-1,1].
* See {@link AudioToGain}.
*
* @example
* var a2g = new GainToAudio();
* @category Signal
*/
export class GainToAudio extends SignalOperator<ToneAudioNodeOptions> {

View file

@ -8,15 +8,19 @@ import { Signal, SignalOptions } from "./Signal";
* multiplies the incoming signal by that value.
*
* @example
* import { Multiply, Signal } from "tone";
* // multiply two signals
* const mult = new Multiply();
* const sigA = new Tone.Signal(3);
* const sigB = new Tone.Signal(4);
* const sigA = new Signal(3);
* const sigB = new Signal(4);
* sigA.connect(mult);
* sigB.connect(mult.factor);
* // output of mult is 12.
* @example
* import { Multiply, Signal } from "tone";
* // multiply a signal and a number
* const mult = new Multiply(10);
* const sig = new Tone.Signal(2).connect(mult);
* const sig = new Signal(2).connect(mult);
* // the output of mult is 20.
* @category Signal
*/

View file

@ -7,8 +7,9 @@ import { SignalOperator } from "./SignalOperator";
* Negate the incoming signal. i.e. an input signal of 10 will output -10
*
* @example
* var neg = new Negate();
* var sig = new Signal(-2).connect(neg);
* import { Negate, Signal } from "tone";
* const neg = new Negate();
* const sig = new Signal(-2).connect(neg);
* // output of neg is positive 2.
* @category Signal
*/

View file

@ -1,7 +1,7 @@
import { WaveShaper, WaveShaperMappingFn } from "./WaveShaper";
import { optionsFromArguments } from "Tone";
import { optionsFromArguments } from "../core/util/Defaults";
import { SignalOperator } from "./SignalOperator";
import { ToneAudioNodeOptions } from "Tone/core/context/ToneAudioNode";
import { ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
export interface PowOptions extends ToneAudioNodeOptions {
value: number;
@ -11,8 +11,9 @@ export interface PowOptions extends ToneAudioNodeOptions {
* Pow applies an exponent to the incoming signal. The incoming signal must be AudioRange [-1, 1]
*
* @example
* var pow = new Pow(2);
* var sig = new Signal(0.5).connect(pow);
* import { Pow, Signal } from "tone";
* const pow = new Pow(2);
* const sig = new Signal(0.5).connect(pow);
* // output of pow is 0.25.
* @category Signal
*/

View file

@ -16,8 +16,9 @@ export interface ScaleOptions extends ToneAudioNodeOptions {
* outputMin and outputMax.
*
* @example
* var scale = new Scale(50, 100);
* var signal = new Signal(0.5).connect(scale);
* import { Scale, Signal } from "tone";
* const scale = new Scale(50, 100);
* const signal = new Signal(0.5).connect(scale);
* // the output of scale equals 75
* @category Signal
*/

View file

@ -22,7 +22,15 @@ export interface SignalOptions<Type> extends ToneAudioNodeOptions {
* [here](https://github.com/Tonejs/Tone.js/wiki/Signals).
*
* @example
* const signal = new Tone.Signal(10);
* import { Oscillator, Signal } from "tone";
* const osc = new Oscillator().toDestination().start();
* // a scheduleable signal which can be connected to control an AudioParam or another Signal
* const signal = new Signal({
* value: "C4",
* units: "frequency"
* }).connect(osc.frequency);
* // the scheduled ramp controls the connected signal
* signal.rampTo("C2", 4, "+0.5");
* @category Signal
*/
export class Signal<Type extends Unit = number> extends ToneAudioNode<SignalOptions<any>>

View file

@ -10,13 +10,17 @@ import { Signal, SignalOptions } from "../signal/Signal";
* The subtrahend.
*
* @example
* var sub = new Subtract(1);
* var sig = new Tone.Signal(4).connect(sub);
* import { Signal, Subtract } from "tone";
* // subtract a scalar from a signal
* const sub = new Subtract(1);
* const sig = new Signal(4).connect(sub);
* // the output of sub is 3.
* @example
* var sub = new Subtract();
* var sigA = new Tone.Signal(10);
* var sigB = new Tone.Signal(2.5);
* import { Signal, Subtract } from "tone";
* // subtract two signals
* const sub = new Subtract();
* const sigA = new Signal(10);
* const sigB = new Signal(2.5);
* sigA.connect(sub);
* sigB.connect(sub.subtrahend);
* // output of sub is 7.5

View file

@ -19,12 +19,11 @@ interface WaveShaperOptions extends ToneAudioNodeOptions {
* [WaveShaperNode](http://webaudio.github.io/web-audio-api/#the-waveshapernode-interface).
*
* @example
* var timesTwo = new WaveShaper(function(val){
* return val * 2;
* }, 2048);
* @example
* //a waveshaper can also be constructed with an array of values
* var invert = new WaveShaper([1, -1]);
* import { Oscillator, Signal, WaveShaper } from "tone";
* const osc = new Oscillator().toDestination().start();
* // multiply the output of the signal by 2 using the waveshaper's function
* const timesTwo = new WaveShaper((val) => val * 2, 2048).connect(osc.frequency);
* const signal = new Signal(440).connect(timesTwo);
* @category Signal
*/
export class WaveShaper extends SignalOperator<WaveShaperOptions> {
@ -85,10 +84,10 @@ export class WaveShaper extends SignalOperator<WaveShaperOptions> {
* which goes from -1 to 1 over the number of elements
* in the curve array. The second argument is the array position.
* @example
* import { WaveShaper } from "tone";
* const shaper = new WaveShaper();
* // map the input signal from [-1, 1] to [0, 10]
* shaper.setMap(function(val, index){
* return (val + 1) * 5;
* })
* shaper.setMap((val, index) => (val + 1) * 5);
*/
setMap(mapping: WaveShaperMappingFn, length: number = 1024): this {
const array = new Float32Array(length);

View file

@ -19,20 +19,19 @@ export interface NoiseOptions extends SourceOptions {
* colors of noise on [Wikipedia](https://en.wikipedia.org/wiki/Colors_of_noise).
*
* @example
* import { AutoFilter, Noise } from "tone";
* // initialize the noise and start
* var noise = new Noise("pink").start();
*
* const noise = new Noise("pink").start();
* // make an autofilter to shape the noise
* var autoFilter = new Tone.AutoFilter({
* "frequency" : "8m",
* "min" : 800,
* "max" : 15000
* }).connect(Tone.Destination);
*
* const autoFilter = new AutoFilter({
* frequency: "8m",
* min: 800,
* max: 15000
* }).toDestination();
* // connect the noise
* noise.connect(autoFilter);
* // start the autofilter LFO
* autoFilter.start()
* autoFilter.start();
* @category Source
*/
export class Noise extends Source<NoiseOptions> {
@ -92,7 +91,9 @@ export class Noise extends Source<NoiseOptions> {
/**
* The type of the noise. Can be "white", "brown", or "pink".
* @example
* noise.type = "white";
* import { Noise } from "tone";
* const noise = new Noise().toDestination().start();
* noise.type = "brown";
*/
get type(): NoiseType {
return this._type;

View file

@ -18,11 +18,9 @@ export interface SourceOptions extends ToneAudioNodeOptions {
}
/**
* Base class for sources. Sources have start/stop methods
* and the ability to be synced to the
* Base class for sources.
* start/stop of this.context.transport.
*
* @example
* ```javascript
* //Multiple state change events can be chained together,
* //but must be set in the correct order and with ascending times
*
@ -35,7 +33,7 @@ export interface SourceOptions extends ToneAudioNodeOptions {
* state.stop("+0.2").start();
* // OR
* state.start("+0.3").stop("+0.2");
*
* ```
*/
export abstract class Source<Options extends SourceOptions> extends ToneAudioNode<Options> {
@ -57,6 +55,8 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
/**
* The volume of the output in decibels.
* @example
* import { PWMOscillator } from "tone";
* const source = new PWMOscillator().toDestination();
* source.volume.value = -6;
*/
volume: Param<Decibels>;
@ -119,6 +119,12 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
/**
* Returns the playback state of the source, either "started" or "stopped".
* @example
* import { Player } from "tone";
* const player = new Player("https://tonejs.github.io/examples/audio/FWDL.mp3", () => {
* player.start();
* console.log(player.state);
* }).toDestination();
*/
get state(): BasicPlaybackState {
if (this._synced) {
@ -135,8 +141,10 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
/**
* Mute the output.
* @example
* import { Oscillator } from "tone";
* const osc = new Oscillator().toDestination().start();
* // mute the output
* source.mute = true;
* osc.mute = true;
*/
get mute(): boolean {
return this._volume.mute;
@ -167,6 +175,8 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
* start the source now.
* @param time When the source should be started.
* @example
* import { Oscillator } from "tone";
* const source = new Oscillator().toDestination();
* source.start("+0.5"); // starts the source 0.5 seconds from now
*/
start(time?: Time, offset?: Time, duration?: Time): this {
@ -209,7 +219,10 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
* stop the source now.
* @param time When the source should be stopped.
* @example
* source.stop(); // stops the source immediately
* import { Oscillator } from "tone";
* const source = new Oscillator().toDestination();
* source.start();
* source.stop("+0.5"); // stops the source 0.5 seconds from now
*/
stop(time?: Time): this {
let computedTime = isUndef(time) && this._synced ? this.context.transport.seconds : this.toSeconds(time);
@ -232,17 +245,12 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
* instead of the AudioContext time.
*
* @example
* import { Oscillator, Transport } from "tone";
* const osc = new Oscillator().toDestination();
* // sync the source so that it plays between 0 and 0.3 on the Transport's timeline
* source.sync().start(0).stop(0.3);
* osc.sync().start(0).stop(0.3);
* // start the transport.
* this.context.transport.start();
*
* @example
* //start the transport with an offset and the sync'ed sources
* //will start in the correct position
* source.sync().start(0.1);
* //the source will be invoked with an offset of 0.4 = (0.5 - 0.1)
* this.context.transport.start("+0.5", 0.5);
* Transport.start();
*/
sync(): this {
if (!this._synced) {

View file

@ -15,11 +15,10 @@ export interface UserMediaOptions extends ToneAudioNodeOptions {
* Check [MediaDevices API Support](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia)
* to see which browsers are supported. Access to an external input
* is limited to secure (HTTPS) connections.
*
* @example
* //list the inputs and open the third one
* const motu = new UserMedia();
* motu.open().then(() => {
* import { UserMedia } from "tone";
* const mic = new UserMedia();
* mic.open().then(() => {
* // promise resolves when input is available
* });
* @category Source
@ -54,8 +53,6 @@ export class UserMedia extends ToneAudioNode<UserMediaOptions> {
/**
* The volume of the output in decibels.
* @example
* input.volume.value = -6;
*/
readonly volume: Param<Decibels>;
@ -158,9 +155,10 @@ export class UserMedia extends ToneAudioNode<UserMediaOptions> {
* Returns a promise which resolves with the list of audio input devices available.
* @return The promise that is resolved with the devices
* @example
* import { UserMedia } from "tone";
* UserMedia.enumerateDevices().then((devices) => {
* console.log(devices)
* })
* console.log(devices);
* });
*/
static async enumerateDevices(): Promise<MediaDeviceInfo[]> {
const allDevices = await navigator.mediaDevices.enumerateDevices();
@ -222,8 +220,13 @@ export class UserMedia extends ToneAudioNode<UserMediaOptions> {
/**
* Mute the output.
* @example
* import { UserMedia } from "tone";
* const mic = new UserMedia();
* mic.open().then(() => {
* // promise resolves when input is available
* });
* // mute the output
* userMedia.mute = true;
* mic.mute = true;
*/
get mute(): boolean {
return this._volume.mute;

View file

@ -22,7 +22,8 @@ export interface PlayerOptions extends SourceOptions {
/**
* Player is an audio file player with start, loop, and stop functions.
* @example
* var player = new Player("./path/to/sample.mp3").toDestination();
* import { Player } from "tone";
* const player = new Player("https://tonejs.github.io/examples/audio/FWDL.mp3").toDestination();
* // play as soon as the buffer is loaded
* player.autostart = true;
* @category Source
@ -34,12 +35,6 @@ export class Player extends Source<PlayerOptions> {
/**
* If the file should play as soon
* as the buffer is loaded.
* @example
* //will play as soon as it's loaded
* var player = new Player({
* "url" : "./path/to/sample.mp3",
* "autostart" : true,
* }).toDestination();
*/
autostart: boolean;
@ -266,8 +261,12 @@ export class Player extends Source<PlayerOptions> {
* @param offset The time to seek to.
* @param when The time for the seek event to occur.
* @example
* source.start(0.2);
* source.stop(0.4);
* import { Player } from "tone";
* const player = new Player("https://tonejs.github.io/examples/audio/FWDL.mp3", () => {
* player.start();
* // seek to the offset in 1 second from now
* player.seek(0.4, "+1");
* }).toDestination();
*/
seek(offset: Time, when?: Time): this {
const computedTime = this.toSeconds(when);
@ -286,9 +285,12 @@ export class Player extends Source<PlayerOptions> {
* @param loopStart The loop end time
* @param loopEnd The loop end time
* @example
* //loop 0.1 seconds of the file.
* import { Player } from "tone";
* const player = new Player("https://tonejs.github.io/examples/audio/FWDL.mp3").toDestination();
* // loop between the given points
* player.setLoopPoints(0.2, 0.3);
* player.loop = true;
* player.autostart = true;
*/
setLoopPoints(loopStart: Time, loopEnd: Time): this {
this.loopStart = loopStart;

View file

@ -35,8 +35,6 @@ export class Players extends ToneAudioNode<PlayersOptions> {
/**
* The volume of the output in decibels.
* @example
* source.volume.value = -6;
*/
readonly volume: Param<Decibels>;
@ -116,9 +114,6 @@ export class Players extends ToneAudioNode<PlayersOptions> {
/**
* Mute the output.
* @example
* //mute the output
* source.mute = true;
*/
get mute(): boolean {
return this._volume.mute;

View file

@ -29,6 +29,7 @@ export { AMOscillatorOptions } from "./OscillatorInterface";
* ```
*
* @example
* import { AMOscillator } from "tone";
* // a sine oscillator amplitude-modulated by a square wave
* const amOsc = new AMOscillator("Ab3", "sine", "square").toDestination().start();
* @category Source
@ -42,14 +43,7 @@ export class AMOscillator extends Source<AMOscillatorOptions> implements ToneOsc
*/
private _carrier: Oscillator;
/**
* The oscillator's frequency
*/
readonly frequency: Signal<Frequency>;
/**
* The detune control signal.
*/
readonly detune: Signal<Cents>;
/**
@ -67,8 +61,10 @@ export class AMOscillator extends Source<AMOscillatorOptions> implements ToneOsc
* A harmonicity of 1 gives both oscillators the same frequency.
* Harmonicity = 2 means a change of an octave.
* @example
* import { AMOscillator } from "tone";
* const amOsc = new AMOscillator("D2").toDestination().start();
* // pitch the modulator an octave below carrier
* synth.harmonicity.value = 0.5;
* amOsc.harmonicity.value = 0.5;
*/
readonly harmonicity: Signal<Positive>;
@ -164,13 +160,6 @@ export class AMOscillator extends Source<AMOscillatorOptions> implements ToneOsc
this._carrier.type = type;
}
/**
* The oscillator type without the partialsCount appended to the end
* @example
* osc.type = 'sine2'
* osc.baseType //'sine'
* osc.partialCount = 2
*/
get baseType(): OscillatorType {
return this._carrier.baseType;
}
@ -178,16 +167,9 @@ export class AMOscillator extends Source<AMOscillatorOptions> implements ToneOsc
this._carrier.baseType = baseType;
}
/**
* 'partialCount' offers an alternative way to set the number of used partials.
* When partialCount is 0, the maximum number of partials are used when representing
* the waveform using the periodicWave. When 'partials' is set, this value is
* not settable, but equals the length of the partials array.
*/
get partialCount(): number {
return this._carrier.partialCount;
}
set partialCount(partialCount: number) {
this._carrier.partialCount = partialCount;
}
@ -198,14 +180,10 @@ export class AMOscillator extends Source<AMOscillatorOptions> implements ToneOsc
get modulationType(): ToneOscillatorType {
return this._modulator.type;
}
set modulationType(type: ToneOscillatorType) {
this._modulator.type = type;
}
/**
* The phase of the oscillator in degrees.
*/
get phase(): Degrees {
return this._carrier.phase;
}
@ -214,16 +192,6 @@ export class AMOscillator extends Source<AMOscillatorOptions> implements ToneOsc
this._modulator.phase = phase;
}
/**
* The partials of the carrier waveform. A partial represents
* the amplitude at a harmonic. The first harmonic is the
* fundamental frequency, the second is the octave and so on
* following the harmonic series.
* Setting this value will automatically set the type to "custom".
* The value is an empty array when the type is not "custom".
* @example
* osc.partials = [1, 0.2, 0.01];
*/
get partials(): number[] {
return this._carrier.partials;
}

View file

@ -25,8 +25,9 @@ export { FMOscillatorOptions } from "./OscillatorInterface";
* ```
*
* @example
* import { FMOscillator } from "tone";
* // a sine oscillator frequency-modulated by a square wave
* var fmOsc = new FMOscillator("Ab3", "sine", "square").toDestination().start();
* const fmOsc = new FMOscillator("Ab3", "sine", "square").toDestination().start();
* @category Source
*/
export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOscillatorInterface {
@ -38,14 +39,7 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOsc
*/
private _carrier: Oscillator;
/**
* The oscillator's frequency
*/
readonly frequency: Signal<Frequency>;
/**
* The detune control signal.
*/
readonly detune: Signal<Cents>;
/**
@ -58,8 +52,10 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOsc
* A harmonicity of 1 gives both oscillators the same frequency.
* Harmonicity = 2 means a change of an octave.
* @example
* import { FMOscillator } from "tone";
* const fmOsc = new FMOscillator("D2").toDestination().start();
* // pitch the modulator an octave below carrier
* synth.harmonicity.value = 0.5;
* fmOsc.harmonicity.value = 0.5;
*/
readonly harmonicity: Signal<Positive>;
@ -170,9 +166,6 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOsc
return this;
}
/**
* The type of the carrier oscillator
*/
get type(): ToneOscillatorType {
return this._carrier.type;
}
@ -180,13 +173,6 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOsc
this._carrier.type = type;
}
/**
* The oscillator type without the partialsCount appended to the end
* @example
* osc.type = 'sine2'
* osc.baseType //'sine'
* osc.partialCount = 2
*/
get baseType(): OscillatorType {
return this._carrier.baseType;
}
@ -194,12 +180,6 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOsc
this._carrier.baseType = baseType;
}
/**
* 'partialCount' offers an alternative way to set the number of used partials.
* When partialCount is 0, the maximum number of partials are used when representing
* the waveform using the periodicWave. When 'partials' is set, this value is
* not settable, but equals the length of the partials array.
*/
get partialCount(): number {
return this._carrier.partialCount;
}
@ -217,9 +197,6 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOsc
this._modulator.type = type;
}
/**
* The phase of the oscillator in degrees.
*/
get phase(): Degrees {
return this._carrier.phase;
}
@ -228,16 +205,6 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOsc
this._modulator.phase = phase;
}
/**
* The partials of the carrier waveform. A partial represents
* the amplitude at a harmonic. The first harmonic is the
* fundamental frequency, the second is the octave and so on
* following the harmonic series.
* Setting this value will automatically set the type to "custom".
* The value is an empty array when the type is not "custom".
* @example
* osc.partials = [1, 0.2, 0.01];
*/
get partials(): number[] {
return this._carrier.partials;
}

View file

@ -1,37 +1,27 @@
import { AudioRange, Cents, Degrees, Frequency, Positive, Time } from "../../core/type/Units";
import { Cents, Degrees, Frequency, Positive, Time } from "../../core/type/Units";
import { optionsFromArguments } from "../../core/util/Defaults";
import { noOp, readOnly } from "../../core/util/Interface";
import { AudioToGain } from "../../signal/AudioToGain";
import { Multiply } from "../../signal/Multiply";
import { Signal } from "../../signal/Signal";
import { Source } from "../Source";
import { Oscillator } from "./Oscillator";
import { FatConstructorOptions, FatOscillatorOptions,
generateWaveform, ToneOscillatorInterface, ToneOscillatorType } from "./OscillatorInterface";
import { assertRange } from "../../core/util/Debug";
export { FatOscillatorOptions } from "./OscillatorInterface";
/**
* FatOscillator is an array of oscillators with detune spread between the oscillators
* @param frequency The oscillator's frequency.
* @param type The type of the oscillator.
* @param spread The detune spread between the oscillators.
* @example
* var fatOsc = new FatOscillator("Ab3", "sine", 40).toDestination().start();
* import { FatOscillator } from "tone";
* const fatOsc = new FatOscillator("Ab3", "sawtooth", 40).toDestination().start();
* @category Source
*/
export class FatOscillator extends Source<FatOscillatorOptions> implements ToneOscillatorInterface {
readonly name: string = "FatOscillator";
/**
* The oscillator's frequency
*/
readonly frequency: Signal<Frequency>;
/**
* The detune control signal.
*/
readonly detune: Signal<Cents>;
/**
@ -64,8 +54,13 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements ToneO
*/
private _partialCount: number;
/**
* @param frequency The oscillator's frequency.
* @param type The type of the oscillator.
* @param spread The detune spread between the oscillators.
*/
constructor(frequency?: Frequency, type?: ToneOscillatorType, spread?: Cents);
constructor(options?: Partial<FatConstructorOptions>);
constructor(frequency?: Frequency, type?: ToneOscillatorType, modulationType?: ToneOscillatorType);
constructor() {
super(optionsFromArguments(FatOscillator.getDefaults(), arguments, ["frequency", "type", "spread"]));
@ -142,7 +137,6 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements ToneO
get type(): ToneOscillatorType {
return this._type;
}
set type(type: ToneOscillatorType) {
this._type = type;
this._forEach(osc => osc.type = type);
@ -153,11 +147,14 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements ToneO
* set to 3 oscillators and the "spread" is set to 40,
* the three oscillators would be detuned like this: [-20, 0, 20]
* for a total detune spread of 40 cents.
* @example
* import { FatOscillator } from "tone";
* const fatOsc = new FatOscillator().toDestination().start();
* fatOsc.spread = 70;
*/
get spread(): Cents {
return this._spread;
}
set spread(spread: Cents) {
this._spread = spread;
if (this._oscillators.length > 1) {
@ -168,13 +165,18 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements ToneO
}
/**
* The number of detuned oscillators. Should be an integer greater than 1.
* The number of detuned oscillators. Must be an integer greater than 1.
* @example
* import { FatOscillator } from "tone";
* const fatOsc = new FatOscillator("C#3", "sawtooth").toDestination().start();
* // use 4 sawtooth oscillators
* fatOsc.count = 4;
*/
get count(): number {
return this._oscillators.length;
}
set count(count: number) {
count = Math.max(count, 1);
assertRange(count, 1);
if (this._oscillators.length !== count) {
// dispose the previous oscillators
this._forEach(osc => osc.dispose());
@ -205,9 +207,6 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements ToneO
}
}
/**
* The phase of the oscillator in degrees.
*/
get phase(): Degrees {
return this._phase;
}
@ -216,13 +215,6 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements ToneO
this._forEach(osc => osc.phase = phase);
}
/**
* The oscillator type without the partialsCount appended to the end
* @example
* osc.type = 'sine2'
* osc.baseType //'sine'
* osc.partialCount = 2
*/
get baseType(): OscillatorType {
return this._oscillators[0].baseType;
}
@ -231,19 +223,6 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements ToneO
this._type = this._oscillators[0].type;
}
/**
* The partials of the carrier waveform. A partial represents
* the amplitude at a harmonic. The first harmonic is the
* fundamental frequency, the second is the octave and so on
* following the harmonic series.
* Setting this value will automatically set the type to "custom".
* The value is an empty array when the type is not "custom".
* @memberOf FatOscillator#
* @type {Array}
* @name partials
* @example
* osc.partials = [1, 0.2, 0.01];
*/
get partials(): number[] {
return this._oscillators[0].partials;
}
@ -256,15 +235,6 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements ToneO
}
}
/**
* 'partialCount' offers an alternative way to set the number of used partials.
* When partialCount is 0, the maximum number of partials are used when representing
* the waveform using the periodicWave. When 'partials' is set, this value is
* not settable, but equals the length of the partials array.
* @memberOf FatOscillator#
* @type {Number}
* @name partialCount
*/
get partialCount(): number {
return this._oscillators[0].partialCount;
}

View file

@ -29,7 +29,11 @@ export interface LFOOptions extends ToneAudioNodeOptions {
* also be synced to the transport to start/stop and change when the tempo changes.
*
* @example
* var lfo = new LFO("4n", 400, 4000);
* import { Filter, LFO, Noise } from "tone";
* const filter = new Filter().toDestination();
* const noise = new Noise().connect(filter).start();
* const lfo = new LFO("4n", 400, 4000).start();
* // have it control the filters cutoff
* lfo.connect(filter.frequency);
* @category Source
*/
@ -196,10 +200,10 @@ export class LFO extends ToneAudioNode<LFOOptions> {
* Sync the start/stop/pause to the transport
* and the frequency to the bpm of the transport
* @example
* lfo.frequency.value = "8n";
* lfo.sync().start(0)
* //the rate of the LFO will always be an eighth note,
* //even as the tempo changes
* import { LFO } from "tone";
* const lfo = new LFO("8n");
* lfo.sync().start(0);
* // the rate of the LFO will always be an eighth note, even as the tempo changes
*/
sync(): this {
this._oscillator.sync();

View file

@ -68,7 +68,8 @@ const OmniOscillatorSourceMap: {
* For example: `omniOsc.type = "fatsawtooth"` will create set the oscillator
* to a FatOscillator of type "sawtooth".
* @example
* var omniOsc = new OmniOscillator("C#4", "pwm");
* import { OmniOscillator } from "tone";
* const omniOsc = new OmniOscillator("C#4", "pwm");
* @category Source
*/
export class OmniOscillator<OscType extends AnyOscillator>
@ -77,14 +78,7 @@ export class OmniOscillator<OscType extends AnyOscillator>
readonly name: string = "OmniOscillator";
/**
* The frequency control.
*/
readonly frequency: Signal<Frequency>;
/**
* The detune control.
*/
readonly detune: Signal<Cents>;
/**
@ -161,11 +155,15 @@ export class OmniOscillator<OscType extends AnyOscillator>
* oscillator's class are accessible when the oscillator is set to that type, but throws an error
* when it's not.
* @example
* import { OmniOscillator } from "tone";
* const omniOsc = new OmniOscillator().toDestination().start();
* omniOsc.type = "pwm";
* // modulationFrequency is parameter which is available
* // only when the type is "pwm".
* omniOsc.modulationFrequency.value = 0.5;
* @example
* import { OmniOscillator } from "tone";
* const omniOsc = new OmniOscillator().toDestination().start();
* // an square wave frequency modulated by a sawtooth
* omniOsc.type = "fmsquare";
* omniOsc.modulationType = "sawtooth";
@ -203,15 +201,9 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
/**
* The partials of the waveform. A partial represents
* the amplitude at a harmonic. The first harmonic is the
* fundamental frequency, the second is the octave and so on
* following the harmonic series.
* Setting this value will automatically set the type to "custom".
* The value is an empty array when the type is not "custom".
* This is not available on "pwm" and "pulse" oscillator types.
* @example
* osc.partials = [1, 0.2, 0.01];
* See [[Oscillator.partials]]
*/
get partials(): number[] {
return this._oscillator.partials;
@ -222,12 +214,6 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
}
/**
* The partial count of the oscillator. This is not available on "pwm" and "pulse" oscillator types.
* @example
* //set the maximum number of partials
* osc.partialCount = 0;
*/
get partialCount(): number {
return this._oscillator.partialCount;
}
@ -237,21 +223,6 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
}
/**
* Set the parameters at once. Either pass in an
* object mapping parameters to values, or to set a
* single parameter, by passing in a string and value.
* The last argument is an optional ramp time which
* will ramp any signal values to their destination value
* over the duration of the rampTime.
* @param props
* @example
* //set values using an object
* filter.set({
* "frequency" : 300,
* "type" : "highpass"
* });
*/
set(props: Partial<OmniOscillatorConstructorOptions>): this {
// make sure the type is set first
if (Reflect.has(props, "type") && props.type) {
@ -290,9 +261,6 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
}
/**
* The phase of the oscillator in degrees.
*/
get phase(): Degrees {
return this._oscillator.phase;
}
@ -303,8 +271,9 @@ export class OmniOscillator<OscType extends AnyOscillator>
/**
* The source type of the oscillator.
* @example
* var omniOsc = new OmniOscillator(440, "fmsquare");
* omniOsc.sourceType // 'fm'
* import { OmniOscillator } from "tone";
* const omniOsc = new OmniOscillator(440, "fmsquare");
* console.log(omniOsc.sourceType); // 'fm'
*/
get sourceType(): OmniOscSourceType {
return this._sourceType;
@ -340,12 +309,13 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
/**
* The base type of the oscillator.
* The base type of the oscillator. See [[Oscillator.baseType]]
* @example
* var omniOsc = new OmniOscillator(440, "fmsquare4");
* omniOsc.sourceType // 'fm'
* omniOsc.baseType //'square'
* omniOsc.partialCount //4
* import { OmniOscillator } from "tone";
* const omniOsc = new OmniOscillator(440, "fmsquare4");
* omniOsc.sourceType; // 'fm'
* omniOsc.baseType; // 'square'
* omniOsc.partialCount; // 4
*/
get baseType(): OscillatorType | "pwm" | "pulse" {
return this._oscillator.baseType;
@ -360,8 +330,10 @@ export class OmniOscillator<OscType extends AnyOscillator>
/**
* The width of the oscillator when sourceType === "pulse".
* See [[PWMOscillator.width]]
* @example
* var omniOsc = new OmniOscillator(440, "pulse");
* import { OmniOscillator } from "tone";
* const omniOsc = new OmniOscillator(440, "pulse");
* // can access the width attribute only if type === "pulse"
* omniOsc.width.value = 0.2;
*/
@ -374,7 +346,8 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
/**
* The number of detuned oscillators, when sourceType === "fat".
* The number of detuned oscillators when sourceType === "fat".
* See [[FatOscillator.count]]
*/
get count(): IsFatOscillator<OscType, number> {
if (this._getOscType(this._oscillator, "fat")) {
@ -390,11 +363,8 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
/**
* The detune spread between the oscillators. If "count" is
* set to 3 oscillators and the "spread" is set to 40,
* the three oscillators would be detuned like this: [-20, 0, 20]
* for a total detune spread of 40 cents. See Tone.FatOscillator
* for more info.
* The detune spread between the oscillators when sourceType === "fat".
* See [[FatOscillator.count]]
*/
get spread(): IsFatOscillator<OscType, Cents> {
if (this._getOscType(this._oscillator, "fat")) {
@ -410,9 +380,8 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
/**
* The type of the modulator oscillator. Only if the oscillator
* is set to "am" or "fm" types. see. Tone.AMOscillator or Tone.FMOscillator
* for more info.
* The type of the modulator oscillator. Only if the oscillator is set to "am" or "fm" types.
* See [[AMOscillator]] or [[FMOscillator]]
*/
get modulationType(): IsAmOrFmOscillator<OscType, ToneOscillatorType> {
if (this._getOscType(this._oscillator, "fm") || this._getOscType(this._oscillator, "am")) {
@ -428,10 +397,8 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
/**
* The modulation index which is in essence the depth or amount of the modulation. In other terms it is the
* ratio of the frequency of the modulating signal (mf) to the amplitude of the
* modulating signal (ma) -- as in ma/mf.
* See Tone.FMOscillator for more info.
* The modulation index when the sourceType === "fm"
* See [[FMOscillator]].
*/
get modulationIndex(): IsFMOscillator<OscType, Signal<Positive>> {
if (this._getOscType(this._oscillator, "fm")) {
@ -443,9 +410,7 @@ export class OmniOscillator<OscType extends AnyOscillator>
/**
* Harmonicity is the frequency ratio between the carrier and the modulator oscillators.
* A harmonicity of 1 gives both oscillators the same frequency.
* Harmonicity = 2 means a change of an octave. See Tone.AMOscillator or Tone.FMOscillator
* for more info.
* See [[AMOscillator]] or [[FMOscillator]]
*/
get harmonicity(): IsAmOrFmOscillator<OscType, Signal<Positive>> {
if (this._getOscType(this._oscillator, "fm") || this._getOscType(this._oscillator, "am")) {
@ -456,13 +421,8 @@ export class OmniOscillator<OscType extends AnyOscillator>
}
/**
* The modulationFrequency Signal of the oscillator
* (only if the oscillator type is set to pwm). See
* Tone.PWMOscillator for more info.
* @example
* var omniOsc = new OmniOscillator(440, "pwm");
* //can access the modulationFrequency attribute only if type === "pwm"
* omniOsc.modulationFrequency.value = 0.2;
* The modulationFrequency Signal of the oscillator when sourceType === "pwm"
* see [[PWMOscillator]]
* @min 0.1
* @max 5
*/

View file

@ -15,8 +15,9 @@ export { ToneOscillatorOptions, ToneOscillatorType } from "./OscillatorInterface
* and Transport syncing (see Oscillator.syncFrequency).
*
* @example
* import { Oscillator } from "tone";
* // make and start a 440hz sine tone
* var osc = new Oscillator(440, "sine").toDestination().start();
* const osc = new Oscillator(440, "sine").toDestination().start();
* @category Source
*/
export class Oscillator extends Source<ToneOscillatorOptions> implements ToneOscillatorInterface {
@ -162,11 +163,13 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements ToneOsc
* Sync the signal to the Transport's bpm. Any changes to the transports bpm,
* will also affect the oscillators frequency.
* @example
* Tone.Transport.bpm.value = 120;
* import { Oscillator, Transport } from "tone";
* const osc = new Oscillator().toDestination().start();
* osc.frequency.value = 440;
* //the ration between the bpm and the frequency will be maintained
* // the ratio between the bpm and the frequency will be maintained
* osc.syncFrequency();
* Tone.Transport.bpm.value = 240;
* // double the tempo
* Transport.bpm.value *= 2;
* // the frequency of the oscillator is doubled to 880
*/
syncFrequency(): this {
@ -217,26 +220,6 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements ToneOsc
}
}
/**
* The type of the oscillator: either sine, square, triangle, or sawtooth. Also capable of
* setting the first x number of partials of the oscillator. For example: "sine4" would
* set be the first 4 partials of the sine wave and "triangle8" would set the first
* 8 partials of the triangle wave.
* <br><br>
* Uses PeriodicWave internally even for native types so that it can set the phase.
* PeriodicWave equations are from the
* [Webkit Web Audio implementation](https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/modules/webaudio/PeriodicWave.cpp&sq=package:chromium).
*
* @memberOf Oscillator#
* @type {string}
* @name type
* @example
* //set it to a square wave
* osc.type = "square";
* @example
* //set the first 6 partials of a sawtooth wave
* osc.type = "sawtooth6";
*/
get type(): ToneOscillatorType {
return this._type;
}
@ -284,13 +267,6 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements ToneOsc
}
}
/**
* The oscillator type without the partialsCount appended to the end
* @example
* osc.type = 'sine2'
* osc.baseType //'sine'
* osc.partialCount = 2
*/
get baseType(): OscillatorType {
return (this._type as string).replace(this.partialCount.toString(), "") as OscillatorType;
}
@ -302,17 +278,6 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements ToneOsc
}
}
/**
* 'partialCount' offers an alternative way to set the number of used partials.
* When partialCount is 0, the maximum number of partials are used when representing
* the waveform using the periodicWave. When 'partials' is set, this value is
* not settable, but equals the length of the partials array.
* @example
* osc.type = 'sine'
* osc.partialCount = 3
* //is equivalent to
* osc.type = 'sine3'
*/
get partialCount(): number {
return this._partialCount;
}
@ -443,16 +408,6 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements ToneOsc
return -this._inverseFFT(real, imag, this._phase) / maxValue;
}
/**
* The partials of the waveform. A partial represents
* the amplitude at a harmonic. The first harmonic is the
* fundamental frequency, the second is the octave and so on
* following the harmonic series.
* Setting this value will automatically set the type to "custom".
* The value is an empty array when the type is not "custom".
* @example
* osc.partials = [1, 0.2, 0.01];
*/
get partials(): number[] {
return this._partials.slice(0, this.partialCount);
}
@ -464,11 +419,6 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements ToneOsc
}
}
/**
* The phase of the oscillator in degrees.
* @example
* osc.phase = 180; //flips the phase of the oscillator
*/
get phase(): Degrees {
return this._phase * (180 / Math.PI);
}

View file

@ -8,13 +8,82 @@ import { OfflineContext } from "../../core/context/OfflineContext";
* The common interface of all Oscillators
*/
export interface ToneOscillatorInterface {
/**
* The oscillator type without the partialsCount appended to the end
* @example
* import { Oscillator } from "tone";
* const osc = new Oscillator();
* osc.type = "sine2";
* console.log(osc.baseType); // "sine"
*/
baseType: OscillatorType | "pulse" | "pwm";
/**
* The oscillator's type. Also capable of setting the first x number of partials of the oscillator.
* For example: "sine4" would set be the first 4 partials of the sine wave and "triangle8" would
* set the first 8 partials of the triangle wave.
* @example
* import { Oscillator } from "tone";
* const osc = new Oscillator();
* osc.type = "sine2";
*/
type: ExtendedToneOscillatorType;
/**
* The frequency value of the oscillator
* @example
* import { FMOscillator } from "tone";
* const osc = new FMOscillator("Bb4").toDestination().start();
* osc.frequency.rampTo("D2", 3);
*/
readonly frequency: Signal<Frequency>;
/**
* The detune value in cents (100th of a semitone).
* @example
* import { PulseOscillator } from "tone";
* const osc = new PulseOscillator("F3").toDestination().start();
* // pitch it 1 octave down (12 semitones)
* osc.detune.value = -1200;
*/
readonly detune: Signal<Cents>;
/**
* The phase is the starting position within the oscillator's cycle. For example
* a phase of 180 would start halfway through the oscillator's cycle.
*/
phase: Degrees;
/**
* The partials describes the relative amplitude of each of the harmonics of the oscillator.
* The first value in the array is the first harmonic (i.e. the fundamental frequency), the
* second harmonic is an octave up, the third harmonic is an octave and a fifth, etc. The resulting
* oscillator output is composed of a sine tone at the relative amplitude at each of the harmonic intervals.
*
* Setting this value will automatically set the type to "custom".
* The value is an empty array when the type is not "custom".
* @example
* import { Oscillator } from "tone";
* const osc = new Oscillator("F3").toDestination().start();
* osc.partials = [1, 0, 0.4, 1, 0.2];
*/
partials: number[];
/**
* 'partialCount' offers an alternative way to set the number of used partials.
* When partialCount is 0, the maximum number of partials are used when representing
* the waveform using the periodicWave. When 'partials' is set, this value is
* not settable, but equals the length of the partials array. A square wave wave
* is composed of only odd harmonics up through the harmonic series. Partial count
* can limit the number of harmonics which are used to generate the waveform.
* @example
* import { Oscillator } from "tone";
* const osc = new Oscillator("C3", "square").toDestination().start();
* osc.partialCount = 5;
*/
partialCount?: number;
/**
* Returns an array of values which represents the waveform.
* @param length The length of the waveform to return

View file

@ -16,7 +16,8 @@ export { PWMOscillatorOptions } from "./OscillatorInterface";
* changing the timbre of the oscillator by altering the harmonics
* generated.
* @example
* var pwm = new PWMOscillator("Ab3", 0.3).toDestination().start();
* import { PWMOscillator } from "tone";
* const pwm = new PWMOscillator("Ab3", 0.3).toDestination().start();
* @category Source
*/
export class PWMOscillator extends Source<PWMOscillatorOptions> implements ToneOscillatorInterface {

View file

@ -40,6 +40,7 @@ export { PulseOscillatorOptions } from "./OscillatorInterface";
* +-----+ +-------+ +-+
* ```
* @example
* import { PulseOscillator } from "tone";
* const pulse = new PulseOscillator("E5", 0.4).toDestination().start();
* @category Source
*/