tone 14.8.31 → 14.8.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,7 @@ export class FrequencyClass<Type extends number = Hertz> extends TimeClass<Type,
50
50
  },
51
51
  },
52
52
  note: {
53
- regexp: /^([a-g]{1}(?:b|#|x|bb)?)(-?[0-9]+)/i,
53
+ regexp: /^([a-g]{1}(?:b|#|##|x|bb|###|#x|x#|bbb)?)(-?[0-9]+)/i,
54
54
  method(pitch, octave): number {
55
55
  const index = noteToScaleIndex[pitch.toLowerCase()];
56
56
  const noteNumber = index + (parseInt(octave, 10) + 1) * 12;
@@ -219,13 +219,13 @@ export class FrequencyClass<Type extends number = Hertz> extends TimeClass<Type,
219
219
  * @hidden
220
220
  */
221
221
  const noteToScaleIndex = {
222
- cbb: -2, cb: -1, c: 0, "c#": 1, cx: 2,
223
- dbb: 0, db: 1, d: 2, "d#": 3, dx: 4,
224
- ebb: 2, eb: 3, e: 4, "e#": 5, ex: 6,
225
- fbb: 3, fb: 4, f: 5, "f#": 6, fx: 7,
226
- gbb: 5, gb: 6, g: 7, "g#": 8, gx: 9,
227
- abb: 7, ab: 8, a: 9, "a#": 10, ax: 11,
228
- bbb: 9, bb: 10, b: 11, "b#": 12, bx: 13,
222
+ cbbb: -3, cbb: -2, cb: -1, c: 0, "c#": 1, cx: 2, "c##": 2, "c###": 3, "cx#": 3, "c#x": 3,
223
+ dbbb: -1, dbb: 0, db: 1, d: 2, "d#": 3, dx: 4, "d##": 4, "d###": 5, "dx#": 5, "d#x": 5,
224
+ ebbb: 1, ebb: 2, eb: 3, e: 4, "e#": 5, ex: 6, "e##": 6, "e###": 7, "ex#": 7, "e#x": 7,
225
+ fbbb: 2, fbb: 3, fb: 4, f: 5, "f#": 6, fx: 7, "f##": 7, "f###": 8, "fx#": 8, "f#x": 8,
226
+ gbbb: 4, gbb: 5, gb: 6, g: 7, "g#": 8, gx: 9, "g##": 9, "g###": 10, "gx#": 10, "g#x": 10,
227
+ abbb: 6, abb: 7, ab: 8, a: 9, "a#": 10, ax: 11, "a##": 11, "a###": 12, "ax#": 12, "a#x": 12,
228
+ bbbb: 8, bbb: 9, bb: 10, b: 11, "b#": 12, bx: 13, "b##": 13, "b###": 14, "bx#": 14, "b#x": 14,
229
229
  };
230
230
 
231
231
  /**
@@ -7,25 +7,29 @@ import { Oscillator } from "Tone/source";
7
7
  import { Offline } from "test/helper/Offline";
8
8
 
9
9
  describe("Chorus", () => {
10
-
11
10
  BasicTests(Chorus);
12
11
  EffectTests(Chorus);
13
12
 
14
13
  it("matches a file", () => {
15
- return CompareToFile(() => {
16
- const chorus = new Chorus().toDestination().start();
17
- const osc = new Oscillator(220, "sawtooth").connect(chorus).start();
18
- }, "chorus.wav", 0.1);
14
+ return CompareToFile(
15
+ () => {
16
+ const chorus = new Chorus().toDestination().start();
17
+ const osc = new Oscillator(220, "sawtooth")
18
+ .connect(chorus)
19
+ .start();
20
+ },
21
+ "chorus.wav",
22
+ 0.25
23
+ );
19
24
  });
20
25
 
21
26
  context("API", () => {
22
-
23
27
  it("can pass in options in the constructor", () => {
24
28
  const chorus = new Chorus({
25
29
  frequency: 2,
26
30
  delayTime: 1,
27
31
  depth: 0.4,
28
- spread: 90
32
+ spread: 90,
29
33
  });
30
34
  expect(chorus.frequency.value).to.be.closeTo(2, 0.01);
31
35
  expect(chorus.delayTime).to.be.closeTo(1, 0.01);
@@ -84,4 +88,3 @@ describe("Chorus", () => {
84
88
  });
85
89
  });
86
90
  });
87
-
@@ -4,7 +4,10 @@ import { omitFromObject, optionsFromArguments } from "../core/util/Defaults";
4
4
  import { RecursivePartial } from "../core/util/Interface";
5
5
  import { Noise, NoiseOptions } from "../source/Noise";
6
6
  import { Instrument, InstrumentOptions } from "./Instrument";
7
- import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
7
+ import {
8
+ ToneAudioNode,
9
+ ToneAudioNodeOptions,
10
+ } from "../core/context/ToneAudioNode";
8
11
  import { Envelope, EnvelopeOptions } from "../component/envelope/Envelope";
9
12
  import { Source } from "../source/Source";
10
13
 
@@ -14,7 +17,7 @@ export interface NoiseSynthOptions extends InstrumentOptions {
14
17
  }
15
18
 
16
19
  /**
17
- * Tone.NoiseSynth is composed of [[Noise]] through an [[AmplitudeEnvelope]].
20
+ * Tone.NoiseSynth is composed of [[Noise]] through an [[AmplitudeEnvelope]].
18
21
  * ```
19
22
  * +-------+ +-------------------+
20
23
  * | Noise +>--> AmplitudeEnvelope +>--> Output
@@ -26,7 +29,6 @@ export interface NoiseSynthOptions extends InstrumentOptions {
26
29
  * @category Instrument
27
30
  */
28
31
  export class NoiseSynth extends Instrument<NoiseSynthOptions> {
29
-
30
32
  readonly name = "NoiseSynth";
31
33
 
32
34
  /**
@@ -39,17 +41,30 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
39
41
  */
40
42
  readonly envelope: AmplitudeEnvelope;
41
43
 
42
- constructor(options?: RecursivePartial<NoiseSynthOptions>)
44
+ constructor(options?: RecursivePartial<NoiseSynthOptions>);
43
45
  constructor() {
44
46
  super(optionsFromArguments(NoiseSynth.getDefaults(), arguments));
45
- const options = optionsFromArguments(NoiseSynth.getDefaults(), arguments);
46
- this.noise = new Noise(Object.assign({
47
- context: this.context,
48
- }, options.noise));
47
+ const options = optionsFromArguments(
48
+ NoiseSynth.getDefaults(),
49
+ arguments
50
+ );
51
+ this.noise = new Noise(
52
+ Object.assign(
53
+ {
54
+ context: this.context,
55
+ },
56
+ options.noise
57
+ )
58
+ );
49
59
 
50
- this.envelope = new AmplitudeEnvelope(Object.assign({
51
- context: this.context,
52
- }, options.envelope));
60
+ this.envelope = new AmplitudeEnvelope(
61
+ Object.assign(
62
+ {
63
+ context: this.context,
64
+ },
65
+ options.envelope
66
+ )
67
+ );
53
68
 
54
69
  // connect the noise to the output
55
70
  this.noise.chain(this.envelope, this.output);
@@ -58,17 +73,23 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
58
73
  static getDefaults(): NoiseSynthOptions {
59
74
  return Object.assign(Instrument.getDefaults(), {
60
75
  envelope: Object.assign(
61
- omitFromObject(Envelope.getDefaults(), Object.keys(ToneAudioNode.getDefaults())),
76
+ omitFromObject(
77
+ Envelope.getDefaults(),
78
+ Object.keys(ToneAudioNode.getDefaults())
79
+ ),
62
80
  {
63
81
  decay: 0.1,
64
82
  sustain: 0.0,
65
- },
83
+ }
66
84
  ),
67
85
  noise: Object.assign(
68
- omitFromObject(Noise.getDefaults(), Object.keys(Source.getDefaults())),
86
+ omitFromObject(
87
+ Noise.getDefaults(),
88
+ Object.keys(Source.getDefaults())
89
+ ),
69
90
  {
70
91
  type: "white",
71
- },
92
+ }
72
93
  ),
73
94
  });
74
95
  }
@@ -87,7 +108,11 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
87
108
  // start the noise
88
109
  this.noise.start(time);
89
110
  if (this.envelope.sustain === 0) {
90
- this.noise.stop(time + this.toSeconds(this.envelope.attack) + this.toSeconds(this.envelope.decay));
111
+ this.noise.stop(
112
+ time +
113
+ this.toSeconds(this.envelope.attack) +
114
+ this.toSeconds(this.envelope.decay)
115
+ );
91
116
  }
92
117
  return this;
93
118
  }
@@ -110,7 +135,21 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
110
135
  return this;
111
136
  }
112
137
 
113
- triggerAttackRelease(duration: Time, time?: Time, velocity: NormalRange = 1): this {
138
+ /**
139
+ * Trigger the attack and then the release after the duration.
140
+ * @param duration The amount of time to hold the note for
141
+ * @param time The time the note should start
142
+ * @param velocity The volume of the note (0-1)
143
+ * @example
144
+ * const noiseSynth = new Tone.NoiseSynth().toDestination();
145
+ * // hold the note for 0.5 seconds
146
+ * noiseSynth.triggerAttackRelease(0.5);
147
+ */
148
+ triggerAttackRelease(
149
+ duration: Time,
150
+ time?: Time,
151
+ velocity: NormalRange = 1
152
+ ): this {
114
153
  time = this.toSeconds(time);
115
154
  duration = this.toSeconds(duration);
116
155
  this.triggerAttack(time, velocity);
@@ -2,11 +2,19 @@ import { Volume } from "../component/channel/Volume";
2
2
  import "../core/context/Destination";
3
3
  import "../core/clock/Transport";
4
4
  import { Param } from "../core/context/Param";
5
- import { OutputNode, ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
5
+ import {
6
+ OutputNode,
7
+ ToneAudioNode,
8
+ ToneAudioNodeOptions,
9
+ } from "../core/context/ToneAudioNode";
6
10
  import { Decibels, Seconds, Time } from "../core/type/Units";
7
11
  import { defaultArg } from "../core/util/Defaults";
8
12
  import { noOp, readOnly } from "../core/util/Interface";
9
- import { BasicPlaybackState, StateTimeline, StateTimelineEvent } from "../core/util/StateTimeline";
13
+ import {
14
+ BasicPlaybackState,
15
+ StateTimeline,
16
+ StateTimelineEvent,
17
+ } from "../core/util/StateTimeline";
10
18
  import { isDefined, isUndef } from "../core/util/TypeCheck";
11
19
  import { assert, assertContextRunning } from "../core/util/Debug";
12
20
  import { GT } from "../core/util/Math";
@@ -20,9 +28,9 @@ export interface SourceOptions extends ToneAudioNodeOptions {
20
28
  }
21
29
 
22
30
  /**
23
- * Base class for sources.
31
+ * Base class for sources.
24
32
  * start/stop of this.context.transport.
25
- *
33
+ *
26
34
  * ```
27
35
  * // Multiple state change events can be chained together,
28
36
  * // but must be set in the correct order and with ascending times
@@ -36,8 +44,9 @@ export interface SourceOptions extends ToneAudioNodeOptions {
36
44
  * state.start("+0.3").stop("+0.2");
37
45
  * ```
38
46
  */
39
- export abstract class Source<Options extends SourceOptions> extends ToneAudioNode<Options> {
40
-
47
+ export abstract class Source<
48
+ Options extends SourceOptions
49
+ > extends ToneAudioNode<Options> {
41
50
  /**
42
51
  * The output volume node
43
52
  */
@@ -129,7 +138,9 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
129
138
  get state(): BasicPlaybackState {
130
139
  if (this._synced) {
131
140
  if (this.context.transport.state === "started") {
132
- return this._state.getValueAtTime(this.context.transport.seconds) as BasicPlaybackState;
141
+ return this._state.getValueAtTime(
142
+ this.context.transport.seconds
143
+ ) as BasicPlaybackState;
133
144
  } else {
134
145
  return "stopped";
135
146
  }
@@ -155,7 +166,11 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
155
166
  // overwrite these functions
156
167
  protected abstract _start(time: Time, offset?: Time, duration?: Time): void;
157
168
  protected abstract _stop(time: Time): void;
158
- protected abstract _restart(time: Seconds, offset?: Time, duration?: Time): void;
169
+ protected abstract _restart(
170
+ time: Seconds,
171
+ offset?: Time,
172
+ duration?: Time
173
+ ): void;
159
174
 
160
175
  /**
161
176
  * Ensure that the scheduled time is not before the current time.
@@ -178,12 +193,24 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
178
193
  * source.start("+0.5"); // starts the source 0.5 seconds from now
179
194
  */
180
195
  start(time?: Time, offset?: Time, duration?: Time): this {
181
- let computedTime = isUndef(time) && this._synced ? this.context.transport.seconds : this.toSeconds(time);
196
+ let computedTime =
197
+ isUndef(time) && this._synced
198
+ ? this.context.transport.seconds
199
+ : this.toSeconds(time);
182
200
  computedTime = this._clampToCurrentTime(computedTime);
183
201
  // if it's started, stop it and restart it
184
- if (!this._synced && this._state.getValueAtTime(computedTime) === "started") {
202
+ if (
203
+ !this._synced &&
204
+ this._state.getValueAtTime(computedTime) === "started"
205
+ ) {
185
206
  // time should be strictly greater than the previous start time
186
- assert(GT(computedTime, (this._state.get(computedTime) as StateTimelineEvent).time), "Start time must be strictly greater than previous start time");
207
+ assert(
208
+ GT(
209
+ computedTime,
210
+ (this._state.get(computedTime) as StateTimelineEvent).time
211
+ ),
212
+ "Start time must be strictly greater than previous start time"
213
+ );
187
214
  this._state.cancel(computedTime);
188
215
  this._state.setStateAtTime("started", computedTime);
189
216
  this.log("restart", computedTime);
@@ -196,18 +223,26 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
196
223
  const event = this._state.get(computedTime);
197
224
  if (event) {
198
225
  event.offset = this.toSeconds(defaultArg(offset, 0));
199
- event.duration = duration ? this.toSeconds(duration) : undefined;
226
+ event.duration = duration
227
+ ? this.toSeconds(duration)
228
+ : undefined;
200
229
  }
201
- const sched = this.context.transport.schedule(t => {
230
+ const sched = this.context.transport.schedule((t) => {
202
231
  this._start(t, offset, duration);
203
232
  }, computedTime);
204
233
  this._scheduled.push(sched);
205
234
 
206
235
  // if the transport is already started
207
236
  // and the time is greater than where the transport is
208
- if (this.context.transport.state === "started" &&
209
- this.context.transport.getSecondsAtTime(this.immediate()) > computedTime) {
210
- this._syncedStart(this.now(), this.context.transport.seconds);
237
+ if (
238
+ this.context.transport.state === "started" &&
239
+ this.context.transport.getSecondsAtTime(this.immediate()) >
240
+ computedTime
241
+ ) {
242
+ this._syncedStart(
243
+ this.now(),
244
+ this.context.transport.seconds
245
+ );
211
246
  }
212
247
  } else {
213
248
  assertContextRunning(this.context);
@@ -227,14 +262,23 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
227
262
  * source.stop("+0.5"); // stops the source 0.5 seconds from now
228
263
  */
229
264
  stop(time?: Time): this {
230
- let computedTime = isUndef(time) && this._synced ? this.context.transport.seconds : this.toSeconds(time);
265
+ let computedTime =
266
+ isUndef(time) && this._synced
267
+ ? this.context.transport.seconds
268
+ : this.toSeconds(time);
231
269
  computedTime = this._clampToCurrentTime(computedTime);
232
- if (this._state.getValueAtTime(computedTime) === "started" || isDefined(this._state.getNextState("started", computedTime))) {
270
+ if (
271
+ this._state.getValueAtTime(computedTime) === "started" ||
272
+ isDefined(this._state.getNextState("started", computedTime))
273
+ ) {
233
274
  this.log("stop", computedTime);
234
275
  if (!this._synced) {
235
276
  this._stop(computedTime);
236
277
  } else {
237
- const sched = this.context.transport.schedule(this._stop.bind(this), computedTime);
278
+ const sched = this.context.transport.schedule(
279
+ this._stop.bind(this),
280
+ computedTime
281
+ );
238
282
  this._scheduled.push(sched);
239
283
  }
240
284
  this._state.cancel(computedTime);
@@ -274,23 +318,36 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
274
318
  if (!this._synced) {
275
319
  this._synced = true;
276
320
  this._syncedStart = (time, offset) => {
277
- if (offset > 0) {
321
+ if (GT(offset, 0)) {
278
322
  // get the playback state at that time
279
323
  const stateEvent = this._state.get(offset);
280
324
  // listen for start events which may occur in the middle of the sync'ed time
281
- if (stateEvent && stateEvent.state === "started" && stateEvent.time !== offset) {
325
+ if (
326
+ stateEvent &&
327
+ stateEvent.state === "started" &&
328
+ stateEvent.time !== offset
329
+ ) {
282
330
  // get the offset
283
- const startOffset = offset - this.toSeconds(stateEvent.time);
331
+ const startOffset =
332
+ offset - this.toSeconds(stateEvent.time);
284
333
  let duration: number | undefined;
285
334
  if (stateEvent.duration) {
286
- duration = this.toSeconds(stateEvent.duration) - startOffset;
335
+ duration =
336
+ this.toSeconds(stateEvent.duration) -
337
+ startOffset;
287
338
  }
288
- this._start(time, this.toSeconds(stateEvent.offset) + startOffset, duration);
339
+ this._start(
340
+ time,
341
+ this.toSeconds(stateEvent.offset) + startOffset,
342
+ duration
343
+ );
289
344
  }
290
345
  }
291
346
  };
292
- this._syncedStop = time => {
293
- const seconds = this.context.transport.getSecondsAtTime(Math.max(time - this.sampleTime, 0));
347
+ this._syncedStop = (time) => {
348
+ const seconds = this.context.transport.getSecondsAtTime(
349
+ Math.max(time - this.sampleTime, 0)
350
+ );
294
351
  if (this._state.getValueAtTime(seconds) === "started") {
295
352
  this._stop(time);
296
353
  }
@@ -317,7 +374,7 @@ export abstract class Source<Options extends SourceOptions> extends ToneAudioNod
317
374
  }
318
375
  this._synced = false;
319
376
  // clear all of the scheduled ids
320
- this._scheduled.forEach(id => this.context.transport.clear(id));
377
+ this._scheduled.forEach((id) => this.context.transport.clear(id));
321
378
  this._scheduled = [];
322
379
  this._state.cancel(0);
323
380
  // stop it also