raudio.c updated

This commit is contained in:
JupiterRider 2024-11-23 16:43:26 +01:00
parent c77be99ddc
commit b1792a31d9

View file

@ -895,8 +895,8 @@ Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileData, int
return wave; return wave;
} }
// Checks if wave data is ready // Checks if wave data is valid (data loaded and parameters)
bool IsWaveReady(Wave wave) bool IsWaveValid(Wave wave)
{ {
bool result = false; bool result = false;
@ -996,8 +996,8 @@ Sound LoadSoundAlias(Sound source)
} }
// Checks if a sound is ready // Checks if a sound is valid (data loaded and buffers initialized)
bool IsSoundReady(Sound sound) bool IsSoundValid(Sound sound)
{ {
bool result = false; bool result = false;
@ -1277,20 +1277,21 @@ Wave WaveCopy(Wave wave)
return newWave; 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 // 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); RL_FREE(wave->data);
wave->data = data; wave->data = data;
wave->frameCount = (unsigned int)frameCount;
} }
else TRACELOG(LOG_WARNING, "WAVE: Crop range out of bounds"); 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++) 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; 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])/32767.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]; else if (wave.sampleSize == 32) samples[i] = ((float *)wave.data)[i];
} }
@ -1351,7 +1352,6 @@ Music LoadMusicStream(const char *fileName)
} }
else else
{ {
drwav_uninit(ctxWav);
RL_FREE(ctxWav); RL_FREE(ctxWav);
} }
} }
@ -1431,7 +1431,9 @@ Music LoadMusicStream(const char *fileName)
{ {
music.ctxType = MUSIC_AUDIO_FLAC; music.ctxType = MUSIC_AUDIO_FLAC;
music.ctxData = ctxFlac; 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.frameCount = (unsigned int)ctxFlac->totalPCMFrameCount;
music.looping = true; // Looping enabled by default music.looping = true; // Looping enabled by default
musicLoaded = true; 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 jar_xm_reset(ctxXm); // Make sure we start at the beginning of the song
musicLoaded = true; musicLoaded = true;
} }
else else
{ {
jar_xm_free_context(ctxXm); 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)) else if ((strcmp(fileType, ".ogg") == 0) || (strcmp(fileType, ".OGG") == 0))
{ {
// Open ogg audio stream // 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) if (ctxOgg != NULL)
{ {
@ -1567,7 +1569,7 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data,
music.looping = true; // Looping enabled by default music.looping = true; // Looping enabled by default
musicLoaded = true; musicLoaded = true;
} }
else else
{ {
stb_vorbis_close(ctxOgg); stb_vorbis_close(ctxOgg);
} }
@ -1627,7 +1629,9 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data,
{ {
music.ctxType = MUSIC_AUDIO_FLAC; music.ctxType = MUSIC_AUDIO_FLAC;
music.ctxData = ctxFlac; 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.frameCount = (unsigned int)ctxFlac->totalPCMFrameCount;
music.looping = true; // Looping enabled by default music.looping = true; // Looping enabled by default
musicLoaded = true; musicLoaded = true;
@ -1725,8 +1729,8 @@ Music LoadMusicStreamFromMemory(const char *fileType, const unsigned char *data,
return music; return music;
} }
// Checks if a music stream is ready // Checks if a music stream is valid (context and buffers initialized)
bool IsMusicReady(Music music) bool IsMusicValid(Music music)
{ {
return ((music.ctxData != NULL) && // Validate context loaded return ((music.ctxData != NULL) && // Validate context loaded
(music.frameCount > 0) && // Validate audio frame count (music.frameCount > 0) && // Validate audio frame count
@ -2118,8 +2122,8 @@ AudioStream LoadAudioStream(unsigned int sampleRate, unsigned int sampleSize, un
return stream; return stream;
} }
// Checks if an audio stream is ready // Checks if an audio stream is valid (buffers initialized)
bool IsAudioStreamReady(AudioStream stream) bool IsAudioStreamValid(AudioStream stream)
{ {
return ((stream.buffer != NULL) && // Validate stream buffer return ((stream.buffer != NULL) && // Validate stream buffer
(stream.sampleRate > 0) && // Validate sample rate is supported (stream.sampleRate > 0) && // Validate sample rate is supported