@@ -255,13 +255,41 @@ void FAudio_INTERNAL_InsertSubmixSorted(
255
255
else
256
256
{
257
257
latest = * start ;
258
- while ( latest -> next != NULL &&
259
- ((FAudioSubmixVoice * ) latest -> next -> entry )-> mix .processingStage < toAdd -> mix .processingStage )
258
+
259
+ /* Special case if the new stage is lower than everyone else */
260
+ if (toAdd -> mix .processingStage < ((FAudioSubmixVoice * ) latest -> entry )-> mix .processingStage )
260
261
{
261
- latest = latest -> next ;
262
+ newEntry -> next = latest ;
263
+ * start = newEntry ;
264
+ }
265
+ else
266
+ {
267
+ /* If we got here, we know that the new stage is
268
+ * _at least_ as high as the first submix in the list.
269
+ *
270
+ * Each loop iteration checks to see if the new stage
271
+ * is smaller than `latest->next`, meaning it fits
272
+ * between `latest` and `latest->next`.
273
+ */
274
+ while (latest -> next != NULL )
275
+ {
276
+ if (toAdd -> mix .processingStage < ((FAudioSubmixVoice * ) latest -> next -> entry )-> mix .processingStage )
277
+ {
278
+ newEntry -> next = latest -> next ;
279
+ latest -> next = newEntry ;
280
+ break ;
281
+ }
282
+ latest = latest -> next ;
283
+ }
284
+ /* If newEntry didn't get a `next` value, that means
285
+ * it didn't fall in between any stages and `latest`
286
+ * is the last entry in the list. Add it to the end!
287
+ */
288
+ if (newEntry -> next == NULL )
289
+ {
290
+ latest -> next = newEntry ;
291
+ }
262
292
}
263
- newEntry -> next = latest -> next ;
264
- latest -> next = newEntry ;
265
293
}
266
294
FAudio_PlatformUnlockMutex (lock );
267
295
}
0 commit comments