2018-01-03 17:02:41 +00:00
|
|
|
define(["Tone/core/Tone"], function(Tone) {
|
2017-02-20 03:50:39 +00:00
|
|
|
|
|
|
|
//augment the built in functions
|
|
|
|
return function(buffer){
|
|
|
|
|
|
|
|
buffer.isSilent = function(channelNum){
|
2017-04-26 04:27:49 +00:00
|
|
|
if (Tone.isUndef(channelNum)){
|
2017-02-20 03:50:39 +00:00
|
|
|
return buffer.toMono().isSilent(0);
|
2017-02-19 16:54:29 +00:00
|
|
|
} else {
|
|
|
|
var array = buffer.toArray(channelNum);
|
|
|
|
for (var i = 0; i < array.length; i++){
|
|
|
|
if (array[i] !== 0){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-20 03:50:39 +00:00
|
|
|
};
|
2017-02-19 16:54:29 +00:00
|
|
|
|
2017-02-20 04:55:55 +00:00
|
|
|
/**
|
|
|
|
* Get the RMS levels for the buffer, returned as an array
|
|
|
|
* @param {blockSize} [blockSize=512]
|
|
|
|
*/
|
|
|
|
buffer.getRMS = function(blockSize){
|
|
|
|
blockSize = blockSize || 512;
|
|
|
|
var rms = [];
|
|
|
|
var array = buffer.toMono().toArray();
|
|
|
|
for (var j = 0; j < array.length; j++){
|
|
|
|
var sum = 0;
|
|
|
|
if (j >= blockSize){
|
|
|
|
for (var k = j - blockSize; k < j; k++){
|
|
|
|
sum += array[k] * array[k];
|
|
|
|
}
|
|
|
|
rms.push(Math.sqrt(sum / blockSize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rms;
|
|
|
|
};
|
|
|
|
|
2018-01-03 17:02:41 +00:00
|
|
|
//get the rms at the given time
|
|
|
|
buffer.getRmsAtTime = function(time){
|
|
|
|
var rms = this.getRMS();
|
|
|
|
var sample = Math.floor(time * this.context.sampleRate);
|
|
|
|
return rms[sample];
|
|
|
|
};
|
|
|
|
|
2017-02-19 16:54:29 +00:00
|
|
|
//return the time when the buffer is no longer silent
|
2017-02-20 03:50:39 +00:00
|
|
|
buffer.getFirstSoundTime = function(channelNum){
|
2017-04-26 04:27:49 +00:00
|
|
|
if (Tone.isUndef(channelNum)){
|
2017-02-20 03:50:39 +00:00
|
|
|
return buffer.toMono().getFirstSoundTime(0);
|
2017-02-19 16:54:29 +00:00
|
|
|
} else {
|
|
|
|
var array = buffer.toArray(channelNum);
|
|
|
|
for (var i = 0; i < array.length; i++){
|
|
|
|
if (array[i] !== 0){
|
|
|
|
return i / buffer.context.sampleRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2017-02-20 03:50:39 +00:00
|
|
|
};
|
2017-02-19 16:54:29 +00:00
|
|
|
|
2017-08-27 23:41:47 +00:00
|
|
|
//returns the value at the given time
|
|
|
|
buffer.getValueAtTime = function(time){
|
|
|
|
var ret = [];
|
|
|
|
var sample = Math.round(time * buffer.context.sampleRate);
|
2017-10-24 15:57:23 +00:00
|
|
|
for (var i = 0; i < buffer.numberOfChannels; i++){
|
2017-08-27 23:41:47 +00:00
|
|
|
ret[i] = buffer.getChannelData(i)[sample];
|
|
|
|
}
|
|
|
|
if (ret.length === 1){
|
2017-12-30 16:26:29 +00:00
|
|
|
return ret[0];
|
2017-08-27 23:41:47 +00:00
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-20 19:05:11 +00:00
|
|
|
//return the time when the buffer is silent to the remainer of the buffer
|
|
|
|
buffer.getLastSoundTime = function(channelNum){
|
2017-04-26 04:27:49 +00:00
|
|
|
if (Tone.isUndef(channelNum)){
|
2017-02-20 19:05:11 +00:00
|
|
|
return buffer.toMono().getLastSoundTime(0);
|
|
|
|
} else {
|
|
|
|
var array = buffer.toArray(channelNum);
|
|
|
|
for (var i = array.length - 1; i >= 0; i--){
|
|
|
|
if (array[i] !== 0){
|
|
|
|
return i / buffer.context.sampleRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-02-19 16:54:29 +00:00
|
|
|
//stops and returns value if callback does
|
2017-02-20 03:50:39 +00:00
|
|
|
buffer.forEach = function(callback, start, end){
|
|
|
|
var i, ret;
|
|
|
|
start = start || 0;
|
|
|
|
end = end || buffer.duration;
|
|
|
|
start = Math.floor((start / buffer.duration) * buffer.length);
|
|
|
|
end = Math.floor((end / buffer.duration) * buffer.length);
|
2017-02-19 16:54:29 +00:00
|
|
|
if (buffer.numberOfChannels === 2){
|
|
|
|
var arrayL = buffer.toArray(0);
|
|
|
|
var arrayR = buffer.toArray(1);
|
2017-02-20 03:50:39 +00:00
|
|
|
for (i = start; i < end; i++){
|
|
|
|
ret = callback(arrayL[i], arrayR[i], (i/buffer.length) * buffer.duration);
|
2017-02-19 16:54:29 +00:00
|
|
|
if (typeof ret !== "undefined"){
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2017-08-27 23:41:47 +00:00
|
|
|
|
2017-02-19 16:54:29 +00:00
|
|
|
} else {
|
|
|
|
var array = buffer.toArray();
|
2017-02-20 03:50:39 +00:00
|
|
|
for (i = start; i < end; i++){
|
|
|
|
ret = callback(array[i], (i/array.length) * buffer.duration);
|
2017-02-19 16:54:29 +00:00
|
|
|
if (typeof ret !== "undefined"){
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-20 03:50:39 +00:00
|
|
|
};
|
2017-02-20 04:55:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the max value in the buffer
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
buffer.max = function(){
|
|
|
|
var max = -Infinity;
|
|
|
|
buffer.toMono().forEach(function(sample){
|
|
|
|
max = Math.max(sample, max);
|
|
|
|
});
|
|
|
|
return max;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the min value in the buffer
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
buffer.min = function(){
|
|
|
|
var min = Infinity;
|
|
|
|
buffer.toMono().forEach(function(sample){
|
|
|
|
min = Math.min(sample, min);
|
|
|
|
});
|
|
|
|
return min;
|
|
|
|
};
|
2017-02-20 19:05:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the value if there is a single value the entire buffer
|
|
|
|
*/
|
|
|
|
buffer.value = function(){
|
|
|
|
var val;
|
|
|
|
buffer.toMono().forEach(function(sample){
|
|
|
|
if (typeof val === "undefined"){
|
2017-08-27 23:41:47 +00:00
|
|
|
val = sample;
|
2017-02-20 19:05:11 +00:00
|
|
|
} else if (Math.abs(val - sample) > 0.0001){
|
|
|
|
throw new Error("multiple values in buffer");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return val;
|
|
|
|
};
|
2017-02-19 16:54:29 +00:00
|
|
|
};
|
2017-08-27 23:41:47 +00:00
|
|
|
});
|