Skip to content

Commit 8e0883d

Browse files
committed
Fix InsertSubmixSorted
1 parent 0dfc942 commit 8e0883d

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/FAudio_internal.c

+33-5
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,41 @@ void FAudio_INTERNAL_InsertSubmixSorted(
255255
else
256256
{
257257
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)
260261
{
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+
}
262292
}
263-
newEntry->next = latest->next;
264-
latest->next = newEntry;
265293
}
266294
FAudio_PlatformUnlockMutex(lock);
267295
}

0 commit comments

Comments
 (0)