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;
}
// 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;
@ -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)
{
@ -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