moving global AudioContext reference to Tone.Context

and also making it an unwrapped AudioContext
This commit is contained in:
tambien 2018-08-13 22:41:22 -04:00
parent a66aefbf8b
commit 2dc10c7432
3 changed files with 54 additions and 26 deletions

View file

@ -11,13 +11,22 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/core/Timeline", "Tone/shim/
var options = Tone.defaults(arguments, ["context"], Tone.Context);
//make sure there is an underlying AudioContext
if (!window.TONE_AUDIO_CONTEXT){
window.TONE_AUDIO_CONTEXT = new window.AudioContext();
}
if (!options.context){
options.context = new window.AudioContext();
options.context = window.TONE_AUDIO_CONTEXT;
if (!options.context){
throw new Error("could not create AudioContext. Possibly too many AudioContexts running already.");
}
}
this._context = options.context;
//make sure it's not an AudioContext wrapper
while (this._context.rawContext){
this._context = this._context.rawContext;
}
// extend all of the methods
for (var prop in this._context){
this._defineProperty(this._context, prop);
@ -169,7 +178,11 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/core/Timeline", "Tone/shim/
* @return {Promise}
*/
Tone.Context.prototype.close = function(){
return this._context.close().then(function(){
var closePromise = Promise.resolve();
if (this.rawContext !== window.TONE_AUDIO_CONTEXT){
closePromise = this.rawContext.close();
}
return closePromise.then(function(){
Tone.Context.emit("close", this);
}.bind(this));
};
@ -261,6 +274,19 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/core/Timeline", "Tone/shim/
}
});
/**
* The unwrapped AudioContext.
* @type {AudioContext}
* @memberOf Tone.Context#
* @name rawContext
* @readOnly
*/
Object.defineProperty(Tone.Context.prototype, "rawContext", {
"get" : function(){
return this._context;
}
});
/**
* What the source of the clock is, either "worker" (Web Worker [default]),
* "timeout" (setTimeout), or "offline" (none).
@ -570,10 +596,8 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/core/Timeline", "Tone/shim/
});
// set the audio context initially, and if one is not already created
if (Tone.supported){
if (!Tone.initialized){
Tone.context = new Tone.Context();
}
if (Tone.supported && !Tone.initialized){
Tone.context = new Tone.Context();
// log on first initialization
// allow optional silencing of this log

View file

@ -619,6 +619,13 @@ define(function(){
// CONTEXT
///////////////////////////////////////////////////////////////////////////
/**
* The shared AudioContext
* @type {Tone.Context}
* @private
*/
Tone._audioContext = null;
/**
* A static pointer to the audio context accessible as Tone.context.
* @type {Tone.Context}
@ -627,16 +634,16 @@ define(function(){
*/
Object.defineProperty(Tone, "context", {
get : function(){
return window.TONE_AUDIO_CONTEXT;
return Tone._audioContext;
},
set : function(context){
if (Tone.Context && context instanceof Tone.Context){
window.TONE_AUDIO_CONTEXT = context;
Tone._audioContext = context;
} else {
window.TONE_AUDIO_CONTEXT = new Tone.Context(context);
Tone._audioContext = new Tone.Context(context);
}
//initialize the new audio context
Tone.Context.emit("init", window.TONE_AUDIO_CONTEXT);
Tone.Context.emit("init", Tone._audioContext);
}
});
@ -723,7 +730,7 @@ define(function(){
*/
Object.defineProperty(Tone, "initialized", {
get : function(){
return Boolean(window.TONE_AUDIO_CONTEXT);
return Boolean(Tone.context);
}
});

View file

@ -36,6 +36,12 @@ define(["helper/Test", "Tone/core/Context", "Tone/core/Tone", "helper/Offline",
});
});
it("has a rawContext", function(){
var ctx = new Context();
expect(ctx.rawContext).is.instanceOf(window.AudioContext);
return ctx.dispose();
});
it("'dispose' returns a promise which resolves", function(){
var ctx = new Context();
var promise = ctx.dispose();
@ -156,26 +162,17 @@ define(["helper/Test", "Tone/core/Context", "Tone/core/Tone", "helper/Offline",
context("Tone", function(){
var orignContext = Tone.context;
afterEach(function(){
if (Tone.context.state !== "closed" && Tone.context !== orignContext){
//reset the context
return Tone.context.dispose().then(function(){
Tone.context = new Context();
});
}
Tone.context = orignContext;
});
it("has a context", function(){
expect(Tone.context).to.exist;
expect(Tone.context).to.be.instanceOf(Context);
});
it("can set a new context", function(){
var originalContext = Tone.context;
Tone.context = new Context();
return Tone.context.dispose();
return Tone.context.dispose().then(function(){
Tone.context = originalContext;
});
});
it("has a consistent context after offline rendering", function(){
@ -223,14 +220,14 @@ define(["helper/Test", "Tone/core/Context", "Tone/core/Tone", "helper/Offline",
return LoadHTML(baseUrl + "multiple_instances.html");
});
/*it("Transport and Master instance is the same after running Tone.Offline", function(){
});
/*it("Transport and Master instance is the same after running Tone.Offline", function(){
var baseUrl = "../test/html/";
if (window.__karma__){
baseUrl = "/base/test/html/";
}
return LoadHTML(baseUrl + "same_transport.html");
});*/
});
context("get/set", function(){