From 14e8c19873819fac9c7a80bd7a1371f9028e7cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agnis=20=22NeZv=C4=93rs=22=20Aldi=C5=86=C5=A1?= Date: Mon, 22 Feb 2021 15:23:04 +0200 Subject: [PATCH] LoadModuleFromData implementation in raudio.c --- src/raudio.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/raudio.c b/src/raudio.c index 6a122720d..f89fd64c6 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -1297,6 +1297,92 @@ Music LoadMusicStream(const char *fileName) return music; } +// extension including period ".mod" +Music LoadModuleFromData(const char *fileType, const char* data, int dataSize){ + Music music = { 0 }; + bool musicLoaded = false; + + char fileExtLower[16] = { 0 }; + strcpy(fileExtLower, TextToLower(fileType)); + + if (false) { } +#if defined(SUPPORT_FILEFORMAT_XM) + if (TextIsEqual(fileExtLower, ".xm")) + { + jar_xm_context_t *ctxXm = NULL; + int result = jar_xm_create_context_safe(&ctxXm, data, dataSize, 48000); + if (result == 0) // XM AUDIO.System.context created successfully + { + music.ctxType = MUSIC_MODULE_XM; + jar_xm_set_max_loop_count(ctxXm, 0); // Set infinite number of loops + + // NOTE: Only stereo is supported for XM + music.stream = InitAudioStream(48000, 16, 2); + music.sampleCount = (unsigned int)jar_xm_get_remaining_samples(ctxXm)*2; // 2 channels + music.looping = true; // Looping enabled by default + jar_xm_reset(ctxXm); // make sure we start at the beginning of the song + + music.ctxData = ctxXm; + musicLoaded = true; + } + } +#endif +#if defined(SUPPORT_FILEFORMAT_MOD) + else if (TextIsEqual(fileExtLower, ".mod")) + { + jar_mod_context_t *ctxMod = RL_MALLOC(sizeof(jar_mod_context_t)); + int result = 0; + + jar_mod_init(ctxMod); + if (dataSize && dataSize < 32*1024*1024) + { + ctxMod->modfilesize = dataSize; + ctxMod->modfile = data; + if(jar_mod_load(ctxMod, (void*)ctxMod->modfile, dataSize)) result = dataSize; + } + + if (result > 0) + { + music.ctxType = MUSIC_MODULE_MOD; + + // NOTE: Only stereo is supported for MOD + music.stream = InitAudioStream(48000, 16, 2); + music.sampleCount = (unsigned int)jar_mod_max_samples(ctxMod)*2; // 2 channels + music.looping = true; // Looping enabled by default + musicLoaded = true; + + music.ctxData = ctxMod; + musicLoaded = true; + } + } +#endif + else TRACELOG(LOG_WARNING, "STREAM: [%s] Fileformat not supported", fileType); + + if (!musicLoaded) + { + if (false) { } + #if defined(SUPPORT_FILEFORMAT_XM) + else if (music.ctxType == MUSIC_MODULE_XM) jar_xm_free_context((jar_xm_context_t *)music.ctxData); + #endif + #if defined(SUPPORT_FILEFORMAT_MOD) + else if (music.ctxType == MUSIC_MODULE_MOD) { jar_mod_unload((jar_mod_context_t *)music.ctxData); RL_FREE(music.ctxData); } + #endif + + TRACELOG(LOG_WARNING, "FILEIO: [%s] Music data could not be loaded", fileType); + } + else + { + // Show some music stream info + TRACELOG(LOG_INFO, "FILEIO: [%s] Music data successfully loaded:", fileType); + TRACELOG(LOG_INFO, " > Total samples: %i", music.sampleCount); + TRACELOG(LOG_INFO, " > Sample rate: %i Hz", music.stream.sampleRate); + TRACELOG(LOG_INFO, " > Sample size: %i bits", music.stream.sampleSize); + TRACELOG(LOG_INFO, " > Channels: %i (%s)", music.stream.channels, (music.stream.channels == 1)? "Mono" : (music.stream.channels == 2)? "Stereo" : "Multi"); + } + + return music; +} + // Unload music stream void UnloadMusicStream(Music music) {