Skip to content

Commit 849a4f6

Browse files
authored
Merge pull request #7118 from Goober5000/reuse_sound_handles
reuse sexp audio handles
2 parents a06da31 + 8c4849a commit 849a4f6

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

code/parse/sexp.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14121,8 +14121,30 @@ void sexp_load_music(const char *filename, int type = -1, int sexp_var = -1)
1412114121
int index;
1412214122
if (sexp_var >= 0)
1412314123
{
14124-
index = static_cast<int>(Sexp_music_handles.size());
14125-
Sexp_music_handles.push_back(-1);
14124+
// if any handle has expired, it can be closed; and any closed or expired index can be reused
14125+
// (note that handles are not unique across an entire mission; any variables pointing to handles
14126+
// that have been closed will see those handles reused for new sounds)
14127+
int found_i = -1, n = sz2i(Sexp_music_handles.size());
14128+
for (int i = 1; i < n; ++i) // skip the default handle
14129+
{
14130+
int this_handle = Sexp_music_handles[i];
14131+
14132+
if ((this_handle < 0) || (!audiostream_is_playing(this_handle) && !audiostream_is_paused(this_handle)))
14133+
{
14134+
audiostream_close_file(this_handle, false);
14135+
found_i = i;
14136+
break;
14137+
}
14138+
}
14139+
14140+
// either reuse an index or choose a new index
14141+
if (found_i >= 0)
14142+
index = found_i;
14143+
else
14144+
{
14145+
index = static_cast<int>(Sexp_music_handles.size());
14146+
Sexp_music_handles.push_back(-1);
14147+
}
1412614148
}
1412714149
// otherwise we'll be reusing the default handle, so close anything that's already playing
1412814150
else
@@ -14133,6 +14155,8 @@ void sexp_load_music(const char *filename, int type = -1, int sexp_var = -1)
1413314155

1413414156
// open the stream and save the handle in our list
1413514157
Sexp_music_handles[index] = audiostream_open(filename, type);
14158+
if (Sexp_music_handles[index] < 0)
14159+
Warning(LOCATION, "In sexp_load_music, could not create audio handle for '%s'! You might be trying to play too many sounds at once.", filename);
1413614160

1413714161
// if we have a variable, save it there too
1413814162
if (sexp_var >= 0)
@@ -39154,7 +39178,8 @@ SCP_vector<sexp_help_struct> Sexp_help = {
3915439178
"\t2: Enter a non-zero number to loop. default is off (optional).\r\n"
3915539179
"\t3: Enter a non-zero number to use environment effects. default is off (optional).\r\n"
3915639180
"\t4: Numeric variable in which to store the music handle (optional). If no variable is specified, the 'default' handle is used. "
39157-
"Only one 'default' track can be played at a time, but multiple variable-managed tracks can be played.\r\n"
39181+
"Only one 'default' track can be played at a time, but multiple variable-managed tracks can be played. NOTE: Handles are not globally unique. If a sound "
39182+
"finishes playing, its handle may be reused for another sound played later in the mission.\r\n"
3915839183
},
3915939184

3916039185
// Goober5000

0 commit comments

Comments
 (0)