Added function: UpdateSound()

This commit is contained in:
raysan5 2016-08-29 11:17:58 +02:00
parent 7dbb17792a
commit be97583f00
3 changed files with 31 additions and 0 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -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