using OscillatorInterface to define all the oscilator properties

This commit is contained in:
Yotam Mann 2019-07-19 12:32:17 -04:00
parent f8d845cede
commit b9dd8440b7
12 changed files with 466 additions and 164 deletions

View file

@ -120,4 +120,3 @@ describe("Synth", () => {
});
});
});

View file

@ -1,16 +1,15 @@
import { AmplitudeEnvelope } from "../component/envelope/AmplitudeEnvelope";
import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
import { omitFromObject, optionsFromArguments } from "../core/util/Defaults";
import { optionsFromArguments } from "../core/util/Defaults";
import { readOnly } from "../core/util/Interface";
import { RecursivePartial } from "../core/util/Interface";
import { OmniOscillator, OmniOscillatorOptions } from "../source/oscillator/OmniOscillator";
import { Source, SourceOptions } from "../source/Source";
import { OmniOscillator } from "../source/oscillator/OmniOscillator";
import { OmniOscillatorConstructorOptions } from "../source/oscillator/OscillatorInterface";
import { Monophonic, MonophonicOptions } from "./Monophonic";
export interface SynthOptions extends MonophonicOptions {
oscillator: Omit<OmniOscillatorOptions, keyof SourceOptions>;
envelope: Omit<EnvelopeOptions, keyof ToneAudioNodeOptions>;
oscillator: OmniOscillatorConstructorOptions;
envelope: EnvelopeOptions;
}
/**
@ -55,7 +54,7 @@ export class Synth extends Monophonic<SynthOptions> {
*/
input: undefined;
protected _internalChannels = [this.oscillator as unknown as ToneAudioNode, this.envelope, this.output];
protected _internalChannels = [this.oscillator, this.envelope, this.output];
constructor(options?: RecursivePartial<SynthOptions>);
constructor() {
@ -73,9 +72,7 @@ export class Synth extends Monophonic<SynthOptions> {
static getDefaults(): SynthOptions {
return Object.assign(Monophonic.getDefaults(), {
envelope: Object.assign(
omitFromObject(
Envelope.getDefaults(), ToneAudioNode.getDefaults(),
),
Envelope.getDefaults(),
{
attack : 0.005,
decay : 0.1,
@ -84,9 +81,7 @@ export class Synth extends Monophonic<SynthOptions> {
},
),
oscillator: Object.assign(
omitFromObject(
OmniOscillator.getDefaults(), Source.getDefaults(),
),
OmniOscillator.getDefaults(),
{
type: "triangle",
},

View file

@ -5,12 +5,10 @@ import { AudioToGain } from "../../signal/AudioToGain";
import { Multiply } from "../../signal/Multiply";
import { Signal } from "../../signal/Signal";
import { Source } from "../Source";
import { Oscillator, OscillatorInterface, ToneOscillatorOptions, ToneOscillatorType } from "./Oscillator";
export interface AMOscillatorOptions extends ToneOscillatorOptions {
harmonicity: Positive;
modulationType: ToneOscillatorType;
}
import { Oscillator } from "./Oscillator";
import { AMConstructorOptions, AMOscillatorOptions,
NonCustomOscillatorType, ToneOscillatorInterface,
ToneOscillatorType } from "./OscillatorInterface";
/**
* An amplitude modulated oscillator node. It is implemented with
@ -33,7 +31,7 @@ export interface AMOscillatorOptions extends ToneOscillatorOptions {
* //a sine oscillator frequency-modulated by a square wave
* var fmOsc = new AMOscillator("Ab3", "sine", "square").toMaster().start();
*/
export class AMOscillator extends Source<AMOscillatorOptions> implements OscillatorInterface {
export class AMOscillator extends Source<AMOscillatorOptions> implements ToneOscillatorInterface {
name = "AMOscillator";
@ -82,7 +80,7 @@ export class AMOscillator extends Source<AMOscillatorOptions> implements Oscilla
context: this.context,
});
constructor(options?: Partial<AMOscillatorOptions>);
constructor(options?: Partial<AMConstructorOptions>);
constructor(frequency?: Frequency, type?: ToneOscillatorType, modulationType?: ToneOscillatorType);
constructor() {
@ -108,7 +106,7 @@ export class AMOscillator extends Source<AMOscillatorOptions> implements Oscilla
static getDefaults(): AMOscillatorOptions {
return Object.assign(Oscillator.getDefaults(), {
harmonicity: 1,
modulationType: "square",
modulationType: "square" as NonCustomOscillatorType,
});
}

View file

@ -4,13 +4,9 @@ import { readOnly } from "../../core/util/Interface";
import { Multiply } from "../../signal/Multiply";
import { Signal } from "../../signal/Signal";
import { Source } from "../Source";
import { Oscillator, OscillatorInterface, ToneOscillatorOptions, ToneOscillatorType } from "./Oscillator";
export interface FMOscillatorOptions extends ToneOscillatorOptions {
harmonicity: Positive;
modulationIndex: Positive;
modulationType: ToneOscillatorType;
}
import { Oscillator } from "./Oscillator";
import { FMConstructorOptions, FMOscillatorOptions,
NonCustomOscillatorType, ToneOscillatorInterface, ToneOscillatorType } from "./OscillatorInterface";
/**
* FMOscillator implements a frequency modulation synthesis
@ -33,7 +29,7 @@ export interface FMOscillatorOptions extends ToneOscillatorOptions {
* //a sine oscillator frequency-modulated by a square wave
* var fmOsc = new FMOscillator("Ab3", "sine", "square").toMaster().start();
*/
export class FMOscillator extends Source<FMOscillatorOptions> implements OscillatorInterface {
export class FMOscillator extends Source<FMOscillatorOptions> implements ToneOscillatorInterface {
name = "FMOscillator";
@ -94,7 +90,7 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements Oscilla
gain: 0,
});
constructor(options?: Partial<FMOscillatorOptions>);
constructor(options?: Partial<FMConstructorOptions>);
constructor(frequency?: Frequency, type?: ToneOscillatorType, modulationType?: ToneOscillatorType);
constructor() {
@ -126,7 +122,7 @@ export class FMOscillator extends Source<FMOscillatorOptions> implements Oscilla
return Object.assign(Oscillator.getDefaults(), {
harmonicity: 1,
modulationIndex: 2,
modulationType: "square",
modulationType: "square" as NonCustomOscillatorType,
});
}

View file

@ -5,13 +5,9 @@ import { AudioToGain } from "../../signal/AudioToGain";
import { Multiply } from "../../signal/Multiply";
import { Signal } from "../../signal/Signal";
import { Source } from "../Source";
import { Oscillator, OscillatorInterface, ToneOscillatorOptions, ToneOscillatorType } from "./Oscillator";
export interface FatOscillatorOptions extends ToneOscillatorOptions {
spread: Cents;
count: Positive;
type: ToneOscillatorType;
}
import { Oscillator } from "./Oscillator";
import { FatConstructorOptions, FatOscillatorOptions,
ToneOscillatorInterface, ToneOscillatorType } from "./OscillatorInterface";
/**
* FatOscillator is an array of oscillators with detune spread between the oscillators
@ -21,7 +17,7 @@ export interface FatOscillatorOptions extends ToneOscillatorOptions {
* @example
* var fatOsc = new FatOscillator("Ab3", "sine", 40).toMaster().start();
*/
export class FatOscillator extends Source<FatOscillatorOptions> implements OscillatorInterface {
export class FatOscillator extends Source<FatOscillatorOptions> implements ToneOscillatorInterface {
name = "FatOscillator";
@ -71,7 +67,7 @@ export class FatOscillator extends Source<FatOscillatorOptions> implements Oscil
*/
private _partialCount: number;
constructor(options?: Partial<FatOscillatorOptions>);
constructor(options?: Partial<FatConstructorOptions>);
constructor(frequency?: Frequency, type?: ToneOscillatorType, modulationType?: ToneOscillatorType);
constructor() {

View file

@ -6,6 +6,7 @@ import { OutputAudio } from "test/helper/OutputAudio";
import { SourceTests } from "test/helper/SourceTests";
import { FMOscillator } from "./FMOscillator";
import { OmniOscillator } from "./OmniOscillator";
import { OmniOscillatorType } from "./OscillatorInterface";
import { PulseOscillator } from "./PulseOscillator";
import { PWMOscillator } from "./PWMOscillator";
@ -104,7 +105,7 @@ describe("OmniOscillator", () => {
it("handles various types", () => {
const osc = new OmniOscillator();
const types = ["triangle3", "sine", "pulse", "pwm", "amsine4", "fatsquare2", "fmsawtooth"];
const types: OmniOscillatorType[] = ["triangle3", "sine", "pulse", "pwm", "amsine4", "fatsquare2", "fmsawtooth"];
types.forEach(type => {
osc.type = type;
expect(osc.type).to.equal(type);
@ -115,6 +116,7 @@ describe("OmniOscillator", () => {
it("throws an error if invalid type is set", () => {
const osc = new OmniOscillator();
expect(() => {
// @ts-ignore
osc.type = "invalid";
}).to.throw(Error);
osc.dispose();

View file

@ -3,14 +3,17 @@ import { readOnly } from "../../core/util/Interface";
import { isNumber, isString } from "../../core/util/TypeCheck";
import { Signal } from "../../signal/Signal";
import { Source } from "../Source";
import { AMOscillator, AMOscillatorOptions } from "./AMOscillator";
import { FatOscillator, FatOscillatorOptions } from "./FatOscillator";
import { FMOscillator, FMOscillatorOptions } from "./FMOscillator";
import { Oscillator, OscillatorInterface,
ToneOscillatorBaseType, ToneOscillatorOptions,
ToneOscillatorType } from "./Oscillator";
import { PulseOscillator, PulseOscillatorOptions } from "./PulseOscillator";
import { PWMOscillator, PWMOscillatorOptions } from "./PWMOscillator";
import { AMOscillator } from "./AMOscillator";
import { FatOscillator } from "./FatOscillator";
import { FMOscillator } from "./FMOscillator";
import { Oscillator } from "./Oscillator";
import { AMOscillatorOptions, FatOscillatorOptions,
FMOscillatorOptions, OmniOscillatorConstructorOptions,
OmniOscillatorOptions, OmniOscillatorType,
PulseOscillatorOptions, PWMOscillatorOptions,
ToneOscillatorInterface, ToneOscillatorOptions, ToneOscillatorType } from "./OscillatorInterface";
import { PulseOscillator } from "./PulseOscillator";
import { PWMOscillator } from "./PWMOscillator";
/**
* All of the oscillator types that OmniOscillator can take on
@ -24,15 +27,6 @@ type TypeofAnyOscillator = typeof Oscillator | typeof PWMOscillator |
typeof PulseOscillator | typeof FatOscillator |
typeof AMOscillator | typeof FMOscillator;
/**
* The type of the oscillator. Can be any of the basic types: sine, square, triangle, sawtooth. Or
* prefix the basic types with "fm", "am", or "fat" to use the FMOscillator, AMOscillator or FatOscillator
* types. The oscillator could also be set to "pwm" or "pulse". All of the parameters of the
* oscillator's class are accessible when the oscillator is set to that type, but throws an error
* when it's not.
*/
type OmniOscillatorType = string;
/**
* Select the Oscillator's Options depending on the generic type.
*/
@ -48,7 +42,7 @@ type ConditionalOptions<Osc extends AnyOscillator> =
/**
* The aggregate options of all of the oscillators
*/
export type OmniOscillatorOptions = ConditionalOptions<AnyOscillator>;
// export type OmniOscillatorOptions = ConditionalOptions<AnyOscillator>;
/**
* All of the Oscillator constructor types mapped to their name.
@ -102,7 +96,8 @@ const OmniOscillatorSourceMap: {
* var omniOsc = new OmniOscillator("C#4", "pwm");
*/
export class OmniOscillator<OscType extends AnyOscillator>
extends Source<OmniOscillatorOptions> implements OscillatorInterface {
extends Source<OmniOscillatorConstructorOptions>
implements Omit<ToneOscillatorInterface, "type"> {
name = "OmniOscillator";
@ -132,7 +127,7 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
*/
private _sourceType!: OmniOscSourceType;
constructor(options?: Partial<ConditionalOptions<OscType>>);
constructor(options?: Partial<OmniOscillatorConstructorOptions>);
constructor(frequency?: Frequency, type?: OmniOscillatorType);
constructor() {
@ -143,14 +138,20 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
this.detune.setValueAtTime(options.detune, 0);
readOnly(this, ["frequency", "detune"]);
// // set the oscillator
// this.type = options.type;
// set the options
this.set<Oscillator>(options);
this.set(options);
}
static getDefaults(): OmniOscillatorOptions {
return Oscillator.getDefaults();
return Object.assign(
Oscillator.getDefaults(),
FMOscillator.getDefaults(),
AMOscillator.getDefaults(),
FatOscillator.getDefaults(),
PulseOscillator.getDefaults(),
PWMOscillator.getDefaults(),
);
// return Oscillator.getDefaults() as OmniOscillatorConstructorOptions;
}
/**
@ -193,21 +194,21 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
if (["am", "fm", "fat"].some(p => this._sourceType === p)) {
prefix = this._sourceType;
}
return prefix + this._oscillator.type;
return prefix + this._oscillator.type as OmniOscillatorType;
}
set type(type) {
if (type.substr(0, 2) === "fm") {
this._createNewOscillator("fm");
this._oscillator = this._oscillator as FMOscillator;
this._oscillator.type = type.substr(2);
this._oscillator.type = type.substr(2) as ToneOscillatorType;
} else if (type.substr(0, 2) === "am") {
this._createNewOscillator("am");
this._oscillator = this._oscillator as AMOscillator;
this._oscillator.type = type.substr(2);
this._oscillator.type = type.substr(2) as ToneOscillatorType;
} else if (type.substr(0, 3) === "fat") {
this._createNewOscillator("fat");
this._oscillator = this._oscillator as FatOscillator;
this._oscillator.type = type.substr(3);
this._oscillator.type = type.substr(3) as ToneOscillatorType;
} else if (type === "pwm") {
this._createNewOscillator("pwm");
this._oscillator = this._oscillator as PWMOscillator;
@ -216,7 +217,7 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
} else {
this._createNewOscillator("oscillator");
this._oscillator = this._oscillator as Oscillator;
this._oscillator.type = type;
this._oscillator.type = (type as ToneOscillatorType);
}
}
@ -270,7 +271,7 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
* "type" : "highpass"
* });
*/
set<Osc extends AnyOscillator = OscType>(props: Partial<ConditionalOptions<Osc>>): this {
set(props: Partial<OmniOscillatorConstructorOptions>): this {
// make sure the type is set first
if (Reflect.has(props, "type") && props.type) {
this.type = props.type;
@ -281,17 +282,11 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
return this;
}
/**
* Get the object's attributes. Given no arguments get
* will return all available object properties and their corresponding
* values. Pass in a single attribute to retrieve or an array
* of attributes. The attribute strings can also include a "."
* to access deeper properties.
*/
get<Osc extends AnyOscillator = OscType>(): ConditionalOptions<Osc> {
get(): OmniOscillatorConstructorOptions {
const options = this._oscillator.get();
// @ts-ignore
options.type = this.type;
return options as ConditionalOptions<Osc>;
return options as OmniOscillatorOptions;
}
/**
@ -309,8 +304,8 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
// dispose the old one
this.context.setTimeout(() => oldOsc.dispose(), this.blockTime);
}
// @ts-ignore
this._oscillator = new oscConstructor({
// @ts-ignore
context : this.context,
});
this.frequency.connect(this._oscillator.frequency);
@ -350,13 +345,13 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
// set the type
if (sType === "fm") {
this.type = "fm" + baseType;
this.type = "fm" + baseType as OmniOscillatorType;
} else if (sType === "am") {
this.type = "am" + baseType;
this.type = "am" + baseType as OmniOscillatorType;
} else if (sType === "fat") {
this.type = "fat" + baseType;
this.type = "fat" + baseType as OmniOscillatorType;
} else if (sType === "oscillator") {
this.type = baseType;
this.type = baseType as OmniOscillatorType;
} else if (sType === "pulse") {
this.type = "pulse";
} else if (sType === "pwm") {
@ -379,7 +374,7 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
* omniOsc.baseType //'square'
* omniOsc.partialCount //4
*/
get baseType(): ToneOscillatorBaseType {
get baseType(): OscillatorType | "pwm" | "pulse" {
return this._oscillator.baseType;
}
set baseType(baseType) {
@ -504,9 +499,6 @@ extends Source<OmniOscillatorOptions> implements OscillatorInterface {
}
}
/**
* Clean up.
*/
dispose(): this {
super.dispose();
this.detune.dispose();

View file

@ -6,6 +6,7 @@ import { OscillatorTests } from "test/helper/OscillatorTests";
import { OutputAudio } from "test/helper/OutputAudio";
import { SourceTests } from "test/helper/SourceTests";
import { Oscillator } from "./Oscillator";
import { ToneOscillatorType } from "./OscillatorInterface";
describe("Oscillator", () => {
@ -26,11 +27,11 @@ describe("Oscillator", () => {
it("can be set with an options object", () => {
const osc = new Oscillator();
osc.set({
detune: -21,
frequency: 231,
type: "square",
});
// osc.set({
// detune: -21,
// frequency: 231,
// type: "square",
// });
expect(osc.frequency.value).to.equal(231);
expect(osc.detune.value).to.equal(-21);
expect(osc.type).to.equal("square");
@ -108,7 +109,7 @@ describe("Oscillator", () => {
it("handles 4 basic types", () => {
const osc = new Oscillator();
const types = ["triangle", "sawtooth", "sine", "square"];
const types: ToneOscillatorType[] = ["triangle", "sawtooth", "sine", "square"];
for (const type of types) {
osc.type = type;
expect(osc.type).to.equal(type);
@ -119,6 +120,7 @@ describe("Oscillator", () => {
it("throws an error if invalid type is set", () => {
const osc = new Oscillator();
expect(() => {
// @ts-ignore
osc.type = "invalid";
}).to.throw(Error);
osc.dispose();

View file

@ -1,36 +1,10 @@
import { optionsFromArguments } from "../../core/util/Defaults";
import { readOnly } from "../../core/util/Interface";
import { Signal } from "../../signal/Signal";
import { Source, SourceOptions } from "../Source";
import { Source } from "../Source";
import { ToneOscillatorConstructorOptions, ToneOscillatorInterface,
ToneOscillatorOptions, ToneOscillatorType } from "./OscillatorInterface";
import { ToneOscillatorNode } from "./OscillatorNode";
// type OmniOscillatorSourceType = import("./OmniOscillator").OmniOscillatorSourceType;
export type ToneOscillatorBaseType = OscillatorType | "pulse" | "pwm";
export type ToneOscillatorType = ToneOscillatorBaseType | string;
export interface ToneOscillatorOptions extends SourceOptions {
type: ToneOscillatorType;
frequency: Frequency;
detune: Cents;
phase: Degrees;
partials: number[];
partialCount: number;
}
/**
* All Oscillators share this interface
*/
export interface OscillatorInterface {
partials: number[];
partialCount: number;
phase: Degrees;
readonly frequency: Signal<Frequency>;
readonly detune: Signal<Cents>;
type: ToneOscillatorType;
baseType: ToneOscillatorBaseType;
// sourceType: OmniOscillatorSourceType;
}
/**
* Oscillator supports a number of features including
@ -43,7 +17,7 @@ export interface OscillatorInterface {
* //make and start a 440hz sine tone
* var osc = new Oscillator(440, "sine").toMaster().start();
*/
export class Oscillator extends Source<ToneOscillatorOptions> implements OscillatorInterface {
export class Oscillator extends Source<ToneOscillatorOptions> implements ToneOscillatorInterface {
name = "Oscillator";
@ -89,7 +63,7 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements Oscilla
*/
private _type;
constructor(options?: Partial<ToneOscillatorOptions>)
constructor(options?: Partial<ToneOscillatorConstructorOptions>)
constructor(frequency?: Frequency, type?: ToneOscillatorType);
constructor() {
@ -129,7 +103,7 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements Oscilla
partials: [],
phase: 0,
type: "sine",
});
}) as ToneOscillatorOptions;
}
/**
@ -272,7 +246,7 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements Oscilla
}
set baseType(baseType: OscillatorType) {
if (this.partialCount && this._type !== "custom" && baseType !== "custom") {
this.type = baseType + this.partialCount;
this.type = baseType + this.partialCount as ToneOscillatorType;
} else {
this.type = baseType;
}
@ -302,7 +276,7 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements Oscilla
if (p === 0) {
this.type = type;
} else {
this.type = type + p.toString();
this.type = type + p.toString() as ToneOscillatorType;
}
}
}
@ -343,7 +317,7 @@ export class Oscillator extends Source<ToneOscillatorOptions> implements Oscilla
if (partial) {
partialCount = parseInt(partial[2], 10) + 1;
this._partialCount = parseInt(partial[2], 10);
type = partial[1];
type = partial[1] as ToneOscillatorType;
partialCount = Math.max(partialCount, 2);
periodicWaveSize = partialCount;
} else {

View file

@ -0,0 +1,352 @@
// tslint:disable: max-line-length
import { Signal } from "../../signal/Signal";
import { SourceOptions } from "../Source";
/**
* The common interface of all Oscillators
*/
export interface ToneOscillatorInterface {
baseType: OscillatorType | "pulse" | "pwm";
type: ExtendedToneOscillatorType;
readonly frequency: Signal<Frequency>;
readonly detune: Signal<Cents>;
phase: Degrees;
partials: number[];
partialCount?: number;
}
/**
* Oscillators with partials
*/
type SineWithPartials =
"sine1" | "sine2" | "sine3" | "sine4" | "sine5" | "sine6" | "sine7" | "sine8" | "sine9" |
"sine10" | "sine11" | "sine12" | "sine13" | "sine14" | "sine15" | "sine16" | "sine17" | "sine18" | "sine19" |
"sine20" | "sine21" | "sine22" | "sine23" | "sine24" | "sine25" | "sine26" | "sine27" | "sine28" | "sine29" |
"sine30" | "sine31" | "sine32";
type SquareWithPartials =
"square1" | "square2" | "square3" | "square4" | "square5" | "square6" | "square7" | "square8" | "square9" |
"square10" | "square11" | "square12" | "square13" | "square14" | "square15" | "square16" | "square17" | "square18" | "square19" |
"square20" | "square21" | "square22" | "square23" | "square24" | "square25" | "square26" | "square27" | "square28" | "square29" |
"square30" | "square31" | "square32";
type SawtoothWithPartials =
"sawtooth1" | "sawtooth2" | "sawtooth3" | "sawtooth4" | "sawtooth5" | "sawtooth6" | "sawtooth7" | "sawtooth8" | "sawtooth9" |
"sawtooth10" | "sawtooth11" | "sawtooth12" | "sawtooth13" | "sawtooth14" | "sawtooth15" | "sawtooth16" | "sawtooth17" | "sawtooth18" | "sawtooth19" |
"sawtooth20" | "sawtooth21" | "sawtooth22" | "sawtooth23" | "sawtooth24" | "sawtooth25" | "sawtooth26" | "sawtooth27" | "sawtooth28" | "sawtooth29" |
"sawtooth30" | "sawtooth31" | "sawtooth32";
type TriangleWithPartials =
"triangle1" | "triangle2" | "triangle3" | "triangle4" | "triangle5" | "triangle6" | "triangle7" | "triangle8" | "triangle9" |
"triangle10" | "triangle11" | "triangle12" | "triangle13" | "triangle14" | "triangle15" | "triangle16" | "triangle17" | "triangle18" | "triangle19" |
"triangle20" | "triangle21" | "triangle22" | "triangle23" | "triangle24" | "triangle25" | "triangle26" | "triangle27" | "triangle28" | "triangle29" |
"triangle30" | "triangle31" | "triangle32";
type TypeWithPartials = SineWithPartials | SquareWithPartials | TriangleWithPartials | SawtoothWithPartials;
interface BaseOscillatorOptions extends SourceOptions {
frequency: Frequency;
detune: Cents;
phase: Degrees;
}
export type NonCustomOscillatorType = Exclude<OscillatorType, "custom">;
type AllNonCustomOscillatorType = NonCustomOscillatorType | TypeWithPartials;
export type ToneOscillatorType = AllNonCustomOscillatorType | "custom";
export type ExtendedToneOscillatorType = ToneOscillatorType | "pwm" | "pulse";
/**
* Oscillator Interfaces
*/
interface ToneCustomOscillatorOptions extends BaseOscillatorOptions {
type: "custom";
partials: number[];
}
interface ToneTypeOscillatorOptions extends BaseOscillatorOptions {
type: NonCustomOscillatorType;
partialCount?: number;
}
interface TonePartialOscillatorOptions extends BaseOscillatorOptions {
type: TypeWithPartials;
}
export type ToneOscillatorConstructorOptions = ToneCustomOscillatorOptions | ToneTypeOscillatorOptions | TonePartialOscillatorOptions;
export interface ToneOscillatorOptions extends BaseOscillatorOptions {
type: ToneOscillatorType;
partialCount: number;
partials: number[];
}
/**
* FMOscillator Interface
*/
interface FMBaseOscillatorOptions extends BaseOscillatorOptions {
harmonicity: Positive;
modulationIndex: Positive;
modulationType: AllNonCustomOscillatorType;
}
interface FMCustomOscillatorOptions extends FMBaseOscillatorOptions {
type: "custom";
partials: number[];
}
interface FMTypeOscillatorOptions extends FMBaseOscillatorOptions {
type: NonCustomOscillatorType;
partialsCount?: number;
}
interface FMPartialsOscillatorOptions extends FMBaseOscillatorOptions {
type: TypeWithPartials;
}
export type FMConstructorOptions = FMTypeOscillatorOptions | FMCustomOscillatorOptions | FMPartialsOscillatorOptions;
export interface FMOscillatorOptions extends ToneOscillatorOptions {
harmonicity: Positive;
modulationIndex: Positive;
modulationType: AllNonCustomOscillatorType;
}
/**
* AMOscillator Interface
*/
interface AMBaseOscillatorOptions extends BaseOscillatorOptions {
harmonicity: Positive;
modulationType: AllNonCustomOscillatorType;
}
interface AMCustomOscillatorOptions extends AMBaseOscillatorOptions {
type: "custom";
partials: number[];
}
interface AMTypeOscillatorOptions extends AMBaseOscillatorOptions {
type: NonCustomOscillatorType;
partialsCount?: number;
}
interface AMPartialsOscillatorOptions extends AMBaseOscillatorOptions {
type: TypeWithPartials;
}
export type AMConstructorOptions = AMCustomOscillatorOptions | AMTypeOscillatorOptions | AMPartialsOscillatorOptions;
export interface AMOscillatorOptions extends ToneOscillatorOptions {
harmonicity: Positive;
modulationType: AllNonCustomOscillatorType;
}
/**
* FatOscillator
*/
interface FatBaseOscillatorOptions extends BaseOscillatorOptions {
spread: Cents;
count: Positive;
}
interface FatCustomOscillatorOptions extends FatBaseOscillatorOptions {
type: "custom";
partials: number[];
}
interface FatTypeOscillatorOptions extends FatBaseOscillatorOptions {
type: NonCustomOscillatorType;
partialCount?: number;
}
interface FatPartialsOscillatorOptions extends FatBaseOscillatorOptions {
type: TypeWithPartials;
}
export type FatConstructorOptions = FatCustomOscillatorOptions | FatTypeOscillatorOptions | FatPartialsOscillatorOptions;
export interface FatOscillatorOptions extends ToneOscillatorOptions {
spread: Cents;
count: Positive;
}
/**
* Pulse Oscillator
*/
export interface PulseOscillatorOptions extends BaseOscillatorOptions {
type: "pulse";
width: AudioRange;
}
/**
* PWM Oscillator
*/
export interface PWMOscillatorOptions extends BaseOscillatorOptions {
type: "pwm";
modulationFrequency: Frequency;
}
/**
* OMNI OSCILLATOR
*/
/**
* FM Oscillators with partials
*/
type FMSineWithPartials =
"fmsine1" | "fmsine2" | "fmsine3" | "fmsine4" | "fmsine5" | "fmsine6" | "fmsine7" | "fmsine8" | "fmsine9" |
"fmsine10" | "fmsine11" | "fmsine12" | "fmsine13" | "fmsine14" | "fmsine15" | "fmsine16" | "fmsine17" | "fmsine18" | "fmsine19" |
"fmsine20" | "fmsine21" | "fmsine22" | "fmsine23" | "fmsine24" | "fmsine25" | "fmsine26" | "fmsine27" | "fmsine28" | "fmsine29" |
"fmsine30" | "fmsine31" | "fmsine32";
type FMSquareWithPartials =
"fmsquare1" | "fmsquare2" | "fmsquare3" | "fmsquare4" | "fmsquare5" | "fmsquare6" | "fmsquare7" | "fmsquare8" | "fmsquare9" |
"fmsquare10" | "fmsquare11" | "fmsquare12" | "fmsquare13" | "fmsquare14" | "fmsquare15" | "fmsquare16" | "fmsquare17" | "fmsquare18" | "fmsquare19" |
"fmsquare20" | "fmsquare21" | "fmsquare22" | "fmsquare23" | "fmsquare24" | "fmsquare25" | "fmsquare26" | "fmsquare27" | "fmsquare28" | "fmsquare29" |
"fmsquare30" | "fmsquare31" | "fmsquare32";
type FMSawtoothWithPartials =
"fmsawtooth1" | "fmsawtooth2" | "fmsawtooth3" | "fmsawtooth4" | "fmsawtooth5" | "fmsawtooth6" | "fmsawtooth7" | "fmsawtooth8" | "fmsawtooth9" |
"fmsawtooth10" | "fmsawtooth11" | "fmsawtooth12" | "fmsawtooth13" | "fmsawtooth14" | "fmsawtooth15" | "fmsawtooth16" | "fmsawtooth17" | "fmsawtooth18" | "fmsawtooth19" |
"fmsawtooth20" | "fmsawtooth21" | "fmsawtooth22" | "fmsawtooth23" | "fmsawtooth24" | "fmsawtooth25" | "fmsawtooth26" | "fmsawtooth27" | "fmsawtooth28" | "fmsawtooth29" |
"fmsawtooth30" | "fmsawtooth31" | "fmsawtooth32";
type FMTriangleWithPartials =
"fmtriangle1" | "fmtriangle2" | "fmtriangle3" | "fmtriangle4" | "fmtriangle5" | "fmtriangle6" | "fmtriangle7" | "fmtriangle8" | "fmtriangle9" |
"fmtriangle10" | "fmtriangle11" | "fmtriangle12" | "fmtriangle13" | "fmtriangle14" | "fmtriangle15" | "fmtriangle16" | "fmtriangle17" | "fmtriangle18" | "fmtriangle19" |
"fmtriangle20" | "fmtriangle21" | "fmtriangle22" | "fmtriangle23" | "fmtriangle24" | "fmtriangle25" | "fmtriangle26" | "fmtriangle27" | "fmtriangle28" | "fmtriangle29" |
"fmtriangle30" | "fmtriangle31" | "fmtriangle32";
type FMTypeWithPartials = FMSineWithPartials | FMSquareWithPartials | FMSawtoothWithPartials | FMTriangleWithPartials;
/**
* AM Oscillators with partials
*/
type AMSineWithPartials =
"amsine1" | "amsine2" | "amsine3" | "amsine4" | "amsine5" | "amsine6" | "amsine7" | "amsine8" | "amsine9" |
"amsine10" | "amsine11" | "amsine12" | "amsine13" | "amsine14" | "amsine15" | "amsine16" | "amsine17" | "amsine18" | "amsine19" |
"amsine20" | "amsine21" | "amsine22" | "amsine23" | "amsine24" | "amsine25" | "amsine26" | "amsine27" | "amsine28" | "amsine29" |
"amsine30" | "amsine31" | "amsine32";
type AMSquareWithPartials =
"amsquare1" | "amsquare2" | "amsquare3" | "amsquare4" | "amsquare5" | "amsquare6" | "amsquare7" | "amsquare8" | "amsquare9" |
"amsquare10" | "amsquare11" | "amsquare12" | "amsquare13" | "amsquare14" | "amsquare15" | "amsquare16" | "amsquare17" | "amsquare18" | "amsquare19" |
"amsquare20" | "amsquare21" | "amsquare22" | "amsquare23" | "amsquare24" | "amsquare25" | "amsquare26" | "amsquare27" | "amsquare28" | "amsquare29" |
"amsquare30" | "amsquare31" | "amsquare32";
type AMSawtoothWithPartials =
"amsawtooth1" | "amsawtooth2" | "amsawtooth3" | "amsawtooth4" | "amsawtooth5" | "amsawtooth6" | "amsawtooth7" | "amsawtooth8" | "amsawtooth9" |
"amsawtooth10" | "amsawtooth11" | "amsawtooth12" | "amsawtooth13" | "amsawtooth14" | "amsawtooth15" | "amsawtooth16" | "amsawtooth17" | "amsawtooth18" | "amsawtooth19" |
"amsawtooth20" | "amsawtooth21" | "amsawtooth22" | "amsawtooth23" | "amsawtooth24" | "amsawtooth25" | "amsawtooth26" | "amsawtooth27" | "amsawtooth28" | "amsawtooth29" |
"amsawtooth30" | "amsawtooth31" | "amsawtooth32";
type AMTriangleWithPartials =
"amtriangle1" | "amtriangle2" | "amtriangle3" | "amtriangle4" | "amtriangle5" | "amtriangle6" | "amtriangle7" | "amtriangle8" | "amtriangle9" |
"amtriangle10" | "amtriangle11" | "amtriangle12" | "amtriangle13" | "amtriangle14" | "amtriangle15" | "amtriangle16" | "amtriangle17" | "amtriangle18" | "amtriangle19" |
"amtriangle20" | "amtriangle21" | "amtriangle22" | "amtriangle23" | "amtriangle24" | "amtriangle25" | "amtriangle26" | "amtriangle27" | "amtriangle28" | "amtriangle29" |
"amtriangle30" | "amtriangle31" | "amtriangle32";
type AMTypeWithPartials = AMSineWithPartials | AMSquareWithPartials | AMSawtoothWithPartials | AMTriangleWithPartials;
/**
* Fat Oscillators with partials
*/
type FatSineWithPartials =
"fatsine1" | "fatsine2" | "fatsine3" | "fatsine4" | "fatsine5" | "fatsine6" | "fatsine7" | "fatsine8" | "fatsine9" |
"fatsine10" | "fatsine11" | "fatsine12" | "fatsine13" | "fatsine14" | "fatsine15" | "fatsine16" | "fatsine17" | "fatsine18" | "fatsine19" |
"fatsine20" | "fatsine21" | "fatsine22" | "fatsine23" | "fatsine24" | "fatsine25" | "fatsine26" | "fatsine27" | "fatsine28" | "fatsine29" |
"fatsine30" | "fatsine31" | "fatsine32";
type FatSquareWithPartials =
"fatsquare1" | "fatsquare2" | "fatsquare3" | "fatsquare4" | "fatsquare5" | "fatsquare6" | "fatsquare7" | "fatsquare8" | "fatsquare9" |
"fatsquare10" | "fatsquare11" | "fatsquare12" | "fatsquare13" | "fatsquare14" | "fatsquare15" | "fatsquare16" | "fatsquare17" | "fatsquare18" | "fatsquare19" |
"fatsquare20" | "fatsquare21" | "fatsquare22" | "fatsquare23" | "fatsquare24" | "fatsquare25" | "fatsquare26" | "fatsquare27" | "fatsquare28" | "fatsquare29" |
"fatsquare30" | "fatsquare31" | "fatsquare32";
type FatSawtoothWithPartials =
"fatsawtooth1" | "fatsawtooth2" | "fatsawtooth3" | "fatsawtooth4" | "fatsawtooth5" | "fatsawtooth6" | "fatsawtooth7" | "fatsawtooth8" | "fatsawtooth9" |
"fatsawtooth10" | "fatsawtooth11" | "fatsawtooth12" | "fatsawtooth13" | "fatsawtooth14" | "fatsawtooth15" | "fatsawtooth16" | "fatsawtooth17" | "fatsawtooth18" | "fatsawtooth19" |
"fatsawtooth20" | "fatsawtooth21" | "fatsawtooth22" | "fatsawtooth23" | "fatsawtooth24" | "fatsawtooth25" | "fatsawtooth26" | "fatsawtooth27" | "fatsawtooth28" | "fatsawtooth29" |
"fatsawtooth30" | "fatsawtooth31" | "fatsawtooth32";
type FatTriangleWithPartials =
"fattriangle1" | "fattriangle2" | "fattriangle3" | "fattriangle4" | "fattriangle5" | "fattriangle6" | "fattriangle7" | "fattriangle8" | "fattriangle9" |
"fattriangle10" | "fattriangle11" | "fattriangle12" | "fattriangle13" | "fattriangle14" | "fattriangle15" | "fattriangle16" | "fattriangle17" | "fattriangle18" | "fattriangle19" |
"fattriangle20" | "fattriangle21" | "fattriangle22" | "fattriangle23" | "fattriangle24" | "fattriangle25" | "fattriangle26" | "fattriangle27" | "fattriangle28" | "fattriangle29" |
"fattriangle30" | "fattriangle31" | "fattriangle32";
type FatTypeWithPartials = FatSineWithPartials | FatSquareWithPartials | FatSawtoothWithPartials | FatTriangleWithPartials;
/**
* Omni FM
*/
interface OmniFMCustomOscillatorOptions extends FMBaseOscillatorOptions {
type: "fmcustom";
partials: number[];
}
interface OmniFMTypeOscillatorOptions extends FMBaseOscillatorOptions {
type: "fmsine" | "fmsquare" | "fmsawtooth" | "fmtriangle";
partialsCount?: number;
}
interface OmniFMPartialsOscillatorOptions extends FMBaseOscillatorOptions {
type: FMTypeWithPartials;
}
/**
* Omni AM
*/
interface OmniAMCustomOscillatorOptions extends AMBaseOscillatorOptions {
type: "amcustom";
partials: number[];
}
interface OmniAMTypeOscillatorOptions extends AMBaseOscillatorOptions {
type: "amsine" | "amsquare" | "amsawtooth" | "amtriangle";
partialsCount?: number;
}
interface OmniAMPartialsOscillatorOptions extends AMBaseOscillatorOptions {
type: AMTypeWithPartials;
}
/**
* Omni Fat
*/
interface OmniFatCustomOscillatorOptions extends FatBaseOscillatorOptions {
type: "fatcustom";
partials: number[];
}
interface OmniFatTypeOscillatorOptions extends FatBaseOscillatorOptions {
type: "fatsine" | "fatsquare" | "fatsawtooth" | "fattriangle";
partialsCount?: number;
}
interface OmniFatPartialsOscillatorOptions extends FatBaseOscillatorOptions {
type: FatTypeWithPartials;
}
export type OmniOscillatorType =
"fatsine" | "fatsquare" | "fatsawtooth" | "fattriangle" | "fatcustom" | FatTypeWithPartials |
"fmsine" | "fmsquare" | "fmsawtooth" | "fmtriangle" | "fmcustom" | FMTypeWithPartials |
"amsine" | "amsquare" | "amsawtooth" | "amtriangle" | "amcustom" | AMTypeWithPartials |
TypeWithPartials | OscillatorType | "pulse" | "pwm";
export type OmniOscillatorConstructorOptions =
PulseOscillatorOptions | PWMOscillatorOptions |
OmniFatCustomOscillatorOptions | OmniFatTypeOscillatorOptions | OmniFatPartialsOscillatorOptions |
OmniFMCustomOscillatorOptions | OmniFMTypeOscillatorOptions | OmniFMPartialsOscillatorOptions |
OmniAMCustomOscillatorOptions | OmniAMTypeOscillatorOptions | OmniAMPartialsOscillatorOptions |
ToneOscillatorConstructorOptions;
// export type OmniOscillatorSourceOptions = OmniOscillatorConstructorOptions & SourceOptions;
export type OmniOscillatorOptions =
PulseOscillatorOptions & PWMOscillatorOptions &
OmniFatCustomOscillatorOptions & OmniFatTypeOscillatorOptions & OmniFatPartialsOscillatorOptions &
OmniFMCustomOscillatorOptions & OmniFMTypeOscillatorOptions & OmniFMPartialsOscillatorOptions &
OmniAMCustomOscillatorOptions & OmniAMTypeOscillatorOptions & OmniAMPartialsOscillatorOptions &
ToneOscillatorConstructorOptions;

View file

@ -4,29 +4,22 @@ import { readOnly } from "../../core/util/Interface";
import { Multiply } from "../../signal/Multiply";
import { Signal } from "../../signal/Signal";
import { Source } from "../Source";
import { Oscillator, OscillatorInterface, ToneOscillatorOptions, ToneOscillatorType } from "./Oscillator";
import { Oscillator } from "./Oscillator";
import { PWMOscillatorOptions, ToneOscillatorInterface } from "./OscillatorInterface";
import { PulseOscillator } from "./PulseOscillator";
export interface PWMOscillatorOptions extends ToneOscillatorOptions {
modulationFrequency: Frequency;
}
type PWMOscillatorType = "pwm";
/**
* @class PWMOscillator modulates the width of a Tone.PulseOscillator
* at the modulationFrequency. This has the effect of continuously
* changing the timbre of the oscillator by altering the harmonics
* generated.
* PWMOscillator modulates the width of a Tone.PulseOscillator
* at the modulationFrequency. This has the effect of continuously
* changing the timbre of the oscillator by altering the harmonics
* generated.
*
* @extends {Tone.Source}
* @constructor
* @param {Frequency} frequency The starting frequency of the oscillator.
* @param {Frequency} modulationFrequency The modulation frequency of the width of the pulse.
* @example
* @param {Frequency} frequency The starting frequency of the oscillator.
* @param {Frequency} modulationFrequency The modulation frequency of the width of the pulse.
* @example
* var pwm = new PWMOscillator("Ab3", 0.3).toMaster().start();
*/
export class PWMOscillator extends Source<PWMOscillatorOptions> implements OscillatorInterface {
export class PWMOscillator extends Source<PWMOscillatorOptions> implements ToneOscillatorInterface {
name = "PWMOscillator";
@ -89,8 +82,12 @@ export class PWMOscillator extends Source<PWMOscillatorOptions> implements Oscil
}
static getDefaults(): PWMOscillatorOptions {
return Object.assign(Oscillator.getDefaults(), {
return Object.assign(Source.getDefaults(), {
detune: 0,
frequency: 440,
modulationFrequency: 0.4,
phase: 0,
type: "pwm" as "pwm",
});
}
/**
@ -123,14 +120,14 @@ export class PWMOscillator extends Source<PWMOscillatorOptions> implements Oscil
/**
* The type of the oscillator. Always returns "pwm".
*/
get type(): PWMOscillatorType {
get type(): "pwm" {
return "pwm";
}
/**
* The baseType of the oscillator. Always returns "pwm".
*/
get baseType(): PWMOscillatorType {
get baseType(): "pwm" {
return "pwm";
}

View file

@ -4,13 +4,8 @@ import { readOnly } from "../../core/util/Interface";
import { Signal } from "../../signal/Signal";
import { WaveShaper } from "../../signal/WaveShaper";
import { Source } from "../Source";
import { Oscillator, OscillatorInterface, ToneOscillatorOptions, ToneOscillatorType } from "./Oscillator";
type PulseOscillatorType = "pulse";
export interface PulseOscillatorOptions extends ToneOscillatorOptions {
width: NormalRange;
}
import { Oscillator } from "./Oscillator";
import { PulseOscillatorOptions, ToneOscillatorInterface } from "./OscillatorInterface";
/**
* PulseOscillator is an oscillator with control over pulse width,
@ -46,7 +41,7 @@ export interface PulseOscillatorOptions extends ToneOscillatorOptions {
* @example
* var pulse = new PulseOscillator("E5", 0.4).toMaster().start();
*/
export class PulseOscillator extends Source<PulseOscillatorOptions> implements OscillatorInterface {
export class PulseOscillator extends Source<PulseOscillatorOptions> implements ToneOscillatorInterface {
name = "PulseOscillator";
@ -93,7 +88,7 @@ export class PulseOscillator extends Source<PulseOscillatorOptions> implements O
});
constructor(options?: Partial<PulseOscillatorOptions>);
constructor(frequency?: Frequency, type?: ToneOscillatorType, modulationType?: ToneOscillatorType);
constructor(frequency?: Frequency, width?: AudioRange);
constructor() {
super(optionsFromArguments(PulseOscillator.getDefaults(), arguments, ["frequency", "width"]));
@ -111,7 +106,11 @@ export class PulseOscillator extends Source<PulseOscillatorOptions> implements O
}
static getDefaults(): PulseOscillatorOptions {
return Object.assign(Oscillator.getDefaults(), {
return Object.assign(Source.getDefaults(), {
detune: 0,
frequency: 440,
phase: 0,
type: "pulse" as "pulse",
width: 0.2,
});
}
@ -160,14 +159,14 @@ export class PulseOscillator extends Source<PulseOscillatorOptions> implements O
/**
* The type of the oscillator. Always returns "pulse".
*/
get type(): PulseOscillatorType {
get type(): "pulse" {
return "pulse";
}
/**
* The baseType of the oscillator. Always returns "pulse".
*/
get baseType(): PulseOscillatorType {
get baseType(): "pulse" {
return "pulse";
}