Skip to content

Commit

Permalink
voices are chosen based on minimum value after the given time
Browse files Browse the repository at this point in the history
fixes #415
  • Loading branch information
tambien committed Dec 26, 2018
1 parent fd62e31 commit 72536e5
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions Tone/instrument/PolySynth.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,22 @@ define(["../core/Tone", "../instrument/Synth", "../source/Source"], function(Ton
return sameNote;
}

//find available notes
var availableVoices = this.voices.filter(function(voice){
//check that it's not ascending in energy (in attack phase)
var levelNow = voice.getLevelAtTime(time);
var nextLevel = voice.getLevelAtTime(time + this.sampleTime);
//and it's near silent
return levelNow >= nextLevel && voice.getLevelAtTime(time) < 1e-5;
}.bind(this));
var sortedVoices = this.voices.slice().sort(function(a, b){
//check that it's not scheduled in the future
var aLevel = a.getLevelAtTime(time + this.blockTime);
var bLevel = b.getLevelAtTime(time + this.blockTime);

if (availableVoices.length){
//return the first one
return availableVoices[0];
}
//otherwise take the one with the lowest energy
var closestVoice = this.voices[0];
this.voices.forEach(function(voice){
if (voice.getLevelAtTime(time) < closestVoice.getLevelAtTime(time)){
closestVoice = voice;
var silenceThresh = 1e-5;
if (aLevel < silenceThresh){
aLevel = 0;
}
});
return closestVoice;
if (bLevel < silenceThresh){
bLevel = 0;
}
return aLevel - bLevel;
}.bind(this));

return sortedVoices[0];
};

/**
Expand Down

0 comments on commit 72536e5

Please sign in to comment.