2017-02-19 00:48:15 +00:00
|
|
|
define(["Tone/core/Tone", "Tone/core/Context"], function (Tone) {
|
|
|
|
|
2017-03-13 05:12:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* shim
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
if (!window.hasOwnProperty("OfflineAudioContext") && window.hasOwnProperty("webkitOfflineAudioContext")){
|
|
|
|
window.OfflineAudioContext = window.webkitOfflineAudioContext;
|
|
|
|
}
|
|
|
|
|
2017-02-19 00:48:15 +00:00
|
|
|
/**
|
|
|
|
* @class Wrapper around the OfflineAudioContext
|
2017-05-08 02:42:03 +00:00
|
|
|
* @extends {Tone.Context}
|
2017-02-19 00:48:15 +00:00
|
|
|
* @param {Number} channels The number of channels to render
|
|
|
|
* @param {Number} duration The duration to render in samples
|
|
|
|
* @param {Number} sampleRate the sample rate to render at
|
|
|
|
*/
|
|
|
|
Tone.OfflineContext = function(channels, duration, sampleRate){
|
2017-10-25 21:59:17 +00:00
|
|
|
|
2017-02-19 00:48:15 +00:00
|
|
|
/**
|
|
|
|
* The offline context
|
|
|
|
* @private
|
|
|
|
* @type {OfflineAudioContext}
|
|
|
|
*/
|
|
|
|
var offlineContext = new OfflineAudioContext(channels, duration * sampleRate, sampleRate);
|
|
|
|
|
|
|
|
//wrap the methods/members
|
2017-07-05 17:59:13 +00:00
|
|
|
Tone.Context.call(this, {
|
|
|
|
"context" : offlineContext,
|
|
|
|
"clockSource" : "offline",
|
|
|
|
"lookAhead" : 0,
|
|
|
|
"updateInterval" : 128 / sampleRate
|
|
|
|
});
|
2017-02-19 00:48:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A private reference to the duration
|
|
|
|
* @private
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
|
|
|
this._duration = duration;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An artificial clock source
|
|
|
|
* @type {Number}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._currentTime = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
Tone.extend(Tone.OfflineContext, Tone.Context);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override the now method to point to the internal clock time
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
Tone.OfflineContext.prototype.now = function(){
|
|
|
|
return this._currentTime;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render the output of the OfflineContext
|
|
|
|
* @return {Promise}
|
|
|
|
*/
|
|
|
|
Tone.OfflineContext.prototype.render = function(){
|
|
|
|
while(this._duration - this._currentTime >= 0){
|
|
|
|
//invoke all the callbacks on that time
|
|
|
|
this.emit("tick");
|
|
|
|
//increment the clock
|
2017-07-27 21:54:57 +00:00
|
|
|
this._currentTime += this.blockTime;
|
2017-02-19 00:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//promise returned is not yet implemented in all browsers
|
|
|
|
return new Promise(function(done){
|
|
|
|
this._context.oncomplete = function(e){
|
|
|
|
done(e.renderedBuffer);
|
|
|
|
};
|
|
|
|
this._context.startRendering();
|
|
|
|
}.bind(this));
|
|
|
|
};
|
|
|
|
|
2017-07-05 17:59:13 +00:00
|
|
|
/**
|
|
|
|
* Close the context
|
2017-10-25 21:59:17 +00:00
|
|
|
* @return {Promise}
|
2017-07-05 17:59:13 +00:00
|
|
|
*/
|
|
|
|
Tone.OfflineContext.prototype.close = function(){
|
|
|
|
this._context = null;
|
2017-10-25 21:59:17 +00:00
|
|
|
return Promise.resolve();
|
2017-07-05 17:59:13 +00:00
|
|
|
};
|
|
|
|
|
2017-02-19 00:48:15 +00:00
|
|
|
return Tone.OfflineContext;
|
2017-10-25 21:59:17 +00:00
|
|
|
});
|