File tree Expand file tree Collapse file tree 3 files changed +23
-5
lines changed Expand file tree Collapse file tree 3 files changed +23
-5
lines changed Original file line number Diff line number Diff line change 1
- // import processor name via preval.require so that it's available as a value at compile time
1
+ // import dependencies via preval.require so that they're available as values at compile time
2
2
const processorNames = preval . require ( './processorNames' ) ;
3
+ const RingBuffer = preval . require ( './ringBuffer' ) . default ;
3
4
4
5
class SoundFileProcessor extends AudioWorkletProcessor {
6
+ constructor ( options ) {
7
+ super ( ) ;
8
+
9
+ const processorOptions = options . processorOptions || { } ;
10
+ this . bufferSize = processorOptions . bufferSize || 256 ;
11
+ this . inputRingBuffer = new RingBuffer ( this . bufferSize , 1 ) ;
12
+ this . inputRingBufferArraySequence = [ new Float32Array ( this . bufferSize ) ] ;
13
+ }
14
+
5
15
process ( inputs ) {
6
16
const input = inputs [ 0 ] ;
7
- const inputChannel = input [ 0 ] ;
8
- const position = inputChannel [ inputChannel . length - 1 ] || 0 ;
17
+ // we only care about the first input channel, because that contains the position data
18
+ this . inputRingBuffer . push ( [ input [ 0 ] ] ) ;
19
+
20
+ if ( this . inputRingBuffer . framesAvailable >= this . bufferSize ) {
21
+ this . inputRingBuffer . pull ( this . inputRingBufferArraySequence ) ;
22
+ const inputChannel = this . inputRingBufferArraySequence [ 0 ] ;
23
+ const position = inputChannel [ inputChannel . length - 1 ] || 0 ;
9
24
10
- this . port . postMessage ( { name : 'position' , position : position } ) ;
25
+ this . port . postMessage ( { name : 'position' , position : position } ) ;
26
+ }
11
27
12
28
return true ;
13
29
}
Original file line number Diff line number Diff line change @@ -1243,7 +1243,9 @@ define(function (require) {
1243
1243
self . _workletNode . disconnect ( ) ;
1244
1244
delete self . _workletNode ;
1245
1245
}
1246
- self . _workletNode = new AudioWorkletNode ( ac , processorNames . soundFileProcessor ) ;
1246
+ self . _workletNode = new AudioWorkletNode ( ac , processorNames . soundFileProcessor , {
1247
+ processorOptions : { bufferSize : 256 }
1248
+ } ) ;
1247
1249
self . _workletNode . port . onmessage = event => {
1248
1250
if ( event . data . name === 'position' ) {
1249
1251
// event.data.position should only be 0 when paused
You can’t perform that action at this time.
0 commit comments