Tone.js/Tone/source/Noise.ts

288 lines
7 KiB
TypeScript
Raw Normal View History

2019-07-11 13:57:06 +00:00
import { ToneAudioBuffer } from "../core/context/ToneAudioBuffer";
import { Positive, Time } from "../core/type/Units";
2019-07-11 13:57:06 +00:00
import { optionsFromArguments } from "../core/util/Defaults";
import { assert } from "../core/util/Debug";
2019-06-19 14:18:06 +00:00
import { Source, SourceOptions } from "../source/Source";
2019-09-04 22:39:28 +00:00
import { ToneBufferSource } from "./buffer/ToneBufferSource";
2019-06-19 14:18:06 +00:00
2019-08-30 16:04:03 +00:00
export type NoiseType = "white" | "brown" | "pink";
2019-06-19 14:18:06 +00:00
2019-08-30 16:04:03 +00:00
export interface NoiseOptions extends SourceOptions {
2019-06-19 14:18:06 +00:00
type: NoiseType;
playbackRate: Positive;
2019-08-20 01:50:11 +00:00
fadeIn: Time;
fadeOut: Time;
2019-06-19 14:18:06 +00:00
}
/**
* Noise is a noise generator. It uses looped noise buffers to save on performance.
* Noise supports the noise types: "pink", "white", and "brown". Read more about
* colors of noise on [Wikipedia](https://en.wikipedia.org/wiki/Colors_of_noise).
*
* @example
2019-10-25 20:54:33 +00:00
* // initialize the noise and start
* const noise = new Tone.Noise("pink").start();
2019-10-25 20:54:33 +00:00
* // make an autofilter to shape the noise
* const autoFilter = new Tone.AutoFilter({
2019-11-03 23:07:03 +00:00
* frequency: "8n",
2019-10-29 01:28:53 +00:00
* baseFrequency: 200,
* octaves: 8
2019-11-03 23:07:03 +00:00
* }).toDestination().start();
2019-10-25 20:54:33 +00:00
* // connect the noise
2019-06-19 14:18:06 +00:00
* noise.connect(autoFilter);
2019-10-25 20:54:33 +00:00
* // start the autofilter LFO
* autoFilter.start();
2019-09-16 14:15:23 +00:00
* @category Source
2019-06-19 14:18:06 +00:00
*/
export class Noise extends Source<NoiseOptions> {
2019-06-19 14:18:06 +00:00
2019-09-04 23:18:44 +00:00
readonly name: string = "Noise";
2019-06-19 14:18:06 +00:00
/**
* Private reference to the source
*/
private _source: ToneBufferSource | null = null;
/**
* private reference to the type
*/
private _type!: NoiseType;
/**
2019-09-14 20:39:18 +00:00
* The playback rate of the noise. Affects
* the "frequency" of the noise.
2019-06-19 14:18:06 +00:00
*/
private _playbackRate: Positive;
2019-08-20 01:50:11 +00:00
/**
2019-09-14 20:39:18 +00:00
* The fadeIn time of the amplitude envelope.
2019-08-20 01:50:11 +00:00
*/
protected _fadeIn: Time;
/**
2019-09-14 20:39:18 +00:00
* The fadeOut time of the amplitude envelope.
2019-08-20 01:50:11 +00:00
*/
protected _fadeOut: Time;
2019-08-27 15:47:52 +00:00
/**
* @param type the noise type (white|pink|brown)
*/
2019-08-20 01:50:11 +00:00
constructor(type?: NoiseType);
constructor(options?: Partial<NoiseOptions>);
2019-06-19 14:18:06 +00:00
constructor() {
super(optionsFromArguments(Noise.getDefaults(), arguments, ["type"]));
const options = optionsFromArguments(Noise.getDefaults(), arguments, ["type"]);
this._playbackRate = options.playbackRate;
this.type = options.type;
2019-08-20 01:50:11 +00:00
this._fadeIn = options.fadeIn;
this._fadeOut = options.fadeOut;
2019-06-19 14:18:06 +00:00
}
static getDefaults(): NoiseOptions {
return Object.assign(Source.getDefaults(), {
2019-08-20 01:50:11 +00:00
fadeIn: 0,
fadeOut: 0,
2019-06-19 14:18:06 +00:00
playbackRate: 1,
type: "white" as NoiseType,
});
}
/**
* The type of the noise. Can be "white", "brown", or "pink".
* @example
* const noise = new Tone.Noise().toDestination().start();
2019-10-25 20:54:33 +00:00
* noise.type = "brown";
2019-06-19 14:18:06 +00:00
*/
get type(): NoiseType {
return this._type;
}
set type(type: NoiseType) {
assert(type in _noiseBuffers, "Noise: invalid type: " + type);
2019-06-19 14:18:06 +00:00
if (this._type !== type) {
2019-06-19 22:17:24 +00:00
this._type = type;
// if it's playing, stop and restart it
if (this.state === "started") {
const now = this.now();
this._stop(now);
this._start(now);
2019-06-19 14:18:06 +00:00
}
}
}
/**
2019-09-14 20:39:18 +00:00
* The playback rate of the noise. Affects
* the "frequency" of the noise.
2019-06-19 14:18:06 +00:00
*/
get playbackRate(): Positive {
return this._playbackRate;
}
set playbackRate(rate: Positive) {
this._playbackRate = rate;
if (this._source) {
this._source.playbackRate.value = rate;
}
}
/**
2019-09-14 20:39:18 +00:00
* internal start method
2019-06-19 14:18:06 +00:00
*/
2019-07-22 20:16:29 +00:00
protected _start(time?: Time): void {
2019-06-19 14:18:06 +00:00
const buffer = _noiseBuffers[this._type];
2019-08-11 15:42:17 +00:00
this._source = new ToneBufferSource({
url: buffer,
2019-08-11 15:42:17 +00:00
context: this.context,
2019-08-20 01:50:11 +00:00
fadeIn: this._fadeIn,
fadeOut: this._fadeOut,
2019-08-11 15:42:17 +00:00
loop: true,
onended: () => this.onstop(this),
playbackRate: this._playbackRate,
}).connect(this.output);
2019-06-19 14:18:06 +00:00
this._source.start(this.toSeconds(time), Math.random() * (buffer.duration - 0.001));
}
/**
* internal stop method
2019-06-19 14:18:06 +00:00
*/
protected _stop(time?: Time): void {
if (this._source) {
this._source.stop(this.toSeconds(time));
this._source = null;
}
}
2019-08-20 01:50:11 +00:00
/**
2019-09-14 20:39:18 +00:00
* The fadeIn time of the amplitude envelope.
2019-08-20 01:50:11 +00:00
*/
get fadeIn(): Time {
return this._fadeIn;
}
set fadeIn(time) {
this._fadeIn = time;
if (this._source) {
this._source.fadeIn = this._fadeIn;
}
}
/**
2019-09-14 20:39:18 +00:00
* The fadeOut time of the amplitude envelope.
2019-08-20 01:50:11 +00:00
*/
get fadeOut(): Time {
return this._fadeOut;
}
set fadeOut(time) {
this._fadeOut = time;
if (this._source) {
this._source.fadeOut = this._fadeOut;
}
}
protected _restart(time?: Time): void {
// TODO could be optimized by cancelling the buffer source 'stop'
this._stop(time);
this._start(time);
2019-06-19 14:18:06 +00:00
}
/**
2019-09-14 20:39:18 +00:00
* Clean up.
2019-06-19 14:18:06 +00:00
*/
dispose(): this {
super.dispose();
if (this._source) {
this._source.disconnect();
}
return this;
}
}
2019-09-16 14:15:23 +00:00
//--------------------
// THE NOISE BUFFERS
//--------------------
// Noise buffer stats
const BUFFER_LENGTH = 44100 * 5;
const NUM_CHANNELS = 2;
/**
* The cached noise buffers
*/
interface NoiseCache {
[key: string]: ToneAudioBuffer | null;
}
/**
* Cache the noise buffers
*/
const _noiseCache: NoiseCache = {
brown: null,
pink: null,
white: null,
};
/**
* The noise arrays. Generated on initialization.
* borrowed heavily from https://github.com/zacharydenton/noise.js
* (c) 2013 Zach Denton (MIT)
*/
const _noiseBuffers = {
get brown(): ToneAudioBuffer {
if (!_noiseCache.brown) {
const buffer: Float32Array[] = [];
for (let channelNum = 0; channelNum < NUM_CHANNELS; channelNum++) {
const channel = new Float32Array(BUFFER_LENGTH);
buffer[channelNum] = channel;
let lastOut = 0.0;
for (let i = 0; i < BUFFER_LENGTH; i++) {
const white = Math.random() * 2 - 1;
channel[i] = (lastOut + (0.02 * white)) / 1.02;
lastOut = channel[i];
channel[i] *= 3.5; // (roughly) compensate for gain
}
}
_noiseCache.brown = new ToneAudioBuffer().fromArray(buffer);
}
return _noiseCache.brown;
},
get pink(): ToneAudioBuffer {
if (!_noiseCache.pink) {
const buffer: Float32Array[] = [];
for (let channelNum = 0; channelNum < NUM_CHANNELS; channelNum++) {
const channel = new Float32Array(BUFFER_LENGTH);
buffer[channelNum] = channel;
let b0, b1, b2, b3, b4, b5, b6;
b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0.0;
for (let i = 0; i < BUFFER_LENGTH; i++) {
const white = Math.random() * 2 - 1;
b0 = 0.99886 * b0 + white * 0.0555179;
b1 = 0.99332 * b1 + white * 0.0750759;
b2 = 0.96900 * b2 + white * 0.1538520;
b3 = 0.86650 * b3 + white * 0.3104856;
b4 = 0.55000 * b4 + white * 0.5329522;
b5 = -0.7616 * b5 - white * 0.0168980;
channel[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + white * 0.5362;
channel[i] *= 0.11; // (roughly) compensate for gain
b6 = white * 0.115926;
}
}
_noiseCache.pink = new ToneAudioBuffer().fromArray(buffer);
}
return _noiseCache.pink;
},
get white(): ToneAudioBuffer {
if (!_noiseCache.white) {
const buffer: Float32Array[] = [];
for (let channelNum = 0; channelNum < NUM_CHANNELS; channelNum++) {
const channel = new Float32Array(BUFFER_LENGTH);
buffer[channelNum] = channel;
for (let i = 0; i < BUFFER_LENGTH; i++) {
channel[i] = Math.random() * 2 - 1;
}
}
_noiseCache.white = new ToneAudioBuffer().fromArray(buffer);
}
return _noiseCache.white;
},
};