Skip to content

Commit

Permalink
[AUDIO] Add a function to make an alias of a sound and share it's sam…
Browse files Browse the repository at this point in the history
…ple data (#3219)

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

* rename items based on feedback

* PR Feedback, use custom unload for sound alias, not variant of normal sound unloading
  • Loading branch information
JeffM2501 committed Aug 4, 2023
1 parent d3058fe commit 601cadb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/raudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,32 @@ Sound LoadSoundFromWave(Wave wave)
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 LoadSoundAlias(Sound source)
{
Sound sound = { 0 };

if (source.stream.buffer->data != NULL)
{
AudioBuffer* audioBuffer = LoadAudioBuffer(AUDIO_DEVICE_FORMAT, AUDIO_DEVICE_CHANNELS, AUDIO.System.device.sampleRate, source.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 = source.stream.buffer->data;
sound.frameCount = source.frameCount;
sound.stream.sampleRate = AUDIO.System.device.sampleRate;
sound.stream.sampleSize = 32;
sound.stream.channels = AUDIO_DEVICE_CHANNELS;
sound.stream.buffer = audioBuffer;
}

return sound;
}

// Checks if a sound is ready
bool IsSoundReady(Sound sound)
{
Expand All @@ -940,6 +966,17 @@ void UnloadSound(Sound sound)
//TRACELOG(LOG_INFO, "SOUND: Unloaded sound data from RAM");
}

void UnloadSoundAlias(Sound alias)
{
// untrack and unload just the sound buffer, not the sample data, it is shared with the source for the alias
if (alias.stream.buffer != NULL)
{
ma_data_converter_uninit(&alias.stream.buffer->converter, NULL);
UntrackAudioBuffer(alias.stream.buffer);
RL_FREE(alias.stream.buffer);
}
}

// Update sound buffer with new data
void UpdateSound(Sound sound, const void *data, int sampleCount)
{
Expand Down
2 changes: 2 additions & 0 deletions src/raylib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1533,10 +1533,12 @@ 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 LoadSoundAlias(Sound source); // 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
RLAPI void UnloadSound(Sound sound); // Unload sound
RLAPI void UnloadSoundAlias(Sound alias); // Unload a sound alias (does not deallocate sample data)
RLAPI bool ExportWave(Wave wave, const char *fileName); // Export wave data to file, returns true on success
RLAPI bool ExportWaveAsCode(Wave wave, const char *fileName); // Export wave sample data to code (.h), returns true on success

Expand Down

0 comments on commit 601cadb

Please sign in to comment.