Skip to content

Commit

Permalink
removing source when sample has finished
Browse files Browse the repository at this point in the history
prevents error where the BufferSource.stop is attempted after the source has already finished
  • Loading branch information
tambien committed Jul 29, 2018
1 parent 99de329 commit e52ce0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
23 changes: 14 additions & 9 deletions Tone/instrument/Sampler.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,15 @@ define(["Tone/core/Tone", "Tone/instrument/Instrument", "Tone/core/Buffers", "To
if (!Tone.isArray(this._activeSources[midi])){
this._activeSources[midi] = [];
}
this._activeSources[midi].push({
note : midi,
source : source
});
this._activeSources[midi].push(source);

//remove it when it's done
source.onended = function(){
var index = this._activeSources[midi].indexOf(source);
if (index !== -1){
this._activeSources[midi].splice(index, 1);
}
}.bind(this);
}
}
return this;
Expand All @@ -160,7 +165,7 @@ define(["Tone/core/Tone", "Tone/instrument/Instrument", "Tone/core/Buffers", "To
var midi = Tone.Frequency(notes[i]).toMidi();
// find the note
if (this._activeSources[midi] && this._activeSources[midi].length){
var source = this._activeSources[midi].shift().source;
var source = this._activeSources[midi].shift();
time = this.toSeconds(time);
source.stop(time);
}
Expand All @@ -179,8 +184,8 @@ define(["Tone/core/Tone", "Tone/instrument/Instrument", "Tone/core/Buffers", "To
for (var note in this._activeSources){
var sources = this._activeSources[note];
while (sources.length){
var source = sources.shift().source;
source.stop(time + this.release, this.release);
var source = sources.shift();
source.stop(time);
}
}
return this;
Expand Down Expand Up @@ -272,8 +277,8 @@ define(["Tone/core/Tone", "Tone/instrument/Instrument", "Tone/core/Buffers", "To
this._buffers.dispose();
this._buffers = null;
for (var midi in this._activeSources){
this._activeSources[midi].forEach(function(event){
event.source.dispose();
this._activeSources[midi].forEach(function(source){
source.dispose();
});
}
this._activeSources = null;
Expand Down
20 changes: 18 additions & 2 deletions test/instrument/Sampler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
define(["Tone/instrument/Sampler", "helper/Basic", "helper/InstrumentTests",
"Tone/core/Buffer", "helper/Offline", "helper/CompareToFile"],
function(Sampler, Basic, InstrumentTest, Buffer, Offline, CompareToFile){
"Tone/core/Buffer", "helper/Offline", "helper/CompareToFile", "helper/Test"],
function(Sampler, Basic, InstrumentTest, Buffer, Offline, CompareToFile, Test){

describe("Sampler", function(){

Expand Down Expand Up @@ -122,6 +122,22 @@ function(Sampler, Basic, InstrumentTest, Buffer, Offline, CompareToFile){
});
});

it("can triggerRelease after the buffer has already stopped", function(){
return Offline(function(){
var sampler = new Sampler({
"A4" : A4_buffer
}, {
release : 0
}).toMaster();
sampler.triggerAttack("A4", 0);
return Test.atTime(A4_buffer.duration + 0.01, function(){
sampler.triggerRelease("A4");
});
}, A4_buffer.duration + 0.1).then(function(buffer){
// expect(buffer.getLastSoundTime()).to.be.closeTo(0.2, 0.01);
});
});

it("can release multiple notes", function(){
return Offline(function(){
var sampler = new Sampler({
Expand Down

0 comments on commit e52ce0d

Please sign in to comment.