diff --git a/jail_audio.cpp b/jail_audio.cpp index 0be6291..e58f22e 100644 --- a/jail_audio.cpp +++ b/jail_audio.cpp @@ -90,22 +90,12 @@ void JA_Quit() { sdlAudioDevice = 0; } -JA_Music_t *JA_LoadMusic(const char* filename) { +JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length) +{ int chan, samplerate; - - // [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid. - FILE *f = fopen(filename, "rb"); - fseek(f, 0, SEEK_END); - long fsize = ftell(f); - fseek(f, 0, SEEK_SET); - Uint8 *buffer = (Uint8*)malloc(fsize + 1); - if (fread(buffer, fsize, 1, f)!=1) return NULL; - fclose(f); - JA_Music_t *music = new JA_Music_t(); - music->samples = stb_vorbis_decode_memory(buffer, fsize, &chan, &samplerate, &music->output); - free(buffer); + music->samples = stb_vorbis_decode_memory(buffer, length, &chan, &samplerate, &music->output); // [RZC 28/08/22] Abans el descomprimiem mentre el teniem obert // music->samples = stb_vorbis_decode_filename(filename, &chan, &samplerate, &music->output); @@ -125,6 +115,24 @@ JA_Music_t *JA_LoadMusic(const char* filename) { return music; } +JA_Music_t *JA_LoadMusic(const char* filename) +{ + // [RZC 28/08/22] Carreguem primer el arxiu en memòria i després el descomprimim. Es algo més rapid. + FILE *f = fopen(filename, "rb"); + fseek(f, 0, SEEK_END); + long fsize = ftell(f); + fseek(f, 0, SEEK_SET); + Uint8 *buffer = (Uint8*)malloc(fsize + 1); + if (fread(buffer, fsize, 1, f)!=1) return NULL; + fclose(f); + + JA_Music_t *music = JA_LoadMusic(buffer, fsize); + + free(buffer); + + return music; +} + void JA_PlayMusic(JA_Music_t *music, const int loop) { if (!JA_musicEnabled) return; @@ -201,6 +209,24 @@ JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length) { return sound; } +JA_Sound_t *JA_LoadSound(uint8_t* buffer, uint32_t size) { + JA_Sound_t *sound = new JA_Sound_t(); + SDL_AudioSpec wavSpec; + SDL_LoadWAV_RW(SDL_RWFromMem(buffer, size),1, &wavSpec, &sound->buffer, &sound->length); + + SDL_AudioCVT cvt; + SDL_BuildAudioCVT(&cvt, wavSpec.format, wavSpec.channels, wavSpec.freq, JA_format, JA_channels, JA_freq); + cvt.len = sound->length; + cvt.buf = (Uint8 *) SDL_malloc(cvt.len * cvt.len_mult); + SDL_memcpy(cvt.buf, sound->buffer, sound->length); + SDL_ConvertAudio(&cvt); + SDL_FreeWAV(sound->buffer); + sound->buffer = cvt.buf; + sound->length = cvt.len_cvt; + + return sound; +} + JA_Sound_t *JA_LoadSound(const char* filename) { JA_Sound_t *sound = new JA_Sound_t(); SDL_AudioSpec wavSpec; @@ -221,7 +247,7 @@ JA_Sound_t *JA_LoadSound(const char* filename) { int JA_PlaySound(JA_Sound_t *sound, const int loop) { - if (!JA_soundEnabled) return; + if (!JA_soundEnabled) return -1; int channel = 0; while (channel < JA_MAX_SIMULTANEOUS_CHANNELS && channels[channel].state != JA_CHANNEL_FREE) { channel++; } diff --git a/jail_audio.h b/jail_audio.h index f4304d1..eac8a82 100644 --- a/jail_audio.h +++ b/jail_audio.h @@ -11,6 +11,7 @@ void JA_Init(const int freq, const SDL_AudioFormat format, const int channels); void JA_Quit(); JA_Music_t *JA_LoadMusic(const char* filename); +JA_Music_t *JA_LoadMusic(Uint8* buffer, Uint32 length); void JA_PlayMusic(JA_Music_t *music, const int loop = -1); void JA_PauseMusic(); void JA_ResumeMusic(); @@ -21,6 +22,7 @@ int JA_SetMusicVolume(int volume); void JA_EnableMusic(const bool value); JA_Sound_t *JA_NewSound(Uint8* buffer, Uint32 length); +JA_Sound_t *JA_LoadSound(Uint8* buffer, Uint32 length); JA_Sound_t *JA_LoadSound(const char* filename); int JA_PlaySound(JA_Sound_t *sound, const int loop = 0); void JA_PauseChannel(const int channel);