From be97583f00997fa918a15d0164190ae6876d0571 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 29 Aug 2016 11:17:58 +0200 Subject: [PATCH] Added function: UpdateSound() --- src/audio.c | 28 ++++++++++++++++++++++++++++ src/audio.h | 1 + src/raylib.h | 2 ++ 3 files changed, 31 insertions(+) diff --git a/src/audio.c b/src/audio.c index 1772196f..3bace5f7 100644 --- a/src/audio.c +++ b/src/audio.c @@ -284,6 +284,7 @@ Sound LoadSoundFromWave(Wave wave) sound.source = source; sound.buffer = buffer; + sound.format = format; } return sound; @@ -409,6 +410,33 @@ void UnloadSound(Sound sound) TraceLog(INFO, "[SND ID %i][BUFR ID %i] Unloaded sound data from RAM", sound.source, sound.buffer); } +// Update sound buffer with new data +// NOTE: data must match sound.format +void UpdateSound(Sound sound, void *data, int numSamples) +{ + ALint sampleRate, sampleSize, channels; + alGetBufferi(sound.buffer, AL_FREQUENCY, &sampleRate); + alGetBufferi(sound.buffer, AL_BITS, &sampleSize); // It could also be retrieved from sound.format + alGetBufferi(sound.buffer, AL_CHANNELS, &channels); // It could also be retrieved from sound.format + + TraceLog(DEBUG, "UpdateSound() : AL_FREQUENCY: %i", sampleRate); + TraceLog(DEBUG, "UpdateSound() : AL_BITS: %i", sampleSize); + TraceLog(DEBUG, "UpdateSound() : AL_CHANNELS: %i", channels); + + unsigned int dataSize = numSamples*sampleSize/8; // Size of data in bytes + + alSourceStop(sound.source); // Stop sound + alSourcei(sound.source, AL_BUFFER, 0); // Unbind buffer from sound to update + //alDeleteBuffers(1, &sound.buffer); // Delete current buffer data + //alGenBuffers(1, &sound.buffer); // Generate new buffer + + // Upload new data to sound buffer + alBufferData(sound.buffer, sound.format, data, dataSize, sampleRate); + + // Attach sound buffer to source again + alSourcei(sound.source, AL_BUFFER, sound.buffer); +} + // Play a sound void PlaySound(Sound sound) { diff --git a/src/audio.h b/src/audio.h index 4ee9559e..923492ca 100644 --- a/src/audio.h +++ b/src/audio.h @@ -110,6 +110,7 @@ bool IsAudioDeviceReady(void); // Check if audi Sound LoadSound(char *fileName); // Load sound to memory Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data Sound LoadSoundFromRES(const char *rresName, int resId); // Load sound to memory from rRES file (raylib Resource) +void UpdateSound(Sound sound, void *data, int numSamples); // Update sound buffer with new data void UnloadSound(Sound sound); // Unload sound void PlaySound(Sound sound); // Play a sound void PauseSound(Sound sound); // Pause a sound diff --git a/src/raylib.h b/src/raylib.h index 68cddc5a..d295ef90 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -486,6 +486,7 @@ typedef struct Ray { typedef struct Sound { unsigned int source; // OpenAL audio source id unsigned int buffer; // OpenAL audio buffer id + int format; // OpenAL audio format specifier } Sound; // Wave type, defines audio wave data @@ -908,6 +909,7 @@ RLAPI bool IsAudioDeviceReady(void); // Check i RLAPI Sound LoadSound(char *fileName); // Load sound to memory RLAPI Sound LoadSoundFromWave(Wave wave); // Load sound to memory from wave data RLAPI Sound LoadSoundFromRES(const char *rresName, int resId); // Load sound to memory from rRES file (raylib Resource) +RLAPI void UpdateSound(Sound sound, void *data, int numSamples); // Update sound buffer with new data RLAPI void UnloadSound(Sound sound); // Unload sound RLAPI void PlaySound(Sound sound); // Play a sound RLAPI void PauseSound(Sound sound); // Pause a sound