diff --git a/README.md b/README.md index 40f9a0f..ed4cb86 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,19 @@ int main() { } ``` +### Music + +raylib streams music directly from the file buffer, so load the file data yourself, keep it alive while the music plays, and free it after unloading the music stream. + +``` c +int size; +unsigned char *data = LoadFileDataFromPhysFS("assets/music.ogg", &size); +Music music = LoadMusicStreamFromMemory(".ogg", data, size); +// ... play the music ... +UnloadMusicStream(music); +UnloadFileData(data); // Free the buffer only after unloading the music stream. +``` + ### API ``` c @@ -72,7 +85,6 @@ Image LoadImageAnimFromPhysFS(const char* fileName, int* frames); // Load an an Texture2D LoadTextureFromPhysFS(const char* fileName); // Load a texture from PhysFS Wave LoadWaveFromPhysFS(const char* fileName); // Load wave data from PhysFS Sound LoadSoundFromPhysFS(const char* fileName); // Load a sound from PhysFS -Music LoadMusicStreamFromPhysFS(const char* fileName); // Load music data from PhysFS Font LoadFontFromPhysFS(const char* fileName, int fontSize, int *fontChars, int charsCount); // Load a font from PhysFS Shader LoadShaderFromPhysFS(const char* vsFileName, const char* fsFileName); // Load shader from PhysFS void SetPhysFSCallbacks(void); // Set the raylib file loader/saver callbacks to use PhysFS diff --git a/examples/audio/audio_music_stream.c b/examples/audio/audio_music_stream.c index b56bd02..dcbe074 100644 --- a/examples/audio/audio_music_stream.c +++ b/examples/audio/audio_music_stream.c @@ -29,7 +29,10 @@ int main(void) InitPhysFS(); MountPhysFS("resources", "res"); - Music music = LoadMusicStreamFromPhysFS("res/country.mp3"); + // Load the music file data manually, as the buffer must stay valid while the music plays. + int musicDataSize = 0; + unsigned char *musicData = LoadFileDataFromPhysFS("res/country.mp3", &musicDataSize); + Music music = LoadMusicStreamFromMemory(".mp3", musicData, musicDataSize); PlayMusicStream(music); @@ -90,6 +93,7 @@ int main(void) // De-Initialization //-------------------------------------------------------------------------------------- UnloadMusicStream(music); // Unload music stream buffers from RAM + UnloadFileData(musicData); // Free the music file data after the stream is unloaded CloseAudioDevice(); // Close audio device (music streaming is automatically stopped) diff --git a/raylib-physfs.h b/raylib-physfs.h index 42c0e95..53b884a 100644 --- a/raylib-physfs.h +++ b/raylib-physfs.h @@ -72,7 +72,6 @@ RAYLIB_PHYSFS_DEF Image LoadImageAnimFromPhysFS(const char* fileName, int* frame RAYLIB_PHYSFS_DEF Texture2D LoadTextureFromPhysFS(const char* fileName); // Load a texture from PhysFS RAYLIB_PHYSFS_DEF Wave LoadWaveFromPhysFS(const char* fileName); // Load wave data from PhysFS RAYLIB_PHYSFS_DEF Sound LoadSoundFromPhysFS(const char* fileName); // Load a sound from PhysFS -RAYLIB_PHYSFS_DEF Music LoadMusicStreamFromPhysFS(const char* fileName); // Load music data from PhysFS RAYLIB_PHYSFS_DEF Font LoadFontFromPhysFS(const char* fileName, int fontSize, int *fontChars, int charsCount); // Load a font from PhysFS RAYLIB_PHYSFS_DEF Shader LoadShaderFromPhysFS(const char* vsFileName, const char* fsFileName); // Load shader from PhysFS RAYLIB_PHYSFS_DEF void SetPhysFSCallbacks(void); // Set the raylib file loader/saver callbacks to use PhysFS @@ -495,35 +494,6 @@ Sound LoadSoundFromPhysFS(const char* fileName) { return sound; } -/** - * Load module music from PhysFS. - * - * @param fileName The file name to load from the PhysFS mount paths. - * - * @return The Music object, or an empty Music object on failure. - * - * @see UnloadMusic() - */ -Music LoadMusicStreamFromPhysFS(const char* fileName) { - int bytesRead; - unsigned char* fileData = LoadFileDataFromPhysFS(fileName, &bytesRead); - if (bytesRead == 0) { - Music output = { 0 }; - return output; - } - - // Load from the memory. - const char* extension = GetFileExtension(fileName); - Music music = LoadMusicStreamFromMemory(extension, fileData, bytesRead); - - // Unload the file data if the music failed to load. - if (music.ctxData == NULL) { - UnloadFileData(fileData); - } - - return music; -} - /** * Load font from PhysFS. *