Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load and unload music files as needed #1135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions desktop_version/src/Music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,20 @@ float SoundTrack::volume = 0.0f;
class MusicTrack
{
public:
MusicTrack(SDL_RWops *rw)
MusicTrack(SDL_RWops *rw, bool autoload = true)
{
SDL_zerop(this);
this->rw = rw;
this->autoload = autoload;
if (autoload)
{
Load();
}
}

void Load(void)
{
loaded = true;
read_buf = (Uint8*) SDL_malloc(rw->size(rw));
SDL_RWread(rw, read_buf, rw->size(rw), 1);
int err;
Expand All @@ -385,7 +396,7 @@ class MusicTrack
{
vlog_error("Unable to create Vorbis handle, error %d", err);
VVV_free(read_buf);
goto end;
return;
}
vorbis_info = stb_vorbis_get_info(vorbis);
format.wFormatTag = FAUDIO_FORMAT_IEEE_FLOAT;
Expand All @@ -407,25 +418,54 @@ class MusicTrack
vorbis_comment = stb_vorbis_get_comment(vorbis);
parseComments(this, vorbis_comment.comment_list, vorbis_comment.comment_list_length);
valid = true;

end:
SDL_RWclose(rw);
}

void Dispose(void)
void Unload(void)
{
if (!loaded)
{
return;
}
VVV_freefunc(stb_vorbis_close, vorbis);
VVV_free(read_buf);
VVV_free(decoded_buf_playing);
VVV_free(decoded_buf_reserve);
/* The song might get loaded again, so reset the read cursor */
SDL_RWseek(rw, 0, RW_SEEK_SET);
loaded = false;
}

void Dispose(void)
{
if (loaded)
{
Unload();
}
if (!IsHalted())
{
VVV_freefunc(FAudioVoice_DestroyVoice, musicVoice);
}
SDL_RWclose(rw);
}

bool Play(bool loop)
{
Halt();

if (currentTrack != this)
{
if (currentTrack && !currentTrack->autoload)
{
currentTrack->Unload();
}
currentTrack = this;
}

if (!loaded)
{
Load();
}

if (!valid)
{
return false;
Expand All @@ -435,8 +475,6 @@ class MusicTrack
sample_pos = 0;
stb_vorbis_seek_start(vorbis);

Halt();

SDL_zero(callbacks);
callbacks.OnBufferStart = &MusicTrack::refillReserve;
callbacks.OnBufferEnd = &MusicTrack::swapBuffers;
Expand Down Expand Up @@ -513,6 +551,7 @@ class MusicTrack
}
}

SDL_RWops *rw;
stb_vorbis* vorbis;
int channels;
Uint32 size;
Expand All @@ -527,9 +566,12 @@ class MusicTrack
Uint8* decoded_buf_reserve;
Uint8* read_buf;
bool shouldloop;
bool loaded;
bool valid;
bool autoload;

static bool paused;
static MusicTrack *currentTrack;
static FAudioSourceVoice* musicVoice;

static void refillReserve(FAudioVoiceCallback* callback, void* ctx)
Expand Down Expand Up @@ -709,6 +751,7 @@ class MusicTrack
};
bool MusicTrack::paused = false;
FAudioSourceVoice* MusicTrack::musicVoice = NULL;
MusicTrack* MusicTrack::currentTrack = NULL;

musicclass::musicclass(void)
{
Expand Down Expand Up @@ -833,7 +876,7 @@ void musicclass::init(void)
} \
else \
{ \
musicTracks.push_back(MusicTrack(rw)); \
musicTracks.push_back(MusicTrack(rw, false)); \
}

TRACK_NAMES(_)
Expand Down
Loading