@@ -118,18 +118,34 @@ class FreeQueue {
118
118
}
119
119
let nextWrite = currentWrite + blockLength ;
120
120
if ( this . bufferLength < nextWrite ) {
121
+ // Handle wrap-around: split data into two chunks
121
122
nextWrite -= this . bufferLength ;
123
+ const firstChunkLength = this . bufferLength - currentWrite ;
124
+ const secondChunkLength = nextWrite ;
125
+
122
126
for ( let channel = 0 ; channel < this . channelCount ; channel ++ ) {
123
- const blockA = this . channelData [ channel ] . subarray ( currentWrite ) ;
124
- const blockB = this . channelData [ channel ] . subarray ( 0 , nextWrite ) ;
125
- blockA . set ( input [ channel ] . subarray ( 0 , blockA . length ) ) ;
126
- blockB . set ( input [ channel ] . subarray ( blockA . length ) ) ;
127
+ const channelData = this . channelData [ channel ] ;
128
+ const inputChannel = input [ channel ] ;
129
+
130
+ // Copy first chunk (from currentWrite to end of buffer)
131
+ for ( let i = 0 ; i < firstChunkLength ; i ++ ) {
132
+ channelData [ currentWrite + i ] = inputChannel [ i ] ;
133
+ }
134
+
135
+ // Copy second chunk (from start of buffer to nextWrite)
136
+ for ( let i = 0 ; i < secondChunkLength ; i ++ ) {
137
+ channelData [ i ] = inputChannel [ firstChunkLength + i ] ;
138
+ }
127
139
}
128
140
} else {
141
+ // No wrap-around: simple linear copy
129
142
for ( let channel = 0 ; channel < this . channelCount ; channel ++ ) {
130
- this . channelData [ channel ]
131
- . subarray ( currentWrite , nextWrite )
132
- . set ( input [ channel ] . subarray ( 0 , blockLength ) ) ;
143
+ const channelData = this . channelData [ channel ] ;
144
+ const inputChannel = input [ channel ] ;
145
+
146
+ for ( let i = 0 ; i < blockLength ; i ++ ) {
147
+ channelData [ currentWrite + i ] = inputChannel [ i ] ;
148
+ }
133
149
}
134
150
if ( nextWrite === this . bufferLength ) nextWrite = 0 ;
135
151
}
@@ -154,18 +170,34 @@ class FreeQueue {
154
170
}
155
171
let nextRead = currentRead + blockLength ;
156
172
if ( this . bufferLength < nextRead ) {
173
+ // Handle wrap-around: split data into two chunks
157
174
nextRead -= this . bufferLength ;
175
+ const firstChunkLength = this . bufferLength - currentRead ;
176
+ const secondChunkLength = nextRead ;
177
+
158
178
for ( let channel = 0 ; channel < this . channelCount ; channel ++ ) {
159
- const blockA = this . channelData [ channel ] . subarray ( currentRead ) ;
160
- const blockB = this . channelData [ channel ] . subarray ( 0 , nextRead ) ;
161
- output [ channel ] . set ( blockA ) ;
162
- output [ channel ] . set ( blockB , blockA . length ) ;
179
+ const channelData = this . channelData [ channel ] ;
180
+ const outputChannel = output [ channel ] ;
181
+
182
+ // Copy first chunk (from currentRead to end of buffer)
183
+ for ( let i = 0 ; i < firstChunkLength ; i ++ ) {
184
+ outputChannel [ i ] = channelData [ currentRead + i ] ;
185
+ }
186
+
187
+ // Copy second chunk (from start of buffer to nextRead)
188
+ for ( let i = 0 ; i < secondChunkLength ; i ++ ) {
189
+ outputChannel [ firstChunkLength + i ] = channelData [ i ] ;
190
+ }
163
191
}
164
192
} else {
193
+ // No wrap-around: simple linear copy
165
194
for ( let channel = 0 ; channel < this . channelCount ; ++ channel ) {
166
- output [ channel ] . set (
167
- this . channelData [ channel ] . subarray ( currentRead , nextRead )
168
- ) ;
195
+ const channelData = this . channelData [ channel ] ;
196
+ const outputChannel = output [ channel ] ;
197
+
198
+ for ( let i = 0 ; i < blockLength ; i ++ ) {
199
+ outputChannel [ i ] = channelData [ currentRead + i ] ;
200
+ }
169
201
}
170
202
if ( nextRead === this . bufferLength ) {
171
203
nextRead = 0 ;
0 commit comments