mirror of
				https://github.com/raysan5/raylib.git
				synced 2025-11-03 17:24:25 +00:00 
			
		
		
		
	Share PCM streaming buffer to reduce dynamic allocations (#2532)
This commit is contained in:
		
							
								
								
									
										33
									
								
								src/raudio.c
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								src/raudio.c
									
									
									
									
									
								
							@@ -351,6 +351,8 @@ typedef struct AudioData {
 | 
			
		||||
        ma_device device;           // miniaudio device
 | 
			
		||||
        ma_mutex lock;              // miniaudio mutex lock
 | 
			
		||||
        bool isReady;               // Check if audio device is ready
 | 
			
		||||
        size_t pcmCapacity;
 | 
			
		||||
        void *pcm;
 | 
			
		||||
    } System;
 | 
			
		||||
    struct {
 | 
			
		||||
        AudioBuffer *first;         // Pointer to first AudioBuffer in the list
 | 
			
		||||
@@ -510,6 +512,7 @@ void CloseAudioDevice(void)
 | 
			
		||||
        ma_context_uninit(&AUDIO.System.context);
 | 
			
		||||
 | 
			
		||||
        AUDIO.System.isReady = false;
 | 
			
		||||
        RL_FREE(AUDIO.System.pcm);
 | 
			
		||||
 | 
			
		||||
        TRACELOG(LOG_INFO, "AUDIO: Device closed successfully");
 | 
			
		||||
    }
 | 
			
		||||
@@ -1726,7 +1729,12 @@ void UpdateMusicStream(Music music)
 | 
			
		||||
    unsigned int subBufferSizeInFrames = music.stream.buffer->sizeInFrames/2;
 | 
			
		||||
 | 
			
		||||
    // 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
 | 
			
		||||
 | 
			
		||||
@@ -1745,8 +1753,8 @@ void UpdateMusicStream(Music music)
 | 
			
		||||
            case MUSIC_AUDIO_WAV:
 | 
			
		||||
            {
 | 
			
		||||
                // 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);
 | 
			
		||||
                else if (music.stream.sampleSize == 32) drwav_read_pcm_frames_f32((drwav *)music.ctxData, frameCountToStream, (float *)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 *)AUDIO.System.pcm);
 | 
			
		||||
 | 
			
		||||
            } break;
 | 
			
		||||
        #endif
 | 
			
		||||
@@ -1754,7 +1762,7 @@ void UpdateMusicStream(Music music)
 | 
			
		||||
            case MUSIC_AUDIO_OGG:
 | 
			
		||||
            {
 | 
			
		||||
                // 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;
 | 
			
		||||
        #endif
 | 
			
		||||
@@ -1762,14 +1770,14 @@ void UpdateMusicStream(Music music)
 | 
			
		||||
            case MUSIC_AUDIO_FLAC:
 | 
			
		||||
            {
 | 
			
		||||
                // 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;
 | 
			
		||||
        #endif
 | 
			
		||||
        #if defined(SUPPORT_FILEFORMAT_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;
 | 
			
		||||
        #endif
 | 
			
		||||
@@ -1777,9 +1785,9 @@ void UpdateMusicStream(Music music)
 | 
			
		||||
            case MUSIC_MODULE_XM:
 | 
			
		||||
            {
 | 
			
		||||
                // 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);
 | 
			
		||||
                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_u8) jar_xm_generate_samples_8bit((jar_xm_context_t *)music.ctxData, (char *)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 *)AUDIO.System.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;
 | 
			
		||||
        #endif
 | 
			
		||||
@@ -1787,13 +1795,13 @@ void UpdateMusicStream(Music music)
 | 
			
		||||
            case MUSIC_MODULE_MOD:
 | 
			
		||||
            {
 | 
			
		||||
                // 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;
 | 
			
		||||
        #endif
 | 
			
		||||
            default: break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        UpdateAudioStream(music.stream, pcm, frameCountToStream);
 | 
			
		||||
        UpdateAudioStream(music.stream, AUDIO.System.pcm, frameCountToStream);
 | 
			
		||||
 | 
			
		||||
        framesLeft -= frameCountToStream;
 | 
			
		||||
 | 
			
		||||
@@ -1804,9 +1812,6 @@ void UpdateMusicStream(Music music)
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Free allocated pcm data
 | 
			
		||||
    RL_FREE(pcm);
 | 
			
		||||
 | 
			
		||||
    // Reset audio stream for looping
 | 
			
		||||
    if (streamEnding)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user