mirror of
https://github.com/Tonejs/Tone.js
synced 2024-11-16 08:38:00 +00:00
convert the buffer to mono
replaces the existing buffer
This commit is contained in:
parent
4e5ff9d41d
commit
b32f2606e7
1 changed files with 26 additions and 0 deletions
|
@ -244,6 +244,32 @@ define(["Tone/core/Tone", "Tone/core/Emitter", "Tone/type/Type"], function(Tone)
|
|||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sums muliple channels into 1 channel
|
||||
* @param {Number=} channel Optionally only copy a single channel from the array.
|
||||
* @return {Array}
|
||||
*/
|
||||
Tone.Buffer.prototype.toMono = function(chanNum){
|
||||
if (this.isNumber(chanNum)){
|
||||
this.fromArray(this.toArray(chanNum));
|
||||
} else {
|
||||
var outputArray = new Float32Array(this.length);
|
||||
var numChannels = this.numberOfChannels;
|
||||
for (var channel = 0; channel < numChannels; channel++){
|
||||
var channelArray = this.toArray(channel);
|
||||
for (var i = 0; i < channelArray.length; i++){
|
||||
outputArray[i] += channelArray[i];
|
||||
}
|
||||
}
|
||||
//divide by the number of channels
|
||||
outputArray = outputArray.map(function(sample){
|
||||
return sample / numChannels;
|
||||
});
|
||||
this.fromArray(outputArray);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the buffer as an array. Single channel buffers will return a 1-dimensional
|
||||
* Float32Array, and multichannel buffers will return multidimensional arrays.
|
||||
|
|
Loading…
Reference in a new issue