From b1792a31d9f4e59d3a0d0aa84a802c06a3d2d7ff Mon Sep 17 00:00:00 2001 From: JupiterRider <60042618+JupiterRider@users.noreply.github.com> Date: Sat, 23 Nov 2024 16:43:26 +0100 Subject: [PATCH] raudio.c updated --- raylib/raudio.c | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/raylib/raudio.c b/raylib/raudio.c index 4a140c5..47d28e0 100644 --- a/raylib/raudio.c +++ b/raylib/raudio.c @@ -895,8 +895,8 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int return wave; } -// Checks if wave data is ready -bool IsWaveReady(Wave wave) +// Checks if wave data is valid (data loaded and parameters) +bool IsWaveValid(Wave wave) { bool result = false; @@ -996,8 +996,8 @@ Sound LoadSoundAlias(Sound source) } -// Checks if a sound is ready -bool IsSoundReady(Sound sound) +// Checks if a sound is valid (data loaded and buffers initialized) +bool IsSoundValid(Sound sound) { bool result = false; @@ -1277,20 +1277,21 @@ Wave WaveCopy(Wave wave) return newWave; } -// Crop a wave to defined samples range +// Crop a wave to defined frames range // NOTE: Security check in case of out-of-range -void WaveCrop(Wave *wave, int initSample, int finalSample) +void WaveCrop(Wave *wave, int initFrame, int finalFrame) { - if ((initSample >= 0) && (initSample < finalSample) && ((unsigned int)finalSample < (wave->frameCount*wave->channels))) + if ((initFrame >= 0) && (initFrame < finalFrame) && ((unsigned int)finalFrame <= wave->frameCount)) { - int sampleCount = finalSample - initSample; + int frameCount = finalFrame - initFrame; - void *data = RL_MALLOC(sampleCount*wave->sampleSize/8); + void *data = RL_MALLOC(frameCount*wave->channels*wave->sampleSize/8); - memcpy(data, (unsigned char *)wave->data + (initSample*wave->channels*wave->sampleSize/8), sampleCount*wave->sampleSize/8); + memcpy(data, (unsigned char *)wave->data + (initFrame*wave->channels*wave->sampleSize/8), frameCount*wave->channels*wave->sampleSize/8); RL_FREE(wave->data); wave->data = data; + wave->frameCount = (unsigned int)frameCount; } else TRACELOG(LOG_WARNING, "WAVE: Crop range out of bounds"); } @@ -1306,8 +1307,8 @@ float *LoadWaveSamples(Wave wave) for (unsigned int i = 0; i < wave.frameCount*wave.channels; i++) { - if (wave.sampleSize == 8) samples[i] = (float)(((unsigned char *)wave.data)[i] - 127)/256.0f; - else if (wave.sampleSize == 16) samples[i] = (float)(((short *)wave.data)[i])/32767.0f; + if (wave.sampleSize == 8) samples[i] = (float)(((unsigned char *)wave.data)[i] - 128)/128.0f; + else if (wave.sampleSize == 16) samples[i] = (float)(((short *)wave.data)[i])/32768.0f; else if (wave.sampleSize == 32) samples[i] = ((float *)wave.data)[i]; } @@ -1351,7 +1352,6 @@ Music LoadMusicStream(const char *fileName) } else { - drwav_uninit(ctxWav); RL_FREE(ctxWav); } } @@ -1431,7 +1431,9 @@ Music LoadMusicStream(const char *fileName) { music.ctxType = MUSIC_AUDIO_FLAC; music.ctxData = ctxFlac; - music.stream = LoadAudioStream(ctxFlac->sampleRate, ctxFlac->bitsPerSample, ctxFlac->channels); + int sampleSize = ctxFlac->bitsPerSample; + if (ctxFlac->bitsPerSample == 24) sampleSize = 16; // Forcing conversion to s16 on UpdateMusicStream() + music.stream = LoadAudioStream(ctxFlac->sampleRate, sampleSize, ctxFlac->channels); music.frameCount = (unsigned int)ctxFlac->totalPCMFrameCount; music.looping = true; // Looping enabled by default musicLoaded = true; @@ -1465,7 +1467,7 @@ Music LoadMusicStream(const char *fileName) jar_xm_reset(ctxXm); // Make sure we start at the beginning of the song musicLoaded = true; } - else + else { jar_xm_free_context(ctxXm); } @@ -1551,7 +1553,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data, else if ((strcmp(fileType, ".ogg") == 0) || (strcmp(fileType, ".OGG") == 0)) { // Open ogg audio stream - stb_vorbis* ctxOgg = stb_vorbis_open_memory((const unsigned char*)data, dataSize, NULL, NULL); + stb_vorbis* ctxOgg = stb_vorbis_open_memory((const unsigned char *)data, dataSize, NULL, NULL); if (ctxOgg != NULL) { @@ -1567,7 +1569,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data, music.looping = true; // Looping enabled by default musicLoaded = true; } - else + else { stb_vorbis_close(ctxOgg); } @@ -1627,7 +1629,9 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data, { music.ctxType = MUSIC_AUDIO_FLAC; music.ctxData = ctxFlac; - music.stream = LoadAudioStream(ctxFlac->sampleRate, ctxFlac->bitsPerSample, ctxFlac->channels); + int sampleSize = ctxFlac->bitsPerSample; + if (ctxFlac->bitsPerSample == 24) sampleSize = 16; // Forcing conversion to s16 on UpdateMusicStream() + music.stream = LoadAudioStream(ctxFlac->sampleRate, sampleSize, ctxFlac->channels); music.frameCount = (unsigned int)ctxFlac->totalPCMFrameCount; music.looping = true; // Looping enabled by default musicLoaded = true; @@ -1725,8 +1729,8 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data, return music; } -// Checks if a music stream is ready -bool IsMusicReady(Music music) +// Checks if a music stream is valid (context and buffers initialized) +bool IsMusicValid(Music music) { return ((music.ctxData != NULL) && // Validate context loaded (music.frameCount > 0) && // Validate audio frame count @@ -2118,8 +2122,8 @@ AudioStream LoadAudioStream(unsigned int sampleRate, unsigned int sampleSize, un return stream; } -// Checks if an audio stream is ready -bool IsAudioStreamReady(AudioStream stream) +// Checks if an audio stream is valid (buffers initialized) +bool IsAudioStreamValid(AudioStream stream) { return ((stream.buffer != NULL) && // Validate stream buffer (stream.sampleRate > 0) && // Validate sample rate is supported