Skip to content

Commit f888ad3

Browse files
committed
added array type conversion supp. to RingBuffer push/pull function
1 parent cc574f1 commit f888ad3

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

src/modules/buffer-switch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//import { SampleSizeException } from './shared/common.js';
33
//import { RingBuffer } from './shared/ring-buffer.min.js';
44
//import { FirFilterResampler } from './shared/fir-filter-resampler.min.js';
5-
class RingBuffer{constructor(a,b,c){this._readIndex=0,this._writeIndex=0,this._framesAvailable=0,this._channelCount=b,this._length=a,this._channelData=[];for(let d=0;d<this._channelCount;++d)this._channelData[d]="Uint16"==c?new Uint16Array(a):"Int16"==c?new Int16Array(a):"Uint8"==c?new Uint8Array(a):"Int8"==c?new Int8Array(a):new Float32Array(a)}get framesAvailable(){return this._framesAvailable}push(a){let b=a[0].length;for(let c,d=0;d<b;++d){c=(this._writeIndex+d)%this._length;for(let b=0;b<this._channelCount;++b)this._channelData[b][c]=a[b][d]}this._writeIndex+=b,this._writeIndex>=this._length&&(this._writeIndex-=this._length),this._framesAvailable+=b,this._framesAvailable>this._length&&(this._framesAvailable=this._length)}pull(a){if(0!==this._framesAvailable){let b=a[0].length;for(let c,d=0;d<b;++d){c=(this._readIndex+d)%this._length;for(let b=0;b<this._channelCount;++b)a[b][d]=this._channelData[b][c]}this._readIndex+=b,this._readIndex>=this._length&&(this._readIndex-=this._length),this._framesAvailable-=b,0>this._framesAvailable&&(this._framesAvailable=0)}}};
5+
class RingBuffer{constructor(a,b,c){this._readIndex=0,this._writeIndex=0,this._framesAvailable=0,this._channelCount=b,this._length=a,this._channelData=[];for(let d=0;d<this._channelCount;++d)this._channelData[d]="Uint16"==c?new Uint16Array(a):"Int16"==c?new Int16Array(a):"Uint8"==c?new Uint8Array(a):"Int8"==c?new Int8Array(a):new Float32Array(a)}get framesAvailable(){return this._framesAvailable}push(a,b){let c=a[0].length,d=b||function(a,b,c){return a[b][c]};for(let e,f=0;f<c;++f){e=(this._writeIndex+f)%this._length;for(let b=0;b<this._channelCount;++b)this._channelData[b][e]=d(a,b,f)}this._writeIndex+=c,this._writeIndex>=this._length&&(this._writeIndex-=this._length),this._framesAvailable+=c,this._framesAvailable>this._length&&(this._framesAvailable=this._length)}pull(a,b){if(0===this._framesAvailable)return;let c=a[0].length,d=this,e=b||function(a,b,c){return a[b][c]};for(let d,f=0;f<c;++f){d=(this._readIndex+f)%this._length;for(let b=0;b<this._channelCount;++b)a[b][f]=e(this._channelData,b,d)}this._readIndex+=c,this._readIndex>=this._length&&(this._readIndex-=this._length),this._framesAvailable-=c,0>this._framesAvailable&&(this._framesAvailable=0)}};
66
//var FirFilterResampler=function(){function a(){this.process=function(a){var b=new Int16Array(a.length);for(let c=0;c<a.length;c++)b[c]=a[c]*(0>a[c]?32768:32767);return b}}function b(a,b,c){function d(a){if(0===a)return 1;var b=Math.PI*a;return Math.sin(b)/b}var e=new Float32Array(0),f=a/b,g=127<=c?127:c%2?c:c-1;this.filterArray=function(b,c,e){if(!e||0>e)return new Float32Array(0);if(0==e%2)throw Error("Filter length must be odd");var f=new Float32Array(e),g=0;for(let h,i=0;i<e;i++)h=d(c/b*(i-(e-1)/2)),f[i]=h,g+=h;for(let d=0;d<e;d++)f[d]/=g;return f}(a,b,g),this.process=function(a){var b=new Float32Array(e.length+a.length);b.set(e,0),b.set(a,e.length);var c=Math.ceil((b.length-this.filterArray.length)/f),d=new Int16Array(c);if(this.filterArray.length)for(let a=0;a<c;a++){let c=Math.round(f*a),e=0;for(let a=0;a<this.filterArray.length;a++)e+=b[c+a]*this.filterArray[a];d[a]=e*(0>e?32768:32767)}else for(let a=0;a<c;a++){let c=Math.round(f*a),e=b[c];d[a]=e*(0>e?32768:32767)}var g=Math.round(f*c);return e=g<b.length?b.subarray(g):new Float32Array(0),d}}return{getProcessor:function(c,d,e){if(c===d)return new a;if(c>d)return new b(c,d,e);throw{name:"NotSupportedError",message:"No processor for upsampling found!"}}}};
77

88
//TODO: add FirFilterResampler + option (see: test-resampler-static.html), add float32toInt16 option

src/modules/example-processor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//TODO: not yet supported by FF :-( (Dec 2020)
22
//import { SampleSizeException } from './shared/common.js';
33
//import { RingBuffer } from './shared/ring-buffer.js';
4-
class RingBuffer{constructor(a,b,c){this._readIndex=0,this._writeIndex=0,this._framesAvailable=0,this._channelCount=b,this._length=a,this._channelData=[];for(let d=0;d<this._channelCount;++d)this._channelData[d]="Uint16"==c?new Uint16Array(a):"Int16"==c?new Int16Array(a):"Uint8"==c?new Uint8Array(a):"Int8"==c?new Int8Array(a):new Float32Array(a)}get framesAvailable(){return this._framesAvailable}push(a){let b=a[0].length;for(let c,d=0;d<b;++d){c=(this._writeIndex+d)%this._length;for(let b=0;b<this._channelCount;++b)this._channelData[b][c]=a[b][d]}this._writeIndex+=b,this._writeIndex>=this._length&&(this._writeIndex-=this._length),this._framesAvailable+=b,this._framesAvailable>this._length&&(this._framesAvailable=this._length)}pull(a){if(0!==this._framesAvailable){let b=a[0].length;for(let c,d=0;d<b;++d){c=(this._readIndex+d)%this._length;for(let b=0;b<this._channelCount;++b)a[b][d]=this._channelData[b][c]}this._readIndex+=b,this._readIndex>=this._length&&(this._readIndex-=this._length),this._framesAvailable-=b,0>this._framesAvailable&&(this._framesAvailable=0)}}};
4+
class RingBuffer{constructor(a,b,c){this._readIndex=0,this._writeIndex=0,this._framesAvailable=0,this._channelCount=b,this._length=a,this._channelData=[];for(let d=0;d<this._channelCount;++d)this._channelData[d]="Uint16"==c?new Uint16Array(a):"Int16"==c?new Int16Array(a):"Uint8"==c?new Uint8Array(a):"Int8"==c?new Int8Array(a):new Float32Array(a)}get framesAvailable(){return this._framesAvailable}push(a,b){let c=a[0].length,d=b||function(a,b,c){return a[b][c]};for(let e,f=0;f<c;++f){e=(this._writeIndex+f)%this._length;for(let b=0;b<this._channelCount;++b)this._channelData[b][e]=d(a,b,f)}this._writeIndex+=c,this._writeIndex>=this._length&&(this._writeIndex-=this._length),this._framesAvailable+=c,this._framesAvailable>this._length&&(this._framesAvailable=this._length)}pull(a,b){if(0===this._framesAvailable)return;let c=a[0].length,d=this,e=b||function(a,b,c){return a[b][c]};for(let d,f=0;f<c;++f){d=(this._readIndex+f)%this._length;for(let b=0;b<this._channelCount;++b)a[b][f]=e(this._channelData,b,d)}this._readIndex+=c,this._readIndex>=this._length&&(this._readIndex-=this._length),this._framesAvailable-=c,0>this._framesAvailable&&(this._framesAvailable=0)}};
55

66
var someExampleVar;
77

src/modules/speex-resample-switch.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/resources/ring-buffer.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,21 @@ class RingBuffer {
4848
* Push a sequence of Arrays to buffer.
4949
*
5050
* @param {array} arraySequence A sequence of Arrays.
51+
* @param {function} customTransform A function of (array, channel, index) to transform the input during push (or null).
5152
*/
52-
push(arraySequence) {
53+
push(arraySequence, customTransform) {
5354
// The channel count of arraySequence and the length of each channel must
5455
// match with this buffer obejct.
5556

5657
// Transfer data from the |arraySequence| storage to the internal buffer.
5758
let sourceLength = arraySequence[0].length;
59+
let transform = customTransform || function(thisArray, channel, i){
60+
return thisArray[channel][i];
61+
}
5862
for (let i = 0; i < sourceLength; ++i) {
5963
let writeIndex = (this._writeIndex + i) % this._length;
6064
for (let channel = 0; channel < this._channelCount; ++channel) {
61-
this._channelData[channel][writeIndex] = arraySequence[channel][i];
65+
this._channelData[channel][writeIndex] = transform(arraySequence, channel, i);
6266
}
6367
}
6468

@@ -78,8 +82,9 @@ class RingBuffer {
7882
* Pull data out of buffer and fill a given sequence of Arrays.
7983
*
8084
* @param {array} arraySequence An array of Arrays.
85+
* @param {function} customTransform A function of (array, channel, index) to transform the output during pull (or null).
8186
*/
82-
pull(arraySequence) {
87+
pull(arraySequence, customTransform) {
8388
// The channel count of arraySequence and the length of each channel must
8489
// match with this buffer obejct.
8590

@@ -89,12 +94,16 @@ class RingBuffer {
8994
}
9095

9196
let destinationLength = arraySequence[0].length;
97+
let that = this;
98+
let transform = customTransform || function(thisArray, channel, i){
99+
return thisArray[channel][i];
100+
}
92101

93102
// Transfer data from the internal buffer to the |arraySequence| storage.
94103
for (let i = 0; i < destinationLength; ++i) {
95104
let readIndex = (this._readIndex + i) % this._length;
96105
for (let channel = 0; channel < this._channelCount; ++channel) {
97-
arraySequence[channel][i] = this._channelData[channel][readIndex];
106+
arraySequence[channel][i] = transform(this._channelData, channel, readIndex);
98107
}
99108
}
100109

0 commit comments

Comments
 (0)