ability to get buffer values at a given time

This commit is contained in:
Yotam Mann 2017-08-27 19:41:47 -04:00
parent b9cef1feb8
commit f687bb8a11

View file

@ -52,6 +52,20 @@ define(["Tone/core/Tone"], function (Tone) {
} }
}; };
//returns the value at the given time
buffer.getValueAtTime = function(time){
var ret = [];
var sample = Math.round(time * buffer.context.sampleRate);
for (let i = 0; i < buffer.numberOfChannels; i++){
ret[i] = buffer.getChannelData(i)[sample];
}
if (ret.length === 1){
return ret[0]
} else {
return ret;
}
};
//return the time when the buffer is silent to the remainer of the buffer //return the time when the buffer is silent to the remainer of the buffer
buffer.getLastSoundTime = function(channelNum){ buffer.getLastSoundTime = function(channelNum){
if (Tone.isUndef(channelNum)){ if (Tone.isUndef(channelNum)){
@ -83,7 +97,7 @@ define(["Tone/core/Tone"], function (Tone) {
return ret; return ret;
} }
} }
} else { } else {
var array = buffer.toArray(); var array = buffer.toArray();
for (i = start; i < end; i++){ for (i = start; i < end; i++){
@ -126,7 +140,7 @@ define(["Tone/core/Tone"], function (Tone) {
var val; var val;
buffer.toMono().forEach(function(sample){ buffer.toMono().forEach(function(sample){
if (typeof val === "undefined"){ if (typeof val === "undefined"){
val = sample; val = sample;
} else if (Math.abs(val - sample) > 0.0001){ } else if (Math.abs(val - sample) > 0.0001){
throw new Error("multiple values in buffer"); throw new Error("multiple values in buffer");
} }
@ -134,4 +148,4 @@ define(["Tone/core/Tone"], function (Tone) {
return val; return val;
}; };
}; };
}); });