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 an unlimited amount of loose music #1171

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
106 changes: 100 additions & 6 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);
NyakoFox marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name, filename and id are used a bit confusingly. In the enumeration code, the file path (with folder and extension) is first written to a buffer name, then a plain version without folder or extension is written to a buffer id, this is passed to the MusicTrack constructor as name and then written to the filename attribute, which can be requested with a function called getid...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you suggest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply!

I'd say:

  • char name[256] (stores e.g. music/mysong.ogg) -> asset_filename? It's used as an argument to FILESYSTEM_loadAssetToMemory()
  • char id[256] (stores e.g. mysong) -> probably fine as-is
  • char* filename in MusicTrack -> id to match
  • const char* name in the MusicTrack constructor -> id to match

The names of the constructor argument and class attribute will clash - I'm not too used to C++ OO but I've usually seen the solution in other languages be something like this.id = id;. Otherwise, naming the argument _id and doing id = SDL_strdup(_id); would in my opinion be better than working around it with filename/name, loose_extra/looseextra, etc. (And that kind of _ prefixing is done in other places in the code as well anyway)

looseextra = loose_extra;
vorbis = stb_vorbis_open_memory(read_buf, rw->size(rw), &err, NULL);
if (vorbis == NULL)
{
Expand Down Expand Up @@ -418,6 +420,7 @@ class MusicTrack
VVV_free(read_buf);
VVV_free(decoded_buf_playing);
VVV_free(decoded_buf_reserve);
VVV_free(filename);
if (!IsHalted())
{
VVV_freefunc(FAudioVoice_DestroyVoice, musicVoice);
Expand Down Expand Up @@ -526,6 +529,8 @@ class MusicTrack
Uint8* decoded_buf_playing;
Uint8* decoded_buf_reserve;
Uint8* read_buf;
char* filename;
bool looseextra;
bool shouldloop;
bool valid;

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

Expand All @@ -829,11 +834,11 @@ void musicclass::init(void)
rw = PHYSFSRWOPS_openRead(track_name); \
if (rw == NULL) \
{ \
vlog_error("Unable to read loose music file: %s", SDL_GetError()); \
vlog_error("Unable to read extra loose music file: %s", SDL_GetError()); \
} \
else \
{ \
musicTracks.push_back(MusicTrack(rw)); \
musicTracks.push_back(MusicTrack(rw, track_name, false)); \
}

TRACK_NAMES(_)
Expand All @@ -859,7 +864,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 +886,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());
NyakoFox marked this conversation as resolved.
Show resolved Hide resolved
}
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 +1039,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