@@ -35,36 +35,44 @@ function createWasmAudioWorkletProcessor(audioParams) {
35
35
this . samplesPerChannel = opts . samplesPerChannel ;
36
36
this . bytesPerChannel = this . samplesPerChannel * { { { getNativeTypeSize ( 'float' ) } } } ;
37
37
38
- // Create up-front as many typed views for marshalling the output data as
39
- // may be required (with an arbitrary maximum of 64, for the case where a
40
- // multi-MB stack is passed), allocated at the *top* of the worklet's
41
- // stack (and whose addresses are fixed). The 'minimum alloc' firstly
42
- // stops STACK_OVERFLOW_CHECK failing (since the stack will be full, and
43
- // 16 being the minimum allocation size due to alignments) and leaves room
44
- // for a single AudioSampleFrame as a minumum.
38
+ // Creates the output views (see createOutputViews() docs)
45
39
this . maxBuffers = Math . min ( ( ( wwParams . stackSize - /*minimum alloc*/ 16 ) / this . bytesPerChannel ) | 0 , /*sensible limit*/ 64 ) ;
40
+ this . outputViews = [ ] ;
46
41
#if ASSERTIONS
47
42
console . assert ( this . maxBuffers > 0 , `AudioWorklet needs more stack allocating (at least ${ this . bytesPerChannel } )` ) ;
48
43
#endif
44
+ this . createOutputViews ( ) ;
45
+
46
+ #if ASSERTIONS
47
+ // Explicitly verify this later in process()
48
+ this . ctorOldStackPtr = stackSave ( ) ;
49
+ #endif
50
+ }
51
+
52
+ /**
53
+ * Create up-front as many typed views for marshalling the output data as
54
+ * may be required (with an arbitrary maximum of 64, for the case where a
55
+ * multi-MB stack is passed), allocated at the *top* of the worklet's stack
56
+ * (and whose addresses are fixed). The 'minimum alloc' firstly stops
57
+ * STACK_OVERFLOW_CHECK failing (since the stack will be full, and 16 bytes
58
+ * being the minimum allocation size due to alignments) and leaves room for
59
+ * a single AudioSampleFrame as a minumum.
60
+ */
61
+ createOutputViews ( ) {
49
62
// These are still alloc'd to take advantage of the overflow checks, etc.
50
63
var oldStackPtr = stackSave ( ) ;
51
64
var viewDataIdx = { { { getHeapOffset ( 'stackAlloc(this.maxBuffers * this.bytesPerChannel)' , 'float' ) } } } ;
52
65
#if WEBAUDIO_DEBUG
53
66
console . log ( `AudioWorklet creating ${ this . maxBuffers } buffer one-time views (for a stack size of ${ wwParams . stackSize } at address 0x${ ( viewDataIdx * 4 ) . toString ( 16 ) } )` ) ;
54
67
#endif
55
- this . outputViews = [ ] ;
68
+ this . outputViews . length = 0 ;
56
69
for ( var n = this . maxBuffers ; n > 0 ; n -- ) {
57
70
// Added in reverse so the lowest indices are closest to the stack top
58
71
this . outputViews . unshift (
59
72
HEAPF32 . subarray ( viewDataIdx , viewDataIdx += this . samplesPerChannel )
60
73
) ;
61
74
}
62
75
stackRestore ( oldStackPtr ) ;
63
-
64
- #if ASSERTIONS
65
- // Explicitly verify this later in process()
66
- this . ctorOldStackPtr = oldStackPtr ;
67
- #endif
68
76
}
69
77
70
78
static get parameterDescriptors ( ) {
@@ -79,6 +87,9 @@ function createWasmAudioWorkletProcessor(audioParams) {
79
87
* @param {Object } parameters
80
88
*/
81
89
process ( inputList , outputList , parameters ) {
90
+ if ( HEAPF32 . buffer != this . outputViews [ 0 ] . buffer ) {
91
+ this . createOutputViews ( ) ;
92
+ }
82
93
var numInputs = inputList . length ;
83
94
var numOutputs = outputList . length ;
84
95
0 commit comments