Tone.js/Tone/source/Source.js

109 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-06-19 05:40:16 +00:00
define(["Tone/core/Tone", "Tone/core/Transport"], function(Tone){
"use strict";
2014-06-16 01:00:22 +00:00
/**
2014-07-20 22:17:24 +00:00
* @class Base class for sources.
* Sources have start/stop/pause and
* the ability to be synced to the
* start/stop/pause of Tone.Transport.
2014-06-19 05:40:16 +00:00
*
2014-06-16 01:00:22 +00:00
* @constructor
* @extends {Tone}
*/
Tone.Source = function(){
//unlike most ToneNodes, Sources only have an output and no input
Tone.call(this, 0, 1);
2014-06-19 05:40:16 +00:00
/**
* @type {Tone.Source.State}
*/
this.state = Tone.Source.State.STOPPED;
2014-06-16 01:00:22 +00:00
};
Tone.extend(Tone.Source);
/**
* @abstract
* @param {Tone.Time} time
*/
Tone.Source.prototype.start = function(){};
/**
* @abstract
* @param {Tone.Time} time
*/
Tone.Source.prototype.stop = function(){};
2014-06-19 05:40:16 +00:00
2014-06-16 01:00:22 +00:00
/**
* @abstract
* @param {Tone.Time} time
*/
2014-06-19 05:40:16 +00:00
Tone.Source.prototype.pause = function(time){
//if there is no pause, just stop it
this.stop(time);
};
/**
* sync the source to the Transport
2014-06-25 16:47:47 +00:00
*
* @param {Tone.Time=} delay optional delay time before starting the source
2014-06-19 05:40:16 +00:00
*/
2014-06-25 16:47:47 +00:00
Tone.Source.prototype.sync = function(delay){
2014-07-30 19:12:47 +00:00
Tone.Transport.syncSource(this, delay);
2014-06-19 05:40:16 +00:00
};
/**
* unsync the source to the Transport
*/
Tone.Source.prototype.unsync = function(){
2014-07-30 19:12:47 +00:00
Tone.Transport.unsyncSource(this);
2014-06-19 05:40:16 +00:00
};
2014-06-16 01:00:22 +00:00
/**
2014-08-26 01:47:22 +00:00
* set the volume in decibels
* @param {number} db in decibels
2014-06-21 17:04:22 +00:00
* @param {Tone.Time=} fadeTime (optional) time it takes to reach the value
2014-06-16 01:00:22 +00:00
*/
2014-08-26 01:47:22 +00:00
Tone.Source.prototype.setVolume = function(db, fadeTime){
2014-06-16 01:00:22 +00:00
var now = this.now();
2014-08-26 01:47:22 +00:00
var gain = this.dbToGain(db);
2014-06-21 17:04:22 +00:00
if (fadeTime){
var currentVolume = this.output.gain.value;
this.output.gain.cancelScheduledValues(now);
this.output.gain.setValueAtTime(currentVolume, now);
2014-08-26 01:47:22 +00:00
this.output.gain.linearRampToValueAtTime(gain, now + this.toSeconds(fadeTime));
2014-06-21 17:04:22 +00:00
} else {
2014-08-26 01:47:22 +00:00
this.output.gain.setValueAtTime(gain, now);
2014-06-21 17:04:22 +00:00
}
2014-06-16 01:00:22 +00:00
};
/**
* set the parameters at once
* @param {Object} params
*/
Tone.Source.prototype.set = function(params){
if (!this.isUndef(params.volume)) this.setVolume(params.volume);
};
/**
* clean up
*/
Tone.Source.prototype.dispose = function(){
Tone.prototype.dispose.call(this);
this.state = null;
};
2014-06-19 05:40:16 +00:00
/**
* @enum {string}
*/
Tone.Source.State = {
STARTED : "started",
PAUSED : "paused",
STOPPED : "stopped",
SYNCED : "synced"
};
2014-06-16 01:00:22 +00:00
return Tone.Source;
});