Tone.js/Tone/event/Part.js

520 lines
13 KiB
JavaScript
Raw Normal View History

2018-03-05 17:25:33 +00:00
define(["Tone/core/Tone", "Tone/event/Event", "Tone/type/Type", "Tone/core/Transport"], function(Tone){
2015-11-03 23:36:36 +00:00
"use strict";
2017-10-25 21:57:52 +00:00
2015-11-03 23:36:36 +00:00
/**
* @class Tone.Part is a collection Tone.Events which can be
2017-06-23 15:33:54 +00:00
* started/stopped and looped as a single unit.
2015-11-03 23:36:36 +00:00
*
* @extends {Tone.Event}
* @param {Function} callback The callback to invoke on each event
2015-11-18 03:52:22 +00:00
* @param {Array} events the array of events
2015-11-03 23:36:36 +00:00
* @example
* var part = new Tone.Part(function(time, note){
2015-11-18 03:52:22 +00:00
* //the notes given as the second element in the array
* //will be passed in as the second argument
2015-11-03 23:36:36 +00:00
* synth.triggerAttackRelease(note, "8n", time);
2015-11-18 03:52:22 +00:00
* }, [[0, "C2"], ["0:2", "C3"], ["0:3:2", "G2"]]);
2015-11-03 23:36:36 +00:00
* @example
2015-12-08 05:07:16 +00:00
* //use an array of objects as long as the object has a "time" attribute
2015-11-03 23:36:36 +00:00
* var part = new Tone.Part(function(time, value){
2015-12-08 05:07:16 +00:00
* //the value is an object which contains both the note and the velocity
2015-11-03 23:36:36 +00:00
* synth.triggerAttackRelease(value.note, "8n", time, value.velocity);
2017-10-25 21:57:52 +00:00
* }, [{"time" : 0, "note" : "C3", "velocity": 0.9},
2015-11-03 23:36:36 +00:00
* {"time" : "0:2", "note" : "C4", "velocity": 0.5}
2015-11-18 03:52:22 +00:00
* ]).start(0);
2015-11-03 23:36:36 +00:00
*/
Tone.Part = function(){
2017-04-26 02:31:06 +00:00
var options = Tone.defaults(arguments, ["callback", "events"], Tone.Part);
Tone.Event.call(this, options);
2015-11-03 23:36:36 +00:00
/**
2017-10-25 21:57:52 +00:00
* An array of Objects.
2015-11-03 23:36:36 +00:00
* @type {Array}
* @private
*/
this._events = [];
//add the events
for (var i = 0; i < options.events.length; i++){
if (Array.isArray(options.events[i])){
this.add(options.events[i][0], options.events[i][1]);
} else {
this.add(options.events[i]);
2015-11-03 23:36:36 +00:00
}
}
};
Tone.extend(Tone.Part, Tone.Event);
/**
* The default values
* @type {Object}
* @const
*/
Tone.Part.defaults = {
"callback" : Tone.noOp,
"loop" : false,
"loopEnd" : "1m",
"loopStart" : 0,
"playbackRate" : 1,
"probability" : 1,
"humanize" : false,
"mute" : false,
2017-04-26 02:31:06 +00:00
"events" : []
2015-11-03 23:36:36 +00:00
};
/**
2017-10-25 21:57:52 +00:00
* Start the part at the given time.
2016-04-18 06:05:04 +00:00
* @param {TransportTime} time When to start the part.
2015-11-03 23:36:36 +00:00
* @param {Time=} offset The offset from the start of the part
* to begin playing at.
* @return {Tone.Part} this
*/
Tone.Part.prototype.start = function(time, offset){
var ticks = this.toTicks(time);
2016-12-19 03:14:14 +00:00
if (this._state.getValueAtTime(ticks) !== Tone.State.Started){
if (this._loop){
2017-04-30 19:03:49 +00:00
offset = Tone.defaultArg(offset, this._loopStart);
} else {
2017-04-30 19:03:49 +00:00
offset = Tone.defaultArg(offset, 0);
}
2015-11-03 23:36:36 +00:00
offset = this.toTicks(offset);
this._state.add({
2017-10-25 21:57:52 +00:00
"state" : Tone.State.Started,
"time" : ticks,
2015-11-03 23:36:36 +00:00
"offset" : offset
});
this._forEach(function(event){
this._startNote(event, ticks, offset);
});
}
return this;
};
/**
* Start the event in the given event at the correct time given
* the ticks and offset and looping.
2017-10-25 21:57:52 +00:00
* @param {Tone.Event} event
2015-11-03 23:36:36 +00:00
* @param {Ticks} ticks
* @param {Ticks} offset
* @private
*/
Tone.Part.prototype._startNote = function(event, ticks, offset){
ticks -= offset;
2015-11-03 23:36:36 +00:00
if (this._loop){
if (event.startOffset >= this._loopStart && event.startOffset < this._loopEnd){
if (event.startOffset < offset){
//start it on the next loop
ticks += this._getLoopDuration();
}
2017-12-17 18:26:04 +00:00
event.start(Tone.Ticks(ticks));
2018-03-05 17:25:33 +00:00
} else if (event.startOffset < this._loopStart && event.startOffset >= offset){
event.loop = false;
2017-12-17 18:26:04 +00:00
event.start(Tone.Ticks(ticks));
2015-11-03 23:36:36 +00:00
}
2017-10-25 21:57:52 +00:00
} else if (event.startOffset >= offset){
2017-12-17 18:26:04 +00:00
event.start(Tone.Ticks(ticks));
2015-11-03 23:36:36 +00:00
}
};
/**
* The start from the scheduled start time
* @type {Ticks}
* @memberOf Tone.Part#
* @name startOffset
* @private
*/
Object.defineProperty(Tone.Part.prototype, "startOffset", {
get : function(){
return this._startOffset;
},
set : function(offset){
this._startOffset = offset;
this._forEach(function(event){
event.startOffset += this._startOffset;
2015-11-03 23:36:36 +00:00
});
}
});
/**
* Stop the part at the given time.
* @param {TimelinePosition} time When to stop the part.
2015-11-03 23:36:36 +00:00
* @return {Tone.Part} this
*/
Tone.Part.prototype.stop = function(time){
var ticks = this.toTicks(time);
2016-04-18 06:05:04 +00:00
this._state.cancel(ticks);
this._state.setStateAtTime(Tone.State.Stopped, ticks);
this._forEach(function(event){
event.stop(time);
});
2015-11-03 23:36:36 +00:00
return this;
};
/**
2017-10-25 21:57:52 +00:00
* Get/Set an Event's value at the given time.
2015-11-03 23:36:36 +00:00
* If a value is passed in and no event exists at
2017-10-25 21:57:52 +00:00
* the given time, one will be created with that value.
2015-11-03 23:36:36 +00:00
* If two events are at the same time, the first one will
* be returned.
2015-11-18 03:52:22 +00:00
* @example
* part.at("1m"); //returns the part at the first measure
*
2017-10-25 21:57:52 +00:00
* part.at("2m", "C2"); //set the value at "2m" to C2.
2015-11-18 03:52:22 +00:00
* //if an event didn't exist at that time, it will be created.
2016-04-18 06:05:04 +00:00
* @param {TransportTime} time The time of the event to get or set.
2015-11-03 23:36:36 +00:00
* @param {*=} value If a value is passed in, the value of the
* event at the given time will be set to it.
* @return {Tone.Event} the event at the time
*/
Tone.Part.prototype.at = function(time, value){
2016-04-18 06:05:04 +00:00
time = Tone.TransportTime(time);
2017-12-17 18:26:04 +00:00
var tickTime = Tone.Ticks(1).toSeconds();
2015-11-03 23:36:36 +00:00
for (var i = 0; i < this._events.length; i++){
var event = this._events[i];
2016-04-18 06:05:04 +00:00
if (Math.abs(time.toTicks() - event.startOffset) < tickTime){
2018-03-05 16:32:08 +00:00
if (Tone.isDefined(value)){
2015-11-03 23:36:36 +00:00
event.value = value;
}
return event;
}
}
//if there was no event at that time, create one
2018-03-05 16:32:08 +00:00
if (Tone.isDefined(value)){
2016-04-18 06:05:04 +00:00
this.add(time, value);
2015-11-03 23:36:36 +00:00
//return the new event
return this._events[this._events.length - 1];
} else {
return null;
}
};
/**
2017-10-25 21:57:52 +00:00
* Add a an event to the part.
2015-11-03 23:36:36 +00:00
* @param {Time} time The time the note should start.
* If an object is passed in, it should
* have a 'time' attribute and the rest
* of the object will be used as the 'value'.
2017-10-25 21:57:52 +00:00
* @param {Tone.Event|*} value
2015-11-03 23:36:36 +00:00
* @returns {Tone.Part} this
* @example
* part.add("1m", "C#+11");
*/
Tone.Part.prototype.add = function(time, value){
//extract the parameters
if (time.hasOwnProperty("time")){
2015-11-03 23:36:36 +00:00
value = time;
time = value.time;
}
2015-11-03 23:36:36 +00:00
time = this.toTicks(time);
var event;
if (value instanceof Tone.Event){
event = value;
event.callback = this._tick.bind(this);
} else {
event = new Tone.Event({
2017-10-25 21:57:52 +00:00
"callback" : this._tick.bind(this),
2015-11-03 23:36:36 +00:00
"value" : value,
});
}
//the start offset
event.startOffset = time;
2015-11-03 23:36:36 +00:00
//initialize the values
event.set({
"loopEnd" : this.loopEnd,
"loopStart" : this.loopStart,
2015-11-03 23:36:36 +00:00
"loop" : this.loop,
"humanize" : this.humanize,
"playbackRate" : this.playbackRate,
"probability" : this.probability
});
this._events.push(event);
//start the note if it should be played right now
this._restartEvent(event);
return this;
};
/**
* Restart the given event
2017-10-25 21:57:52 +00:00
* @param {Tone.Event} event
2015-11-03 23:36:36 +00:00
* @private
*/
Tone.Part.prototype._restartEvent = function(event){
this._state.forEach(function(stateEvent){
if (stateEvent.state === Tone.State.Started){
this._startNote(event, stateEvent.time, stateEvent.offset);
} else {
//stop the note
2017-12-17 18:26:04 +00:00
event.stop(Tone.Ticks(stateEvent.time));
}
}.bind(this));
2015-11-03 23:36:36 +00:00
};
/**
* Remove an event from the part. Will recursively iterate
2015-12-08 05:07:16 +00:00
* into nested parts to find the event.
* @param {Time} time The time of the event
* @param {*} value Optionally select only a specific event value
* @return {Tone.Part} this
2015-11-03 23:36:36 +00:00
*/
Tone.Part.prototype.remove = function(time, value){
//extract the parameters
if (time.hasOwnProperty("time")){
2015-11-03 23:36:36 +00:00
value = time;
time = value.time;
}
2015-11-03 23:36:36 +00:00
time = this.toTicks(time);
for (var i = this._events.length - 1; i >= 0; i--){
var event = this._events[i];
if (event instanceof Tone.Part){
event.remove(time, value);
2017-10-25 21:57:52 +00:00
} else if (event.startOffset === time){
2018-03-05 16:32:08 +00:00
if (Tone.isUndef(value) || (Tone.isDefined(value) && event.value === value)){
2017-10-25 21:57:52 +00:00
this._events.splice(i, 1);
event.dispose();
2015-11-03 23:36:36 +00:00
}
}
}
2015-11-03 23:36:36 +00:00
return this;
};
/**
2017-10-25 21:57:52 +00:00
* Remove all of the notes from the group.
2015-11-03 23:36:36 +00:00
* @return {Tone.Part} this
*/
Tone.Part.prototype.removeAll = function(){
this._forEach(function(event){
event.dispose();
});
this._events = [];
return this;
};
/**
* Cancel scheduled state change events: i.e. "start" and "stop".
* @param {TimelinePosition} after The time after which to cancel the scheduled events.
2015-11-03 23:36:36 +00:00
* @return {Tone.Part} this
*/
Tone.Part.prototype.cancel = function(after){
this._forEach(function(event){
event.cancel(after);
});
2017-06-29 16:33:16 +00:00
this._state.cancel(this.toTicks(after));
2015-11-03 23:36:36 +00:00
return this;
};
/**
* Iterate over all of the events
2015-11-03 23:36:36 +00:00
* @param {Function} callback
* @param {Object} ctx The context
2015-11-03 23:36:36 +00:00
* @private
*/
Tone.Part.prototype._forEach = function(callback, ctx){
2017-04-26 02:31:06 +00:00
if (this._events){
2017-04-30 19:03:49 +00:00
ctx = Tone.defaultArg(ctx, this);
2017-04-26 02:31:06 +00:00
for (var i = this._events.length - 1; i >= 0; i--){
var e = this._events[i];
if (e instanceof Tone.Part){
e._forEach(callback, ctx);
} else {
callback.call(ctx, e);
}
}
2015-11-03 23:36:36 +00:00
}
return this;
};
/**
* Set the attribute of all of the events
* @param {String} attr the attribute to set
* @param {*} value The value to set it to
* @private
*/
Tone.Part.prototype._setAll = function(attr, value){
this._forEach(function(event){
event[attr] = value;
});
};
/**
* Internal tick method
* @param {Number} time The time of the event in seconds
* @private
*/
Tone.Part.prototype._tick = function(time, value){
if (!this.mute){
this.callback(time, value);
2015-11-03 23:36:36 +00:00
}
};
/**
* Determine if the event should be currently looping
* given the loop boundries of this Part.
* @param {Tone.Event} event The event to test
* @private
*/
Tone.Part.prototype._testLoopBoundries = function(event){
if (event.startOffset < this._loopStart || event.startOffset >= this._loopEnd){
event.cancel(0);
2017-10-25 21:57:52 +00:00
} else if (event.state === Tone.State.Stopped){
2015-11-03 23:36:36 +00:00
//reschedule it if it's stopped
2017-10-25 21:57:52 +00:00
this._restartEvent(event);
2015-11-03 23:36:36 +00:00
}
};
/**
* The probability of the notes being triggered.
* @memberOf Tone.Part#
* @type {NormalRange}
* @name probability
*/
Object.defineProperty(Tone.Part.prototype, "probability", {
get : function(){
return this._probability;
},
set : function(prob){
this._probability = prob;
this._setAll("probability", prob);
}
});
/**
2015-11-18 03:52:22 +00:00
* If set to true, will apply small random variation
* to the callback time. If the value is given as a time, it will randomize
* by that amount.
* @example
* event.humanize = true;
2015-11-03 23:36:36 +00:00
* @type {Boolean|Time}
2015-12-08 05:07:16 +00:00
* @name humanize
2015-11-03 23:36:36 +00:00
*/
Object.defineProperty(Tone.Part.prototype, "humanize", {
get : function(){
return this._humanize;
},
set : function(variation){
this._humanize = variation;
this._setAll("humanize", variation);
}
});
/**
* If the part should loop or not
2017-10-25 21:57:52 +00:00
* between Tone.Part.loopStart and
2015-11-03 23:36:36 +00:00
* Tone.Part.loopEnd. An integer
* value corresponds to the number of
* loops the Part does after it starts.
* @memberOf Tone.Part#
* @type {Boolean|Positive}
* @name loop
2015-11-18 03:52:22 +00:00
* @example
* //loop the part 8 times
* part.loop = 8;
2015-11-03 23:36:36 +00:00
*/
Object.defineProperty(Tone.Part.prototype, "loop", {
get : function(){
return this._loop;
},
set : function(loop){
this._loop = loop;
this._forEach(function(event){
event._loopStart = this._loopStart;
event._loopEnd = this._loopEnd;
2015-11-03 23:36:36 +00:00
event.loop = loop;
this._testLoopBoundries(event);
});
}
});
/**
2017-10-25 21:57:52 +00:00
* The loopEnd point determines when it will
2015-11-03 23:36:36 +00:00
* loop if Tone.Part.loop is true.
* @memberOf Tone.Part#
* @type {Time}
2015-11-03 23:36:36 +00:00
* @name loopEnd
*/
Object.defineProperty(Tone.Part.prototype, "loopEnd", {
get : function(){
2017-12-17 18:26:04 +00:00
return Tone.Ticks(this._loopEnd).toSeconds();
2015-11-03 23:36:36 +00:00
},
set : function(loopEnd){
this._loopEnd = this.toTicks(loopEnd);
if (this._loop){
this._forEach(function(event){
event.loopEnd = loopEnd;
2015-11-03 23:36:36 +00:00
this._testLoopBoundries(event);
});
}
}
});
/**
2017-10-25 21:57:52 +00:00
* The loopStart point determines when it will
2015-11-03 23:36:36 +00:00
* loop if Tone.Part.loop is true.
* @memberOf Tone.Part#
* @type {Time}
2015-11-03 23:36:36 +00:00
* @name loopStart
*/
Object.defineProperty(Tone.Part.prototype, "loopStart", {
get : function(){
2017-12-17 18:26:04 +00:00
return Tone.Ticks(this._loopStart).toSeconds();
2015-11-03 23:36:36 +00:00
},
set : function(loopStart){
this._loopStart = this.toTicks(loopStart);
if (this._loop){
this._forEach(function(event){
event.loopStart = this.loopStart;
2015-11-03 23:36:36 +00:00
this._testLoopBoundries(event);
});
}
}
});
/**
* The playback rate of the part
* @memberOf Tone.Part#
* @type {Positive}
* @name playbackRate
*/
Object.defineProperty(Tone.Part.prototype, "playbackRate", {
get : function(){
return this._playbackRate;
},
set : function(rate){
this._playbackRate = rate;
this._setAll("playbackRate", rate);
}
});
/**
2017-10-25 21:57:52 +00:00
* The number of scheduled notes in the part.
2015-11-03 23:36:36 +00:00
* @memberOf Tone.Part#
* @type {Positive}
* @name length
* @readOnly
*/
Object.defineProperty(Tone.Part.prototype, "length", {
get : function(){
return this._events.length;
}
});
/**
* Clean up
* @return {Tone.Part} this
*/
Tone.Part.prototype.dispose = function(){
2018-05-25 22:25:08 +00:00
Tone.Event.prototype.dispose.call(this);
2015-11-03 23:36:36 +00:00
this.removeAll();
this.callback = null;
this._events = null;
return this;
};
return Tone.Part;
2017-06-23 15:33:54 +00:00
});