Add a function to clone a sound and share data with another sound.

This commit is contained in:
Jeff Myers 2023-08-02 10:19:21 -07:00
commit d25c662f24
2 changed files with 36 additions and 5 deletions

View file

@ -420,7 +420,7 @@ static bool SaveFileText(const char *fileName, char *text); // Save text
// NOTE: Those functions are not exposed by raylib... for the moment
//----------------------------------------------------------------------------------
AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sampleRate, ma_uint32 sizeInFrames, int usage);
void UnloadAudioBuffer(AudioBuffer *buffer);
void UnloadAudioBuffer(AudioBuffer *buffer, bool freeSampleData);
bool IsAudioBufferPlaying(AudioBuffer *buffer);
void PlayAudioBuffer(AudioBuffer *buffer);
@ -591,13 +591,14 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
}
// Delete an audio buffer
void UnloadAudioBuffer(AudioBuffer *buffer)
void UnloadAudioBuffer(AudioBuffer *buffer, bool freeSampleData)
{
if (buffer != NULL)
{
ma_data_converter_uninit(&buffer->converter, NULL);
UntrackAudioBuffer(buffer);
RL_FREE(buffer->data);
if (freeSampleData)
RL_FREE(buffer->data);
RL_FREE(buffer);
}
}
@ -911,6 +912,34 @@ Sound LoadSoundFromWave(Wave wave)
sound.stream.sampleSize = 32;
sound.stream.channels = AUDIO_DEVICE_CHANNELS;
sound.stream.buffer = audioBuffer;
sound.noFree = false;
}
return sound;
}
// Clone sound from existing sound data, clone does not own wave data
// Wave data must
// NOTE: Wave data must be unallocated manually and will be shared across all clones
Sound CloneSound(Sound sourceSound)
{
Sound sound = { 0 };
if (sourceSound.stream.buffer->data != NULL)
{
AudioBuffer* audioBuffer = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, sourceSound.frameCount, AUDIO_BUFFER_USAGE_STATIC);
if (audioBuffer == NULL)
{
TRACELOG(LOG_WARNING, "SOUND: Failed to create buffer");
return sound; // early return to avoid dereferencing the audioBuffer null pointer
}
audioBuffer->data = sourceSound.stream.buffer->data;
sound.frameCount = sourceSound.frameCount;
sound.stream.sampleRate = AUDIO.System.device.sampleRate;
sound.stream.sampleSize = 32;
sound.stream.channels = AUDIO_DEVICE_CHANNELS;
sound.stream.buffer = audioBuffer;
sound.noFree = true;
}
return sound;
@ -936,7 +965,7 @@ void UnloadWave(Wave wave)
// Unload sound
void UnloadSound(Sound sound)
{
UnloadAudioBuffer(sound.stream.buffer);
UnloadAudioBuffer(sound.stream.buffer, !sound.noFree);
//TRACELOG(LOG_INFO, "SOUND: Unloaded sound data from RAM");
}
@ -2027,7 +2056,7 @@ bool IsAudioStreamReady(AudioStream stream)
// Unload audio stream and free memory
void UnloadAudioStream(AudioStream stream)
{
UnloadAudioBuffer(stream.buffer);
UnloadAudioBuffer(stream.buffer, true);
TRACELOG(LOG_INFO, "STREAM: Unloaded audio stream data from RAM");
}

View file

@ -461,6 +461,7 @@ typedef struct AudioStream {
typedef struct Sound {
AudioStream stream; // Audio stream
unsigned int frameCount; // Total number of frames (considering channels)
bool noFree; // The sound data buffer is owned by something else, don't free it
} Sound;
// Music, audio stream, anything longer than ~10 seconds should be streamed
@ -1531,6 +1532,7 @@ RLAPI Wave LoadWaveFromMemory(const char *fileType, const unsigned char *fileDat
RLAPI bool IsWaveReady(Wave wave); // Checks if wave data is ready
RLAPI Sound LoadSound(const char *fileName); // Load sound from file
RLAPI Sound LoadSoundFromWave(Wave wave); // Load sound from wave data
RLAPI Sound CloneSound(Sound sourceSound); // Create a new sound that shares the same sample data as the source sound, does not own the sound data
RLAPI bool IsSoundReady(Sound sound); // Checks if a sound is ready
RLAPI void UpdateSound(Sound sound, const void *data, int sampleCount); // Update sound buffer with new data
RLAPI void UnloadWave(Wave wave); // Unload wave data