reorganizing source file locations

This commit is contained in:
tambien 2019-06-19 09:54:47 -04:00
parent 863072f8c8
commit d16786ba7d
13 changed files with 111 additions and 357 deletions

View file

@ -1,243 +0,0 @@
import Tone from "../core/Tone";
import "../core/Buffer";
import "../source/Source";
import "../core/Gain";
import "../core/AudioNode";
/**
* @class Wrapper around the native fire-and-forget OscillatorNode. Adds the
* ability to reschedule the stop method. ***[Tone.Oscillator](Oscillator) is better
* for most use-cases***
* @extends {Tone.AudioNode}
* @param {AudioBuffer|Tone.Buffer} buffer The buffer to play
* @param {Function} onload The callback to invoke when the
* buffer is done playing.
*/
Tone.OscillatorNode = function(){
var options = Tone.defaults(arguments, ["frequency", "type"], Tone.OscillatorNode);
Tone.AudioNode.call(this, options);
/**
* The callback to invoke after the
* buffer source is done playing.
* @type {Function}
*/
this.onended = options.onended;
/**
* The oscillator start time
* @type {Number}
* @private
*/
this._startTime = -1;
/**
* The oscillator stop time
* @type {Number}
* @private
*/
this._stopTime = -1;
/**
* The gain node which envelopes the OscillatorNode
* @type {Tone.Gain}
* @private
*/
this._gainNode = this.output = new Tone.Gain(0);
/**
* The oscillator
* @type {OscillatorNode}
* @private
*/
this._oscillator = this.context.createOscillator();
Tone.connect(this._oscillator, this._gainNode);
this.type = options.type;
/**
* The frequency of the oscillator
* @type {Frequency}
* @signal
*/
this.frequency = new Tone.Param({
param : this._oscillator.frequency,
units : Tone.Type.Frequency,
value : options.frequency
});
/**
* The detune of the oscillator
* @type {Frequency}
* @signal
*/
this.detune = new Tone.Param({
param : this._oscillator.detune,
units : Tone.Type.Cents,
value : options.detune
});
/**
* The value that the buffer ramps to
* @type {Gain}
* @private
*/
this._gain = 1;
};
Tone.extend(Tone.OscillatorNode, Tone.AudioNode);
/**
* The defaults
* @const
* @type {Object}
*/
Tone.OscillatorNode.defaults = {
"frequency" : 440,
"detune" : 0,
"type" : "sine",
"onended" : Tone.noOp
};
/**
* Returns the playback state of the oscillator, either "started" or "stopped".
* @type {Tone.State}
* @readOnly
* @memberOf Tone.OscillatorNode#
* @name state
*/
Object.defineProperty(Tone.OscillatorNode.prototype, "state", {
get : function(){
return this.getStateAtTime(this.now());
}
});
/**
* Get the playback state at the given time
* @param {Time} time The time to test the state at
* @return {Tone.State} The playback state.
*/
Tone.OscillatorNode.prototype.getStateAtTime = function(time){
time = this.toSeconds(time);
if (this._startTime !== -1 && time >= this._startTime && (this._stopTime === -1 || time <= this._stopTime)){
return Tone.State.Started;
} else {
return Tone.State.Stopped;
}
};
/**
* Start the oscillator node at the given time
* @param {Time=} time When to start the oscillator
* @return {OscillatorNode} this
*/
Tone.OscillatorNode.prototype.start = function(time){
this.log("start", time);
if (this._startTime === -1){
this._startTime = this.toSeconds(time);
this._startTime = Math.max(this._startTime, this.context.currentTime);
this._oscillator.start(this._startTime);
this._gainNode.gain.setValueAtTime(1, this._startTime);
} else {
throw new Error("cannot call OscillatorNode.start more than once");
}
return this;
};
/**
* Sets an arbitrary custom periodic waveform given a PeriodicWave.
* @param {PeriodicWave} periodicWave PeriodicWave should be created with context.createPeriodicWave
* @return {OscillatorNode} this
*/
Tone.OscillatorNode.prototype.setPeriodicWave = function(periodicWave){
this._oscillator.setPeriodicWave(periodicWave);
return this;
};
/**
* Stop the oscillator node at the given time
* @param {Time=} time When to stop the oscillator
* @return {OscillatorNode} this
*/
Tone.OscillatorNode.prototype.stop = function(time){
this.log("stop", time);
this.assert(this._startTime !== -1, "'start' must be called before 'stop'");
//cancel the previous stop
this.cancelStop();
//reschedule it
this._stopTime = this.toSeconds(time);
this._stopTime = Math.max(this._stopTime, this.context.currentTime);
if (this._stopTime > this._startTime){
this._gainNode.gain.setValueAtTime(0, this._stopTime);
this.context.clearTimeout(this._timeout);
this._timeout = this.context.setTimeout(function(){
this._oscillator.stop(this.now());
this.onended();
//disconnect the object when it's ended
setTimeout(function(){
if (this._oscillator){
this._oscillator.disconnect();
this._gainNode.disconnect();
}
}.bind(this), 100);
}.bind(this), this._stopTime - this.context.currentTime);
} else {
//cancel the stop envelope
this._gainNode.gain.cancelScheduledValues(this._startTime);
}
return this;
};
/**
* Cancel a scheduled stop event
* @return {Tone.OscillatorNode} this
*/
Tone.OscillatorNode.prototype.cancelStop = function(){
if (this._startTime !== -1){
//cancel the stop envelope
this._gainNode.gain.cancelScheduledValues(this._startTime+this.sampleTime);
this.context.clearTimeout(this._timeout);
this._stopTime = -1;
}
return this;
};
/**
* The oscillator type. Either 'sine', 'sawtooth', 'square', or 'triangle'
* @memberOf Tone.OscillatorNode#
* @type {Time}
* @name type
*/
Object.defineProperty(Tone.OscillatorNode.prototype, "type", {
get : function(){
return this._oscillator.type;
},
set : function(type){
this._oscillator.type = type;
}
});
/**
* Clean up.
* @return {Tone.OscillatorNode} this
*/
Tone.OscillatorNode.prototype.dispose = function(){
if (!this._wasDisposed){
this._wasDisposed = true;
this.context.clearTimeout(this._timeout);
Tone.AudioNode.prototype.dispose.call(this);
this.onended = null;
this._oscillator.disconnect();
this._oscillator = null;
this._gainNode.dispose();
this._gainNode = null;
this.frequency.dispose();
this.frequency = null;
this.detune.dispose();
this.detune = null;
}
return this;
};
export default Tone.OscillatorNode;

View file

@ -11,9 +11,9 @@ import "../core/AudioNode";
* @param {Function} onload The callback to invoke when the
* buffer is done playing.
*/
Tone.BufferSource = function(){
const ToneBufferSource = function() {
var options = Tone.defaults(arguments, ["buffer", "onload"], Tone.BufferSource);
var options = Tone.defaults(arguments, ["buffer", "onload"], ToneBufferSource);
Tone.AudioNode.call(this, options);
/**
@ -84,7 +84,7 @@ Tone.BufferSource = function(){
this.playbackRate = new Tone.Param({
param : this._source.playbackRate,
units : Tone.Type.Positive,
value : options.playbackRate
value : options.playbackRate,
});
/**
@ -118,49 +118,49 @@ Tone.BufferSource = function(){
this.loopEnd = options.loopEnd;
};
Tone.extend(Tone.BufferSource, Tone.AudioNode);
// Tone.extend(ToneBufferSource, Tone.AudioNode);
/**
* The defaults
* @const
* @type {Object}
*/
Tone.BufferSource.defaults = {
"onended" : Tone.noOp,
"onload" : Tone.noOp,
"loop" : false,
"loopStart" : 0,
"loopEnd" : 0,
"fadeIn" : 0,
"fadeOut" : 0,
"curve" : "linear",
"playbackRate" : 1
};
// ToneBufferSource.defaults = {
// "onended" : Tone.noOp,
// "onload" : Tone.noOp,
// "loop" : false,
// "loopStart" : 0,
// "loopEnd" : 0,
// "fadeIn" : 0,
// "fadeOut" : 0,
// "curve" : "linear",
// "playbackRate" : 1,
// };
/**
* Returns the playback state of the source, either "started" or "stopped".
* @type {Tone.State}
* @readOnly
* @memberOf Tone.BufferSource#
* @memberOf ToneBufferSource#
* @name state
*/
Object.defineProperty(Tone.BufferSource.prototype, "state", {
get : function(){
Object.defineProperty(ToneBufferSource.prototype, "state", {
get : function() {
return this.getStateAtTime(this.now());
}
},
});
/**
* Get the playback state at the given time
* @param {Time} time The time to test the state at
* @return {Tone.State} The playback state.
* @return {Tone.State} The playback state.
*/
Tone.BufferSource.prototype.getStateAtTime = function(time){
ToneBufferSource.prototype.getStateAtTime = function(time) {
time = this.toSeconds(time);
if (this._startTime !== -1 &&
this._startTime <= time &&
(this._stopTime === -1 || time < this._stopTime) &&
!this._sourceStopped){
if (this._startTime !== -1 &&
this._startTime <= time &&
(this._stopTime === -1 || time < this._stopTime) &&
!this._sourceStopped) {
return Tone.State.Started;
} else {
return Tone.State.Stopped;
@ -176,9 +176,9 @@ Tone.BufferSource.prototype.getStateAtTime = function(time){
* is given, it will default to the full length
* of the sample (minus any offset)
* @param {Gain} [gain=1] The gain to play the buffer back at.
* @return {Tone.BufferSource} this
* @return {ToneBufferSource} this
*/
Tone.BufferSource.prototype.start = function(time, offset, duration, gain){
ToneBufferSource.prototype.start = function(time, offset, duration, gain) {
this.log("start", time, offset, duration, gain);
this.assert(this._startTime === -1, "can only be started once");
this.assert(this.buffer.loaded, "buffer is either not set or not loaded");
@ -186,7 +186,7 @@ Tone.BufferSource.prototype.start = function(time, offset, duration, gain){
time = this.toSeconds(time);
//if it's a loop the default offset is the loopstart point
if (this.loop){
if (this.loop) {
offset = Tone.defaultArg(offset, this.loopStart);
} else {
//otherwise the default offset is 0
@ -200,9 +200,9 @@ Tone.BufferSource.prototype.start = function(time, offset, duration, gain){
//apply a fade in envelope
var fadeInTime = this.toSeconds(this.fadeIn);
if (fadeInTime > 0){
if (fadeInTime > 0) {
this._gainNode.gain.setValueAtTime(0, time);
if (this.curve === "linear"){
if (this.curve === "linear") {
this._gainNode.gain.linearRampToValueAtTime(gain, time + fadeInTime);
} else {
this._gainNode.gain.exponentialApproachValueAtTime(gain, time, fadeInTime);
@ -214,7 +214,7 @@ Tone.BufferSource.prototype.start = function(time, offset, duration, gain){
this._startTime = time;
//if a duration is given, schedule a stop
if (Tone.isDefined(duration)){
if (Tone.isDefined(duration)) {
var computedDur = this.toSeconds(duration);
//make sure it's never negative
computedDur = Math.max(computedDur, 0);
@ -223,19 +223,19 @@ Tone.BufferSource.prototype.start = function(time, offset, duration, gain){
}
//start the buffer source
if (this.loop){
if (this.loop) {
//modify the offset if it's greater than the loop time
var loopEnd = this.loopEnd || this.buffer.duration;
var loopStart = this.loopStart;
var loopDuration = loopEnd - loopStart;
//move the offset back
if (offset >= loopEnd){
if (offset >= loopEnd) {
offset = ((offset - loopStart) % loopDuration) + loopStart;
}
}
this._source.buffer = this.buffer.get();
this._source.loopEnd = this.loopEnd || this.buffer.duration;
if (offset < this.buffer.duration){
if (offset < this.buffer.duration) {
this._sourceStarted = true;
this._source.start(time, offset);
}
@ -244,11 +244,11 @@ Tone.BufferSource.prototype.start = function(time, offset, duration, gain){
};
/**
* Stop the buffer.
* Stop the buffer.
* @param {Time=} time The time the buffer should stop.
* @return {Tone.BufferSource} this
* @return {ToneBufferSource} this
*/
Tone.BufferSource.prototype.stop = function(time){
ToneBufferSource.prototype.stop = function(time) {
this.log("stop", time);
this.assert(this.buffer.loaded, "buffer is either not set or not loaded");
this.assert(!this._sourceStopped, "source is already stopped");
@ -256,7 +256,7 @@ Tone.BufferSource.prototype.stop = function(time){
time = this.toSeconds(time);
//if the event has already been scheduled, clear it
if (this._stopTime !== -1){
if (this._stopTime !== -1) {
this.cancelStop();
}
@ -266,9 +266,9 @@ Tone.BufferSource.prototype.stop = function(time){
//cancel the previous curve
this._stopTime = time + fadeOutTime;
if (fadeOutTime > 0){
if (fadeOutTime > 0) {
//start the fade out curve at the given time
if (this.curve === "linear"){
if (this.curve === "linear") {
this._gainNode.gain.linearRampTo(0, fadeOutTime, time);
} else {
this._gainNode.gain.targetRampTo(0, fadeOutTime, time);
@ -287,10 +287,10 @@ Tone.BufferSource.prototype.stop = function(time){
/**
* Cancel a scheduled stop event
* @return {Tone.BufferSource} this
* @return {ToneBufferSource} this
*/
Tone.BufferSource.prototype.cancelStop = function(){
if (this._startTime !== -1 && !this._sourceStopped){
ToneBufferSource.prototype.cancelStop = function() {
if (this._startTime !== -1 && !this._sourceStopped) {
//cancel the stop envelope
var fadeInTime = this.toSeconds(this.fadeIn);
this._gainNode.gain.cancelScheduledValues(this._startTime + fadeInTime + this.sampleTime);
@ -305,20 +305,20 @@ Tone.BufferSource.prototype.cancelStop = function(){
* Invokes `onended` and disposes the node.
* @private
*/
Tone.BufferSource.prototype._onended = function(){
if (!this._sourceStopped){
ToneBufferSource.prototype._onended = function() {
if (!this._sourceStopped) {
this._sourceStopped = true;
//allow additional time for the exponential curve to fully decay
var additionalTail = this.curve === "exponential" ? this.fadeOut * 2 : 0;
if (this._sourceStarted && this._stopTime !== -1){
if (this._sourceStarted && this._stopTime !== -1) {
this._source.stop(this._stopTime + additionalTail);
}
this.onended(this);
//dispose the source after it's come to a stop
setTimeout(function(){
setTimeout(function() {
//if it hasn't already been disposed
if (this._source){
if (this._source) {
this._source.disconnect();
this._gainNode.disconnect();
}
@ -328,71 +328,71 @@ Tone.BufferSource.prototype._onended = function(){
/**
* If loop is true, the loop will start at this position.
* @memberOf Tone.BufferSource#
* @memberOf ToneBufferSource#
* @type {Time}
* @name loopStart
*/
Object.defineProperty(Tone.BufferSource.prototype, "loopStart", {
get : function(){
Object.defineProperty(ToneBufferSource.prototype, "loopStart", {
get : function() {
return this._source.loopStart;
},
set : function(loopStart){
set : function(loopStart) {
this._source.loopStart = this.toSeconds(loopStart);
}
},
});
/**
* If loop is true, the loop will end at this position.
* @memberOf Tone.BufferSource#
* @memberOf ToneBufferSource#
* @type {Time}
* @name loopEnd
*/
Object.defineProperty(Tone.BufferSource.prototype, "loopEnd", {
get : function(){
Object.defineProperty(ToneBufferSource.prototype, "loopEnd", {
get : function() {
return this._source.loopEnd;
},
set : function(loopEnd){
set : function(loopEnd) {
this._source.loopEnd = this.toSeconds(loopEnd);
}
},
});
/**
* The audio buffer belonging to the player.
* @memberOf Tone.BufferSource#
* @memberOf ToneBufferSource#
* @type {Tone.Buffer}
* @name buffer
*/
Object.defineProperty(Tone.BufferSource.prototype, "buffer", {
get : function(){
Object.defineProperty(ToneBufferSource.prototype, "buffer", {
get : function() {
return this._buffer;
},
set : function(buffer){
set : function(buffer) {
this._buffer.set(buffer);
}
},
});
/**
* If the buffer should loop once it's over.
* @memberOf Tone.BufferSource#
* @memberOf ToneBufferSource#
* @type {Boolean}
* @name loop
*/
Object.defineProperty(Tone.BufferSource.prototype, "loop", {
get : function(){
Object.defineProperty(ToneBufferSource.prototype, "loop", {
get : function() {
return this._source.loop;
},
set : function(loop){
set : function(loop) {
this._source.loop = loop;
this.cancelStop();
}
},
});
/**
* Clean up.
* @return {Tone.BufferSource} this
* @return {ToneBufferSource} this
*/
Tone.BufferSource.prototype.dispose = function(){
if (!this._wasDisposed){
ToneBufferSource.prototype.dispose = function() {
if (!this._wasDisposed) {
this._wasDisposed = true;
Tone.AudioNode.prototype.dispose.call(this);
this.onended = null;
@ -409,6 +409,3 @@ Tone.BufferSource.prototype.dispose = function(){
}
return this;
};
export default Tone.BufferSource;

View file

@ -1,7 +1,7 @@
import Tone from "../core/Tone";
import "../source/Source";
import Tone from "../../core/Tone";
import "../Source";
import "../core/Buffer";
import "../source/BufferSource";
import "../BufferSource";
/**
* @class Tone.GrainPlayer implements [granular synthesis](https://en.wikipedia.org/wiki/Granular_synthesis).

View file

@ -1,8 +1,8 @@
import Tone from "../core/Tone";
import Tone from "../../core/Tone";
import "../core/Buffer";
import "../source/Source";
import "../Source";
import "../source/TickSource";
import "../source/BufferSource";
import "../BufferSource";
/**
* @class Tone.Player is an audio file player with start, loop, and stop functions.

View file

@ -1,4 +1,4 @@
import Tone from "../core/Tone";
import Tone from "../../core/Tone";
import "../source/Player";
import "../component/Volume";
import "../core/AudioNode";
@ -11,7 +11,7 @@ import "../core/AudioNode";
* @param {Object} urls An object mapping a name to a url.
* @param {function=} onload The function to invoke when all buffers are loaded.
*/
Tone.Players = function(urls){
Tone.Players = function(urls) {
var args = Array.prototype.slice.call(arguments);
args.shift();
@ -70,7 +70,7 @@ Tone.Players = function(urls){
this._fadeOut = options.fadeOut;
//add all of the players
for (var name in urls){
for (var name in urls) {
this._loadingCount++;
this.add(name, urls[name], this._bufferLoaded.bind(this, options.onload));
}
@ -87,7 +87,7 @@ Tone.Players.defaults = {
"mute" : false,
"onload" : Tone.noOp,
"fadeIn" : 0,
"fadeOut" : 0
"fadeOut" : 0,
};
/**
@ -95,9 +95,9 @@ Tone.Players.defaults = {
* @param {Function} callback
* @private
*/
Tone.Players.prototype._bufferLoaded = function(callback){
Tone.Players.prototype._bufferLoaded = function(callback) {
this._loadingCount--;
if (this._loadingCount === 0 && callback){
if (this._loadingCount === 0 && callback) {
callback(this);
}
};
@ -112,12 +112,12 @@ Tone.Players.prototype._bufferLoaded = function(callback){
* source.mute = true;
*/
Object.defineProperty(Tone.Players.prototype, "mute", {
get : function(){
get : function() {
return this._volume.mute;
},
set : function(mute){
set : function(mute) {
this._volume.mute = mute;
}
},
});
/**
@ -127,15 +127,15 @@ Object.defineProperty(Tone.Players.prototype, "mute", {
* @name fadeIn
*/
Object.defineProperty(Tone.Players.prototype, "fadeIn", {
get : function(){
get : function() {
return this._fadeIn;
},
set : function(fadeIn){
set : function(fadeIn) {
this._fadeIn = fadeIn;
this._forEach(function(player){
this._forEach(function(player) {
player.fadeIn = fadeIn;
});
}
},
});
/**
@ -145,15 +145,15 @@ Object.defineProperty(Tone.Players.prototype, "fadeIn", {
* @name fadeOut
*/
Object.defineProperty(Tone.Players.prototype, "fadeOut", {
get : function(){
get : function() {
return this._fadeOut;
},
set : function(fadeOut){
set : function(fadeOut) {
this._fadeOut = fadeOut;
this._forEach(function(player){
this._forEach(function(player) {
player.fadeOut = fadeOut;
});
}
},
});
/**
@ -164,13 +164,13 @@ Object.defineProperty(Tone.Players.prototype, "fadeOut", {
* @readOnly
*/
Object.defineProperty(Tone.Players.prototype, "state", {
get : function(){
get : function() {
var playing = false;
this._forEach(function(player){
this._forEach(function(player) {
playing = playing || player.state === Tone.State.Started;
});
return playing ? Tone.State.Started : Tone.State.Stopped;
}
},
});
/**
@ -179,7 +179,7 @@ Object.defineProperty(Tone.Players.prototype, "state", {
* buffer.
* @return {Boolean}
*/
Tone.Players.prototype.has = function(name){
Tone.Players.prototype.has = function(name) {
return this._players.hasOwnProperty(name);
};
@ -189,11 +189,11 @@ Tone.Players.prototype.has = function(name){
* the constructor object or `add` method.
* @return {Tone.Player}
*/
Tone.Players.prototype.get = function(name){
if (this.has(name)){
Tone.Players.prototype.get = function(name) {
if (this.has(name)) {
return this._players[name];
} else {
throw new Error("Tone.Players: no player named "+name);
throw new Error("Tone.Players: no player named " + name);
}
};
@ -203,8 +203,8 @@ Tone.Players.prototype.get = function(name){
* @return {Tone.Players} this
* @private
*/
Tone.Players.prototype._forEach = function(callback){
for (var playerName in this._players){
Tone.Players.prototype._forEach = function(callback) {
for (var playerName in this._players) {
callback(this._players[playerName], playerName);
}
return this;
@ -218,13 +218,13 @@ Tone.Players.prototype._forEach = function(callback){
* @readOnly
*/
Object.defineProperty(Tone.Players.prototype, "loaded", {
get : function(){
get : function() {
var isLoaded = true;
this._forEach(function(player){
this._forEach(function(player) {
isLoaded = isLoaded && player.loaded;
});
return isLoaded;
}
},
});
/**
@ -236,7 +236,7 @@ Object.defineProperty(Tone.Players.prototype, "loaded", {
* @param {Function=} callback The callback to invoke
* when the url is loaded.
*/
Tone.Players.prototype.add = function(name, url, callback){
Tone.Players.prototype.add = function(name, url, callback) {
this._players[name] = new Tone.Player(url, callback).connect(this.output);
this._players[name].fadeIn = this._fadeIn;
this._players[name].fadeOut = this._fadeOut;
@ -248,8 +248,8 @@ Tone.Players.prototype.add = function(name, url, callback){
* @param {Time} time The time to stop all of the players.
* @return {Tone.Players} this
*/
Tone.Players.prototype.stopAll = function(time){
this._forEach(function(player){
Tone.Players.prototype.stopAll = function(time) {
this._forEach(function(player) {
player.stop(time);
});
};
@ -258,14 +258,14 @@ Tone.Players.prototype.stopAll = function(time){
* Dispose and disconnect.
* @return {Tone.Players} this
*/
Tone.Players.prototype.dispose = function(){
Tone.Players.prototype.dispose = function() {
Tone.AudioNode.prototype.dispose.call(this);
this._volume.dispose();
this._volume = null;
this._writable("volume");
this.volume = null;
this.output = null;
this._forEach(function(player){
this._forEach(function(player) {
player.dispose();
});
this._players = null;