Skip to content

Commit

Permalink
Load an unlimited amount of loose music
Browse files Browse the repository at this point in the history
Binary blobs can now store 128 tracks, and music can now be read from
loose files. Unfortunately, the two cannot be used together -- loose
music can still only use 16 tracks. This commit attempts to fix that,
allowing for music playback by filename.
  • Loading branch information
NyakoFox committed Nov 25, 2024
1 parent d4e472d commit 6de0372
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
103 changes: 98 additions & 5 deletions desktop_version/src/Music.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,16 @@ float SoundTrack::volume = 0.0f;
class MusicTrack
{
public:
MusicTrack(SDL_RWops *rw)
MusicTrack(SDL_RWops *rw, const char* name, bool loose_extra)
{
SDL_zerop(this);
read_buf = (Uint8*) SDL_malloc(rw->size(rw));
SDL_RWread(rw, read_buf, rw->size(rw), 1);
int err;
stb_vorbis_info vorbis_info;
stb_vorbis_comment vorbis_comment;
filename = SDL_strdup(name);
looseextra = loose_extra;
vorbis = stb_vorbis_open_memory(read_buf, rw->size(rw), &err, NULL);
if (vorbis == NULL)
{
Expand Down Expand Up @@ -526,6 +528,8 @@ class MusicTrack
Uint8* decoded_buf_playing;
Uint8* decoded_buf_reserve;
Uint8* read_buf;
const char* filename;
bool looseextra;
bool shouldloop;
bool valid;

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

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

TRACK_NAMES(_)
Expand All @@ -859,7 +863,7 @@ void musicclass::init(void)
while (mmmmmm_blob.nextExtra(&index_))
{
rw = SDL_RWFromConstMem(mmmmmm_blob.getAddress(index_), mmmmmm_blob.getSize(index_));
musicTracks.push_back(MusicTrack( rw ));
musicTracks.push_back(MusicTrack( rw, mmmmmm_blob.m_headers[index_].name, false));

num_mmmmmm_tracks++;
index_++;
Expand All @@ -881,11 +885,53 @@ void musicclass::init(void)
while (pppppp_blob.nextExtra(&index_))
{
rw = SDL_RWFromConstMem(pppppp_blob.getAddress(index_), pppppp_blob.getSize(index_));
musicTracks.push_back(MusicTrack( rw ));
musicTracks.push_back(MusicTrack( rw, pppppp_blob.m_headers[index_].name, false));

num_pppppp_tracks++;
index_++;
}

EnumHandle handle = {};
const char* item;
while ((item = FILESYSTEM_enumerateAssets("music", &handle)) != NULL)
{
char name[256];
char id[256];
SDL_snprintf(name, sizeof(name), "music/%s", item);

// We need an ID, so chop off the extension
SDL_strlcpy(id, item, sizeof(id));
char* dot = SDL_strrchr(id, '.');
if (dot != NULL)
{
*dot = '\0';
}

vlog_info("Reading loose music file %s as %s", item, id);

unsigned char* mem;
size_t len;
FILESYSTEM_loadAssetToMemory(name, &mem, &len);
if (mem == NULL)
{
vlog_error("Unable to read loose music file: %s", SDL_GetError());
}
else
{
rw = SDL_RWFromConstMem(mem, len);
if (rw == NULL)
{
vlog_error("Unable to read loose music file: %s", SDL_GetError());
}
else
{
musicTracks.push_back(MusicTrack(rw, id, true));
num_pppppp_tracks++;
}
VVV_free(mem);
}
}
FILESYSTEM_freeEnumerate(&handle);
}

void musicclass::destroy(void)
Expand Down Expand Up @@ -992,6 +1038,53 @@ void musicclass::play(int t)
}
}

void musicclass::playid(const char* id)
{
for (size_t i = 0; i < musicTracks.size(); i++)
{
if (SDL_strcmp(musicTracks[i].filename, id) == 0)
{
play(i);
return;
}
}
vlog_error("playid() couldn't find music ID: %s", id);
}

bool musicclass::idexists(const char* id)
{
for (size_t i = 0; i < musicTracks.size(); i++)
{
if (SDL_strcmp(musicTracks[i].filename, id) == 0)
{
return true;
}
}
return false;
}

bool musicclass::isextra(int t)
{
for (size_t i = 0; i < musicTracks.size(); i++)
{
if (musicTracks[i].looseextra)
{
return true;
}
}

return false;
}

const char* musicclass::getid(int t)
{
if (INBOUNDS_VEC(t, musicTracks))
{
return musicTracks[t].filename;
}
return "?";
}

void musicclass::resume(void)
{
if (currentsong == -1)
Expand Down
4 changes: 4 additions & 0 deletions desktop_version/src/Music.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class musicclass
void destroy(void);

void play(int t);
void playid(const char* id);
bool idexists(const char* id);
bool isextra(int t);
const char* getid(int t);
void resume(void);
void resumefade(const int fadein_ms);
void pause(void);
Expand Down
9 changes: 8 additions & 1 deletion desktop_version/src/Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,14 @@ void scriptclass::run(void)
}
if (words[0] == "play")
{
music.play(ss_toi(words[1]));
if (music.idexists(words[1].c_str()))
{
music.playid(words[1].c_str());
}
else if (!music.isextra(ss_toi(words[1])))
{
music.play(ss_toi(words[1]));
}
}
if (words[0] == "stopmusic")
{
Expand Down

0 comments on commit 6de0372

Please sign in to comment.