Share PCM streaming buffer to reduce dynamic allocations (#2532)
This commit is contained in:
parent
0afa79067b
commit
8bd3ecaa66
1 changed files with 19 additions and 14 deletions
33
src/raudio.c
33
src/raudio.c
|
@ -351,6 +351,8 @@ typedef struct AudioData {
|
||||||
ma_device device; // miniaudio device
|
ma_device device; // miniaudio device
|
||||||
ma_mutex lock; // miniaudio mutex lock
|
ma_mutex lock; // miniaudio mutex lock
|
||||||
bool isReady; // Check if audio device is ready
|
bool isReady; // Check if audio device is ready
|
||||||
|
size_t pcmCapacity;
|
||||||
|
void *pcm;
|
||||||
} System;
|
} System;
|
||||||
struct {
|
struct {
|
||||||
AudioBuffer *first; // Pointer to first AudioBuffer in the list
|
AudioBuffer *first; // Pointer to first AudioBuffer in the list
|
||||||
|
@ -510,6 +512,7 @@ void CloseAudioDevice(void)
|
||||||
ma_context_uninit(&AUDIO.System.context);
|
ma_context_uninit(&AUDIO.System.context);
|
||||||
|
|
||||||
AUDIO.System.isReady = false;
|
AUDIO.System.isReady = false;
|
||||||
|
RL_FREE(AUDIO.System.pcm);
|
||||||
|
|
||||||
TRACELOG(LOG_INFO, "AUDIO: Device closed successfully");
|
TRACELOG(LOG_INFO, "AUDIO: Device closed successfully");
|
||||||
}
|
}
|
||||||
|
@ -1726,7 +1729,12 @@ void UpdateMusicStream(Music music)
|
||||||
unsigned int subBufferSizeInFrames = music.stream.buffer->sizeInFrames/2;
|
unsigned int subBufferSizeInFrames = music.stream.buffer->sizeInFrames/2;
|
||||||
|
|
||||||
// NOTE: Using dynamic allocation because it could require more than 16KB
|
// NOTE: Using dynamic allocation because it could require more than 16KB
|
||||||
void *pcm = RL_CALLOC(subBufferSizeInFrames*music.stream.channels*music.stream.sampleSize/8, 1);
|
size_t pcmSize = subBufferSizeInFrames * music.stream.channels * music.stream.sampleSize / 8;
|
||||||
|
if (AUDIO.System.pcmCapacity < pcmSize) {
|
||||||
|
RL_FREE(AUDIO.System.pcm);
|
||||||
|
AUDIO.System.pcm = RL_CALLOC(1, pcmSize);
|
||||||
|
AUDIO.System.pcmCapacity = pcmSize;
|
||||||
|
}
|
||||||
|
|
||||||
int frameCountToStream = 0; // Total size of data in frames to be streamed
|
int frameCountToStream = 0; // Total size of data in frames to be streamed
|
||||||
|
|
||||||
|
@ -1745,8 +1753,8 @@ void UpdateMusicStream(Music music)
|
||||||
case MUSIC_AUDIO_WAV:
|
case MUSIC_AUDIO_WAV:
|
||||||
{
|
{
|
||||||
// NOTE: Returns the number of samples to process (not required)
|
// NOTE: Returns the number of samples to process (not required)
|
||||||
if (music.stream.sampleSize == 16) drwav_read_pcm_frames_s16((drwav *)music.ctxData, frameCountToStream, (short *)pcm);
|
if (music.stream.sampleSize == 16) drwav_read_pcm_frames_s16((drwav *)music.ctxData, frameCountToStream, (short *)AUDIO.System.pcm);
|
||||||
else if (music.stream.sampleSize == 32) drwav_read_pcm_frames_f32((drwav *)music.ctxData, frameCountToStream, (float *)pcm);
|
else if (music.stream.sampleSize == 32) drwav_read_pcm_frames_f32((drwav *)music.ctxData, frameCountToStream, (float *)AUDIO.System.pcm);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
#endif
|
#endif
|
||||||
|
@ -1754,7 +1762,7 @@ void UpdateMusicStream(Music music)
|
||||||
case MUSIC_AUDIO_OGG:
|
case MUSIC_AUDIO_OGG:
|
||||||
{
|
{
|
||||||
// NOTE: Returns the number of samples to process (be careful! we ask for number of shorts!)
|
// NOTE: Returns the number of samples to process (be careful! we ask for number of shorts!)
|
||||||
stb_vorbis_get_samples_short_interleaved((stb_vorbis *)music.ctxData, music.stream.channels, (short *)pcm, frameCountToStream*music.stream.channels);
|
stb_vorbis_get_samples_short_interleaved((stb_vorbis *)music.ctxData, music.stream.channels, (short *)AUDIO.System.pcm, frameCountToStream*music.stream.channels);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
#endif
|
#endif
|
||||||
|
@ -1762,14 +1770,14 @@ void UpdateMusicStream(Music music)
|
||||||
case MUSIC_AUDIO_FLAC:
|
case MUSIC_AUDIO_FLAC:
|
||||||
{
|
{
|
||||||
// NOTE: Returns the number of samples to process (not required)
|
// NOTE: Returns the number of samples to process (not required)
|
||||||
drflac_read_pcm_frames_s16((drflac *)music.ctxData, frameCountToStream*music.stream.channels, (short *)pcm);
|
drflac_read_pcm_frames_s16((drflac *)music.ctxData, frameCountToStream*music.stream.channels, (short *)AUDIO.System.pcm);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
#endif
|
#endif
|
||||||
#if defined(SUPPORT_FILEFORMAT_MP3)
|
#if defined(SUPPORT_FILEFORMAT_MP3)
|
||||||
case MUSIC_AUDIO_MP3:
|
case MUSIC_AUDIO_MP3:
|
||||||
{
|
{
|
||||||
drmp3_read_pcm_frames_f32((drmp3 *)music.ctxData, frameCountToStream, (float *)pcm);
|
drmp3_read_pcm_frames_f32((drmp3 *)music.ctxData, frameCountToStream, (float *)AUDIO.System.pcm);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
#endif
|
#endif
|
||||||
|
@ -1777,9 +1785,9 @@ void UpdateMusicStream(Music music)
|
||||||
case MUSIC_MODULE_XM:
|
case MUSIC_MODULE_XM:
|
||||||
{
|
{
|
||||||
// NOTE: Internally we consider 2 channels generation, so sampleCount/2
|
// NOTE: Internally we consider 2 channels generation, so sampleCount/2
|
||||||
if (AUDIO_DEVICE_FORMAT == ma_format_f32) jar_xm_generate_samples((jar_xm_context_t *)music.ctxData, (float *)pcm, frameCountToStream);
|
if (AUDIO_DEVICE_FORMAT == ma_format_f32) jar_xm_generate_samples((jar_xm_context_t *)music.ctxData, (float *)AUDIO.System.pcm, frameCountToStream);
|
||||||
else if (AUDIO_DEVICE_FORMAT == ma_format_s16) jar_xm_generate_samples_16bit((jar_xm_context_t *)music.ctxData, (short *)pcm, frameCountToStream);
|
else if (AUDIO_DEVICE_FORMAT == ma_format_s16) jar_xm_generate_samples_16bit((jar_xm_context_t *)music.ctxData, (short *)AUDIO.System.pcm, frameCountToStream);
|
||||||
else if (AUDIO_DEVICE_FORMAT == ma_format_u8) jar_xm_generate_samples_8bit((jar_xm_context_t *)music.ctxData, (char *)pcm, frameCountToStream);
|
else if (AUDIO_DEVICE_FORMAT == ma_format_u8) jar_xm_generate_samples_8bit((jar_xm_context_t *)music.ctxData, (char *)AUDIO.System.pcm, frameCountToStream);
|
||||||
|
|
||||||
} break;
|
} break;
|
||||||
#endif
|
#endif
|
||||||
|
@ -1787,13 +1795,13 @@ void UpdateMusicStream(Music music)
|
||||||
case MUSIC_MODULE_MOD:
|
case MUSIC_MODULE_MOD:
|
||||||
{
|
{
|
||||||
// NOTE: 3rd parameter (nbsample) specify the number of stereo 16bits samples you want, so sampleCount/2
|
// NOTE: 3rd parameter (nbsample) specify the number of stereo 16bits samples you want, so sampleCount/2
|
||||||
jar_mod_fillbuffer((jar_mod_context_t *)music.ctxData, (short *)pcm, frameCountToStream, 0);
|
jar_mod_fillbuffer((jar_mod_context_t *)music.ctxData, (short *)AUDIO.System.pcm, frameCountToStream, 0);
|
||||||
} break;
|
} break;
|
||||||
#endif
|
#endif
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|
||||||
UpdateAudioStream(music.stream, pcm, frameCountToStream);
|
UpdateAudioStream(music.stream, AUDIO.System.pcm, frameCountToStream);
|
||||||
|
|
||||||
framesLeft -= frameCountToStream;
|
framesLeft -= frameCountToStream;
|
||||||
|
|
||||||
|
@ -1804,9 +1812,6 @@ void UpdateMusicStream(Music music)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free allocated pcm data
|
|
||||||
RL_FREE(pcm);
|
|
||||||
|
|
||||||
// Reset audio stream for looping
|
// Reset audio stream for looping
|
||||||
if (streamEnding)
|
if (streamEnding)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue