2015-08-21 21:03:14 +00:00
|
|
|
define(["Tone/core/Tone"], function (Tone) {
|
2015-08-16 18:23:40 +00:00
|
|
|
|
|
|
|
//hold onto the current context
|
|
|
|
var onlineContext = Tone.context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OFFLINE TESTING
|
|
|
|
*/
|
2015-08-24 21:33:28 +00:00
|
|
|
var Offline = function(duration, channels){
|
2015-08-28 03:02:29 +00:00
|
|
|
duration = duration || 0.7;
|
2015-08-16 18:23:40 +00:00
|
|
|
var sampleRate = 44100;
|
|
|
|
//dummy functions
|
|
|
|
this._before = Tone.noOp;
|
|
|
|
this._after = Tone.noOp;
|
|
|
|
this._test = Tone.noOp;
|
2015-08-21 21:03:14 +00:00
|
|
|
channels = channels || 1;
|
2015-08-28 03:02:29 +00:00
|
|
|
duration = Math.floor(duration * sampleRate);
|
2015-08-16 18:23:40 +00:00
|
|
|
//offline rendering context
|
2015-08-28 03:02:29 +00:00
|
|
|
this.context = new OfflineAudioContext(channels, duration, sampleRate);
|
2015-08-16 18:23:40 +00:00
|
|
|
this.context.oncomplete = function(e){
|
2015-08-24 15:13:56 +00:00
|
|
|
|
2015-08-28 03:02:29 +00:00
|
|
|
for (var i = 0; i < duration; i++){
|
2015-08-24 15:13:56 +00:00
|
|
|
|
|
|
|
var ret = [];
|
|
|
|
for (var channel = 0; channel < channels; channel++){
|
|
|
|
var buffer = e.renderedBuffer.getChannelData(channel);
|
|
|
|
ret[channel] = buffer[i];
|
2015-08-16 18:23:40 +00:00
|
|
|
}
|
2015-08-24 15:13:56 +00:00
|
|
|
if (channels === 1) {
|
|
|
|
ret = ret[0];
|
2015-08-16 18:23:40 +00:00
|
|
|
}
|
2015-10-09 14:57:37 +00:00
|
|
|
try {
|
|
|
|
this._test(ret, i / sampleRate);
|
|
|
|
} catch (e){
|
|
|
|
//reset the old context
|
|
|
|
Tone.setContext(onlineContext);
|
|
|
|
throw new Error(e);
|
|
|
|
}
|
2015-08-16 18:23:40 +00:00
|
|
|
}
|
|
|
|
this._after();
|
|
|
|
//reset the old context
|
|
|
|
Tone.setContext(onlineContext);
|
|
|
|
}.bind(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Offline.prototype.run = function(){
|
|
|
|
Tone.setContext(this.context);
|
|
|
|
this._before(this.context.destination);
|
|
|
|
this.context.startRendering();
|
2015-08-28 22:33:50 +00:00
|
|
|
return this;
|
2015-08-16 18:23:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Offline.prototype.before = function(cb){
|
|
|
|
this._before = cb;
|
2015-08-28 22:33:50 +00:00
|
|
|
return this;
|
2015-08-16 18:23:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Offline.prototype.after = function(cb){
|
|
|
|
this._after = cb;
|
2015-08-28 22:33:50 +00:00
|
|
|
return this;
|
2015-08-16 18:23:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Offline.prototype.test = function(cb){
|
|
|
|
this._test = cb;
|
2015-08-28 22:33:50 +00:00
|
|
|
return this;
|
2015-08-16 18:23:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return Offline;
|
|
|
|
});
|